Cotton code
66 lines · 3.4 KB · TypeScriptRaw
import { spawnSync } from "node:child_process";import { readFileSync } from "node:fs";import { homedir } from "node:os";import { join } from "node:path";import { describe, expect, it } from "vitest";/** * Nothing but the app and its installer names the owner's real data folder * (architect, 15 Oct 2026, after a lane's refusal test made a folder in it on * the serving Mac). A test or a script that spells the folder — `~/CottonDB`, * `$HOME/CottonDB`, this machine's home expanded in front of the name, or the * home lookup joined to it — is one bad run away from touching the household; * a refusal test runs against a synthetic home instead (a made-up * `/Users/x/CottonDB` is a string, not the folder), and a comment says "the * real data folder". * * A script whose job IS the real folder — the installer, the in-place move * on the Mac's own file, the nightly restore drill, the removal guard that * refuses it — says so in a `cotton:real-data-root — <why>` line, and the * coordinator runs it outside every lane's sandbox. This guard is one of them. * * cotton:real-data-root — the guard spells the folder to find it. */const ROOT = join(__dirname, "..", "..");const MARK = /cotton:real-data-root — \S/;/** Tests and scripts, by where they live and what they are called. */const SCOPE = /(^|\/)(__tests__|tests|Tests)\/|\.test\.(ts|mts|mjs|js)$|_tests?\.rs$|(^|\/)test_[^/]*\.py$|_test\.go$|^scripts\/|^apps\/[^/]+\/scripts\/|^infra\/|^fastlane\/|^\.forgejo\/|^\.claude\//;/** The real folder's name, ended: `~/CottonDB-install` and `~/CottonDB.v48` are other folders. */const NAME = String.raw`CottonDB(?![-\w]|\.\w)`;const SPELLINGS = [  new RegExp(String.raw`(~|\$HOME|\$\{HOME\}|${homedir().replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})/${NAME}`),  new RegExp(String.raw`(homedir\(\)|home_dir\(\)|Path\.home\(\)|NSHomeDirectory|homeDirectoryForCurrentUser|expanduser\(\s*["']~).*["'/]${NAME}`),];function offenders(): string[] {  const files = spawnSync("git", ["ls-files", "-z"], { cwd: ROOT, encoding: "utf8" }).stdout.split("\0").filter((f) => SCOPE.test(f));  const out: string[] = [];  for (const file of files) {    let text: string;    try { text = readFileSync(join(ROOT, file), "utf8"); } catch { continue; }    if (MARK.test(text)) continue;    text.split("\n").forEach((line, i) => {      if (SPELLINGS.some((s) => s.test(line))) out.push(`${file}:${i + 1}: ${line.trim().slice(0, 140)}`);    });  }  return out;}describe("the real data folder", () => {  // RULE: No test or script names the owner's real data folder (~/CottonDB in any spelling) unless its job is that folder and it says why; refusal tests use a synthetic home.  it("is named by no test and no script but the ones whose job it is", () => {    expect(offenders()).toEqual([]);  });  it("is caught in every spelling, and its neighbours are not", () => {    const hit = (line: string) => SPELLINGS.some((s) => s.test(line));    for (const line of ["rm -rf ~/CottonDB", 'D="$HOME/CottonDB/files"', "${HOME}/CottonDB", `${homedir()}/CottonDB/runs`, "join(homedir(), \"CottonDB\")", "home_dir().join(\"CottonDB\")", "see ~/CottonDB."]) {      expect(hit(line), line).toBe(true);    }    for (const line of ["~/CottonDB-install", "$HOME/CottonDB.v48", "~/CottonDev", "/Users/x/CottonDB/runs", "/Applications/CottonDB.app", "join(home, \"CottonDB\")", "~/.cache/cottondb/lane"]) {      expect(hit(line), line).toBe(false);    }  });});