// An article written by hand (release 32 R2): a blank start, and a claim added by // hand — new quotes checked strictly against a synthetic record before anything // is written. Temp stores and a synthetic channel only. // // Run with: node_modules/.bin/tsx --test controller/handEdits.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { createIteration, currentIteration, makeCurrent, readIteration, readIterations, updateIteration, writeIterationFile } from "../lib/report/iterations"; import { addClaimByHand, defaultHandBase, HandEditError, parseMoment, replacePageCitationWithRecord, startBlankArticle } from "./handEdits"; const ROOTS: string[] = []; after(() => { for (const r of ROOTS) rmSync(r, { recursive: true, force: true }); }); const NOW = new Date("2026-10-16T12:00:00.000Z"); const CHAN = "synth-hand"; const ts = (s: number) => new Date(s * 1000).toISOString().slice(11, 23); const vtt = (cues: [number, number, string][]) => "WEBVTT\nKind: captions\nLanguage: en\n\n" + cues.map(([a, b, text]) => `${ts(a)} --> ${ts(b)}\n${text}\n`).join("\n"); function store() { const root = mkdtempSync(path.join(tmpdir(), "hand-edits-")); ROOTS.push(root); const channelsDir = path.join(root, "channels"); const put = (file: string, text: string) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, text); }; put(path.join(channelsDir, CHAN, "config.json"), JSON.stringify({ handling: "youtube", name: "Synth Hand", url: "https://www.youtube.com/@synthhand/videos" })); const dir = path.join(channelsDir, CHAN, "data", "rec1"); put( path.join(dir, "metadata.info.json"), JSON.stringify({ id: "rec1", title: "Synthetic stream", channel: CHAN, upload_date: "20260110", duration: 600, webpage_url: "https://www.youtube.com/watch?v=rec1", extractor_key: "Youtube" }), ); put( path.join(dir, "transcript.en.vtt"), vtt([ [10, 14, "The bakery opened in the spring of that year."], [14, 18, "I was there for the first loaf, honestly."], [18, 22, "Nobody believed the oven would hold up."], ]), ); return { articlesDir: path.join(root, "reports"), exportIndexDir: path.join(root, ".export-index"), channelsDir }; } const reportOf = (paths: ReturnType, id: string, n: number) => JSON.parse(readFileSync(path.join(paths.articlesDir, id, "iterations", String(n), "report.json"), "utf8")); test("a blank start: i1 of kind hand, done, a titled report with no sections; an existing id is refused", async () => { const paths = store(); const { iteration, report } = await startBlankArticle("bakery", { title: "The Bakery", kind: "factcheck" }, paths, NOW); assert.equal(iteration.n, 1); assert.equal(iteration.kind, "hand"); assert.equal(iteration.model, "hand"); assert.equal(iteration.status, "done"); assert.deepEqual(report.sections, []); assert.equal(reportOf(paths, "bakery", 1).title, "The Bakery"); // Nothing made current: an article in the making. assert.equal((await currentIteration("bakery", paths)).report, null); await assert.rejects(startBlankArticle("bakery", { title: "Again" }, paths, NOW), /has iterations already/); await assert.rejects(startBlankArticle("other", { title: " " }, paths, NOW), HandEditError); }); test("a claim by hand on a blank start: amends the hand draft in place, a new section, a new quote checked", async () => { const paths = store(); await startBlankArticle("bakery", { title: "The Bakery" }, paths, NOW); const r = await addClaimByHand( "bakery", { section: { title: "The Opening" }, claim: { text: "The article says the bakery opened in spring.", verdict: "CORROBORATED" }, add: [{ kind: "video", channel: CHAN, id: "rec1", start: 10, end: 14, quote: "The bakery opened in the spring of that year." }], }, paths, NOW, ); assert.equal(r.n, 1); assert.equal(r.amended, true); assert.equal(r.section, "the-opening"); assert.equal(r.claim, "c1"); assert.deepEqual(r.added.map((a) => [a.id, a.check.status]), [["h1", "ok"]]); const doc = reportOf(paths, "bakery", 1); assert.deepEqual(doc.sections[0].claims[0], { id: "c1", text: "The article says the bakery opened in spring.", verdict: "CORROBORATED", citations: ["h1"] }); assert.equal(doc.citations.h1.quote, "The bakery opened in the spring of that year."); assert.match((await readIteration("bakery", 1, paths))!.feedback ?? "", /added c1 to “The Opening” \(1 new citation\)/); // A second claim, in the same section, citing the report's h1: c2, still i1. const r2 = await addClaimByHand("bakery", { section: { id: "the-opening" }, claim: { text: "The article says she was there." }, cite: ["h1"] }, paths, NOW); assert.deepEqual([r2.n, r2.claim, r2.amended], [1, "c2", true]); assert.equal(reportOf(paths, "bakery", 1).sections[0].claims.length, 2); }); test("a quote that is not verbatim in the record is refused, and nothing is written", async () => { const paths = store(); await startBlankArticle("bakery", { title: "The Bakery" }, paths, NOW); const before = readFileSync(path.join(paths.articlesDir, "bakery", "iterations", "1", "report.json"), "utf8"); await assert.rejects( addClaimByHand( "bakery", { section: { title: "Wrong" }, claim: { text: "A paraphrase." }, add: [{ kind: "video", channel: CHAN, id: "rec1", start: 10, end: 14, quote: "The bakery never opened in the spring." }], }, paths, NOW, ), (e: unknown) => e instanceof HandEditError && /does not check against the record/.test(e.message) && /inexact|drift/.test(e.problems.join(" ")), ); await assert.rejects( addClaimByHand("bakery", { section: { title: "Gone" }, claim: { text: "x" }, add: [{ kind: "video", channel: CHAN, id: "nope", start: 1, end: 2, quote: "a b c" }] }, paths, NOW), (e: unknown) => e instanceof HandEditError && /missing-record/.test(e.problems.join(" ")), ); assert.equal(readFileSync(path.join(paths.articlesDir, "bakery", "iterations", "1", "report.json"), "utf8"), before); }); test("a claim on a model's iteration is a NEW hand iteration, its parent the base; nothing made current", async () => { const paths = store(); const it = await createIteration("swept", { kind: "sweep", model: "qwen3:8b", question: "What was said?" }, paths, NOW); await writeIterationFile( "swept", it.n, "report.json", { format: "archilyzer-report", version: 1, id: "swept", kind: "factcheck", title: "Swept", sections: [{ id: "s1", title: "One", claims: [{ id: "c1", text: "First." }] }] }, paths, ); await updateIteration("swept", it.n, { status: "done" }, paths, NOW); await makeCurrent("swept", it.n, paths, NOW); assert.equal(await defaultHandBase("swept", paths), 1); const r = await addClaimByHand("swept", { section: { id: "s1" }, claim: { text: "Second.", verdict: "NOT_FOUND" } }, paths, NOW); assert.deepEqual([r.n, r.amended, r.claim], [2, false, "c2"]); const i2 = (await readIterations("swept", paths)).iterations.find((x) => x.n === 2)!; assert.deepEqual([i2.kind, i2.parent, i2.status, i2.model], ["hand", 1, "done", "hand"]); assert.equal(reportOf(paths, "swept", 2).sections[0].claims[1].verdict, "NOT_FOUND"); assert.equal((await currentIteration("swept", paths)).report, 1); // The base (i1) is current: a claim on it again is another new iteration, i3. const r3 = await addClaimByHand("swept", { base: 1, section: { id: "s1" }, claim: { text: "Third." } }, paths, NOW); assert.equal(r3.n, 3); // i2 is hand and not current: amended. const r4 = await addClaimByHand("swept", { base: 2, section: { title: "More" }, claim: { text: "Fourth." } }, paths, NOW); assert.deepEqual([r4.n, r4.amended], [2, true]); }); test("refusals: a verdict on a sweep, an unknown citation or section, no report to add to", async () => { const paths = store(); await startBlankArticle("plain", { title: "Plain", kind: "sweep" }, paths, NOW); await assert.rejects(addClaimByHand("plain", { section: { title: "S" }, claim: { text: "x", verdict: "PARTLY" } }, paths, NOW), /carry no verdict/); await assert.rejects(addClaimByHand("plain", { section: { title: "S" }, claim: { text: "x" }, cite: ["q9"] }, paths, NOW), /no citation "q9"/); await assert.rejects(addClaimByHand("plain", { section: { id: "zz" }, claim: { text: "x" } }, paths, NOW), /no section "zz"/); await assert.rejects(addClaimByHand("nothing", { section: { title: "S" }, claim: { text: "x" } }, paths, NOW), /no report to add to/); await assert.rejects( addClaimByHand("plain", { section: { title: "S" }, claim: { text: "x" }, add: [{ kind: "video", channel: CHAN, id: "rec1", start: 10, end: 300, quote: "a" }] }, paths, NOW), /at most 120 s/, ); }); test("parseMoment: an archive link, the house citation, channel/id@time", () => { assert.deepEqual(parseMoment("https://archive.example/?v=synth-hand%2Frec1&t=12"), { channel: "synth-hand", id: "rec1", kind: "span", at: 12, quote: null }); assert.deepEqual(parseMoment('"The bakery opened" [Synthetic stream @ 0:12](https://archive.example/?v=synth-hand/rec1&t=12)'), { channel: "synth-hand", id: "rec1", kind: "span", at: 12, quote: "The bakery opened", }); assert.deepEqual(parseMoment("synth-hand/rec1@1:05"), { channel: "synth-hand", id: "rec1", kind: "span", at: 65, quote: null }); assert.deepEqual(parseMoment("synth-hand/rec1"), { channel: "synth-hand", id: "rec1", kind: "span", at: null, quote: null }); assert.equal(parseMoment("not a moment"), null); }); test("an article older than iterations: its report.json is the base; the claim's iteration is i1, no parent", async () => { const paths = store(); const { writeArticle } = await import("../lib/report/registry"); await writeArticle({ format: "archilyzer-report", version: 1, id: "older", kind: "factcheck", title: "Older", sections: [{ id: "s1", title: "One", claims: [{ id: "c1", text: "First." }] }] }, { paths }); assert.equal(await defaultHandBase("older", paths), null); const r = await addClaimByHand("older", { section: { id: "s1" }, claim: { text: "Second." } }, paths, NOW); assert.deepEqual([r.n, r.amended, r.claim], [1, false, "c2"]); const i1 = (await readIterations("older", paths)).iterations[0]!; assert.deepEqual([i1.kind, i1.parent, i1.status], ["hand", null, "done"]); assert.match(i1.feedback ?? "", /^from the article's report\.json: added c2/); assert.equal(reportOf(paths, "older", 1).sections[0].claims.length, 2); }); test("Import this capture: a page citation becomes a post citation of its page record, the page kept as its fallback", async () => { const paths = store(); const PAGES = "synth-pages"; const put = (file: string, text: string) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, text); }; put( path.join(paths.channelsDir, PAGES, "config.json"), JSON.stringify({ handling: "transcribe", sourceKind: "social", platform: "webpage", name: "Pages", url: "https://web.archive.org/web/*/blog.example/*" }), ); const rid = "20140305120000-0123456789ab"; put( path.join(paths.channelsDir, PAGES, "posts", "2014-03.jsonl"), JSON.stringify({ id: rid, slug: `${PAGES}/${rid}`, channelSlug: PAGES, author: "blog.example", createdAt: "2014-03-04T18:30:00.000Z", uploadDate: "20140304", text: "The first loaf came out flat, and the second, and the third.", url: "https://blog.example/a.html", platform: "webpage", isReply: false, isRepost: false, links: [], }) + "\n", ); await startBlankArticle("bread", { title: "Bread" }, paths, NOW); const page = { kind: "page", url: "https://blog.example/a.html", title: "A post", archiveUrl: `https://web.archive.org/web/20140305120000/https://blog.example/a.html`, quote: "The first loaf came out flat", speaker: "Pat" }; await writeIterationFile( "bread", 1, "report.json", { ...reportOf(paths, "bread", 1), citations: { p1: page, p2: { ...page, quote: "words the page never said" } }, sections: [{ id: "s", title: "S", claims: [{ id: "c1", text: "x", citations: ["p1", "p2"] }] }] }, paths, ); const r = await replacePageCitationWithRecord("bread", { cite: "p1", channel: PAGES, id: rid }, paths, NOW); assert.equal(r.n, 1); assert.equal(r.amended, true); assert.equal(r.check.status, "ok"); const after = reportOf(paths, "bread", 1); assert.deepEqual(after.citations.p1, { kind: "post", channel: PAGES, id: rid, quote: "The first loaf came out flat", speaker: "Pat", fallback: { kind: "page", url: page.url, title: "A post", archiveUrl: page.archiveUrl }, }); // A quote the record does not hold: refused, nothing written. await assert.rejects(replacePageCitationWithRecord("bread", { cite: "p2", channel: PAGES, id: rid }, paths, NOW), /does not check against the page record/); assert.equal(reportOf(paths, "bread", 1).citations.p2.kind, "page"); // Only a page citation. await assert.rejects(replacePageCitationWithRecord("bread", { cite: "p1", channel: PAGES, id: rid }, paths, NOW), /is a post, not a page/); });