import { requireEnvironment, macOS } from "./requires"; // Guard for scripts/__tests__/helpers/disk-reading.ts: the published reading is what // admits a Mac build wrapper under a fixture HOME, and a fixture that forgets it is // told which helper it forgot. import { spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { DISK_READING_HELPER, NO_READING, forgottenReadingHint, linuxHostPath, publishDiskReading } from "./helpers/disk-reading"; const SCRIPTS = join(__dirname, ".."); const scratches: string[] = []; afterEach(() => { while (scratches.length) rmSync(scratches.pop() as string, { recursive: true, force: true }); }); function scratchHome(): string { const root = mkdtempSync(join(tmpdir(), "disk-reading-")); scratches.push(root); const home = join(root, "home"); mkdirSync(join(home, "work"), { recursive: true }); return home; } // Each wrapper a script test drives that, on a Mac, admits through the device policy. const WRAPPERS: Array<[string, string[]]> = [ ["mac-build-policy.sh", ["--check"]], ["disk-guard.sh", []], ["ci/sweep-cargo-targets.sh", ["--no-remote"]], ["ci/require-free-disk.sh", []], ["ci/build-slot.sh", ["run", "--", "true"]], ]; function wrapper(home: string, script: string, args: string[], path = process.env.PATH ?? "/usr/bin:/bin") { return spawnSync("bash", [join(SCRIPTS, script), ...args], { cwd: join(home, "work"), encoding: "utf8", env: { PATH: path, HOME: home, COTTON_BUILD_SLOTS_ROOT: join(home, "slots"), COTTON_BUILD_SLOT_WAIT_S: "5", COTTON_BUILD_SLOT_SWEEP_BELOW_GB: "0" }, }); } describe("a Mac build wrapper under a fixture HOME", () => { requireEnvironment(macOS()); // RULE: On a Mac a build wrapper admits by the device's published keep-free reading and refuses without one, so a script fixture under a scratch HOME publishes the reading with publishDiskReading — the helper, not the machine's own app, is what admits it. it.each(WRAPPERS)("%s refuses without the reading and is admitted by the helper's", (script, args) => { const home = scratchHome(); const bare = wrapper(home, script, args); expect(bare.status, bare.stderr).not.toBe(0); expect(bare.stderr).toMatch(NO_READING); expect(forgottenReadingHint(bare.stderr)).toContain(DISK_READING_HELPER); publishDiskReading(home); const admitted = wrapper(home, script, args); expect(admitted.status, forgottenReadingHint(admitted.stderr) ?? admitted.stderr).toBe(0); expect(admitted.stderr).toMatch(/device keep free: 1 bytes/); }); // RULE: A reading for another volume, or a stale one, admits nothing — the helper stamps the volume and the time it is called, so a fixture cannot pass on a reading the policy would refuse in production. it("a stale or foreign reading is no reading", () => { const home = scratchHome(); publishDiskReading(home, { volumeOf: "/dev" }); const foreign = wrapper(home, "mac-build-policy.sh", ["--check"]); expect(foreign.stderr).toMatch(/another volume/); expect(foreign.status).not.toBe(0); }); // RULE: linuxHostPath runs a script's Linux body on a Mac: the policy is never asked, so a test of the body cannot pass by the reading. it("the Linux host shim keeps the policy out of a body test", () => { const home = scratchHome(); const run = wrapper(home, "ci/require-free-disk.sh", ["", "0"], linuxHostPath()); expect(run.stderr).not.toMatch(NO_READING); expect(run.stderr).not.toMatch(/device keep free/); }); }); describe("the hint a forgotten reading earns", () => { // RULE: A test that fails on the policy's no-reading refusal names the helper it forgot; any other failure is left as it was. it("names the helper on the refusal and nowhere else", () => { const refusal = "REFUSING TO START: the device's keep free reading is unavailable. scripts/dev/machine-row.sh publishes it every five minutes under the launchd agent com.cottondb.machine-row"; expect(forgottenReadingHint(refusal)).toContain(DISK_READING_HELPER); expect(forgottenReadingHint(forgottenReadingHint(refusal)!)).toBeUndefined(); expect(forgottenReadingHint("expected 1 to be 0")).toBeUndefined(); }); });