// READ AGAIN on the post bench (lane line-rerun, S3 of the Orpheus re-tune plan): a re-read that joins the list // unpicked, the next reading of a text, a script saved FOR READING (`narration.forReading`: the one writer marks it, // undo restores it), a spoken edit that would say what a screen masks refused when written, and an item edited // before the rule found by --list. Temp dirs, a stub tts, invented people. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { postsCommand } from "./postsCli.mjs"; import { LEGACY_EDIT, makeTake } from "./postNarrationCli.mjs"; import { StructureRefused, manifestToken, undoStructural, updatePostNarration } from "./manifest.mjs"; import { nextReadingOf } from "umtool-report-to-video/post-narration"; import { scriptEditOf, shownDraftOf } from "umtool-report-to-video/post-item"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-read-again-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); const wav = (f, secs) => execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", `sine=f=220:d=${secs}`, "-ar", "48000", "-ac", "1", f]); const TEXT = "The ferry runs at noon.\n\nBring a coat for the crossing."; const item = (o = {}) => ({ type: "post", id: "p-1", post: "ferry-x/1", platform: "x", author: "Ferry Fan", handle: "ferryfan", date: "2026-10-30T10:00:00.000Z", text: TEXT, ...o }); async function project(name, timeline) { const dir = path.join(REPORTS, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "", channelSlug: "ferry-x", channelsDir: path.join(ROOT, "channels") }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" } }, timeline, }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const stub = (asked) => async (body) => { asked.push(body); wav(body.out, 1.1); return { jobId: `job-${asked.length}`, result: { wav: body.out, engine: body.engine, voice: body.voice, seconds: 1.1, tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3, ...(body.tuning ?? {}) }, reading: body.reading ?? 0 } }; }; const cli = async (args) => { const lines = []; const code = await postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(s) }); return { code, out: lines.join("\n") }; }; test("READ AGAIN: the next reading of the saved script, a take that joins the list UNPICKED (keepPick) and records its reading", async () => { const p = await project("again", [item({ narration: { script: shownDraftOf(item()), engine: "orpheus", voice: "zac" } })]); const asked = []; await makeTake(p, (await manifest(p)).timeline[0], { ask: stub(asked), redaction: null }); let e = (await manifest(p)).timeline[0]; assert.equal(e.narration.pick, 0); const reading = nextReadingOf(e.narration.takes, e.narration.script); assert.equal(reading, 1); await makeTake(p, e, { reading, tuning: { temperature: 0.7 }, keepPick: true, ask: stub(asked), redaction: null }); e = (await manifest(p)).timeline[0]; assert.equal(e.narration.takes.length, 2); assert.equal(e.narration.pick, 0, "the playing take stays the one"); assert.equal(e.narration.takes[1].reading, 1); assert.equal(e.narration.takes[1].tuning.temperature, 0.7); assert.equal(asked[1].reading, 1); assert.equal(nextReadingOf(e.narration.takes, e.narration.script), 2); }); test("THE ONE WRITER marks a script saved for reading (forReading) and clears it when it says the post's words as drafted; undo restores it with the script", async () => { const p = await project("marker", [item({ narration: { script: shownDraftOf(item()) } })]); const set = async (script) => updatePostNarration(p.dir, "p-1", { script }, { token: await manifestToken(p.dir) }); await set("The ferry runs at twelve noon.\n\nBring a coat for the crossing."); let e = (await manifest(p)).timeline[0]; assert.equal(e.narration.forReading, true); assert.equal(scriptEditOf(e), "reading"); // Paragraphs merged are an edit too. await set("The ferry runs at noon. Bring a coat for the crossing."); assert.equal((await manifest(p)).timeline[0].narration.forReading, true); await set(shownDraftOf(item())); e = (await manifest(p)).timeline[0]; assert.equal("forReading" in e.narration, false); assert.equal(scriptEditOf(e), null); // Undo goes back to an undo point (writes close together share one): whichever script it restores, the marker is // restored with it -- never an edited script without it (that would be "legacy"). await undoStructural(p.dir, { token: await manifestToken(p.dir) }); e = (await manifest(p)).timeline[0]; assert.notEqual(scriptEditOf(e), "legacy", "undo restores the marker with the script"); assert.equal(e.narration.forReading === true, scriptEditOf(e) === "reading"); }); test("A SPOKEN EDIT that says what a screen masks (an e-mail, a number) or a redacted line holds is refused when it is written", async () => { const sc = { id: "s1", media: 0, kind: "email", read: "all", lines: [{ text: "Tickets from desk@ferry.example" }, { text: "booking ref 88231907" }, { text: "the captain's home street name", redact: true }] }; const media = [{ src: "posts-media/ferry-x/1/1_1.png", kind: "image", width: 900, height: 1400 }]; const p = await project("hidden", [item({ media, screens: [sc], narration: { script: shownDraftOf(item()) } })]); const set = async (script) => updatePostNarration(p.dir, "p-1", { script }, { token: await manifestToken(p.dir) }); await assert.rejects(() => set("Write to desk@ferry.example for tickets."), (err) => err instanceof StructureRefused && /the script says an e-mail address that line 0 of screen s1 masks/.test(err.errors.join(" "))); await assert.rejects(() => set("Your booking is 88231907."), (err) => err instanceof StructureRefused && /a number that line 1 of screen s1 masks/.test(err.errors.join(" "))); await assert.rejects(() => set("He named the captain's home street name aloud."), (err) => err instanceof StructureRefused && /redacted/.test(err.errors.join(" "))); await set("The ferry runs at twelve noon."); assert.equal((await manifest(p)).timeline[0].narration.forReading, true); }); test("A PLAYING TAKE FROM BEFORE THE RULE: --list (and --json) says which items play a take read from other words with no stamp -- read it again to move it", async () => { const old = (script) => ({ script, pick: 0, takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 1, script }] }); const p = await project("legacy", [item({ narration: old("Something else entirely.") }), { ...item({ narration: old(shownDraftOf(item())) }), id: "p-2", post: "ferry-x/2" }, { ...item({ narration: { script: "Unread other words." } }), id: "p-3", post: "ferry-x/3" }]); const r = await cli(["narrate", p.id, "--list"]); assert.equal(r.code, 0, r.out); assert.equal(r.out.split(LEGACY_EDIT).length - 1, 1, r.out); const j = await cli(["narrate", p.id, "--list", "--json"]); const items = JSON.parse(j.out).items; assert.deepEqual(items.map((x) => x.readEdit ?? null), ["legacy", null, null], "an item with no take playing is not flagged"); }); // --------------------------------------------------------------------------- // THE PLAYING TAKE DECIDES (review 1, finding 3; the orchestrator's ruling): a take is stamped `forReading` when it is // made from words that are not the post's as drafted, and the card follows the shown text while a stamped take plays -- // whatever the script says afterwards. `narration.forReading` decides only with no take playing. // --------------------------------------------------------------------------- test("RULING 3 HOLDS when the script is saved back to the post's words while an edited take still plays, when an older edited take is picked again, and after --draft-scripts --force", async () => { const { postNarrationIn } = await import("./manifest.mjs"); const { FIXTURES, drawn, singleItem, threadItem } = await import("../../report-to-video/post-reading-pin.fixtures.mjs"); void FIXTURES; const drafted = shownDraftOf(singleItem()); const edited = "The council met on Tuesday about the new bridge."; const m = { timeline: [singleItem({ parents: [threadItem().parents[0]], thread: { parent: "excerpt" } })] }; const base = drawn(m.timeline[0]); // Edited, read, picked. postNarrationIn(m, "p-7003", { script: edited, addTake: { file: "narration/posts/7003/take-2.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 3, script: edited } }, { forReading: true }); assert.equal(m.timeline[0].narration.takes[1].forReading, true, "the take is stamped when it is made"); assert.deepEqual(drawn(m.timeline[0]), base); // The script saved back to the post's words, no read: the edited take still plays -- the card as before. postNarrationIn(m, "p-7003", { script: drafted }, { forReading: true }); assert.deepEqual(drawn(m.timeline[0]), base); // A read of the post's words (no stamp), picked; then the older edited take picked again. postNarrationIn(m, "p-7003", { addTake: { file: "narration/posts/7003/take-3.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 6, script: drafted } }, { forReading: true }); assert.equal("forReading" in m.timeline[0].narration.takes[2], false, "a take of the post's words is not stamped"); assert.deepEqual(drawn(m.timeline[0]), base); postNarrationIn(m, "p-7003", { pick: 1 }, { forReading: true }); assert.deepEqual(drawn(m.timeline[0]), base); // --all --draft-scripts --force (the plural writer) while the edited take plays. postNarrationIn(m, "p-7003", { script: drafted }); assert.deepEqual(drawn(m.timeline[0]), base); }); // --------------------------------------------------------------------------- // AN UNEDITED READ IS NOT "EDITED" (review 2, finding 1): a paragraph over 60 words read in several parts, the parts // saying a lexicon word, an anonymous handle said as its shown name -- none is an edit for reading: no stamp, no notice. // --------------------------------------------------------------------------- test("AN UNEDITED READ in several parts of one long paragraph is neither stamped nor flagged (probe 5)", async () => { const { postNarrationIn } = await import("./manifest.mjs"); const { singleItem } = await import("../../report-to-video/post-reading-pin.fixtures.mjs"); const { draftScript, scriptParts } = await import("umtool-report-to-video/post-narration"); const { legacyReadOf, readEditOf } = await import("umtool-report-to-video/post-item"); const long = Array.from({ length: 9 }, (_, i) => `The council met on day ${i + 1} about the new bridge and nobody came.`).join(" "); const e = singleItem({ text: long }); const d = draftScript(long).script; let t = 0; const parts = scriptParts(d).map((p) => { const q = { n: p.n, para: p.para, text: p.text, start: t, seconds: 5 }; t += 5.25; return q; }); assert.ok(parts.length > 1 && d.split("\n\n").length === 1, "one paragraph, read in several parts"); const take = { file: "narration/posts/7003/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: t, script: d, parts }; // A take from before the rule: no notice. assert.equal(legacyReadOf({ ...e, narration: { script: d, pick: 0, takes: [take] } }), false); // A take made now: no stamp. const m = { timeline: [{ ...e, narration: { script: d, takes: [] } }] }; postNarrationIn(m, e.id, { addTake: { ...take } }); assert.equal("forReading" in m.timeline[0].narration.takes[0], false); assert.equal(readEditOf(m.timeline[0]), null); // Its parts' words without its script (an imported take): rebuilt into the paragraph, still not an edit. const { script: _s, ...noScript } = take; void _s; assert.equal(legacyReadOf({ ...e, narration: { script: d, pick: 0, takes: [noScript] } }), false); }); test("A LEXICON WORD or AN ANONYMOUS HANDLE said another way is not an edit: a single read's written script decides; a thread read's post piece's written words", async () => { const { postNarrationIn } = await import("./manifest.mjs"); const { singleItem, threadItem } = await import("../../report-to-video/post-reading-pin.fixtures.mjs"); const { legacyReadOf } = await import("umtool-report-to-video/post-item"); const { postWrittenEdited } = await import("./postThreadRead.mjs"); const e = singleItem(); const d = shownDraftOf(e); // Lexicon: its parts say "the B R I D G E" where the post says "the bridge"; its script is the post's words. const said = d.replace("bridge", "B R I D G E"); const lex = { file: "narration/posts/7003/take-2.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 6, script: d, parts: said.split("\n\n").map((text, i) => ({ n: i + 1, para: i, text, start: i * 3, seconds: 3 })), lexicon: [{ word: "bridge", say: "B R I D G E" }] }; // An anonymous handle said as its shown name (`said`), the script the post's words. const anon = { file: "narration/posts/7003/take-3.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 6, script: d, said: d.replace("council", "Anon 1") }; for (const t of [lex, anon]) { const m = { timeline: [{ ...e, narration: { script: d, takes: [] } }] }; postNarrationIn(m, e.id, { addTake: { ...t } }); assert.equal("forReading" in m.timeline[0].narration.takes[0], false, t.file); assert.equal(legacyReadOf({ ...e, narration: { script: d, pick: 0, takes: [t] } }), false, t.file); } // A thread read: stamped from its post piece's WRITTEN words, not its spoken ones. const th = threadItem(); assert.equal(postWrittenEdited(th, { pieces: [{ role: "post", written: shownDraftOf(th), text: "the B R I D G E said" }] }), false); assert.equal(postWrittenEdited(th, { pieces: [{ role: "post", written: "Other words entirely.", text: "Other words entirely." }] }), true); // An unstamped thread read whose parts say a lexicon word, its script the post's: no notice. assert.equal(legacyReadOf(th), false); }); test("AN IMPORTED READ of words edited for reading is stamped FIRST and its parts are his post's, never a role inferred from the edited words (review 2, finding 6)", async () => { const { importPatches } = await import("./postNarrationCli.mjs"); const { singleItem, threadItem } = await import("../../report-to-video/post-reading-pin.fixtures.mjs"); const e = singleItem({ parents: [threadItem().parents[0]] }); const parentWords = "Does anyone know why the tolls on the old crossing keep rising?"; const script = `The council met on Tuesday about the new bridge.\n\n${parentWords}`; const rows = [{ id: "7003", entry: e.id, script, takes: [{ file: "narration/posts/7003/take-5.orpheus-zac.wav", script, seconds: 6, parts: script.split("\n\n").map((text, i) => ({ n: i + 1, para: i, text, start: i * 3, seconds: 3 })) }] }]; const measure = async () => ({ seconds: 6, at: "2026-11-17T00:00:00.000Z", parts: null }); const { patches } = await importPatches({ timeline: [e] }, rows, measure); const t = patches.find((p) => p.patch.addTake)?.patch.addTake; assert.equal(t.forReading, true); assert.ok(t.parts.every((p) => p.role === undefined), JSON.stringify(t.parts)); });