|
1 | 1 | import fs from "node:fs/promises"; |
2 | | -import type { Server } from "node:http"; |
| 2 | +import type { IncomingMessage, Server } from "node:http"; |
3 | 3 | import path from "node:path"; |
| 4 | +import { Readable } from "node:stream"; |
4 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
5 | 6 | import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
6 | 7 | import { NODE_WORKER_WORKSPACE_EXEC_COMMAND } from "../../infra/node-commands.js"; |
@@ -369,6 +370,132 @@ describe("node workspace transfer service", () => { |
369 | 370 | expect(service.isAuthorizationCurrent(authorization!)).toBe(false); |
370 | 371 | }); |
371 | 372 |
|
| 373 | + it("clears crash scratch eagerly and removes the transfer root on shutdown", async () => { |
| 374 | + const root = tempDirs.make("node-workspace-transfer-lifecycle-"); |
| 375 | + const temporaryRoot = path.join(root, "transfer-tmp"); |
| 376 | + const staleRoot = path.join(temporaryRoot, "context-stale"); |
| 377 | + await fs.mkdir(staleRoot, { recursive: true }); |
| 378 | + await fs.writeFile(path.join(staleRoot, "base.pack"), "stale"); |
| 379 | + const service = createNodeWorkspaceTransferService({ |
| 380 | + getOwner: () => undefined, |
| 381 | + temporaryRoot, |
| 382 | + }); |
| 383 | + |
| 384 | + await service.initialize(); |
| 385 | + |
| 386 | + await expect(fs.readdir(temporaryRoot)).resolves.toEqual([]); |
| 387 | + await service.closeAll(); |
| 388 | + await expect(fs.stat(temporaryRoot)).rejects.toMatchObject({ code: "ENOENT" }); |
| 389 | + }); |
| 390 | + |
| 391 | + it("serializes transfer context replacement for one environment", async () => { |
| 392 | + const root = tempDirs.make("node-workspace-transfer-serialization-"); |
| 393 | + const localPath = path.join(root, "workspace"); |
| 394 | + const temporaryRoot = path.join(root, "transfer-tmp"); |
| 395 | + await fs.mkdir(localPath); |
| 396 | + await fs.writeFile(path.join(localPath, "input.txt"), "input\n"); |
| 397 | + const service = createNodeWorkspaceTransferService({ |
| 398 | + getOwner: () => ({ |
| 399 | + credential: { |
| 400 | + ownerEpoch: 1, |
| 401 | + expiresAtMs: Date.now() + 60_000, |
| 402 | + sessionId: "session-serialize", |
| 403 | + }, |
| 404 | + environment: { |
| 405 | + ownerEpoch: 1, |
| 406 | + attachedSessionIds: ["session-serialize"], |
| 407 | + destroyRequestedAtMs: null, |
| 408 | + state: "attached", |
| 409 | + }, |
| 410 | + }), |
| 411 | + temporaryRoot, |
| 412 | + }); |
| 413 | + |
| 414 | + await Promise.all([ |
| 415 | + service.prepareSync({ |
| 416 | + environmentId: "environment-serialize", |
| 417 | + ownerEpoch: 1, |
| 418 | + sessionId: "session-serialize", |
| 419 | + generation: 1, |
| 420 | + localPath, |
| 421 | + isAuthorized: () => true, |
| 422 | + }), |
| 423 | + service.prepareSync({ |
| 424 | + environmentId: "environment-serialize", |
| 425 | + ownerEpoch: 1, |
| 426 | + sessionId: "session-serialize", |
| 427 | + generation: 2, |
| 428 | + localPath, |
| 429 | + isAuthorized: () => true, |
| 430 | + }), |
| 431 | + ]); |
| 432 | + |
| 433 | + const contexts = (await fs.readdir(temporaryRoot)).filter((name) => |
| 434 | + name.startsWith("context-"), |
| 435 | + ); |
| 436 | + expect(contexts).toHaveLength(1); |
| 437 | + await service.closeAll(); |
| 438 | + }); |
| 439 | + |
| 440 | + it("releases an upload owner after validation fails before staging", async () => { |
| 441 | + const root = tempDirs.make("node-workspace-transfer-upload-release-"); |
| 442 | + const localPath = path.join(root, "workspace"); |
| 443 | + await fs.mkdir(localPath); |
| 444 | + await fs.writeFile(path.join(localPath, "input.txt"), "input\n"); |
| 445 | + const service = createNodeWorkspaceTransferService({ |
| 446 | + getOwner: () => ({ |
| 447 | + credential: { |
| 448 | + ownerEpoch: 1, |
| 449 | + expiresAtMs: Date.now() + 60_000, |
| 450 | + sessionId: "session-upload-release", |
| 451 | + }, |
| 452 | + environment: { |
| 453 | + ownerEpoch: 1, |
| 454 | + attachedSessionIds: ["session-upload-release"], |
| 455 | + destroyRequestedAtMs: null, |
| 456 | + state: "attached", |
| 457 | + }, |
| 458 | + }), |
| 459 | + temporaryRoot: path.join(root, "transfer-tmp"), |
| 460 | + }); |
| 461 | + const prepared = await service.prepareSync({ |
| 462 | + environmentId: "environment-upload-release", |
| 463 | + ownerEpoch: 1, |
| 464 | + sessionId: "session-upload-release", |
| 465 | + generation: 1, |
| 466 | + localPath, |
| 467 | + isAuthorized: () => true, |
| 468 | + }); |
| 469 | + const token = service.prepareUpload( |
| 470 | + "environment-upload-release", |
| 471 | + prepared.snapshot.manifestRef, |
| 472 | + ); |
| 473 | + const route = { |
| 474 | + kind: "reconcile", |
| 475 | + direction: "upload", |
| 476 | + environmentId: "environment-upload-release", |
| 477 | + baseManifestRef: prepared.snapshot.manifestRef, |
| 478 | + } as const; |
| 479 | + const authorization = service.authorize({ route, token }); |
| 480 | + if (!authorization) { |
| 481 | + throw new Error("upload authorization was not created"); |
| 482 | + } |
| 483 | + const request = Readable.from([]) as unknown as IncomingMessage; |
| 484 | + request.headers = { "content-length": "0" }; |
| 485 | + |
| 486 | + await expect( |
| 487 | + service.receiveUpload({ |
| 488 | + authorization, |
| 489 | + request, |
| 490 | + signal: new AbortController().signal, |
| 491 | + }), |
| 492 | + ).rejects.toThrow("byte limit"); |
| 493 | + expect(() => |
| 494 | + service.prepareUpload("environment-upload-release", prepared.snapshot.manifestRef), |
| 495 | + ).not.toThrow(); |
| 496 | + await service.closeAll(); |
| 497 | + }); |
| 498 | + |
372 | 499 | it("rejects a retained tunnel callback after durable transfer ownership changes", async () => { |
373 | 500 | const root = tempDirs.make("node-workspace-transfer-owner-"); |
374 | 501 | const localPath = path.join(root, "workspace"); |
|
0 commit comments