// The narration review's umtool side (release 25, T5): the manifest writer's // `updateNarration` (only the words change; the build's validator refuses bad // ones; a snapshot first) and the page's view (each narrated card's words, // what it shows, its state, whether its voice is kept). // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { StructureRefused, updateNarration } from "./manifest.mjs"; import { narrationReviewView } from "./narration-review.mjs"; import { narrationsOf } from "umtool-report-to-video/narration"; import { setNarrationReview } from "umtool-report-to-video/narration-review"; const MANIFEST = { slug: "nr", title: "NR", render: { transition: 0.5 }, timeline: [ { type: "card", id: "k1", kicker: "2024", heading: "Then", sub: "the county", seconds: "auto", narration: { text: "Then it happened." }, onscreen: { title: "Deck title" } }, { type: "card", id: "k2", heading: "Now", seconds: 6, narration: { text: "And again.", voice: "am_adam" } }, { type: "card", id: "plain", heading: "Plain", seconds: 2 }, { type: "clip", id: "c1", video: "v", start: 1, end: 2 }, ], }; function project(t) { const dir = mkdtempSync(path.join(tmpdir(), "umtool-narration-")); t.after(() => rmSync(dir, { recursive: true, force: true })); writeFileSync(path.join(dir, "video.manifest.json"), JSON.stringify(MANIFEST, null, 2) + "\n"); return dir; } const read = (dir) => JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8")); test("updateNarration changes the words alone, trimmed, after a snapshot; the build's validator refuses bad words", async (t) => { const dir = project(t); const r = await updateNarration(dir, "k1", " Then, at last, it happened. "); assert.equal(r.changed, true); const m = read(dir); assert.deepEqual(m.timeline[0].narration, { text: "Then, at last, it happened. " }); assert.equal(m.timeline[0].heading, "Then"); assert.ok(readdirSync(path.join(dir, "revisions")).some((n) => /auto-before-narration/.test(n))); await assert.rejects(updateNarration(dir, "k1", "a b"), (e) => e instanceof StructureRefused && //.test(e.message)); await assert.rejects(updateNarration(dir, "k1", " "), /text must be non-empty words/); await assert.rejects(updateNarration(dir, "plain", "Hi."), /plain is not a card with a narration/); await assert.rejects(updateNarration(dir, "c1", "Hi."), /c1 is not a card with a narration/); assert.equal((await updateNarration(dir, "k2", "And again.")).changed, false); }); test("the view: each narrated card's words, voice, screen, state and kept voice", async (t) => { const dir = project(t); let v = await narrationReviewView(dir, MANIFEST); assert.equal(v.exists, false); assert.deepEqual(v.rows.map((r) => [r.id, r.state, r.voice, r.engine]), [["k1", "unreviewed", "af_heart", "kokoro"], ["k2", "unreviewed", "am_adam", "kokoro"]]); assert.deepEqual(v.rows[0].screen, { kicker: "2024", heading: "Then", sub: "the county", title: "Deck title", style: null }); assert.equal(v.rows[0].words, 3); assert.equal(v.rows[0].kept, null); const [n1] = narrationsOf(MANIFEST); await setNarrationReview(dir, "k1", { verdict: "approved", key: n1.key, text: n1.text }); // A kept voice: its record and its sound beside the build. const store = path.join(dir, "out", "narration"); mkdirSync(store, { recursive: true }); writeFileSync(path.join(store, `${n1.key}.json`), JSON.stringify({ key: n1.key, seconds: 1.4, disclosure: "Narration: synthetic voice (Kokoro-82M ยท af_heart)" })); writeFileSync(path.join(store, `${n1.key}.wav`), "RIFF"); v = await narrationReviewView(dir, MANIFEST); assert.equal(v.exists, true); assert.deepEqual(v.counts, { approved: 1, rejected: 0, edited: 0, unreviewed: 1 }); assert.equal(v.rows[0].kept.seconds, 1.4); // New words: the approval does not carry over, and the old voice is not theirs. const edited = { ...MANIFEST, timeline: [{ ...MANIFEST.timeline[0], narration: { text: "Other words." } }, ...MANIFEST.timeline.slice(1)] }; v = await narrationReviewView(dir, edited); assert.equal(v.rows[0].state, "edited"); assert.equal(v.rows[0].judgedText, "Then it happened."); assert.equal(v.rows[0].kept, null); });