// with-build-lock.sh on a Mac: the build policy runs where its installer left // its marker; a Mac the fleet file lists without the marker stops; any other // Mac (a checkout of the public source) builds straight through. import { spawnSync } from "node:child_process"; import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { hostname, tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; const SCRIPT = resolve(__dirname, "../with-build-lock.sh"); const HOST = spawnSync("hostname", ["-s"], { encoding: "utf8" }).stdout.trim().toLowerCase() || hostname().split(".")[0]!.toLowerCase(); let scratch = ""; afterEach(() => { if (scratch) rmSync(scratch, { recursive: true, force: true }); scratch = ""; }); /** A scripts folder holding only with-build-lock.sh (and a fleet file when given), and a bare home. */ function place(fleetHosts: string[] | null, marker: boolean) { scratch = mkdtempSync(join(tmpdir(), "with-build-lock-policy-")); const scripts = join(scratch, "scripts"); mkdirSync(scripts); copyFileSync(SCRIPT, join(scripts, "with-build-lock.sh")); mkdirSync(join(scripts, "lib")); copyFileSync(resolve(__dirname, "../lib/build-policy-state.sh"), join(scripts, "lib", "build-policy-state.sh")); if (fleetHosts) { const macs = Object.fromEntries(fleetHosts.map((h) => [h, { hostname: h }])); writeFileSync(join(scripts, "lane-macs.json"), JSON.stringify({ macs })); } const home = join(scratch, "home"); mkdirSync(home); if (marker) { mkdirSync(join(home, ".cotton-agent/mac-build-policy"), { recursive: true }); writeFileSync(join(home, ".cotton-agent/mac-build-policy/activated"), "installed\n"); } const lock = join(scratch, "lock"); const run = spawnSync("bash", [join(scripts, "with-build-lock.sh"), "bash", "-c", "echo built"], { encoding: "utf8", env: { PATH: process.env.PATH ?? "/usr/bin:/bin", HOME: home, BUILD_LOCK_DIR: lock, BUILD_LOCK_LOG: join(scratch, "wait.log") }, timeout: 30_000, }); return { run, lock }; } describe.runIf(process.platform === "darwin")("with-build-lock.sh: which Macs have the build policy", () => { // RULE: On a Mac without the build policy's marker and outside the fleet file, with-build-lock runs the build straight through: no policy, no lock. it("builds straight through on a Mac with no marker and no fleet file, taking no lock", () => { const { run, lock } = place(null, false); expect(run.status, run.stderr).toBe(0); expect(run.stdout.trim()).toBe("built"); expect(existsSync(lock)).toBe(false); }); it("builds straight through on a Mac the fleet file does not list", () => { const { run } = place(["not-this-mac-1", "not-this-mac-2"], false); expect(run.status, run.stderr).toBe(0); expect(run.stdout.trim()).toBe("built"); }); // RULE: A Mac the fleet file lists by hostname must have the build policy installed; without its marker with-build-lock stops and says what to run. it("stops on a fleet Mac without the marker, and says what to run", () => { const { run } = place([HOST], false); expect(run.status).toBe(2); expect(run.stdout).not.toContain("built"); expect(run.stderr).toContain("build policy is not installed"); expect(run.stderr).toContain("pnpm mac-build-policy:install"); }); it("goes through the policy where the marker is", () => { const { run } = place([HOST], true); // The scratch folder carries none of the lock's helpers, so the lock's own path fails there: that it was // taken at all (not the straight-through run) is the proof. expect(run.status).not.toBe(0); expect(run.stdout).not.toContain("built"); expect(run.stderr).not.toContain("build policy is not installed"); }); }); /** disk-guard.sh in a scripts folder of its own, with a stand-in policy that says it ran. */ function guard(fleetHosts: string[] | null, marker: boolean) { scratch = mkdtempSync(join(tmpdir(), "disk-guard-policy-")); const scripts = join(scratch, "scripts"); mkdirSync(join(scripts, "lib"), { recursive: true }); copyFileSync(resolve(__dirname, "../disk-guard.sh"), join(scripts, "disk-guard.sh")); copyFileSync(resolve(__dirname, "../lib/build-policy-state.sh"), join(scripts, "lib", "build-policy-state.sh")); writeFileSync(join(scripts, "mac-build-policy.sh"), 'echo "policy ran: $*"\n'); if (fleetHosts) writeFileSync(join(scripts, "lane-macs.json"), JSON.stringify({ macs: Object.fromEntries(fleetHosts.map((h) => [h, { hostname: h }])) })); const home = join(scratch, "home"); mkdirSync(home); if (marker) { mkdirSync(join(home, ".cotton-agent/mac-build-policy"), { recursive: true }); writeFileSync(join(home, ".cotton-agent/mac-build-policy/activated"), "installed\n"); } return spawnSync("bash", [join(scripts, "disk-guard.sh"), "--pre-build"], { encoding: "utf8", env: { PATH: process.env.PATH ?? "/usr/bin:/bin", HOME: home }, timeout: 30_000 }); } describe.runIf(process.platform === "darwin")("disk-guard.sh: the same three cases on a Mac", () => { // RULE: disk-guard.sh decides like with-build-lock.sh: the policy's check where it is installed, a stop on a fleet Mac without it, and nothing on any other Mac. it("runs the policy's disk check where the marker is", () => { const run = guard(null, true); expect(run.status, run.stderr).toBe(0); expect(run.stdout).toContain("policy ran: --path"); }); it("does nothing on a Mac outside the fleet", () => { const run = guard(["not-this-mac"], false); expect(run.status, run.stderr).toBe(0); expect(run.stdout).toBe(""); }); it("stops on a fleet Mac without the marker", () => { const run = guard([HOST], false); expect(run.status).toBe(2); expect(run.stderr).toContain("pnpm mac-build-policy:install"); }); });