36 lines · 2.4 KB · TypeScriptRaw
import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { spawnSync } from "node:child_process";import { tmpdir } from "node:os";import { join } from "node:path";import { expect, it } from "vitest";/// RULE: The orphan inventory names Cotton binaries that no job or app owns. An interpreter running a script whose file name/// starts with "cotton" (bash running the phone-launch poller) and a toolchain compiling under a cotton-named folder are not/// Cotton processes, while an unquoted app helper path with spaces still is — a false orphan refuses every cut on that Mac/// until somebody kills a process that was never Cotton's.it("names only Cotton binaries as orphans, never an interpreter or a toolchain with a cotton path in its arguments", () => { const dir = mkdtempSync(join(tmpdir(), "orphan-processes-")); try { const fake = (name: string, source: string) => { writeFileSync(join(dir, name), `#!/bin/bash\n${source}\n`); chmodSync(join(dir, name), 0o755); }; fake("ps", [ "echo ' 101 1 Thu Oct 8 17:18:20 2026 /bin/bash /Users/x/.claude/scripts/cotton-session-launcher.sh'", "echo ' 102 101 Thu Oct 8 17:18:20 2026 sleep 60'", "echo ' 103 1 Thu Oct 8 17:18:20 2026 python3 /Users/x/cotton-tools/probe.py'", "echo ' 104 1 Thu Oct 8 17:18:20 2026 /tmp/stale/cotton-headless-abc1234 serve'", "echo ' 105 1 Thu Oct 8 17:18:20 2026 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -module-cache-path /private/var/folders/x/T/cotton-ui-tests-stub/3c743a3fa8f5 -o /private/var/folders/x/T/cotton-ui-tests-stub/3c743a3fa8f5/out.o'", "echo ' 106 1 Thu Oct 8 17:18:20 2026 /Applications/Cotton Dev.app/Contents/Helpers/cotton-whatsapp-link --socket /tmp/x/link.sock'", ].join("\n")); fake("git", "exit 0"); fake("lsof", "exit 1"); mkdirSync(join(dir, "claims")); const run = spawnSync("bash", [join(import.meta.dirname, "../ci/orphan-processes.sh"), "--json"], { encoding: "utf8", env: { ...process.env, PATH: `${dir}:${process.env.PATH ?? ""}`, COTTON_CLAIMS_DIR: join(dir, "claims") }, }); const orphans = JSON.parse(run.stdout) as { pid: number; command: string }[]; expect(orphans.map((row) => row.pid)).toEqual([104, 106]); expect(run.status).toBe(1); } finally { rmSync(dir, { recursive: true, force: true }); }});