// Grounding: the hard checks every narration line passes, and the Q0 lints. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { checkNarration, firstPersonWords, groundingInputOf, lintNarration, reportText, QUOTE_RUN_MESSAGE, THIRD_PERSON_MESSAGE } from "./grounding"; const REPORTS = path.join(path.dirname(fileURLToPath(import.meta.url)), "../../../testing/draft/reports"); const basic = JSON.parse(await readFile(path.join(REPORTS, "basic.report.json"), "utf8")); const input = groundingInputOf(basic); test("the report's text: title, sections, claims, quotes and labels", () => { const t = reportText(basic); assert.match(t, /Synth Kitchen — what the baker says about the bakery/); assert.match(t, /The contest/); assert.match(t, /I paid for the new mixer myself/); assert.match(t, /Bread day/); }); test("a grounded line passes clean", () => { assert.deepEqual(checkNarration("She talks about the bakery, the oven and the contest. Here's what she said.", input), { hard: [], soft: [], }); }); test("hard: numbers and names the report lacks", () => { const r = checkNarration("She sold 7 bakeries to Megacorp in twenty minutes.", input); assert.ok(r.hard.includes("number 7 is not in the report"), r.hard.join("|")); assert.ok(r.hard.includes("name “Megacorp” is not in the report"), r.hard.join("|")); assert.ok(r.hard.includes("number “twenty” is not in the report"), r.hard.join("|")); // A name the report has passes, possessive or not; the first word of a sentence is not a name. assert.deepEqual(checkNarration("Kitchen talk. Bread day is where Synth Kitchen's baker speaks.", input).hard, []); }); test("hard: tags, stock phrases, quotation marks, quotes read out", () => { assert.deepEqual(checkNarration(" So she did.", input).hard, ["has a tag in angle brackets"]); assert.deepEqual(checkNarration("Let's dive in to the bakery.", input).hard, [ "stock phrase “dive in”", "stock phrase “let's dive in”", "stock phrase “let's dive”", ]); assert.deepEqual(checkNarration("She said “no” to the bakery.", input).hard, [ "text in quotation marks: no quotes in narration", ]); // Six words of a quote, said as the narrator's own. assert.deepEqual(checkNarration("She says she would never sell the bakery to a chain.", input).hard, [QUOTE_RUN_MESSAGE]); // Five words are not a quote run. assert.deepEqual(checkNarration("She would never sell the bakery.", input).hard, []); }); test("soft (Q0): a negation mismatch — the meaning reversed against its evidence", () => { const ev = ["I have never met the other finalist", "She says she paid for the new mixer herself"]; const w = lintNarration("She met the other finalist backstage.", ev); assert.equal(w.length, 1); assert.match(w[0]!, /^negation mismatch: “She met the other finalist backstage\.” against “I have never met the other finalist”/); assert.deepEqual(lintNarration("She has never met the other finalist.", ev), []); // A line about something else is not compared. assert.deepEqual(lintNarration("The pie needs an hour.", ev), []); }); test("soft (Q0): a dropped hedge", () => { const ev = ["next year maybe I will enter the contest again"]; const w = lintNarration("Next year she will enter the contest again.", ev); assert.equal(w.length, 1); assert.match(w[0]!, /^dropped hedge: /); assert.deepEqual(lintNarration("Maybe she will enter the contest again next year.", ev), []); }); test("checkNarration carries the lints as soft warnings", () => { const r = checkNarration("She paid nothing for the new mixer.", input); assert.deepEqual(r.hard, []); assert.equal(r.soft.length, 1); assert.match(r.soft[0]!, /negation mismatch/); }); test("hard (Q1): the narrator is third person — a first-person sentence is refused", () => { // The live smoke's line: no cited quote it restates. const r = checkNarration("She talks about the contest. I've already finished mine.", input); assert.equal(r.hard.length, 1, r.hard.join("|")); assert.ok(r.hard[0]!.startsWith(`${THIRD_PERSON_MESSAGE} (“I've”, “mine”)`), r.hard[0]); for (const line of ["We tried the bakery.", "That's my oven.", "The oven is ours.", "Let me be clear: the bakery stays.", "So we're done.", "It was us all along."]) { assert.ok(checkNarration(line, input).hard.some((h) => h.startsWith(THIRD_PERSON_MESSAGE)), line); } }); test("hard (Q1): a first-person sentence that restates a cited quote names whose line it is and where", () => { const r = checkNarration("I paid for the mixer myself.", input); assert.equal(r.hard.length, 1, r.hard.join("|")); assert.match(r.hard[0]!, /^the narrator is third person \(“I”, “myself”\): this is the speaker's line from c03 \(Bread day @ 0:45\), “I paid for the new mixer myself”/); // A named speaker is named. const named = groundingInputOf({ ...basic, citations: { ...basic.citations, c03: { ...basic.citations.c03, speaker: "the baker" } } }); assert.match(checkNarration("I paid for the mixer myself.", named).hard[0]!, /this is the baker's line from c03/); }); test("Q1: first person inside quotation marks is not the narrator's; word boundaries, not substrings", () => { assert.deepEqual(firstPersonWords('She said "I quit" and “we are done” twice.'), []); const quoted = checkNarration('She said "I quit" twice.', input).hard; assert.ok(!quoted.some((h) => h.startsWith(THIRD_PERSON_MESSAGE)), quoted.join("|")); assert.ok(quoted.includes("text in quotation marks: no quotes in narration"), "the quote itself is still refused"); // Not first person: names and words that only contain the letters. assert.deepEqual(firstPersonWords("Iowa's myth of the mineral museum in the US is idle."), []); // Accepted collateral: "mine" the noun. assert.deepEqual(firstPersonWords("The old mine shaft."), ["mine"]); // Curly apostrophes read as straight ones. assert.deepEqual(firstPersonWords("I’m sure."), ["I’m".replace("’", "'")]); });