import { test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/lookoutNoChannelNames.test.ts // // NO CHANNEL IS NAMED IN THE LOOKOUT'S CODE (lane/lookout, acceptance c). A // lookout is configured on the machine that runs it; the standing one is a // settings patch there, never a default, an example or a fixture here. This // test walks every file whose path names a lookout — the libs, the // controllers, the ops routes and their tests, and whatever later slices add // under those names — and fails on any word of the pilot lookout's channel // names. The words are kept as SHA-256 digests so the guard does not itself // spell what it forbids. const HERE = path.dirname(fileURLToPath(import.meta.url)); const REPO = path.resolve(HERE, "..", ".."); const ROOTS = ["common/lib", "common/controller", "common/bin", "common/jobs", "editor/app", "umtool/lib", "umtool/app", "umtool/bin"]; // sha256 of each forbidden word (lowercase): the two distinctive words of the // pilot lookout's four channel slugs. const FORBIDDEN = new Set([ "7a0b56a32407bfffffb453b775850130b36277a20de9611f153c0d3bf3dfa153", "877a89b30f7ffb2b5b079e4a84066c05c476a1dda97209fce21d3a4aefac3dfc", ]); const sha = (w: string) => createHash("sha256").update(w).digest("hex"); async function lookoutFiles(dir: string, under = false): Promise { let entries; try { entries = await readdir(dir, { withFileTypes: true }); } catch { return []; } const out: string[] = []; for (const e of entries) { if (e.name === "node_modules" || e.name.startsWith(".")) continue; const p = path.join(dir, e.name); const named = under || /lookout/i.test(e.name); if (e.isDirectory()) out.push(...(await lookoutFiles(p, named))); else if (named && /\.(ts|tsx|mjs|js|json|md)$/.test(e.name)) out.push(p); } return out; } test("the guard is live: its digests match words the repo's own docs use", async () => { // A positive control, so a mistyped digest cannot silently disable the // guard: the repo's instructions and the ops examples DO name these // channels (they predate the lookout), and every digest must match there. const docs = ["AGENTS.md", "plans/FACTS.md", "scripts/archilyzer-ops.mjs"]; const seen = new Set(); for (const f of docs) { for (const w of (await readFile(path.join(REPO, f), "utf8")).toLowerCase().match(/[a-z0-9]+/g) ?? []) { const d = sha(w); if (FORBIDDEN.has(d)) seen.add(d); } } assert.deepEqual([...seen].sort(), [...FORBIDDEN].sort()); }); test("no lookout file names a channel of the pilot lookout", async () => { const files = (await Promise.all(ROOTS.map((r) => lookoutFiles(path.join(REPO, r))))).flat(); assert.ok(files.length >= 6, `found the lookout's files (${files.length})`); const hits: string[] = []; for (const f of files) { const words = new Set((await readFile(f, "utf8")).toLowerCase().match(/[a-z0-9]+/g) ?? []); for (const w of words) if (FORBIDDEN.has(sha(w))) hits.push(path.relative(REPO, f)); } assert.deepEqual(hits, []); });