// The sweep.md model: byte-identical round trip over every fixture sweep, // and the claim / sep / rest split at the first quote. import { test } from "node:test"; import assert from "node:assert/strict"; import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { numberFindings, numberedView, parseSweep, quotesOf, renderSweep, sectionsNamedBy, splitFinding, trimmedNumberedView } from "./sweepModel"; const SWEEPS = path.join(path.dirname(fileURLToPath(import.meta.url)), "../../../testing/draft/sweeps"); test("round trip: parseSweep → renderSweep is byte-identical over every fixture sweep", async () => { const files = (await readdir(SWEEPS)).filter((f) => f.endsWith(".md")); assert.ok(files.length >= 6, files.join(",")); for (const f of files) { const md = await readFile(path.join(SWEEPS, f), "utf8"); assert.equal(renderSweep(parseSweep(md)), md, f); // And without the final newline. assert.equal(renderSweep(parseSweep(md.trimEnd())), md.trimEnd(), `${f} (no final newline)`); } }); test("round trip: odd shapes — no title, lines before it, indented continuations, headings with extra spaces", () => { for (const md of [ "", "no title at all\n", "intro\n# Title\n\n## A\n\n- x: \"q\" [l](https://a.example/?v=c%2Fv&t=1)\n continued line\n\ntext\n", "# Spaced title \n\n## Spaced section \n\n* star: “curly” [l](https://a.example/?v=c%2Fv&t=2)\n", "# T\n\n## S\n\n- \"starts with a quote\" [l](https://a.example/?v=c%2Fv&t=3)\n- a plain list item, no link\n", ]) { assert.equal(renderSweep(parseSweep(md)), md, JSON.stringify(md)); } }); test("the model: title, preamble, sections in body order; findings only where a link is", async () => { const doc = parseSweep(await readFile(path.join(SWEEPS, "paragraphs.md"), "utf8")); assert.equal(doc.title, "Paragraphs between findings"); // The preamble's bullets have no links: they are preamble lines, not findings. assert.ok(doc.preamble.some((l) => l.startsWith("- **The second shop"))); assert.deepEqual(doc.sections.map((s) => s.title), ["The second shop", "Coverage"]); const shop = doc.sections[0]!; assert.deepEqual( shop.body.filter((b) => b.type === "line" ? b.text.trim() : true).map((b) => (b.type === "line" ? b.text : `[F] ${b.claim}`)), [ "She talks about it near the end of a long episode.", "[F] She says it opened in the spring", "### A sub-heading inside a section", "The other kitchen mentions the recipe, not the shop.", "[F] The other kitchen tried the recipe", ], ); assert.equal(numberFindings(doc).length, 2); }); test("split: the claim, the separator, and the rest from the first quote before the first link", () => { assert.deepEqual(splitFinding('She says she would not sell: "I would never sell" [Bread day @ 0:15](https://s.example/?v=a%2Fb&t=15)'), { claim: "She says she would not sell", sep: ": ", rest: '"I would never sell" [Bread day @ 0:15](https://s.example/?v=a%2Fb&t=15)', }); // No quote: cut at the link. assert.deepEqual(splitFinding("A claim — [x @ 0:01](https://s.example/?v=a%2Fb&t=1)"), { claim: "A claim", sep: " — ", rest: "[x @ 0:01](https://s.example/?v=a%2Fb&t=1)", }); // A quote AFTER the first link does not move the cut. assert.equal(splitFinding('Claim [x](https://s.example/?v=a%2Fb&t=1) then "late"').claim, "Claim"); }); test("quotes-in-claims: the split puts the claim's quoted words in the rest, never in the claim", async () => { const doc = parseSweep(await readFile(path.join(SWEEPS, "quotes-in-claims.md"), "utf8")); const fs = numberFindings(doc).map((x) => x.finding); assert.deepEqual(fs.map((f) => f.claim), ["She", "The", "She says"]); for (const f of fs) assert.doesNotMatch(f.claim, /["“”]/); assert.deepEqual(quotesOf(fs[2]!), ["nobody sponsored", "nobody sponsored this kitchen"]); }); test("the numbered view a model reads", async () => { const doc = parseSweep(await readFile(path.join(SWEEPS, "basic.md"), "utf8")); const view = numberedView(doc); assert.match(view, /^TITLE: Synth Kitchen — what the baker says about the bakery\nSUMMARY: Scope: channel/); assert.match(view, /\n## The bakery\n\[1\] She says she would not sell: "I would never sell the bakery to a chain" \[Bread day @ 0:15\]/); assert.match(view, /\n\[6\] She will not discuss the lease: /); const p = numberedView(parseSweep(await readFile(path.join(SWEEPS, "paragraphs.md"), "utf8"))); assert.match(p, /## Coverage {2}\(coverage — keep\)/); }); // A synthetic report of `sections` × `per` findings with long quotes. function bigSweep(sections: number, per: number): string { const out = ["# Big", "", "A summary.", ""]; let n = 0; for (let s = 1; s <= sections; s++) { out.push(`## Part ${s}`); for (let k = 0; k < per; k++) { n++; out.push(`- Claim ${n} of part ${s}: "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen" [Rec @ 0:${k}](https://synth.example/?v=ch%2Fv${n}&t=${k})`); } } return out.join("\n"); } test("trimmed view (Q3): the named sections first, the rest while the budget lasts; numbers the document's; the omitted said", () => { const doc = parseSweep(bigSweep(20, 5)); const full = numberedView(doc); const v = trimmedNumberedView(doc, { maxChars: 2500, feedback: "Rework Part 17 and drop 3." }); assert.ok(v.text.length <= 2500 + 400, String(v.text.length)); assert.ok(full.length > 4 * v.text.length); assert.deepEqual(v.named, [0, 16], "part 17 by its title; finding 3's section by its number"); for (const n of [81, 82, 83, 84, 85, 3]) assert.ok(v.shown.includes(n), `[${n}] shown`); assert.match(v.text, /## Part 17\n\[81\] Claim 81 of part 17/); // Shown in document order; the left-out findings named by number. assert.ok(v.text.indexOf("## Part 1\n") < v.text.indexOf("## Part 17")); assert.match(v.text, /## Part 20 {2}\(findings \[96\]–\[100\] not shown\)/); assert.equal(v.shown.length + v.omitted.length, 100); // Quotes shortened and links reduced to their labels, in the view only. assert.match(v.text, /\[81\] Claim 81 of part 17: "one two three four five six seven eight …" \[Rec @ 0:0\]/); assert.ok(!v.text.includes("https://")); assert.ok(renderSweep(doc).includes("fifteen\" [Rec @ 0:0](https://synth.example/?v=ch%2Fv81&t=0)"), "the document is untouched"); // No feedback: document order. const plain = trimmedNumberedView(doc, { maxChars: 2500 }); assert.ok(plain.shown.includes(1) && !plain.shown.includes(81)); // A budget that holds everything shows everything. assert.equal(trimmedNumberedView(doc, { maxChars: 1e6 }).omitted.length, 0); }); test("sectionsNamedBy: titles, section numbers, finding numbers", () => { const doc = parseSweep(bigSweep(4, 2)); assert.deepEqual([...sectionsNamedBy(doc, "In part 2, reword the claim.")], [1]); assert.deepEqual([...sectionsNamedBy(doc, "section 4 is too long")].sort(), [3]); assert.deepEqual([...sectionsNamedBy(doc, "merge [5] into 6")].sort(), [2]); assert.deepEqual([...sectionsNamedBy(doc, "trim it to 30 s")], []); });