Cotton code
84 lines · 4.5 KB · TypeScriptRaw
import { spawnSync } from "node:child_process";import { existsSync, mkdirSync, mkdtempSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { describe, expect, it } from "vitest";/** * `scripts/lib/smoke-out.sh` decides where the iOS launch smoke writes and the * only thing it may delete. On 4 Oct 2026 the smoke's prune removed "every * sibling of the output directory except the last ten" with the output set to * `$HOME/x`, and deleted most of a home folder. Every case here runs the real * functions against a FAKE home and looks at what is left on disk. */const ROOT = join(__dirname, "..", "..");const LIB = join(ROOT, "scripts", "lib", "smoke-out.sh");function fakeHome(): string {  const home = mkdtempSync(join(tmpdir(), "smoke-out-home-"));  for (const precious of ["Code", "Desktop", "Documents", "Downloads", "Library"]) {    mkdirSync(join(home, precious, "kept"), { recursive: true });    writeFileSync(join(home, precious, "kept", "file.txt"), "a person's own file");  }  return home;}function bash(home: string, script: string) {  return spawnSync("bash", ["-euo", "pipefail", "-c", `. "${LIB}"\n${script}`], {    env: { PATH: process.env.PATH ?? "", HOME: home },    encoding: "utf8",  });}/// RULE: Walk producers write persistent marked evidence outside caches and never prune prior runs.it("makes marked persistent default runs and leaves earlier evidence intact", () => {  const home = fakeHome();  const first = bash(home, 'smoke_out_default "feature/x y"');  expect(first.status).toBe(0);  expect(first.stdout).toContain(`${home}/cotton-captures/ios-smoke/feature-x-y/`);  expect(existsSync(join(first.stdout, ".cotton-evidence"))).toBe(true);  const second = bash(home, 'smoke_out_default "other"');  expect(second.status).toBe(0);  expect(existsSync(first.stdout)).toBe(true);  expect(bash(home, `smoke_out_refusal "${home}/.cache/walk"`).status).toBe(1);});describe("a caller's output directory is judged before anything is written", () => {  // RULE: The iOS smoke refuses an output directory that is not at least two levels below the home, is not under the home, is relative, holds . or .., is a symlink, or already holds files; an empty or new directory two levels down is taken.  it("refuses a directory directly under the home and takes one two levels down", () => {    const home = fakeHome();    const refused = (dir: string) => bash(home, `smoke_out_refusal "${dir}"`);    expect(refused(join(home, "x")).status).toBe(1);    expect(refused(home).status).toBe(1);    expect(refused(`${home}/`).status).toBe(1);    expect(refused("/tmp/somewhere/else").status).toBe(1);    expect(refused("relative/dir").status).toBe(1);    expect(refused(join(home, "a") + "/../b/c").status).toBe(1);    expect(refused(join(home, "Code", "kept")).status).toBe(1); // holds files    symlinkSync(join(home, "Documents"), join(home, "Code", "link"));    expect(refused(join(home, "Code", "link")).status).toBe(1);    expect(refused(join(home, "smoke", "run-1")).status).toBe(0); // new    mkdirSync(join(home, "smoke", "empty"), { recursive: true });    expect(refused(join(home, "smoke", "empty")).status).toBe(0);    expect(refused(join(home, "x")).stdout).toContain("at least two levels below");  });  // RULE: Walk evidence never enters a repository (owner, 10 Oct 2026) — a walk's or a smoke's output directory inside a git work tree is refused before anything is written, whether the folder exists yet or not, and every walk (walk-board.sh through smoke_out_refusal, release-walk.sh on its cache) asks.  it("refuses an output directory inside a git repository", () => {    const home = fakeHome();    const repo = join(home, "Code", "repo");    mkdirSync(repo, { recursive: true });    expect(spawnSync("git", ["init", "-q", repo]).status).toBe(0);    const refused = (dir: string) => bash(home, `smoke_out_refusal "${dir}"`);    expect(refused(join(repo, "walks", "run-1")).status).toBe(1);    mkdirSync(join(repo, "evidence"), { recursive: true });    expect(refused(join(repo, "evidence")).stdout).toContain("inside a git repository");    expect(refused(join(home, "walks", "run-1")).status).toBe(0);    const walkBoard = readFileSync(join(ROOT, "scripts", "walk-board.sh"), "utf8");    expect(walkBoard).toMatch(/smoke_out_refusal "\$RUN"/);    const releaseWalk = readFileSync(join(ROOT, "scripts", "release-walk.sh"), "utf8");    expect(releaseWalk).toMatch(/out_in_repository "\$BASE"/);  });});