// parseScrubRules' line split moved to lib/scrubRuleLine.ts (demo epic D4) as // a PURE MOVE. This keeps the pre-move implementation, frozen, and asserts the // new parseScrubRules answers exactly what it answered — the lines filter-repo // reads, every denied literal and where it came from, and every refusal — over // a rule file covering all four forms, comments, blanks, a BOM and CRLF. // // Run with: node_modules/.bin/tsx --test common/publish/scrubRuleLine.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { parseScrubRules, HOME_REPLACEMENT, BUILT_IN_HOME_RULE, SourceRefusal } from "./source"; import { isScrubSkip, operatorLines, splitScrubLine, scrubLineDenied } from "../lib/scrubRuleLine"; import { MASKED_HOME } from "../lib/privacy/compile"; // ── the implementation as it was before the move (06c95c3f), verbatim ──────── function legacyOperatorLines(text: string): string[] { return text.replace(/^\uFEFF/, "").split("\n").map((l) => l.replace(/\r$/, "")); } function legacyParseScrubRules(text: string, homeDir: string) { const lines: string[] = []; const denied: Array<{ text: string; from: string }> = []; const home = homeDir.replace(/\/+$/, ""); if (home.length > 1 && home !== HOME_REPLACEMENT) { lines.push(`${home}==>${HOME_REPLACEMENT}`); denied.push({ text: home, from: BUILT_IN_HOME_RULE }); } legacyOperatorLines(text).forEach((line, n) => { const t = line.trim(); if (t === "" || t.startsWith("#")) return; const i = line.lastIndexOf("==>"); let lhs = i === -1 ? line : line.slice(0, i); const from = `scrub line ${n + 1} lhs`; for (const prefix of ["regex:", "glob:", "literal:"]) { if (lhs.startsWith(prefix)) { if (lhs.length === prefix.length) { throw new SourceRefusal(`scrub line ${n + 1} has an empty left side — it would match nothing and deny nothing`); } if (prefix === "literal:") lhs = lhs.slice(prefix.length); else lhs = ""; break; } } if (i === 0) { throw new SourceRefusal(`scrub line ${n + 1} has an empty left side — it would match nothing and deny nothing`); } lines.push(line); if (lhs) denied.push({ text: lhs, from }); }); return { lines, denied }; } const outcome = (f: () => unknown) => { try { return { ok: f() }; } catch (err) { return { err: `${(err as Error).name}: ${(err as Error).message}` }; } }; const DIR = mkdtempSync(path.join(tmpdir(), "scrub-line-")); const FILE = path.join(DIR, "scrub.txt"); const RULE_FILE = "\uFEFFalpha==>beta\r\n" + "# a comment\r\n" + "\r\n" + " \r\n" + " # an indented comment\n" + "literal:gamma==>delta\n" + "regex:ep+silon(\\d+)==>zeta\\1\n" + "glob:eta*theta==>iota\n" + "kappa with no arrow\n" + "lambda==>mu==>nu\n" + "literal:xi==>\n" + "regex:omicron\n" + " spaced ==> rhs \n" + "trailing\r"; test("parseScrubRules answers exactly what it did before the move", () => { writeFileSync(FILE, RULE_FILE); const text = readFileSync(FILE, "utf8"); for (const home of ["/home/someone", "/home/someone/", "/", HOME_REPLACEMENT, ""]) { assert.deepEqual(outcome(() => parseScrubRules(text, home)), outcome(() => legacyParseScrubRules(text, home)), home); } }); test("the refusals are the same, at the same line", () => { for (const bad of ["==>x\n", "a==>b\nregex:\n", "literal:==>x", "ok==>1\n\nglob:==>y\n", "\uFEFF==>z\r\n"]) { writeFileSync(FILE, bad); const text = readFileSync(FILE, "utf8"); const got = outcome(() => parseScrubRules(text, "/home/someone")); assert.deepEqual(got, outcome(() => legacyParseScrubRules(text, "/home/someone")), JSON.stringify(bad)); assert.ok("err" in got, JSON.stringify(bad)); } }); test("the displayers' masked home dir is the mirror's HOME_REPLACEMENT", () => { assert.equal(MASKED_HOME, HOME_REPLACEMENT); }); test("operatorLines is the old one", () => { for (const t of [RULE_FILE, "", "\uFEFF", "a\r\nb\r", "\r\n\r\n"]) assert.deepEqual(operatorLines(t), legacyOperatorLines(t)); }); test("splitScrubLine: the four forms, the missing arrow, the last arrow", () => { const split = (l: string) => { const r = splitScrubLine(l, 1); assert.ok(r.ok); return r.line; }; assert.deepEqual(split("a==>b"), { kind: "plain", pattern: "a", replacement: "b" }); assert.deepEqual(split("literal:a==>b"), { kind: "literal", pattern: "a", replacement: "b" }); assert.deepEqual(split("regex:a+==>b"), { kind: "regex", pattern: "a+", replacement: "b" }); assert.deepEqual(split("glob:a*==>b"), { kind: "glob", pattern: "a*", replacement: "b" }); assert.deepEqual(split("a"), { kind: "plain", pattern: "a", replacement: null }); assert.deepEqual(split("a==>b==>c"), { kind: "plain", pattern: "a==>b", replacement: "c" }); assert.equal(scrubLineDenied(split("regex:a==>b")), ""); assert.equal(scrubLineDenied(split("literal:a==>b")), "a"); assert.equal(isScrubSkip(" # x"), true); assert.equal(isScrubSkip(" x"), false); }); test.after(() => rmSync(DIR, { recursive: true, force: true }));