// REPLACE OVER A VIDEO's READS (lane reads-find, readsReplace.mjs): These reads rewrites the SPOKEN text of each kind // through its own writer in one undo point, the shown text untouched (a post's script, a screen line's said-as made // when it has none, a card's narration; a context piece and a recorded read listed, never changed); undo restores // every line; Everywhere adds a pronunciation entry, previews what it makes stale, never reaches a card, and is whole // words only. Neither synthesizes. Temp dirs, invented people. import "./noLiveEditor.mjs"; import assert from "node:assert/strict"; import { mkdtemp, readdir, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { cardText } from "umtool-report-to-video/post-item"; import { projectReads } from "./reads.mjs"; import { ReplaceError, SAY_WHOLE_WORDS, applyReplace, applySayOnly, replacePlan, sayOnlyPlan, undoReplace } from "./readsReplace.mjs"; import { manifestToken, updatePostNarration } from "./manifest.mjs"; import { readLexicon } from "./postSay.mjs"; import { readManifestOf, readsProject } from "./reads.fixtures.mjs"; import { drawn } from "../../report-to-video/post-reading-pin.fixtures.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-reads-replace-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; const o = (p, x = {}) => ({ reportsRoot: p.root, ...x }); /** What a manifest SHOWS: every item with its spoken-only fields left out (narration, a screen's saidAs). */ const shown = (m) => m.timeline.map(({ narration: _n, ...e }) => ({ ...e, ...(Array.isArray(e.screens) ? { screens: e.screens.map(({ saidAs: _s, ...sc }) => sc) } : {}) })); const takeFiles = async (p) => (await readdir(path.join(p.dir, "narration", "posts"), { recursive: true })).filter((f) => f.endsWith(".wav")).sort(); test("These reads: a post's script, a screen line (said-as created), a card -- one undo point; shown text and drawn card identical; nothing synthesized", async () => { const p = await readsProject(ROOT, "a"); const m0 = await readManifestOf(p); const files0 = await takeFiles(p); const plan = await replacePlan(p, "north", "northern", o(p)); assert.deepEqual(plan.lines.map((l) => [l.id, l.creates]), [["p-1", null], ["c-1", null]]); assert.deepEqual(plan.skipped.map((s) => s.id), ["p-3"], "a recorded read is never replaced"); // A screen line with no said-as: created from the line as read. const tolls = await replacePlan(p, "tolls", "toll charges", o(p)); assert.deepEqual(tolls.lines.map((l) => [l.id, l.before, l.after, l.creates]), [["p-2/screen:s1:0", "Are the tolls going up again?", "Are the toll charges going up again?", "creates its said-as (the line as shown is untouched)"]]); // A context piece: listed, not changed. const bridge = await replacePlan(p, "Bridge works", "The bridge works", o(p)); assert.deepEqual(bridge.lines, []); assert.match(bridge.skipped[0].why, /said as shown: only Everywhere changes this/); await applyReplace(p, "north", "northern", o(p)); await applyReplace(p, "tolls", "toll charges", o(p)); const m1 = await readManifestOf(p); assert.match(m1.timeline[0].narration.script, /northern pier/); assert.equal(m1.timeline[0].narration.forReading, true, "marked for reading by the one writer"); assert.equal(m1.timeline[1].screens[0].saidAs["0"], "Are the toll charges going up again?"); assert.equal(m1.timeline[3].narration.text, "Six months later, the northern pier reopened."); // RULING 3: what is shown is byte-identical, and so is what the cards draw. assert.deepEqual(shown(m1), shown(m0)); assert.equal(cardText(m1.timeline[0]), cardText(m0.timeline[0])); for (const k of [0, 1]) assert.deepEqual(drawn(m1.timeline[k]), drawn(m0.timeline[k]), m0.timeline[k].id); assert.deepEqual(await takeFiles(p), files0, "a replace never synthesizes"); // The changed reads are stale; the others are not. const by = Object.fromEntries((await projectReads(p, o(p))).rows.map((r) => [r.id, r.state])); assert.deepEqual([by["p-1"], by["p-2/screen:s1:0"], by["c-1"], by["p-2/screen:s1:1"], by["p-3"]], ["stale", "stale", "stale", "fresh", "recorded"]); // Undo takes back the newest replace -- every line of it -- and only a replace. await undoReplace(p); assert.equal("saidAs" in (await readManifestOf(p)).timeline[1].screens[0], false); await undoReplace(p); assert.deepEqual(await readManifestOf(p), m0); await updatePostNarration(p.dir, "p-1", { approved: true }, { token: await manifestToken(p.dir) }); await assert.rejects(undoReplace(p), /nothing to undo: the newest replace made here is not one of These reads/); }); test("These reads: the preview says what goes (approval), what the build would refuse, and only the ticked reads change", async () => { const p = await readsProject(ROOT, "b"); await updatePostNarration(p.dir, "p-1", { approved: true }, { token: await manifestToken(p.dir) }); const plan = await replacePlan(p, "north", "northern", o(p)); assert.match(plan.notes["p-1"].join(" "), /its approval goes, as a new script takes it away/); // A said-as that says a number the card masks is refused, and the whole write with it. const bad = await replacePlan(p, "this year", "on 5551234567", o(p)); assert.ok(bad.refused["p-2"]?.some((x) => /number/.test(x)), JSON.stringify(bad.refused)); await assert.rejects(applyReplace(p, "this year", "on 5551234567", o(p)), ReplaceError); // --only: the card alone. await applyReplace(p, "north", "northern", o(p, { only: ["c-1"] })); const m = await readManifestOf(p); assert.match(m.timeline[0].narration.script, /the north pier/); assert.equal(m.timeline[0].narration.approved, true); assert.match(m.timeline[3].narration.text, /northern pier/); }); test("Everywhere: a pronunciation entry -- previews the reads it makes stale and the cards it does not reach; whole words only", async () => { const p = await readsProject(ROOT, "say"); await assert.rejects(sayOnlyPlan(p, "nort", "x", o(p, { word: false })), (e) => e.message === SAY_WHOLE_WORDS); const plan = await sayOnlyPlan(p, "pier", "peer", o(p)); assert.deepEqual(plan.stale.map((s) => s.id), ["p-1"], "the recorded read never; the card is not said through the lexicon"); assert.deepEqual(plan.cards, ["c-1"]); const m0 = await readManifestOf(p); await applySayOnly(p, "pier", "peer", o(p)); assert.deepEqual(await readLexicon(p.dir), [{ word: "pier", say: "peer" }]); assert.deepEqual(await readManifestOf(p), m0, "the draft is not touched"); const row = (await projectReads(p, o(p))).rows.find((r) => r.id === "p-1"); assert.deepEqual([row.state, row.because], ["stale", ["lexicon"]]); // The same word again changes what is said for it (the writer's rule): `was` says so. assert.equal((await sayOnlyPlan(p, "pier", "pee-er", o(p))).was, "peer"); }); // --------------------------------------------------------------------------- // REVIEW 1, MUST 2: Undo the replace never takes a later edit back with it. Other writers keep one undo point per op // for two minutes, so a write after a replace may leave the replace's undo point the newest: the undo is refused // unless the draft is still exactly what the replace left (its token, kept with the replace's record). // --------------------------------------------------------------------------- test("MUST 2: a bench write before the replace, the replace, a re-read landing after it with no undo point of its own -- the undo is refused", async () => { const p = await readsProject(ROOT, "undo-after"); const write = async (patch) => updatePostNarration(p.dir, "p-1", patch, { token: await manifestToken(p.dir) }); await write({ lead: 0.3 }); // a bench edit: its op's undo point (post-narration) await applyReplace(p, "north", "northern", o(p)); const { replaceUndoState } = await import("./readsReplace.mjs"); assert.equal((await replaceUndoState(p)).available, true); // A re-read lands: the same op as the bench edit, inside its two minutes -- no undo point of its own. const m = await readManifestOf(p); await write({ addTake: { ...m.timeline[0].narration.takes[0], file: "narration/posts/1/take-2.orpheus-tara.wav", script: m.timeline[0].narration.script }, keepPick: true }); const st = await replaceUndoState(p); assert.equal(st.available, false); assert.match(st.why, /the draft changed after the replace/); await assert.rejects(undoReplace(p), /not undone: the draft changed after the replace/); assert.equal((await readManifestOf(p)).timeline[0].narration.takes.length, 2, "the re-read is still there"); }); // REVIEW 1, SHOULD 4: Everywhere's undo puts back the entry it changed (its spoken form and its note), and removes only // the entry it added -- never another of the same word. test("SHOULD 4: an Everywhere over an existing entry is taken back to it; with a case and a no-case entry, only its own goes", async () => { const p = await readsProject(ROOT, "say-undo"); const { updateLexicon } = await import("./postSay.mjs"); const { undoSayOnly } = await import("./readsReplace.mjs"); await updateLexicon(p.dir, { add: { word: "pier", say: "peer", note: "the harbour's word" } }); const plan = await sayOnlyPlan(p, "pier", "pee-er", o(p)); assert.equal(plan.was, "peer"); await applySayOnly(p, "pier", "pee-er", o(p)); assert.deepEqual(await readLexicon(p.dir), [{ word: "pier", say: "pee-er" }]); await undoSayOnly(p); assert.deepEqual(await readLexicon(p.dir), [{ word: "pier", say: "peer", note: "the harbour's word" }]); // A case entry and a no-case one of the same word: the undo removes only the one Everywhere added. await updateLexicon(p.dir, { add: { word: "Pier", say: "PEER", case: true } }); await applySayOnly(p, "north", "nor-th", o(p)); await undoSayOnly(p); assert.deepEqual((await readLexicon(p.dir)).map((x) => x.word), ["pier", "Pier"]); await applySayOnly(p, "pier", "pier two", o(p, { matchCase: false })); await undoSayOnly(p); assert.deepEqual(await readLexicon(p.dir), [{ word: "pier", say: "peer", note: "the harbour's word" }, { word: "Pier", say: "PEER", case: true }]); });