// respellReport.ts and respellManifest.mjs: which strings a respelling changes, and how. import { test } from "node:test"; import assert from "node:assert/strict"; import { respellMarkdown, respellReport } from "./respellReport"; import { respellManifest } from "./respellManifest.mjs"; import type { Report } from "./schema"; const KCUP = [{ heard: ["cake cup", "cakeup"], reads: "K-Cup" }]; const KCUP_B = [{ ...KCUP[0], bracket: true }]; const report = (): Report => ({ format: "archilyzer-report", version: 1, id: "demo", kind: "sweep", title: "The cake cup story", summary: 'He said [“my own cakeup machine”](cite:a), and [a cake cup page](https://example.com/cake-cup "cake cup").', sections: [ { id: "s1", title: "Coffee", claims: [{ id: "c1", text: "The cakeup machine.", findings: "[“a cake cup machine”](cite:b)", gist: "cake cup", slide: { points: ["cakeup"] } }], }, ], sources: { doc: { kind: "web", title: "A cake cup review", url: "https://example.com" } }, citations: { a: { kind: "video", channel: "ch", id: "v1", start: 1, end: 2, quote: "my own cakeup machine" }, b: { kind: "video", channel: "ch", id: "v1", start: 3, end: 4, quote: "a cake cup machine" }, }, }) as unknown as Report; test("quotes take the respelling as it says; the report's own words read plainly", () => { const { report: r, changes } = respellReport(report(), KCUP_B); assert.equal(r.title, "The K-Cup story"); assert.match(r.summary!, /\[“my own \[K-Cup\] machine”\]\(cite:a\)/); // A link that is not a citation: its label is the report's words (plain), its href untouched. assert.match(r.summary!, /\[a K-Cup page\]\(https:\/\/example\.com\/cake-cup "cake cup"\)/); const c = r.sections[0].claims![0]; assert.equal(c.text, "The K-Cup machine."); assert.equal(c.findings, "[“a [K-Cup] machine”](cite:b)"); assert.equal(c.gist, "K-Cup"); assert.deepEqual(c.slide?.points, ["K-Cup"]); assert.equal((r.citations!.a as { quote: string }).quote, "my own [K-Cup] machine"); // Sources are the document's own words. assert.equal((r.sources!.doc as { title: string }).title, "A cake cup review"); assert.deepEqual(r.respellings, KCUP_B); assert.ok(changes.some((ch) => ch.at === "citations.b.quote")); }); test("plain respellings mark nothing; the input is not mutated; a second pass changes nothing", () => { const input = report(); const { report: once } = respellReport(input, KCUP); assert.equal((once.citations!.a as { quote: string }).quote, "my own K-Cup machine"); assert.equal((input.citations!.a as { quote: string }).quote, "my own cakeup machine"); const { changes } = respellReport(once, KCUP); assert.deepEqual(changes, []); }); test("markdown: a cite label is a quote, another label and the text around are the author's", () => { assert.equal( respellMarkdown("cake cup [“cake cup”](cite:x) [cake cup](https://a.b/c)", KCUP_B), "K-Cup [“[K-Cup]”](cite:x) [K-Cup](https://a.b/c)", ); }); test("a manifest: quotes and flip words take the respelling, titles plain, ids and paths untouched", () => { const m = { slug: "cake-cup", title: "The cake cup story", render: { chrome: { flips: { pairs: [{ id: "p", then: { entry: "cake cup", when: "2022", words: "my own cakeup machine" } }] } } }, timeline: [{ type: "clip", id: "c1", src: "clips/cake cup.mp4", quote: "a cake cup machine", onscreen: { title: "The cakeup machine" } }], provenance: { siteOrigin: "https://x", note: "cake cup" }, }; const { manifest: out, changes } = respellManifest(m, KCUP_B); assert.equal(out.title, "The K-Cup story"); assert.equal(out.slug, "cake-cup"); assert.equal(out.render.chrome.flips.pairs[0].then.words, "my own [K-Cup] machine"); assert.equal(out.render.chrome.flips.pairs[0].then.entry, "cake cup"); assert.equal(out.timeline[0].quote, "a [K-Cup] machine"); assert.equal(out.timeline[0].src, "clips/cake cup.mp4"); assert.equal(out.timeline[0].onscreen.title, "The K-Cup machine"); assert.equal(out.provenance.note, "cake cup"); assert.deepEqual(out.respellings, KCUP_B); assert.equal(m.timeline[0].quote, "a cake cup machine"); assert.equal(changes.length, 4); });