// narration-review.mjs: the review file (takes/verdicts.json's shape), the // states -- an edit or a voice change un-approves -- and what the lint and the // build make of them: no file a warning, an un-approved card an error unless // --narration-unreviewed, a rejected card an error always. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { buildVideo, setProgressMode } from "./build-video.mjs"; import { lintManifest } from "./lint.mjs"; import { narrationsOf } from "./narration.mjs"; import { DRAFTS_REVIEW_FILE, NARRATION_REVIEW_FILE, cleanReview, readNarrationReview, reviewProblems, reviewRows, reviewState, setNarrationReview, } from "./narration-review.mjs"; const card = (id, text, extra = {}) => ({ type: "card", id, heading: id, seconds: "auto", narration: { text, ...extra } }); const M = (timeline, render = {}) => ({ slug: "rv", title: "RV", render: { transition: 0, ...render }, timeline }); test("the file: a missing one is absent, a broken one is an error and is never overwritten", async (t) => { const dir = mkdtempSync(path.join(tmpdir(), "rtv-review-")); t.after(() => rmSync(dir, { recursive: true, force: true })); assert.deepEqual(await readNarrationReview(dir), { exists: false, rows: {} }); writeFileSync(path.join(dir, NARRATION_REVIEW_FILE), "{ not json"); const broken = await readNarrationReview(dir); assert.equal(broken.exists, true); assert.match(broken.error, /does not parse/); await assert.rejects(setNarrationReview(dir, "k1", { verdict: null, note: "x" }), /does not parse/); assert.equal(readFileSync(path.join(dir, NARRATION_REVIEW_FILE), "utf8"), "{ not json"); // Rows not of the shape are dropped on read. assert.deepEqual(cleanReview({ k1: { verdict: "maybe", note: 3 }, "": {}, k2: "x" }), { k1: { verdict: null, note: "", key: "", text: "", at: "" } }); }); test("setNarrationReview: a verdict is on words; a rejection needs its note; an empty row goes", async (t) => { const dir = mkdtempSync(path.join(tmpdir(), "rtv-review-set-")); t.after(() => rmSync(dir, { recursive: true, force: true })); await assert.rejects(setNarrationReview(dir, "k1", { verdict: "approved" }), /key and text are required/); await assert.rejects(setNarrationReview(dir, "k1", { verdict: "fine", key: "a", text: "b" }), /verdict must be one of approved, rejected/); await assert.rejects(setNarrationReview(dir, "k1", { verdict: "rejected", key: "a", text: "b" }), /a rejection needs its note/); const a = await setNarrationReview(dir, "k1", { verdict: "approved", key: "K", text: "Words." }); assert.equal(a.entry.verdict, "approved"); assert.equal(a.entry.key, "K"); const r = await setNarrationReview(dir, "k2", { verdict: "rejected", key: "K2", text: "Bad.", note: " overstates it " }); assert.equal(r.entry.note, "overstates it"); // Clearing the verdict forgets the words judged, keeps the note. const c = await setNarrationReview(dir, "k2", { verdict: null }); assert.deepEqual([c.entry.verdict, c.entry.key, c.entry.text, c.entry.note], [null, "", "", "overstates it"]); // No verdict and no note: removed. const gone = await setNarrationReview(dir, "k2", { note: "" }); assert.equal(gone.entry, null); // Written to the drafts review, by the card's address. assert.deepEqual(Object.keys(JSON.parse(readFileSync(path.join(dir, DRAFTS_REVIEW_FILE), "utf8"))), ["narrate/k1/narration"]); assert.deepEqual(Object.keys((await readNarrationReview(dir)).rows), ["k1"]); }); test("states: approved on its words; an edit or another voice leaves it edited; none is unreviewed", () => { const m = M([card("k1", "Then it happened."), card("k2", "And again.")]); const [n1, n2] = narrationsOf(m); const rows = { k1: { verdict: "approved", key: n1.key, text: n1.text, note: "", at: "" } }; assert.equal(reviewState(n1, rows.k1), "approved"); assert.equal(reviewState(n2, undefined), "unreviewed"); const [e1] = narrationsOf(M([card("k1", "Then it happened, twice.")])); assert.equal(reviewState(e1, rows.k1), "edited"); const [v1] = narrationsOf(M([card("k1", "Then it happened.", { voice: "am_adam" })])); assert.equal(reviewState(v1, rows.k1), "edited"); assert.equal(reviewState(n1, { ...rows.k1, verdict: "rejected" }), "rejected"); assert.deepEqual(reviewRows(m, { exists: true, rows }).map((r) => [r.id, r.state]), [["k1", "approved"], ["k2", "unreviewed"]]); }); test("reviewProblems: no file warns; a file holds every un-approved card; the flag lets unreviewed and edited through, never rejected", () => { const m = M([card("k1", "One."), card("k2", "Two."), card("k3", "Three."), { type: "card", id: "plain", seconds: 2 }]); const [n1, , n3] = narrationsOf(m); const review = { exists: true, rows: { k1: { verdict: "approved", key: n1.key, text: n1.text, note: "", at: "" }, k3: { verdict: "rejected", key: n3.key, text: n3.text, note: "says more than the clip", at: "" }, }, }; const none = reviewProblems(m, { exists: false, rows: {} }); assert.deepEqual(none.errors, []); assert.match(none.warnings[0], /the narration \(3 cards\) has no review — drafts\.review\.json has no narration verdicts/); const held = reviewProblems(m, review); assert.equal(held.errors.length, 2, held.errors.join("\n")); assert.match(held.errors[0], /timeline k2: its narration is not reviewed — approve it on umtool's narration page, or build with --narration-unreviewed/); assert.match(held.errors[1], /timeline k3: its narration was rejected \("says more than the clip"\) — rewrite it and approve it, or take the narration out/); const flagged = reviewProblems(m, review, { allowUnreviewed: true }); assert.deepEqual(flagged.errors.map((e) => e.slice(0, 12)), ["timeline k3:"]); assert.match(flagged.notes[0], /k2: its narration is not reviewed — built anyway \(--narration-unreviewed\)/); assert.deepEqual(reviewProblems(m, review, { only: "k1" }), { errors: [], notes: [], warnings: [] }); assert.match(reviewProblems(m, { exists: true, rows: {}, error: "broken" }).errors[0], /broken/); // A cut without narration says nothing, file or not. assert.deepEqual(reviewProblems(M([{ type: "card", id: "a", seconds: 1 }]), { exists: false, rows: {} }), { errors: [], notes: [], warnings: [] }); // Through the lint. const lint = lintManifest(m, { narrationReview: review }); assert.equal(lint.errors.filter((e) => /its narration/.test(e)).length, 2); assert.ok(lintManifest(m, { narrationReview: { exists: false, rows: {} } }).warnings.some((w) => /has no review/.test(w))); }); async function quietly(fn) { const events = []; setProgressMode((e) => events.push(e)); try { return { result: await fn().catch((err) => ({ err })), events }; } finally { setProgressMode("human"); } } test("the build refuses an un-approved narrated card before it asks for a voice; --narration-unreviewed lets it through", async (t) => { const dir = mkdtempSync(path.join(tmpdir(), "rtv-review-build-")); t.after(() => rmSync(dir, { recursive: true, force: true })); const manifestPath = path.join(dir, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify(M([card("k1", "One.")], { audioRate: 48000, audioChannels: 2, fps: 10 }))); // No editor is reachable: a build that asked for the voice would fail on that instead. const editor = { url: "http://127.0.0.1:9", token: "t" }; // No review file: nothing is held -- it goes on to the voice (and, with // --skip-fetch, refuses for want of one), the lint's warning said. const free = await quietly(() => buildVideo({ manifestPath, opts: { skipFetch: true, editor } })); assert.match(String(free.result?.err?.message), /k1: its narration has not been synthesized/); assert.ok(free.events.some((e) => e.ev === "note" && /lint: the narration \(1 card\) has no review/.test(e.message))); await setNarrationReview(dir, "other", { note: "a review file now exists" }); const held = await quietly(() => buildVideo({ manifestPath, opts: { editor } })); assert.match(String(held.result?.err?.message), /lint: timeline k1: its narration is not reviewed/); assert.ok(!held.events.some((e) => e.ev === "note" && /asking the editor/.test(e.message)), "refused before any ask"); const through = await quietly(() => buildVideo({ manifestPath, opts: { skipFetch: true, editor, narrationUnreviewed: true } })); assert.match(String(through.result?.err?.message), /k1: its narration has not been synthesized/); assert.ok(through.events.some((e) => e.ev === "note" && /built anyway \(--narration-unreviewed\)/.test(e.message))); const [n] = narrationsOf(M([card("k1", "One.")])); await setNarrationReview(dir, "k1", { verdict: "rejected", key: n.key, text: n.text, note: "no" }); const rejected = await quietly(() => buildVideo({ manifestPath, opts: { skipFetch: true, editor, narrationUnreviewed: true } })); assert.match(String(rejected.result?.err?.message), /timeline k1: its narration was rejected/); });