// THE READ MODEL (lane reads-find, reads.mjs): every read of a video -- a post's script, a thread read's pieces, its // screen lines, a card's narration -- with what it SAYS, where that is kept, the take that plays, and its state; and // FIND over the spoken text only. Temp dirs, invented people. import "./noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { tuningStale } from "umtool-report-to-video/cast"; import { filterReads, findInReads, findPattern, projectReads, replaceIn } from "./reads.mjs"; import { manifestToken, updatePostNarration } from "./manifest.mjs"; import { resolverFor } from "./castProject.mjs"; import { LIVELY, castDoc, readManifestOf, readsProject } from "./reads.fixtures.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-reads-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; const opts = (p) => ({ reportsRoot: p.root }); test("every kind: a post's script, a thread read's pieces and screen lines, a recorded read, a card -- in cut order, fresh as made", async () => { const p = await readsProject(ROOT, "kinds"); const { rows, counts } = await projectReads(p, opts(p)); assert.deepEqual(rows.map((r) => [r.id, r.kind]), [ ["p-1", "post"], ["p-2/parent:0", "thread"], ["p-2/quoted", "thread"], ["p-2/post", "thread"], ["p-2/screen:s1:0", "screen"], ["p-2/screen:s1:1", "screen"], ["p-3", "post"], ["c-1", "card"], ]); const by = Object.fromEntries(rows.map((r) => [r.id, r])); assert.equal(by["p-1"].state, "fresh"); assert.equal(by["p-1"].character?.id, "ferry"); assert.equal(by["p-1"].storedAt, "narration.script"); assert.equal(by["p-2/post"].state, "fresh", JSON.stringify(by["p-2/post"].because)); assert.equal(by["p-2/screen:s1:0"].spoken, "Are the tolls going up again?"); assert.equal(by["p-2/screen:s1:0"].storedAt, null, "no said-as yet: the line as shown"); assert.equal(by["p-2/screen:s1:0"].shownSpoken, true); assert.equal(by["p-2/parent:0"].editable, false); assert.match(by["p-2/parent:0"].why, /said as shown: only Everywhere changes this/); assert.equal(by["p-3"].state, "recorded"); assert.equal(by["p-3"].editable, false); assert.equal(by["c-1"].state, "fresh"); assert.equal(by["c-1"].storedAt, "narration.text"); assert.deepEqual(counts, { fresh: 7, stale: 0, unpicked: 0, none: 0, recorded: 1 }); // REVIEW 1, SHOULD 5: a thread read's piece plays ITS part (its paragraphs' parts, in order), never the joined take. assert.equal(by["p-2/screen:s1:0"].take.file, "narration/parts-cache/fixture-5.wav"); assert.deepEqual(by["p-2/screen:s1:0"].take.files, ["narration/parts-cache/fixture-5.wav"]); assert.deepEqual(by["p-2/post"].take.files, ["narration/parts-cache/fixture-3.wav", "narration/parts-cache/fixture-4.wav"]); assert.equal(by["p-1"].take.file, "narration/posts/1/take-1.orpheus-tara.wav"); }); test("stale: the words changed (post, screen, card), the lexicon, the character's settings -- and none, unpicked", async () => { const p = await readsProject(ROOT, "stale"); const m = await readManifestOf(p); const tl = m.timeline; tl[0].narration.script = tl[0].narration.script.replace("noon", "twelve noon"); tl[1].screens[0].saidAs = { 1: "Not this year, they told us." }; tl[3].narration.text = "Six months later, the north pier opened again."; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); let by = Object.fromEntries((await projectReads(p, opts(p))).rows.map((r) => [r.id, r])); assert.deepEqual([by["p-1"].state, by["p-1"].because], ["stale", ["words"]]); assert.deepEqual([by["p-2/screen:s1:1"].state, by["p-2/screen:s1:1"].because], ["stale", ["words"]]); assert.equal(by["p-2/screen:s1:1"].storedAt, "screens[0].saidAs.1"); assert.equal(by["p-2/screen:s1:0"].state, "fresh"); assert.deepEqual([by["c-1"].state, by["c-1"].because], ["stale", ["words"]]); // The lexicon: a word the single read says, said another way now. const q = await readsProject(ROOT, "lexicon"); await writeFile(path.join(q.dir, "narration", "lexicon.json"), JSON.stringify([{ word: "pier", say: "peer" }])); by = Object.fromEntries((await projectReads(q, opts(q))).rows.map((r) => [r.id, r])); assert.deepEqual([by["p-1"].state, by["p-1"].because], ["stale", ["lexicon"]]); assert.equal(by["p-1"].said.includes("north peer"), true); // The character's tuning changed: the very rows tuningStale lists (cast stale, the bench's count). const s = await readsProject(ROOT, "settings", { cast: castDoc(LIVELY) }); const r = await projectReads(s, opts(s)); const settings = r.rows.filter((x) => x.because.includes("settings")).map((x) => x.id); assert.deepEqual(settings, ["p-1"]); assert.deepEqual(r.tuningStale.map((x) => x.id), ["p-1"]); assert.deepEqual(tuningStale(await readManifestOf(s), resolverFor({ cast: castDoc(LIVELY) }), { settingTakes: 3 }).map((x) => x.id), ["p-1"]); assert.deepEqual(filterReads(r.rows, { stale: true, character: "ferry" }).map((x) => x.id), ["p-1"]); }); test("unpicked: a re-read joined the list unpicked -- the row waits for a Pick; picked, fresh", async () => { const p = await readsProject(ROOT, "unpicked"); const m = await readManifestOf(p); const e = m.timeline[0]; e.narration.script = e.narration.script.replace("noon", "twelve noon"); e.narration.takes.push({ ...e.narration.takes[0], file: "narration/posts/1/take-2.orpheus-tara.wav", script: e.narration.script, forReading: true }); await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); let row = (await projectReads(p, opts(p))).rows.find((r) => r.id === "p-1"); assert.equal(row.state, "unpicked"); assert.equal(row.newTake.index, 1); await updatePostNarration(p.dir, "p-1", { pick: 1 }, { token: await manifestToken(p.dir) }); row = (await projectReads(p, opts(p))).rows.find((r) => r.id === "p-1"); assert.equal(row.state, "fresh"); // No take at all: none. const q = await readsProject(ROOT, "none", { timeline: [{ type: "post", id: "p-9", post: "ferry-x/9", platform: "x", author: "A", handle: "invented_a", date: "2026-10-30T10:00:00.000Z", text: "Just words.", narration: { script: "Just words." } }] }); assert.equal((await projectReads(q, opts(q))).rows[0].state, "none"); }); test("FIND: plain text, case on or off, whole word -- matches in the spoken text only, with a count of matches and lines", async () => { const p = await readsProject(ROOT, "find"); const { rows } = await projectReads(p, opts(p)); const all = findInReads(rows, "north"); assert.deepEqual(all.rows.map((r) => r.id), ["p-1", "p-3", "c-1"]); assert.equal(all.matches, 4); assert.equal(all.lines, 3); assert.equal(findInReads(rows, "North", { matchCase: true }).matches, 0); assert.equal(findInReads(rows, "nort").matches, 4); assert.equal(findInReads(rows, "nort", { word: true }).matches, 0); // A phrase across any spaces; regex characters literal. assert.equal(findInReads(rows, "north pier").matches, 3); assert.equal(findInReads(rows, "(north)").matches, 0); // Shown text is never searched: the bridge post's card words that no read says. assert.equal(findInReads(rows, "second paragraph nobody reads").matches, 0); assert.equal(replaceIn("North north", findPattern("north"), "$&-x"), "$&-x $&-x"); });