| ... | @@ -134,6 +134,25 @@ test("workspace lifecycle through nightly, JJ, APFS, and reference sockets", { | ... | @@ -134,6 +134,25 @@ test("workspace lifecycle through nightly, JJ, APFS, and reference sockets", { |
| 134 | ); | 134 | ); |
| 135 | const { main, t3 } = await import("./workspaces.mjs"); | 135 | const { main, t3 } = await import("./workspaces.mjs"); |
| 136 | const client = await t3(); | 136 | const client = await t3(); |
| | 137 | const archiveRequests = []; |
| | 138 | let archiveHandler = () => ({ archivedThreadIds: [] }); |
| | 139 | const WebSocket = globalThis.WebSocket; |
| | 140 | mock.method(globalThis, "WebSocket", function (...args) { |
| | 141 | const socket = new WebSocket(...args); |
| | 142 | const send = socket.send.bind(socket); |
| | 143 | socket.send = (data) => { |
| | 144 | const request = JSON.parse(String(data).trim()); |
| | 145 | if (request.tag !== "managedWorkspaces.archiveCompleted") return send(data); |
| | 146 | archiveRequests.push(request.payload); |
| | 147 | Promise.resolve().then(() => archiveHandler(request.payload)).then( |
| | 148 | (value) => ({ _tag: "Success", value }), |
| | 149 | (error) => ({ _tag: "Failure", cause: error.message }), |
| | 150 | ).then((exit) => socket.onmessage(new MessageEvent("message", { |
| | 151 | data: JSON.stringify({ _tag: "Exit", requestId: request.id, exit }) + "\n", |
| | 152 | }))); |
| | 153 | }; |
| | 154 | return socket; |
| | 155 | }); |
| 137 | const project = (await client.snapshot()).projects[0]; | 156 | const project = (await client.snapshot()).projects[0]; |
| 138 | await client.dispatch({ | 157 | await client.dispatch({ |
| 139 | type: "project.update", | 158 | type: "project.update", |
| ... | @@ -603,8 +622,21 @@ exec "$WORKSPACE_TEST_PNPM" "$@" | ... | @@ -603,8 +622,21 @@ exec "$WORKSPACE_TEST_PNPM" "$@" |
| 603 | ); | 622 | ); |
| 604 | assert.equal(readRecord().idleSince, null); | 623 | assert.equal(readRecord().idleSince, null); |
| 605 | referenceMock.mock.restore(); | 624 | referenceMock.mock.restore(); |
| | 625 | archiveHandler = () => { throw new Error("PR read unavailable"); }; |
| | 626 | age(); |
| | 627 | await assert.rejects(main(["gc"]), /PR read unavailable/); |
| | 628 | assert.ok(fs.existsSync(first), "merge read failure stops eligible cleanup"); |
| | 629 | assert.equal(readRecord().idleSince, null); |
| | 630 | archiveHandler = () => ({ archivedThreadIds: "unknown" }); |
| 606 | age(); | 631 | age(); |
| | 632 | await assert.rejects(main(["gc"]), /unknown archive result/); |
| | 633 | assert.ok(fs.existsSync(first), "unknown merge response stops cleanup"); |
| | 634 | assert.equal(readRecord().idleSince, null); |
| | 635 | archiveHandler = () => ({ archivedThreadIds: [] }); |
| | 636 | age(); |
| | 637 | const requestsBeforeDryRun = archiveRequests.length; |
| 607 | await main(["gc", "--dry-run"]); | 638 | await main(["gc", "--dry-run"]); |
| | 639 | assert.equal(archiveRequests.length, requestsBeforeDryRun, "dry run never requests archival"); |
| 608 | assert.ok(fs.existsSync(first), "dry run never removes a workspace"); | 640 | assert.ok(fs.existsSync(first), "dry run never removes a workspace"); |
| 609 | fs.writeFileSync( | 641 | fs.writeFileSync( |
| 610 | path.join(first, "saved.txt"), | 642 | path.join(first, "saved.txt"), |
| ... | @@ -648,6 +680,32 @@ exec "$WORKSPACE_TEST_PNPM" "$@" | ... | @@ -648,6 +680,32 @@ exec "$WORKSPACE_TEST_PNPM" "$@" |
| 648 | await main(["gc"]); | 680 | await main(["gc"]); |
| 649 | assert.ok(fs.existsSync(first), "a settled replacement chat keeps its workspace"); | 681 | assert.ok(fs.existsSync(first), "a settled replacement chat keeps its workspace"); |
| 650 | assert.equal(readRecord().idleSince, null); | 682 | assert.equal(readRecord().idleSince, null); |
| | 683 | archiveHandler = async ({ workspacePath }) => { |
| | 684 | if (workspacePath !== first) return { archivedThreadIds: [] }; |
| | 685 | await client.dispatch({ type: "thread.unsettle", threadId: freshRecord.threadId, reason: "user" }); |
| | 686 | return { archivedThreadIds: [freshRecord.threadId] }; |
| | 687 | }; |
| | 688 | age(); |
| | 689 | await main(["gc"]); |
| | 690 | assert.ok(fs.existsSync(first), "a resumed chat keeps its workspace after the merge check"); |
| | 691 | assert.equal(readRecord().idleSince, null); |
| | 692 | await client.dispatch({ type: "thread.settle", threadId: freshRecord.threadId }); |
| | 693 | archiveHandler = async ({ workspacePath }) => { |
| | 694 | if (workspacePath !== first) return { archivedThreadIds: [] }; |
| | 695 | await client.dispatch({ type: "thread.archive", threadId: freshRecord.threadId }); |
| | 696 | return { archivedThreadIds: [freshRecord.threadId] }; |
| | 697 | }; |
| | 698 | await main(["gc"]); |
| | 699 | assert.ok(fs.existsSync(first), "verified merge archival starts the deletion grace period"); |
| | 700 | assert.ok(readRecord().idleSince > Date.now() - 5000); |
| | 701 | archiveHandler = () => ({ archivedThreadIds: [] }); |
| | 702 | age(); |
| | 703 | await main(["gc"]); |
| | 704 | assert.equal(fs.existsSync(first), false, "merged archived workspace is collected after grace"); |
| | 705 | assert.ok(archiveRequests.some(({ workspacePath }) => workspacePath === first)); |
| | 706 | assert.ok(archiveRequests.every(({ workspacePath }) => |
| | 707 | path.dirname(workspacePath) === path.join(directory, "workspaces") |
| | 708 | ), "only registered managed folders are checked"); |
| 651 | const secondRecord = JSON.parse( | 709 | const secondRecord = JSON.parse( |
| 652 | fs.readFileSync( | 710 | fs.readFileSync( |
| 653 | path.join(directory, ".local/state/workspaces/workspaces/second.json"), | 711 | path.join(directory, ".local/state/workspaces/workspaces/second.json"), |