// The research-case pipeline contract (lib/research/caseTypes.ts): a whole case typed end to end, the guards, the // statuses the lanes rely on (`quarantined`), and the private-name gate's hashing (never the term in a file). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/research/caseTypes.test.ts import test from "node:test"; import assert from "node:assert/strict"; import { BATCH_LINE_MAX, CASE_FILES, CASE_PIPELINE_FORMAT, CASE_PIPELINE_VERSION, type CaseBatch, type CaseDefinition, type CaseLead, type CandidateWindow, type FindingRow, isCaseStatus, isFindingBy, isLeadDisposition, isRelevance, isVerifyStatus, privateNameCandidates, VERIFY_STATUSES, } from "./caseTypes"; import { privateNameHash, privateNameHits } from "./privateNames-server"; const definition = { format: CASE_PIPELINE_FORMAT, version: CASE_PIPELINE_VERSION, case: "example-case", subjects: [{ name: "Example Subject", aliases: ["ex", "the subject"] }], scope: { channels: ["example"], groups: [], sites: ["jeralyzer"] }, eras: [{ id: "early", from: "2014-01-01", to: "2016-12-31", families: { name: "\\bexample\\b" } }], groups: [ { id: "own", label: "The subject's own", channels: "subject", era: "early" }, { id: "others", label: "Coverage", channels: "coverage" }, ], privateNames: { sha256: [privateNameHash("Jane Q Private")], note: "a private person named in a forum thread" }, readerPrompts: { common: "Quote verbatim.", own: "The subject's own words." }, status: "reading", createdAt: "2026-11-01T00:00:00Z", updatedAt: "2026-11-01T00:00:00Z", } satisfies CaseDefinition; const window = { key: "example/abc123@120", slug: "example", id: "abc123", date: "20150301", title: "A stream", from: 120, to: 180, era: "early", group: "own", track: "en-orig", hits: [{ t: 131.5, family: "name", match: "example" }], text: "and then the example came up", } satisfies CandidateWindow; const batch = { id: "own-1-001", group: "own", round: 1, windows: [window.key], chars: window.text.length, file: `${CASE_FILES.batchDir}/own-1-001.txt`, state: "read", } satisfies CaseBatch; const finding = { row: "f1", batch: batch.id, slug: window.slug, id: window.id, date: window.date, title: window.title, t: 131.5, url: "https://example.invalid/v/abc123?t=131", category: "first mention", relevance: 3, quote: "the example came up", summary: "The subject names it on stream.", speaker: "Example Subject", confidence: "high", round: 1, by: "local", quoteCheck: "found", verify: { status: "quarantined", at: "2026-11-01T01:00:00Z", how: "hand", note: "forum-only" }, evidence: [{ kind: "forum-attachment", post: "12", url: "https://forum.invalid/t/1#12" }], } satisfies FindingRow; const lead = { key: "x:@someone", url: "https://x.com/someone", platform: "x", count: 4, disposition: "never-archive", } satisfies CaseLead; test("a whole case types end to end and round-trips through JSON", () => { for (const value of [definition, window, batch, finding, lead]) { assert.deepEqual(JSON.parse(JSON.stringify(value)), value); } assert.equal(batch.file.startsWith(`${CASE_FILES.batchDir}/`), true); assert.ok(BATCH_LINE_MAX <= 200, "a batch line over 200 chars is truncated by readers' file tools"); }); test("the guards accept the contract's values and refuse others", () => { assert.ok(VERIFY_STATUSES.includes("quarantined")); for (const s of VERIFY_STATUSES) assert.ok(isVerifyStatus(s)); assert.equal(isVerifyStatus("ok"), false, "the board's QuoteVerify states are not a finding's"); assert.ok(isLeadDisposition("never-archive")); assert.equal(isLeadDisposition("ignore"), false); assert.ok(isFindingBy("hand") && isFindingBy("code")); assert.equal(isFindingBy("agent"), false); assert.ok(isCaseStatus(definition.status)); assert.ok(isRelevance(1) && isRelevance(3)); assert.equal(isRelevance(0) || isRelevance(4) || isRelevance("2"), false); }); test("private names: stored as hashes, matched on words and 2–3-word phrases, reported as a count", () => { const hash = privateNameHash("Jane Q Private"); assert.match(hash, /^[0-9a-f]{64}$/); assert.equal(privateNameHash(" jane q PRIVATE "), hash, "case and spacing do not change the hash"); assert.equal(JSON.stringify(definition).toLowerCase().includes("jane"), false, "the definition never holds the term"); assert.deepEqual(privateNameCandidates("Hi, Jane Q."), ["hi", "hi jane", "hi jane q", "jane", "jane q", "q"]); assert.equal(privateNameHits("then Jane Q Private, posted it", definition.privateNames.sha256), 1); assert.equal(privateNameHits("then Jane posted it", definition.privateNames.sha256), 0); assert.equal(privateNameHits("anything", []), 0); }); test("lane C: the evidence labels, the excluded sentence the editor says, and a redaction that never names", async () => { const { EXCLUDED_SENTENCE, evidenceLabel, forumAttachmentLabel, isEvidenceKind, isEvidenceState } = await import("./caseTypes"); const { WAYBACK_EXCLUDED_PHRASE } = await import("../waybackClient"); const { withholdPrivateNames } = await import("./casePrivacy-server"); const redactPrivateNames = (t: string, h: string[]) => { const r = withholdPrivateNames(t, h); return { text: r.text, hits: r.replaced }; }; assert.equal(EXCLUDED_SENTENCE, WAYBACK_EXCLUDED_PHRASE, "one sentence in every surface"); assert.equal(forumAttachmentLabel(14, "880011"), "forum attachment, post #14, unverified"); assert.equal(forumAttachmentLabel(undefined, "880011"), "forum attachment, post #880011, unverified"); assert.equal(evidenceLabel({ kind: "forum-attachment", position: 3 }), "forum attachment, post #3, unverified"); assert.equal(evidenceLabel({ kind: "wayback", state: "excluded" }), EXCLUDED_SENTENCE); assert.equal(evidenceLabel({ kind: "wayback", captureTs: "20140304120000" }), "Wayback capture, 2014-03-04"); assert.ok(isEvidenceKind("wayback") && isEvidenceState("excluded")); assert.equal(isEvidenceState("done"), false); const hashes = definition.privateNames.sha256; const r = redactPrivateNames("then Jane Q Private posted it, and @jane posted too", hashes); assert.equal(r.text, "then [name withheld] posted it, and @jane posted too"); assert.equal(r.hits, 1); const handle = redactPrivateNames("see x.com/QuietHandle for more", [privateNameHash("quiethandle")]); assert.equal(handle.text, "see x.com/[name withheld] for more"); assert.deepEqual(redactPrivateNames("nothing here", []), { text: "nothing here", hits: 0 }); // A possessive does not slip a name past the gate, nor past a redaction. assert.equal(privateNameHits("ask QuietHandle’s friend", [privateNameHash("quiethandle")]), 1); assert.equal(privateNameHits("Jane Q Private's post", hashes), 1); assert.equal(redactPrivateNames("found QuietHandle's account", [privateNameHash("quiethandle")]).text, "found [name withheld]'s account"); });