// Report ops over the fixture sweeps: batch numbering, merge chains, reasons // on every refusal, no quoted words in claims, quotes untouched, a strict // check on every added quote. import { test } from "node:test"; import assert from "node:assert/strict"; import { sweepToReport } from "../convertSweep"; import { cuesOf, momentHref, recordOf } from "../../../testing/draft/corpus.mjs"; import { readSweep } from "../../../testing/draft/derive"; import { numberFindings, parseSweep, renderSweep, type SweepDoc } from "./sweepModel"; import { applyReportOps, quotedClaimProblems, strictCheckerOver, type ReportOpsContext } from "./reportOps"; const load = async (name: Parameters[0]) => parseSweep(await readSweep(name)); const claims = (doc: SweepDoc) => numberFindings(doc).map((x) => x.finding.claim); const ctx: ReportOpsContext = { checkQuote: strictCheckerOver(cuesOf), titleOf: async (c, id) => recordOf(c, id)?.title ?? null, origin: "https://synth.example", }; const quotesOfReport = async (md: string) => Object.values((await sweepToReport(md, { cuesOf })).report.citations ?? {}) .map((c) => ("quote" in c ? c.quote : "")) .sort(); test("a batch numbers against the document at its START: drop 1 then drop 3 drops the 1 and 3 the model saw", async () => { const doc = await load("basic"); const before = claims(doc); const r = await applyReportOps(doc, [{ op: "drop", n: 1 }, { op: "drop", n: 3 }, { op: "claim", n: 4, text: "She did not enter" }]); assert.deepEqual(r.refused, []); assert.deepEqual(claims(r.doc), [before[1], "She did not enter", before[4], before[5]]); // The input is not touched. assert.deepEqual(claims(doc), before); // A second batch numbers afresh. const r2 = await applyReportOps(r.doc, [{ op: "drop", n: 1 }]); assert.deepEqual(claims(r2.doc), ["She did not enter", before[4], before[5]]); }); test("merge chains resolve: merge 1→2, 2→3, then an op on 1 lands on 3; dropping 1 is refused with its reason", async () => { const doc = await load("basic"); const r = await applyReportOps(doc, [ { op: "merge", n: 1, into: 2 }, { op: "merge", n: 2, into: 3, text: "She denies three stories" }, { op: "claim", n: 1, text: "She denies all three" }, { op: "drop", n: 1 }, { op: "merge", n: 3, into: 1 }, ]); assert.deepEqual(r.applied.map((a) => a.index), [0, 1, 2]); assert.equal(r.refused.length, 2); assert.match(r.refused[0]!.why, /finding 1 was merged into finding 3; drop that one/); assert.match(r.refused[1]!.why, /cannot merge into itself/); const fs = numberFindings(r.doc).map((x) => x.finding); assert.equal(fs.length, 4); assert.equal(fs[0]!.claim, "She denies all three"); // Every quote kept its own link, in order. const links = [...fs[0]!.rest.matchAll(/&t=(\d+)\)/g)].map((m) => Number(m[1])); assert.deepEqual(links, [45, 25, 15]); // Converted, the quotes are the original quotes — none edited. assert.deepEqual(await quotesOfReport(renderSweep(r.doc)), await quotesOfReport(await readSweep("basic"))); }); test("every refusal carries a reason, and refuses that op only", async () => { const doc = await load("basic"); const r = await applyReportOps(doc, [ { op: "drop", n: 99 }, { op: "drop" }, { op: "explode", n: 1 }, { op: "claim", n: 2 }, { op: "move", n: 2 }, { op: "section", section: "Nope", text: "x" }, { op: "section", section: "The bakery", text: "The contest" }, { op: "title", text: " " }, "nonsense", { op: "drop", n: 2 }, { op: "drop", n: 2 }, { op: "claim", n: 2, text: "after the drop" }, ]); assert.deepEqual(r.applied.map((a) => a.index), [9]); assert.equal(r.refused.length, 11); for (const x of r.refused) assert.ok(x.why.length > 8, JSON.stringify(x)); const why = r.refused.map((x) => x.why); assert.match(why[0]!, /no finding 99 \(the document has 6\)/); assert.match(why[2]!, /no op "explode"/); assert.match(why[5]!, /no section "Nope"/); assert.match(why[6]!, /already a section "The contest"/); assert.match(why[9]!, /already dropped/); assert.match(why[10]!, /dropped earlier in this batch/); }); test("claims never carry quoted words or a link: the op that would put one in is refused (quotes-in-claims.md)", async () => { const doc = await load("quotes-in-claims"); // The fixture breaks the rule; the lint names each finding. assert.deepEqual(quotedClaimProblems(doc).map((p) => p.n), [1, 2, 3]); const r = await applyReportOps(doc, [ { op: "claim", n: 1, text: 'She "would never" sell the bakery' }, { op: "claim", n: 2, text: "The “old oven” story is denied" }, { op: "merge", n: 3, into: 1, text: 'She says "nobody"' }, { op: "claim", n: 1, text: "See [this](https://synth.example/?v=a%2Fb&t=1)" }, { op: "add", section: "Findings", text: 'She "sold"', quote: "I would never sell the bakery to a chain", link: momentHref("synth-kitchen", "bread-day-01", 15) }, { op: "claim", n: 1, text: "She says she would never sell the bakery" }, ], ctx); assert.deepEqual(r.applied.map((a) => a.index), [5]); for (const x of r.refused.slice(0, 3)) assert.match(x.why, /no quotation marks/); assert.match(r.refused[3]!.why, /no link/); assert.match(r.refused[4]!.why, /no quotation marks.*put the words in "quote"/); // The clean basic and multi-quote sweeps pass the lint. assert.deepEqual(quotedClaimProblems(await load("basic")), []); assert.deepEqual(quotedClaimProblems(await load("multi-quote")), []); }); test("move: to a new section before Coverage, and the document still converts", async () => { const doc = await load("paragraphs"); const r = await applyReportOps(doc, [{ op: "move", n: 2, section: "The recipe" }, { op: "move", n: 1, section: "The second shop" }]); assert.match(r.refused[0]!.why, /already in "The second shop"/); assert.deepEqual(r.doc.sections.map((s) => s.title), ["The second shop", "The recipe", "Coverage"]); const md = renderSweep(r.doc); assert.match(md, /\n## The recipe\n\n- The other kitchen tried the recipe: "we tried the sourdough recipe from last week" \[The flat loaf @ 0:05\]\([^)]+\)\n\n## Coverage\n/); const { report, problems } = await sweepToReport(md, { cuesOf }); assert.deepEqual(problems, []); assert.deepEqual(report.sections.map((s) => s.title), ["The second shop", "The recipe", "Coverage"]); }); test("a section a batch empties goes; one with text left stays", async () => { const basic = await load("basic"); const r = await applyReportOps(basic, [{ op: "drop", n: 4 }, { op: "drop", n: 5 }, { op: "drop", n: 6 }]); assert.deepEqual(r.doc.sections.map((s) => s.title), ["The bakery"]); assert.ok(renderSweep(r.doc).endsWith("&t=45)\n")); const para = await load("paragraphs"); const r2 = await applyReportOps(para, [{ op: "drop", n: 1 }, { op: "drop", n: 2 }]); assert.deepEqual(r2.doc.sections.map((s) => s.title), ["The second shop", "Coverage"]); }); test("add: the quote is strict-checked; exact words in, a reversed quote out; no checker, no add", async () => { const doc = await load("basic"); const link = (s: number) => momentHref("synth-kitchen", "bread-day-01", s); const r = await applyReportOps( doc, [ { op: "add", section: "The oven", text: "She says the oven ran hot", quote: "“the oven was fine it just ran hot”", link: link(30) }, { op: "add", section: "The oven", text: "She says she would sell", quote: "I would sell the bakery to a chain", link: link(15) }, { op: "add", section: "The oven", text: "Far away", quote: "thanks for watching see you next week", link: momentHref("synth-kitchen", "qa-long-03", 205) }, { op: "add", section: "The oven", text: "No link", quote: "the oven was fine it just ran hot", link: "https://video.example/watch?v=x" }, { op: "add", section: "The oven", text: "Other origin", quote: "the oven was fine it just ran hot", link: "https://elsewhere.example/?v=synth-kitchen%2Fbread-day-01&t=30" }, { op: "add", section: "The oven", text: "Inner quote", quote: 'the "oven"', link: link(30) }, ], ctx, ); assert.deepEqual(r.applied.map((a) => a.index), [0]); assert.match(r.refused[0]!.why, /not the transcript's words within 90 s of 15 s: "I would sell the bakery to a chain" is not there/); assert.match(r.refused[1]!.why, /not the transcript's words/); assert.match(r.refused[2]!.why, /needs a moment link/); assert.match(r.refused[3]!.why, /origin https:\/\/synth\.example/); assert.match(r.refused[4]!.why, /no quotation marks inside/); const added = numberFindings(r.doc).map((x) => x.finding).find((f) => f.added)!; assert.equal( `- ${added.claim}${added.sep}${added.rest}`, `- She says the oven ran hot: "the oven was fine it just ran hot" [Bread day @ 0:30](${link(30)})`, ); assert.deepEqual(r.doc.sections.map((s) => s.title), ["The bakery", "The contest", "The oven"]); const conv = await sweepToReport(renderSweep(r.doc), { cuesOf }); assert.deepEqual(conv.problems, []); assert.ok(Object.values(conv.report.citations ?? {}).some((c) => "quote" in c && c.quote === "the oven was fine it just ran hot")); const none = await applyReportOps(doc, [{ op: "add", section: "X", text: "y", quote: "the oven was fine it just ran hot", link: link(30) }]); assert.match(none.refused[0]!.why, /needs the quote checker/); }); test("section, title and summary", async () => { const doc = await load("basic"); const r = await applyReportOps(doc, [ { op: "section", section: "The contest", text: "The pie contest" }, { op: "title", text: "What the baker says" }, { op: "summary", text: "Six findings from two recordings." }, ]); assert.deepEqual(r.refused, []); const md = renderSweep(r.doc); assert.ok(md.startsWith("# What the baker says\n\nSix findings from two recordings.\n\n## The bakery\n")); assert.match(md, /\n## The pie contest\n/); const { report } = await sweepToReport(md, { cuesOf }); assert.equal(report.title, "What the baker says"); assert.equal(report.summary, "Six findings from two recordings."); }); test("two-line findings (the claim on the bullet, its quote and link under it) are findings the ops can reach", async () => { const md = await readSweep("two-line"); const doc = parseSweep(md); assert.equal(renderSweep(doc), md, "round trip"); assert.equal(numberFindings(doc).length, 3, "three findings; the line with no link is not one"); const { doc: out, applied, refused } = await applyReportOps(doc, [ { op: "claim", n: 1, text: "She says she will not sell the bakery to a chain." }, { op: "merge", n: 2, into: 3 }, ], ctx); assert.deepEqual(refused, []); assert.equal(applied.length, 2); const after = renderSweep(out); assert.match(after, /^- She says she will not sell the bakery to a chain\.\n "I would never sell the bakery to a chain" — \[Bread day @ 0:15\]/m); // The merged finding keeps every quote with its own link, still under its claim. assert.match(after, /- She says she did not enter, and why\.\n "I did not enter the contest this year".*\n "the judges were friends.*\n "we never said that about the old oven"/); const { problems } = await sweepToReport(after, { cuesOf }); assert.deepEqual(problems, []); }); test("add: a fragment quote is refused, and the refusal carries the full sentence (release 28 Q2)", async () => { const doc = parseSweep(await readSweep("basic")); const link = momentHref("synth-kitchen", "pie-stream-02", 40); const r = await applyReportOps( doc, [ { op: "add", section: "The lease", text: "She will not discuss the lease", quote: "my lawyer told me not to", link }, { op: "add", section: "The lease", text: "She will not discuss the lease", quote: "my lawyer told me not to talk about the lease", link }, ], { checkQuote: strictCheckerOver(cuesOf), origin: "https://synth.example" }, ); assert.equal(r.refused.length, 1); assert.match(r.refused[0]!.why, /a fragment \(ends on “to”\).*"my lawyer told me not to talk about the lease"/); assert.equal(r.applied.length, 1); // The words-only checker lets it through. const words = await strictCheckerOver(cuesOf, { fragments: false })({ channel: "synth-kitchen", id: "pie-stream-02", second: 40, quote: "my lawyer told me not to" }); assert.deepEqual(words, { ok: true }); }); // A speaker-neutral report (review of round b, 12 Nov 2026): its lines, // headings and title stay code's through a revision. test("a neutral report: a line is not rewritten, an added line is code's, a heading or summary that attributes is refused, the title is code's", async () => { const doc = await load("basic"); const before = claims(doc); const seen: string[] = []; const neutral: ReportOpsContext["neutral"] = { line: async (o) => { seen.push(o.text); return o.text.startsWith("In ") ? { line: `In Contest results, on ${o.text.split(", on ")[1]}` } : { why: "not the neutral shape" }; }, heading: (t) => (/\b(?:her|says|reaction)\b/i.test(t) ? `“${t}” attributes` : null), summary: (t) => (/\bshe\b/i.test(t) ? "a pronoun" : null), }; const add = (text: string, section = "The contest") => ({ op: "add", section, text, quote: "I did not enter the contest this year", link: momentHref("synth-kitchen", "pie-stream-02", 10) }); const r = await applyReportOps(doc, [ { op: "claim", n: 1, text: "She says she would never sell" }, { op: "merge", n: 2, into: 3, text: "She denies it all" }, { op: "merge", n: 2, into: 3 }, { op: "move", n: 4, section: "Her reaction" }, { op: "move", n: 4, section: "The pie contest" }, { op: "section", section: "The bakery", text: "What she says about the bakery" }, add("She did not enter"), add("In Whatever, on entering the contest:"), add("In Whatever, on entering the contest:", "Her reaction"), { op: "title", text: "The baker slams the contest" }, { op: "summary", text: "She hates it." }, { op: "summary", text: "Two recordings were since removed from the platform." }, ], { ...ctx, neutral }); assert.deepEqual( r.refused.map((x) => [x.index, x.why.split(":")[0]]), [ [0, "a speaker-neutral report's lines are written by code"], [1, "a speaker-neutral report's lines are written by code"], [3, "a speaker-neutral report's heading is a topic"], [5, "a speaker-neutral report's heading is a topic"], [6, "not the neutral shape"], [8, "a speaker-neutral report's heading is a topic"], [9, "a speaker-neutral report's title is written by code"], [10, "a speaker-neutral report's summary says where the words are, not who says what"], ], ); assert.equal(claims(r.doc)[0], before[0], "the line stands"); assert.ok(claims(r.doc).includes("In Contest results, on entering the contest:"), "the added line is the one code wrote"); assert.deepEqual(seen, ["She did not enter", "In Whatever, on entering the contest:"], "a refused section title stops an add before its line is written"); assert.ok(renderSweep(r.doc).includes('In Contest results, on entering the contest: "I did not enter the contest this year"'), "a line ending on a colon takes no second one"); assert.ok(r.doc.sections.some((s) => s.title === "The pie contest")); assert.match(r.doc.preamble.join("\n"), /since removed/); });