|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { describe, it, beforeEach, afterEach } from "node:test"; |
| 6 | +import assert from "node:assert/strict"; |
| 7 | + |
| 8 | +import { |
| 9 | + buildSingleJobSnapshot, |
| 10 | + resolveCancelableJob, |
| 11 | + resolveResultJob |
| 12 | +} from "../plugins/codex/scripts/lib/job-control.mjs"; |
| 13 | + |
| 14 | +describe("job-control cross-workspace fallback", () => { |
| 15 | + let pluginDataDir; |
| 16 | + let currentWorkspace; |
| 17 | + let previousPluginData; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + pluginDataDir = mkdtempSync(path.join(tmpdir(), "job-control-cross-")); |
| 21 | + currentWorkspace = mkdtempSync(path.join(tmpdir(), "job-control-cwd-")); |
| 22 | + previousPluginData = process.env.CLAUDE_PLUGIN_DATA; |
| 23 | + process.env.CLAUDE_PLUGIN_DATA = pluginDataDir; |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + if (previousPluginData === undefined) { |
| 28 | + delete process.env.CLAUDE_PLUGIN_DATA; |
| 29 | + } else { |
| 30 | + process.env.CLAUDE_PLUGIN_DATA = previousPluginData; |
| 31 | + } |
| 32 | + rmSync(pluginDataDir, { recursive: true, force: true }); |
| 33 | + rmSync(currentWorkspace, { recursive: true, force: true }); |
| 34 | + }); |
| 35 | + |
| 36 | + function writeRemoteWorkspaceJob(slug, job) { |
| 37 | + const stateDir = path.join(pluginDataDir, "state", slug); |
| 38 | + fs.mkdirSync(stateDir, { recursive: true }); |
| 39 | + fs.writeFileSync( |
| 40 | + path.join(stateDir, "state.json"), |
| 41 | + `${JSON.stringify({ version: 1, jobs: [job] }, null, 2)}\n`, |
| 42 | + "utf8" |
| 43 | + ); |
| 44 | + return stateDir; |
| 45 | + } |
| 46 | + |
| 47 | + it("buildSingleJobSnapshot falls back to cross-workspace by job id", () => { |
| 48 | + const job = { |
| 49 | + id: "task-abc-running", |
| 50 | + status: "running", |
| 51 | + workspaceRoot: "/some/other/repo", |
| 52 | + logFile: path.join(pluginDataDir, "log.txt"), |
| 53 | + createdAt: new Date().toISOString(), |
| 54 | + updatedAt: new Date().toISOString() |
| 55 | + }; |
| 56 | + const stateDir = writeRemoteWorkspaceJob("remote-aaaaaaaaaaaaaaaa", job); |
| 57 | + |
| 58 | + const snapshot = buildSingleJobSnapshot(currentWorkspace, "task-abc-running"); |
| 59 | + assert.equal(snapshot.crossWorkspace, true); |
| 60 | + assert.equal(snapshot.crossWorkspaceStateDir, stateDir); |
| 61 | + assert.equal(snapshot.workspaceRoot, "/some/other/repo"); |
| 62 | + assert.equal(snapshot.job.id, "task-abc-running"); |
| 63 | + assert.equal(snapshot.job.status, "running"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("buildSingleJobSnapshot still throws when job id is unknown anywhere", () => { |
| 67 | + assert.throws( |
| 68 | + () => buildSingleJobSnapshot(currentWorkspace, "task-nope"), |
| 69 | + /No job found for "task-nope"/ |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("resolveResultJob falls back to cross-workspace finished job", () => { |
| 74 | + const job = { |
| 75 | + id: "task-done-1", |
| 76 | + status: "completed", |
| 77 | + workspaceRoot: "/some/other/repo", |
| 78 | + createdAt: new Date().toISOString(), |
| 79 | + updatedAt: new Date().toISOString() |
| 80 | + }; |
| 81 | + writeRemoteWorkspaceJob("remote-bbbbbbbbbbbbbbbb", job); |
| 82 | + |
| 83 | + const result = resolveResultJob(currentWorkspace, "task-done-1"); |
| 84 | + assert.equal(result.crossWorkspace, true); |
| 85 | + assert.equal(result.workspaceRoot, "/some/other/repo"); |
| 86 | + assert.equal(result.job.id, "task-done-1"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("resolveResultJob rejects cross-workspace job that is still running", () => { |
| 90 | + const job = { |
| 91 | + id: "task-still-running", |
| 92 | + status: "running", |
| 93 | + workspaceRoot: "/some/other/repo" |
| 94 | + }; |
| 95 | + writeRemoteWorkspaceJob("remote-cccccccccccccccc", job); |
| 96 | + |
| 97 | + assert.throws( |
| 98 | + () => resolveResultJob(currentWorkspace, "task-still-running"), |
| 99 | + /is still running in another workspace/ |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("resolveCancelableJob falls back to cross-workspace running job by id", () => { |
| 104 | + const job = { |
| 105 | + id: "task-cancelable", |
| 106 | + status: "running", |
| 107 | + workspaceRoot: "/some/other/repo", |
| 108 | + pid: 0 |
| 109 | + }; |
| 110 | + writeRemoteWorkspaceJob("remote-dddddddddddddddd", job); |
| 111 | + |
| 112 | + const result = resolveCancelableJob(currentWorkspace, "task-cancelable", { env: {} }); |
| 113 | + assert.equal(result.crossWorkspace, true); |
| 114 | + assert.equal(result.workspaceRoot, "/some/other/repo"); |
| 115 | + assert.equal(result.job.id, "task-cancelable"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("resolveCancelableJob rejects a non-active cross-workspace match with a clear message", () => { |
| 119 | + const job = { |
| 120 | + id: "task-not-active", |
| 121 | + status: "completed", |
| 122 | + workspaceRoot: "/some/other/repo" |
| 123 | + }; |
| 124 | + writeRemoteWorkspaceJob("remote-eeeeeeeeeeeeeeee", job); |
| 125 | + |
| 126 | + assert.throws( |
| 127 | + () => resolveCancelableJob(currentWorkspace, "task-not-active", { env: {} }), |
| 128 | + /Nothing to cancel/ |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("resolveCancelableJob without reference still requires a local active job", () => { |
| 133 | + assert.throws( |
| 134 | + () => resolveCancelableJob(currentWorkspace, "", { env: {} }), |
| 135 | + /No active Codex jobs to cancel/ |
| 136 | + ); |
| 137 | + }); |
| 138 | +}); |
0 commit comments