// TASK onscreen (release 28 M2) over the S0 kit's manifests and the basic // report, against the fake ollama: several clips in one ask, the re-ask only // for the refused, each code check refusing what it should, the better of two // kept, a clip with no clean draft not written, the operator's text kept and // the draft an alternative. Real iterations store and project in a temp dir. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { jsonReply, startFakeOllama, type FakeOllama } from "../../../../testing/llm/fakeOllama.mjs"; import { createIteration, projectOf, readIteration, updateIteration, writeIterationFile } from "../../iterations"; import { checkOnscreen, isTitleCase, isVerbatimOf, onscreen, onscreenBatches, quotedParts, titleAsSentence, titleLineGrounding, ONSCREEN_SPEC } from "./onscreen"; import { groundedProblems } from "../grounding"; import type { DraftFile } from "./media"; import type { TaskDeps } from "./types"; const DRAFT = new URL("../../../../testing/draft/", import.meta.url); const kit = async (rel: string) => JSON.parse(await readFile(new URL(rel, DRAFT), "utf8")); let tmp: string; let ol: FakeOllama; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "onscreen-task-")); ol = await startFakeOllama(); }); after(async () => { await ol.close(); await rm(tmp, { recursive: true, force: true }); }); let caseN = 0; async function project(manifest: unknown) { caseN++; const paths = { articlesDir: path.join(tmp, `reports-${caseN}`), exportIndexDir: path.join(tmp, `idx-${caseN}`) }; const projectsRoot = path.join(tmp, `projects-${caseN}`); await mkdir(paths.articlesDir, { recursive: true }); await createIteration("synth-basic", { kind: "sweep", model: "qwen3:8b", question: "q", scope: { channels: ["synth-kitchen"] } }, paths); const report = await kit("reports/basic.report.json"); await writeIterationFile("synth-basic", 1, "report.json", report, paths); const dir = projectOf(projectsRoot, "synth-basic", 1); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); await writeFile(path.join(dir, "report.json"), JSON.stringify(report)); await updateIteration("synth-basic", 1, { status: "done", projectDir: dir }, paths); const lines: string[] = []; const progress: unknown[] = []; const deps: TaskDeps = { llm: { ollama: { baseUrl: ol.url } }, corpus: { source: "none", tools: [], call: async () => "" }, cuesOf: async () => null, paths, log: (l) => lines.push(l), progress: (p) => progress.push(p), now: () => new Date("2026-10-13T12:00:00.000Z"), }; const manifestNow = async () => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const draft = async () => JSON.parse(await readFile(path.join(dir, "onscreen-draft.json"), "utf8")) as DraftFile; const input = (o: Record = {}) => ({ article: "synth-basic", n: 1, model: "qwen3:8b", projectsRoot, ...o }); return { dir, paths, deps, lines, progress, manifestNow, draft, input, report }; } const entry = (m: { timeline: { id: string }[] }, id: string) => m.timeline.find((e) => e.id === id) as Record; test("onscreen, the recorded run: six clips in one ask, the four refused asked again, the better of two kept, a clip with none clean not written", async () => { const m = await kit("manifests/basic.video.manifest.json"); const p = await project(m); const r = await kit("runs/onscreen.json"); const asked: string[] = []; ol.push(...r.turns.map((t: unknown) => (body: { messages: { content: string }[] }) => (asked.push(body.messages.at(-1)!.content), t))); const out = await onscreen(p.input(), p.deps); assert.equal(out.status, "done", `${out.summary}\n${p.lines.join("\n")}`); assert.equal(ol.remaining(), 0); assert.equal(asked.length, 2, "one ask for six clips, one re-ask"); assert.match(asked[0]!, /### Clip c01[\s\S]*### Clip c06/); assert.doesNotMatch(asked[1]!, /### Clip c01|### Clip c05/); assert.match(asked[1]!, /Your last drafts were refused: c02: the title has quotation marks/); assert.match(out.summary, /^5 clip\(s\) titled \(undo: /); const after = await p.manifestNow(); assert.deepEqual(entry(after, "c01").onscreen, { title: "She will not sell to a chain", subtitle: '"I would never sell the bakery to a chain"' }); assert.deepEqual(entry(after, "c02").onscreen, { title: "The oven story, denied", subtitle: '"we never said that about the old oven"' }); assert.deepEqual(entry(after, "c03").onscreen, { title: "Who paid for the mixer", subtitle: '"I paid for the new mixer myself"' }); assert.equal(entry(after, "c04").onscreen.title, "Out of the contest this year"); assert.equal(entry(after, "c04").drafted.onscreen.attempt, 2); assert.equal(entry(after, "c05").drafted.onscreen.attempt, 1); assert.equal(entry(after, "c06").onscreen, undefined, "no clean draft: not written"); const file = await p.draft(); const c06 = file.units.c06 as { attempts: { hard: string[] }[] }; assert.match(c06.attempts[0]!.hard.join(), /number 2024 is not in the clip's evidence/); assert.match(c06.attempts[1]!.hard.join(), /name “Marco” is not in the clip's evidence/); const c04 = file.units.c04 as { attempts: { hard: string[] }[] }; assert.match(c04.attempts[0]!.hard.join(), /the title is \d+ characters \(at most 60\)/); const c03 = file.units.c03 as { attempts: { hard: string[] }[] }; assert.match(c03.attempts[0]!.hard.join(), /the subtitle quotes “I bought the mixer”, which is not the clip's quote word for word/); assert.deepEqual(p.progress, [ { metric: "entries", current: 0, total: 6 }, { metric: "entries", current: 6, total: 6 }, ]); const it = (await readIteration("synth-basic", 1, p.paths))!; assert.equal(it.checks!.find((c) => c.name === "onscreen")!.ok, false, "a clip not written fails the check"); assert.equal(it.steps.find((s) => s.task === "onscreen")!.status, "done"); }); test("onscreen's checks, each alone", () => { const u = { quote: "I would never sell the bakery to a chain", evidence: "Report: Synth Kitchen\nSection: The bakery\nQuote: \"I would never sell the bakery to a chain\"\nSource: Bread day, 2026-01-05" }; const hard = (title: string, subtitle?: string) => checkOnscreen({ title, ...(subtitle ? { subtitle } : {}) }, u).hard; assert.deepEqual(hard("Not for sale", "The baker on a chain"), []); assert.deepEqual(hard("Not for sale", '"never sell the bakery"'), [], "a verbatim part of the quote"); assert.deepEqual(hard("Not for sale", "“Never sell the bakery,” on Bread day"), [], "curly marks, case and punctuation aside"); assert.match(hard("x".repeat(61)).join(), /the title is 61 characters \(at most 60\)/); assert.match(hard("Not for sale", "y".repeat(91)).join(), /the subtitle is 91 characters \(at most 90\)/); assert.match(hard("Not for\nsale").join(), /the title is more than one line/); assert.match(hard('"Not for sale"').join(), /the title has quotation marks/); assert.match(hard("The ‘chain’ line").join(), /the title has quotation marks/); assert.deepEqual(hard("She won't sell"), [], "an apostrophe is not a quotation"); assert.match(hard("Not for sale", '"never sell it"').join(), /quotes “never sell it”, which is not the clip's quote word for word/); assert.match(hard("Not for sale", 'She said "never sell').join(), /quotation marks do not pair up/); assert.match(hard("Not for sale", "Sold in 2025").join(), /number 2025 is not in the clip's evidence/); assert.deepEqual(hard("Not for sale", "Bread day, 2026"), []); assert.match(hard("Not for sale", "Her friend Marco agrees").join(), /name “Marco” is not in the clip's evidence/); assert.match(hard("I will never sell").join(), /the narrator is third person/); assert.deepEqual(quotedParts('a "b c" d «e»'), { parts: ["b c", "e"], balanced: true }); assert.equal(isVerbatimOf("sell the", "I would never sell the bakery"), true); assert.equal(isVerbatimOf("sel the", "I would never sell the bakery"), false, "whole words"); }); test("onscreen: a Title Case title passes as its sentence case; an unknown name in one is flagged soft (release 28 M9 ruling, smoke)", () => { const u = { quote: "I would never sell the bakery to a chain", evidence: "Report: Synth Kitchen\nSection: The bakery\nClaim: The baker says she will not sell to a chain.\nQuote: \"I would never sell the bakery to a chain\"\nSource: Bread day, 2026-01-05" }; const hard = (title: string) => checkOnscreen({ title }, u).hard; const soft = (title: string) => checkOnscreen({ title }, u).soft; // Ordinary Title Case: every word capitalised, none a name. assert.deepEqual(hard("The Baker Will Never Sell The Bakery"), []); assert.deepEqual(hard("She Refuses To Sell To A Chain"), []); assert.deepEqual(hard("Not For Sale, Says The Baker"), []); // A name the evidence has keeps its capital and passes. assert.deepEqual(hard("Synth Kitchen Will Not Sell"), []); // A name it lacks still fails, Title Case or not. // In a Title Case title an unknown capital is soft (the review holds it): code // cannot tell a headline word from a name the evidence lacks. assert.match(soft("Why Marco Says The Bakery Stays").join(), /name “Marco” is not in the clip's evidence/); assert.match(soft("The Bakery Stays, Says Marco").join(), /name “Marco” is not in the clip's evidence/); assert.deepEqual(hard("Why Marco Says The Bakery Stays"), []); assert.match(hard("The bakery stays, says Marco").join(), /name “Marco” is not in the clip's evidence/); assert.equal(isTitleCase("The Baker Will Never Sell"), true); assert.equal(isTitleCase("The baker will never sell"), false); assert.equal(isTitleCase("Bakery"), false, "one word is not a case"); assert.equal(titleAsSentence("Never Sell The Bakery To Marco", u.evidence), "Never sell the bakery to Marco"); assert.equal(titleAsSentence("The baker said no", u.evidence), "The baker said no", "a sentence-case title as it is"); }); test("onscreen: the operator's text kept (forced, the draft an alternative); entries asks only those; batches fit the local cap", async () => { const full = await kit("manifests/full.video.manifest.json"); const p = await project(full); // c01 and c04 hold the operator's text: skipped unforced, so c02 alone is asked. ol.push(jsonReply({ entries: [{ id: "c02", title: "The oven story, denied", subtitle: "" }] })); const one = await onscreen(p.input({ entries: ["c01", "c02", "c04"] }), p.deps); assert.equal(one.status, "done", `${one.summary}\n${p.lines.join("\n")}`); const f1 = await p.draft(); assert.deepEqual(Object.keys(f1.units).sort(), ["c01", "c02", "c04"]); assert.match(String(f1.units.c01!.skipped), /the operator's/); // Forced over c01: its words stand, the draft is an alternative. ol.push(jsonReply({ entries: [{ id: "c01", title: "She will not sell to a chain", subtitle: "" }] })); const forced = await onscreen(p.input({ entries: ["c01"], force: true }), p.deps); assert.match(forced.summary, /^0 clip\(s\) titled, 1 as alternatives/); const after = await p.manifestNow(); assert.deepEqual(entry(after, "c01").onscreen, entry(full, "c01").onscreen); assert.deepEqual(entry(after, "c02").onscreen, { title: "The oven story, denied" }); const alt = (await p.draft()).alternatives; assert.deepEqual(alt.map((a) => [a.target.entry, a.reason, (a.value as { title: string }).title]), [["c01", "operator", "She will not sell to a chain"]]); assert.equal(ol.remaining(), 0); // Batches: at most eight, and as many as fit a small cap. const ctx = { manifest: full, report: p.report, input: p.input(), handle: { deps: p.deps } } as never; const units = (await ONSCREEN_SPEC.units(ctx)) as never[]; const many = Array.from({ length: 20 }, (_, i) => ({ ...(units[i % units.length] as object), id: `x${i}` })) as never[]; assert.deepEqual(onscreenBatches(many, ctx).map((b) => b.length), [8, 8, 4]); const small = { ...(ctx as object), handle: { deps: { ...p.deps, llm: { ollama: { baseUrl: ol.url, promptCapTokens: 1100 } } } } } as never; const sizes = onscreenBatches(many, small).map((b) => b.length); assert.ok(sizes.every((n) => n >= 1 && n < 8) && sizes.reduce((a, b) => a + b, 0) === 20, String(sizes)); }); test("short lines (release 28 smoke): a Title Case word the evidence lacks is soft, numbers stay hard; a spelled-out number matches its digits", () => { const ev = "The rescue grew from about four ferrets to 56. Loki was found at 55."; const t = titleLineGrounding("Ferret Rescue Expansion", ev, "the report"); assert.deepEqual(t.hard, []); assert.match(t.soft.join(), /name “Expansion”.*Title Case/); // Not Title Case: an unknown name is hard, as it always was. assert.match(titleLineGrounding("The rescue hired Gordon", ev, "the report").hard.join(), /name “Gordon”/); // A number is hard in either case; "four" in the evidence is 4. assert.match(titleLineGrounding("Ferret Rescue Grows To 70", ev, "the report").hard.join(), /number 70/); assert.deepEqual(groundedProblems("Did they grow from 4 to 56?", ev), []); });