/** * A SCRIPT THAT WRITES `cotton_links` TAKES THE ROW'S ID FROM * `scripts/lib/link-id.mjs`. * * Since schema v53 the file a link's target lives in is part of the link's * identity, and the store derives the id with `link_id_for`. A script deriving * its own id (a name-hash, a random uuid, the four ends without the target's * file) writes a second row for an edge the app writes once, which then clashes * with it on `uq_cotton_links_ends`; an id derived without the target file * collides with the same ends pointing into another file. `link-id.mjs` is the * port pinned to the store's vectors, so naming the table means importing it. * * The Rust half is `crates/cotton-store/tests/no_record_link_id_call_outside_link_id_for.rs`. */ import { 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)), "../.."); const SCRIPTS = join(REPO_ROOT, "scripts"); /** Files that name the table without writing a row into it, each with why. */ const NAMES_THE_TABLE_ONLY: Record = { "scripts/lib/link-id.mjs": "the port itself: it derives the id and writes nothing", "scripts/lib/gen-kind.ts": "it only names the table, to check the kind columns of LINKS_TABLE's DDL; it writes no row", }; const SOURCE = /\.(mjs|cjs|js|ts|mts)$/; function walk(dir: string, found: string[]): void { for (const name of readdirSync(dir).sort()) { if (name === "node_modules" || name === "__tests__") continue; const path = join(dir, name); if (statSync(path).isDirectory()) { walk(path, found); } else if (SOURCE.test(name)) { found.push(path); } } } /** Whether `text` imports the port, by any relative path ending in `lib/link-id.mjs`. */ function importsLinkId(text: string): boolean { return /from\s+["'][^"']*lib\/link-id\.mjs["']/.test(text) || /from\s+["']\.\/link-id\.mjs["']/.test(text); } function scriptsNamingTheTable(): { file: string; text: string }[] { const files: string[] = []; walk(SCRIPTS, files); expect(files.length, "the walk found scripts").toBeGreaterThan(50); return files .map((path) => ({ file: relative(REPO_ROOT, path), text: readFileSync(path, "utf8") })) .filter(({ text }) => text.includes("cotton_links")); } describe("a script writing cotton_links takes the id from link-id.mjs", () => { it("every script naming cotton_links imports the port, or is named above with why", () => { const strays = scriptsNamingTheTable() .filter(({ file }) => !(file in NAMES_THE_TABLE_ONLY)) .filter(({ text }) => !importsLinkId(text)) .map(({ file }) => file); expect(strays).toEqual([]); }); it("the exemptions still name the table, so an entry leaves with the change that ends it", () => { const naming = new Set(scriptsNamingTheTable().map(({ file }) => file)); for (const file of Object.keys(NAMES_THE_TABLE_ONLY)) { expect(naming.has(file), file).toBe(true); } }); it("the import check finds what it says it finds", () => { expect(importsLinkId('import { linkIdFor } from "./lib/link-id.mjs";')).toBe(true); expect(importsLinkId("import { linkIdFor } from '../lib/link-id.mjs';")).toBe(true); expect(importsLinkId('import { uid } from "./lib/other.mjs";')).toBe(false); expect(importsLinkId("// see lib/link-id.mjs")).toBe(false); }); });