// `archilyzer case …` (bin/case.ts) end to end over a temp reports root and a temp corpus: define, coverage, // candidates, batches (no line over 200), hand-written findings, the gate blocking a planted private name at the // export (counts and row ids, never the name), and the export once the row is fixed. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test bin/case.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { privateNameHash } from "../lib/research/privateNames-server"; import { mutateDefinition } from "../lib/research/caseStore-server"; import { emptyDefinition, longestLine } from "../lib/research/casePipeline"; import { main } from "./case"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "case-cli-")); test.after(() => rm(ROOT, { recursive: true, force: true })); const reports = path.join(ROOT, "reports"); const caseDir = path.join(reports, ".umtool", "research", "wren"); const paths = { transcriptsDir: ROOT, channelsDir: path.join(ROOT, "channels"), sitesDir: path.join(ROOT, "sites"), lmdbPath: path.join(ROOT, "none.mdb") } as Paths; const PLANTED = "Ottoline Brackenridge"; const vtt = (cues: [number, string][]) => "WEBVTT\n\n" + cues.map(([a, t]) => `00:${String(Math.floor(a / 60)).padStart(2, "0")}:${String(a % 60).padStart(2, "0")}.000 --> 00:${String(Math.floor((a + 3) / 60)).padStart(2, "0")}:${String((a + 3) % 60).padStart(2, "0")}.000\n${t}\n`).join("\n"); await mkdir(caseDir, { recursive: true }); await writeFile(path.join(caseDir, "case.json"), JSON.stringify({ format: "umtool-research-case", version: 1, id: "wren", title: "Wren and the shop" })); for (const [slug, id, date, line] of [ ["wren-own", "o1", "20150310", "welcome to the lantern shop"], ["talk-show", "t1", "20150320", "Wren Halloway at the lantern shop"], ] as const) { const dir = path.join(paths.channelsDir, slug, "data", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(paths.channelsDir, slug, "config.json"), JSON.stringify({ handling: "youtube", name: slug })); await writeFile(path.join(dir, "metadata.info.json"), JSON.stringify({ id, title: `${slug} ${id}`, upload_date: date, duration: 600, webpage_url: `https://www.youtube.com/watch?v=${id}` })); await writeFile(path.join(dir, "transcript.en-orig.vtt"), vtt([[30, line]])); } const flags = { root: reports }; const quiet = async (fn: () => Promise): Promise<{ value: T; out: string; err: string }> => { const log = console.log; const error = console.error; let out = ""; let err = ""; console.log = (...a: unknown[]) => void (out += `${a.join(" ")}\n`); console.error = (...a: unknown[]) => void (err += `${a.join(" ")}\n`); try { return { value: await fn(), out, err }; } finally { console.log = log; console.error = error; } }; test("a case through the CLI: define → coverage → candidates → batches → findings → the gate → export", async () => { const defFile = path.join(ROOT, "pipeline.json"); await writeFile( defFile, JSON.stringify({ subjects: [{ name: "Wren Halloway", aliases: ["Wren"], channels: ["wren-own"] }], scope: { channels: ["talk-show"], groups: [], sites: [] }, eras: [{ id: "early", from: "2015-01-01", to: "2016-12-31", families: { shop: "lantern shop" } }], groups: [{ id: "own", label: "Her own", channels: "subject" }, { id: "others", label: "Coverage", channels: "coverage" }], }), ); assert.equal((await quiet(() => main("define", ["wren"], { ...flags, file: defFile }))).value, 0); await writeFile(path.join(ROOT, "bad.json"), JSON.stringify({ privateNames: { terms: ["x"] } })); const refused = await quiet(() => main("define", ["wren"], { ...flags, file: path.join(ROOT, "bad.json") })); assert.equal(refused.value, 2); assert.match(refused.err, /privateNames\.terms is never stored/); assert.equal((await quiet(() => main("show", ["nosuch"], flags))).value, 1); const cov = await quiet(() => main("coverage", ["wren"], { ...flags, json: true }, { paths })); assert.equal(cov.value, 0); assert.equal(JSON.parse(cov.out).rows.length, 2); const cand = await quiet(() => main("candidates", ["wren"], { ...flags, json: true }, { paths })); assert.equal(JSON.parse(cand.out).windows, 2); const bat = await quiet(() => main("batches", ["wren"], { ...flags, json: true })); const batches = JSON.parse(bat.out) as { id: string; file: string; state: string }[]; assert.deepEqual(batches.map((b) => [b.id, b.state]), [["own-1-001", "pending"], ["others-1-001", "pending"]]); for (const b of batches) assert.ok(longestLine(await readFile(path.join(caseDir, b.file), "utf8")) <= 200); // The private name lands in the case's definition AFTER a row naming that person was written by hand. const rows = path.join(ROOT, "rows.jsonl"); const r = (o: Record) => JSON.stringify({ batch: "own-1-001", slug: "wren-own", id: "o1", date: "20150310", title: "wren-own o1", t: 30, url: "https://www.youtube.com/watch?v=o1", category: "shop", relevance: 3, quote: "welcome to the lantern shop", summary: "She opens the shop", speaker: "Wren Halloway", confidence: "high", round: 1, by: "hand", ...o }); await writeFile(rows, `${r({})}\n${r({ batch: "others-1-001", slug: "talk-show", id: "t1", date: "20150320", title: "talk-show t1", summary: `Her friend ${PLANTED} was there`, quote: "Wren Halloway at the lantern shop", speaker: "host" })}\n`); assert.equal((await quiet(() => main("findings", ["wren"], { ...flags, file: rows }))).value, 0); await mutateDefinition(caseDir, "wren", (d) => void (d.privateNames.sha256 = [privateNameHash(PLANTED)]), () => emptyDefinition("wren")); const blocked = await quiet(() => main("export", ["wren"], flags, { paths })); assert.equal(blocked.value, 1); assert.match(blocked.err, /the privacy gate blocked the export: \d+ private-name hits? in 1 item \(f2\)/); assert.doesNotMatch(blocked.err + blocked.out, /Ottoline|Brackenridge/i); assert.ok(!(await readdir(caseDir)).includes("REPORT.md")); const stages = JSON.parse(await readFile(path.join(caseDir, "stages.json"), "utf8")); assert.equal(stages.export.state, "failed"); assert.doesNotMatch(JSON.stringify(stages), /Ottoline|Brackenridge/i); // A new row withheld; and adding the same row again is refused by the gate at the write, naming the row. const again = await quiet(() => main("findings", ["wren"], { ...flags, file: rows })); assert.equal(again.value, 1); assert.match(again.err, /blocked these findings: .*\(f4\)/); const fixed = (await readFile(path.join(caseDir, "findings.jsonl"), "utf8")).replace(PLANTED, "[name withheld]"); await writeFile(path.join(caseDir, "findings.jsonl"), fixed); const ok = await quiet(() => main("export", ["wren"], flags, { paths })); assert.equal(ok.value, 0, ok.err); const md = await readFile(path.join(caseDir, "REPORT.md"), "utf8"); assert.match(md, /^# Wren and the shop/); assert.match(md, /## Her own\n\n- She opens the shop: "welcome to the lantern shop"/); assert.match(md, /## Coverage\n\nScope: 2 channels \(talk-show, wren-own\)/); assert.match(await readFile(path.join(caseDir, "REPORT.html"), "utf8"), /

Coverage<\/h2>/); });