| 1 | import assert from "node:assert/strict"; |
| 2 | import { |
| 3 | mkdtemp, |
| 4 | mkdir, |
| 5 | writeFile, |
| 6 | symlink, |
| 7 | access, |
| 8 | rm, |
| 9 | copyFile, |
| 10 | } from "node:fs/promises"; |
| 11 | import os from "node:os"; |
| 12 | import path from "node:path"; |
| 13 | import test from "node:test"; |
| 14 | import { execFileSync, spawn } from "node:child_process"; |
| 15 | import { cleanupApps } from "./cleanup-apps.mjs"; |
| 16 | |
| 17 | const cageID = "io.github.lee-xeo.claude-cage"; |
| 18 | test("cleanup preserves a Cage executable still in use", { skip: process.platform !== "darwin" }, async () => { |
| 19 | const directory = await mkdtemp(path.join(os.tmpdir(), "cage-cleanup-running-")); |
| 20 | let child; |
| 21 | try { |
| 22 | const latest = await app(path.join(directory, "new.app")); |
| 23 | const running = await app(path.join(directory, "running.app")); |
| 24 | const executable = path.join(running, "Contents/MacOS/Cage"); |
| 25 | child = spawn(executable, ["30"], { stdio: "ignore" }); |
| 26 | await new Promise((resolve, reject) => { child.once("spawn", resolve); child.once("error", reject); }); |
| 27 | const skipped = []; |
| 28 | assert.deepEqual(await cleanupApps({ keep: [latest], roots: [directory], onInUse: (bundle) => skipped.push(bundle) }), []); |
| 29 | assert.deepEqual(skipped, [running]); |
| 30 | await access(executable); |
| 31 | await new Promise((resolve) => { child.once("exit", resolve); child.kill("SIGTERM"); }); |
| 32 | child = null; |
| 33 | assert.deepEqual(await cleanupApps({ keep: [latest], roots: [directory] }), [running]); |
| 34 | } finally { |
| 35 | if (child && child.exitCode === null) await new Promise((resolve) => { child.once("exit", resolve); child.kill("SIGTERM"); }); |
| 36 | await rm(directory, { recursive: true, force: true }); |
| 37 | } |
| 38 | }); |
| 39 | async function app(directory, id = cageID) { |
| 40 | await mkdir(path.join(directory, "Contents/MacOS"), { recursive: true }); |
| 41 | await writeFile( |
| 42 | path.join(directory, "Contents/Info.plist"), |
| 43 | `<?xml version="1.0"?><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>${id}</string><key>CFBundleExecutable</key><string>Cage</string><key>CFBundlePackageType</key><string>APPL</string></dict></plist>`, |
| 44 | ); |
| 45 | await copyFile('/bin/sleep', path.join(directory, 'Contents/MacOS/Cage')); |
| 46 | execFileSync('/usr/bin/codesign', ['--force', '--sign', '-', directory], { stdio: 'ignore' }); |
| 47 | return directory; |
| 48 | } |
| 49 | |
| 50 | test('cleanup retains project files added to a previously signed app', { skip: process.platform !== 'darwin' }, async (t) => { |
| 51 | const directory = await mkdtemp(path.join(os.tmpdir(), 'cage-cleanup-modified-')); |
| 52 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 53 | const latest = await app(path.join(directory, 'new.app')); |
| 54 | const modified = await app(path.join(directory, 'old.app')); |
| 55 | const project = path.join(modified, 'Contents/my-project'); |
| 56 | await mkdir(project); |
| 57 | await writeFile(path.join(project, 'work.txt'), 'user work'); |
| 58 | const retained = []; |
| 59 | assert.deepEqual(await cleanupApps({ keep: [latest], roots: [directory], onModified: value => retained.push(value) }), []); |
| 60 | assert.deepEqual(retained, [modified]); |
| 61 | await access(path.join(project, 'work.txt')); |
| 62 | }); |
| 63 | |
| 64 | test( |
| 65 | "cleanup removes old Cage copies while retaining replacement, foreign apps and linked directories", |
| 66 | { skip: process.platform !== "darwin" }, |
| 67 | async (t) => { |
| 68 | const directory = await mkdtemp(path.join(os.tmpdir(), "cage-cleanup-")); |
| 69 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 70 | const root = path.join(directory, "output"), |
| 71 | outside = path.join(directory, "outside"); |
| 72 | const latest = await app(path.join(root, "Cage.app")); |
| 73 | const old = await app(path.join(root, "qa backup", "previous-Cage.app")); |
| 74 | const foreign = await app(path.join(root, "Other.app"), "example.other"); |
| 75 | const external = await app(path.join(outside, "Cage.app")); |
| 76 | await symlink(outside, path.join(root, "linked-backup")); |
| 77 | assert.deepEqual( |
| 78 | await cleanupApps({ |
| 79 | keep: [latest], |
| 80 | roots: [root, path.join(directory, "missing")], |
| 81 | }), |
| 82 | [old], |
| 83 | ); |
| 84 | await assert.rejects(access(old), { code: "ENOENT" }); |
| 85 | for (const preserved of [latest, foreign, external]) |
| 86 | await access(preserved); |
| 87 | }, |
| 88 | ); |
| 89 | |
| 90 | test( |
| 91 | "cleanup refuses to delete anything without a valid Cage replacement", |
| 92 | { skip: process.platform !== "darwin" }, |
| 93 | async (t) => { |
| 94 | const directory = await mkdtemp(path.join(os.tmpdir(), "cage-cleanup-")); |
| 95 | t.after(() => rm(directory, { recursive: true, force: true })); |
| 96 | const old = await app(path.join(directory, "old.app")); |
| 97 | const foreign = await app( |
| 98 | path.join(directory, "Other.app"), |
| 99 | "example.other", |
| 100 | ); |
| 101 | await assert.rejects(cleanupApps({ keep: [], roots: [directory] })); |
| 102 | await assert.rejects(cleanupApps({ keep: [foreign], roots: [directory] })); |
| 103 | await access(old); |
| 104 | }, |
| 105 | ); |