Cotton code
115 lines · 4.6 KB · TypeScriptRaw
/** * A message's sender is its participant, whose row carries the contact. * The retired sender_contact_id column remains inert in the baseline DDL and * codec table: no current reader or writer may use it. * This guard scans every Rust crate and first proves its allowed files exist. */import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";import { dirname, join, relative, resolve } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");/** * Source trees a reader or writer of the column could live in. * * Every crate, not the four that happen to mention it today: the point of a * grave-keeping guard is that it notices the fifth crate somebody teaches to * read the column. */const CRATES = "crates";/** The column's grave — the only files allowed to spell it. */const ALLOWED = new Set([  // The inert baseline column and its foreign key.  "crates/cotton-store/src/schema/ddl.rs",  // The codec describes the baseline storage columns.  "crates/cotton-store/src/schema/table_specs.rs",]);const PATTERN = /sender_contact_id|senderContactId/;/** * A Rust unit test lives INSIDE `src/`, so `*_tests.rs` and `tests.rs` are * dropped by name — a test may spell the column as freely as it likes, and * several do: they assert it comes back NULL after an import and that a peer * cannot patch it. */function isTestFile(name: string): boolean {  return name.endsWith("_tests.rs") || name === "tests.rs";}function* walk(dir: string): Generator<string> {  for (const entry of readdirSync(dir)) {    const full = join(dir, entry);    if (entry === "target" || entry === "tests" || entry === "node_modules") continue;    if (statSync(full).isDirectory()) yield* walk(full);    else if (entry.endsWith(".rs") && !isTestFile(entry)) yield full;  }}/** * Code, with the prose taken out — whole lines only. * * The two production files that mention the column both do so in a COMMENT * saying they do not touch it (`messages/service.rs`, `mcp/handlers/records_2/ * messages.rs`), and a guard that could not tell the difference would turn * every future sentence about this rule into an allow-list entry. */function codeOnly(text: string): string {  return text    .split("\n")    .filter((line) => {      const start = line.trimStart();      return !start.startsWith("//") && !start.startsWith("*") && !start.startsWith("/*");    })    .join("\n");}function offenders(): string[] {  const root = join(REPO_ROOT, CRATES);  const found: string[] = [];  for (const file of walk(root)) {    const rel = relative(REPO_ROOT, file);    if (ALLOWED.has(rel)) continue;    if (PATTERN.test(codeOnly(readFileSync(file, "utf8")))) found.push(rel);  }  return found.sort();}describe("sender_contact_id is retired — nothing reads it, nothing writes it", () => {  it("the Rust tree this guard reads by path is actually there", () => {    // The loud-null half of the repo's by-path rule. A sweep over a directory    // that moved is a sweep over nothing, and it passes.    expect(existsSync(join(REPO_ROOT, CRATES)), `${CRATES}/ is not in the tree — did the crates move?`).toBe(true);    for (const file of ALLOWED) {      expect(existsSync(join(REPO_ROOT, file)), `${file} is the column's grave but is not on disk`).toBe(true);    }  });  it("no source file outside the grave spells the column", () => {    expect(      offenders(),      "a message's sender is its PARTICIPANT — `sender_participant_id`, whose row carries the contact. " +        "Reading the retired column names nobody for every message imported since schema v5",    ).toEqual([]);  });  it("and the grave still holds it — the sweep is not vacuous", () => {    // If the DDL or the codec table stopped naming the column, this guard    // would pass by sweeping a tree the column had left — which is exactly    // what a silent schema change looks like from here.    for (const file of ALLOWED) {      const text = readFileSync(join(REPO_ROOT, file), "utf8");      expect(text, `${file} read empty`).not.toHaveLength(0);      expect(PATTERN.test(text), `${file} no longer names the retired column`).toBe(true);    }  });  it("a comment naming the column is not a reader", () => {    expect(PATTERN.test(codeOnly("    // sender_contact_id was retired in schema v5"))).toBe(false);    expect(PATTERN.test(codeOnly("/// the message: sender_contact_id was retired in v5"))).toBe(false);    expect(PATTERN.test(codeOnly('    ("sender_contact_id", Codec::Text),'))).toBe(true);  });});