// `umtool narration` (cardNarrationCli.mjs; lane line-rerun): a card's narration read again, kept beside the build // without changing the card, picked, and its verdict kept on the same words. A stub for the editor; temp dirs. 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 { cardNarrationCommand, cardReadings, nextCardReading } from "./cardNarrationCli.mjs"; import { keepNarration } from "umtool-report-to-video/narration-store"; import { readNarrationReview, reviewState, setNarrationReview } from "umtool-report-to-video/narration-review"; import { narrationsOf } from "umtool-report-to-video/narration"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-card-narration-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); const CARD = { type: "card", id: "c1", seconds: "auto", heading: "The crossing", narration: { text: "Six months later, the ferry ran again.", engine: "orpheus", voice: "tara" } }; async function project(name) { 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 }, timeline: [CARD], }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); /** The editor, stubbed: a tone kept as the editor's result would be (narration-store.mjs keepNarration). */ const narrate = (asked) => async ({ narrations, dir }) => { const recs = new Map(); for (const n of narrations) { asked.push(n); const wav = path.join(ROOT, `${n.key}.src.wav`); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "sine=f=330:d=1.4", "-ar", "48000", "-ac", "1", wav]); recs.set(n.id, await keepNarration(dir, n, { wav, engine: n.engine, voice: n.voice, reading: n.reading ?? 0, ...(n.tuning ? { tuning: n.tuning } : {}) }, { jobId: `job-${asked.length}` })); } return recs; }; const run = async (args, asked = []) => { const lines = []; const code = await cardNarrationCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(s), narrate: narrate(asked) }); return { code, out: lines.join("\n"), json: () => JSON.parse(lines.join("\n")) }; }; test("--read keeps a reading WITHOUT changing the card; --reading next is one past the highest of these words; --pick plays it, a verdict on the same words kept", async () => { const p = await project("read"); const asked = []; let r = await run([p.id, "c1", "--read", "--json"], asked); assert.equal(r.code, 0, r.out); assert.equal(r.json().plays, true); r = await run([p.id, "c1", "--read", "--reading", "next", "--temperature", "0.7", "--json"], asked); assert.equal(r.code, 0, r.out); assert.deepEqual([asked[1].reading, asked[1].tuning], [1, { temperature: 0.7 }]); assert.equal(r.json().plays, false); assert.deepEqual((await manifest(p)).timeline[0].narration, CARD.narration, "the card is not changed by a reading"); const readings = await cardReadings(p.dir, "c1"); assert.equal(readings.length, 2); assert.equal(nextCardReading(readings, CARD.narration.text), 2); // Approved as it plays now; picking reading 1 of the same words keeps the verdict. const now = narrationsOf(await manifest(p))[0]; await setNarrationReview(p.dir, "c1", { verdict: "approved", key: now.key, text: now.text }); r = await run([p.id, "c1", "--pick", readings[1].key, "--json"]); assert.equal(r.code, 0, r.out); assert.equal(r.json().verdictKept, true); const after1 = (await manifest(p)).timeline[0].narration; assert.deepEqual([after1.reading, after1.tuning], [1, { temperature: 0.7 }]); const n1 = narrationsOf(await manifest(p))[0]; assert.equal(n1.key, readings[1].key, "the card plays the picked reading's file"); assert.equal(reviewState(n1, (await readNarrationReview(p.dir)).rows.c1), "approved"); // Back to the first: one press, nothing synthesized. r = await run([p.id, "c1", "--pick", readings[0].key.slice(0, 12)], asked); assert.equal(r.code, 0, r.out); assert.equal(asked.length, 2); assert.equal("reading" in (await manifest(p)).timeline[0].narration, false); }); test("--read --text reads other words (the card keeps its own until a pick); setting the words, a reading and the tuning; refusals", async () => { const p = await project("set"); const asked = []; const r = await run([p.id, "c1", "--read", "--text", "Six months on, the ferry ran.", "--reading", "next", "--json"], asked); assert.equal(r.code, 0, r.out); assert.equal(asked[0].reading ?? 0, 0, "new words: the first reading"); assert.equal((await manifest(p)).timeline[0].narration.text, CARD.narration.text); assert.equal((await run([p.id, "c1", "--reading", "3"])).code, 0); assert.equal((await manifest(p)).timeline[0].narration.reading, 3); assert.equal((await run([p.id, "c1", "--tuning-off"])).code, 0); assert.equal((await run([p.id, "c1", "--reading", "next"])).code, 1); assert.equal((await run([p.id, "c1", "--pick", "0000000000"])).code, 1); assert.equal((await run([p.id, "nope", "--read"])).code, 1); const list = await run([p.id, "--list", "--json"]); assert.equal(list.json().cards[0].readings.length, 1); }); test("AN ENGINE WITH NO SEED (Kokoro reports no reading): a reading asked is kept under it, listed as it, and picked back to its key", async () => { const p = await project("kokoro"); const m = await manifest(p); m.timeline[0].narration = { text: CARD.narration.text, engine: "kokoro", voice: "af_heart" }; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); const asked = []; const noSeed = async ({ narrations, dir }) => { const recs = new Map(); for (const n of narrations) { asked.push(n); const wav = path.join(ROOT, `${n.key}.k.wav`); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "sine=f=330:d=1.2", "-ar", "48000", "-ac", "1", wav]); recs.set(n.id, await keepNarration(dir, n, { wav, engine: n.engine, voice: n.voice }, { jobId: "job" })); } return recs; }; const go = async (args) => { const lines = []; const code = await cardNarrationCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(s), narrate: noSeed }); return { code, out: lines.join("\n") }; }; assert.equal((await go([p.id, "c1", "--read"])).code, 0); assert.equal((await go([p.id, "c1", "--read", "--reading", "next"])).code, 0); assert.equal(asked[1].reading, 1); const readings = await cardReadings(p.dir, "c1"); const second = readings.find((x) => x.key === asked[1].key); const r = await go([p.id, "c1", "--pick", second.key]); assert.equal(r.code, 0, r.out); assert.equal((await manifest(p)).timeline[0].narration.reading, 1); assert.equal(narrationsOf(await manifest(p))[0].key, second.key); });