| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import { spawn, execFile } from "node:child_process"; |
| 5 | import { promisify } from "node:util"; |
| 6 | import { mkdtemp, readFile, rm } from "node:fs/promises"; |
| 7 | import { tmpdir } from "node:os"; |
| 8 | import path from "node:path"; |
| 9 | |
| 10 | const runFile = promisify(execFile); |
| 11 | const [legacy, current, next] = process.argv.slice(2).map((file) => path.resolve(file)); |
| 12 | assert(legacy && current && next, "Pass three cage executables"); |
| 13 | const profile = await mkdtemp(path.join(tmpdir(), "cage-control-upgrade-")); |
| 14 | const env = { ...process.env, CLAUDE_CAGE_CONFIG: path.join(profile, "config.json") }; |
| 15 | const call = async (binary, ...args) => { |
| 16 | const { stdout } = await runFile(binary, [...args, "--json"], { env, timeout: 150_000, maxBuffer: 512 * 1024 }); |
| 17 | return JSON.parse(stdout); |
| 18 | }; |
| 19 | const record = async (entry = false) => JSON.parse(await readFile(path.join(profile, entry ? "desktop-entry" : ".", "control.json"), "utf8")); |
| 20 | const wait = async (test) => { |
| 21 | for (let i = 0; i < 100; i++) { |
| 22 | try { if (await test()) return; } catch {} |
| 23 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 24 | } |
| 25 | throw new Error("Timed out waiting for the disposable controller"); |
| 26 | }; |
| 27 | const old = spawn(legacy, ["serve"], { cwd: profile, env, stdio: "ignore" }); |
| 28 | try { |
| 29 | await wait(async () => (await record()).pid === old.pid); |
| 30 | assert.equal((await call(current, "desktop")).status, "ready", "Chooser can open without taking over the older workload"); |
| 31 | assert.equal((await record()).pid, old.pid); |
| 32 | assert.equal((await call(current, "desktop", "--existing")).status, "existing"); |
| 33 | assert.equal((await call(current, "prepare-update")).status, "stop_required"); |
| 34 | assert.equal((await call(current, "prepare-update", "--stop")).status, "prepared"); |
| 35 | await wait(() => old.exitCode !== null || old.signalCode !== null); |
| 36 | assert.equal((await call(current, "desktop")).status, "ready"); |
| 37 | const first = await record(true); |
| 38 | assert.equal((await call(current, "desktop")).status, "ready"); |
| 39 | assert.equal((await record(true)).pid, first.pid, "Same build must reuse its controller"); |
| 40 | assert.equal((await call(next, "desktop")).status, "ready"); |
| 41 | const replacement = await record(true); |
| 42 | assert.notEqual(replacement.pid, first.pid, "Different build must replace an idle controller"); |
| 43 | assert.notEqual(replacement.controller.build, first.controller.build); |
| 44 | assert.equal(replacement.controller.version, first.controller.version, "Exercise same-version distinct builds"); |
| 45 | assert.equal((await call(next, "prepare-update")).status, "prepared"); |
| 46 | console.log("PASS: legacy requires explicit stop, verified legacy migration, same-build reuse, same-version build replacement, and graceful shutdown"); |
| 47 | } finally { |
| 48 | |
| 49 | await call(current, "prepare-update", "--stop").catch(() => {}); |
| 50 | if (old.exitCode === null && old.signalCode === null) old.kill("SIGTERM"); |
| 51 | await rm(profile, { recursive: true, force: true }); |
| 52 | await rm(profile + "-workspace", { recursive: true, force: true }); |
| 53 | } |