// quoteCheck.mjs (lane one-clock): the plan finds what the render would play, // the run judges each clip from the words the editor heard and the cues, the // gate refuses a moved or missing quote unless ruled, and --align snaps the cut // to the words. The words and cues are the 6 Nov c43 window's // (report-to-video/fixtures/rev9-a3-rumble-words.json): heard 13 s late. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { alignPatch, judgeClip, planQuoteCheck, quoteCheckCommand, quoteGate, readQuoteCheck, rowIsCurrent, runQuoteCheck } from "./quoteCheck.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIX = JSON.parse(await readFile(path.join(HERE, "..", "..", "report-to-video", "fixtures", "rev9-a3-rumble-words.json"), "utf8")); const C43 = FIX.clips.find((c) => c.id === "c43"); const C44 = FIX.clips.find((c) => c.id === "c44"); async function project() { const root = await mkdtemp(path.join(os.tmpdir(), "quote-check-")); const dir = path.join(root, "diet"); const raw = path.join(dir, "out", "clips-raw"); await mkdir(raw, { recursive: true }); const channels = path.join(root, "channels"); await mkdir(channels, { recursive: true }); // The windows the render would play: the project's raw cache, named for the cue clock. for (const c of [C43, C44]) await writeFile(path.join(raw, `${c.video}_${c.window[0].toFixed(2)}-${c.window[1].toFixed(2)}.mp4`), "mp4"); const manifest = { provenance: { channelSlug: "the-quartering-rumble", channelsDir: channels, channelsFallback: false }, timeline: [ { type: "clip", id: "c43", channel: C43.channel, video: C43.video, start: 2119.34, end: 2127.44, quote: C43.quote }, { type: "clip", id: "c44", channel: C44.channel, video: C44.video, start: 633.42, end: 644.16, quote: C44.quote }, { type: "clip", id: "c99", channel: C44.channel, video: C44.video, start: 10, end: 20 }, ], }; await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest)); return { root, dir, channels, manifest }; } // The editor, stood in for: the fixture's words between start and end, on the file's clock. const fakeTranscribe = async (req) => { const clip = [C43, C44].find((c) => req.path.includes(c.video)); return clip.words.filter((w) => w.start >= req.start - 0.01 && w.end <= req.end + 0.01); }; const fakeCues = async (_slug, video) => [C43, C44].find((c) => c.video === video)?.cues ?? null; test("plan, run, gate, align: c43 is heard 13 s late; c44 where its cues say", async () => { const p = await project(); try { const plan = await planQuoteCheck(p.dir, p.manifest, { channelsDirs: [p.channels] }); const c43 = plan.find((r) => r.id === "c43"); assert.equal(c43.source.kind, "raw-cache"); assert.equal(c43.source.windowStart, C43.window[0]); // The cut ±15 s, inside the window the render would cut from. assert.deepEqual(c43.span, { from: 2104.34, to: 2142.44 }); assert.deepEqual(c43.file, { start: 0, end: 38.1 }); assert.equal(plan.find((r) => r.id === "c99").skip, "no-quote"); const lines = []; const { doc } = await runQuoteCheck(p.dir, p.manifest, plan, { transcribe: fakeTranscribe, cuesOf: fakeCues, log: (l) => lines.push(l) }); assert.equal(doc.clips.c43.status, "moved"); assert.ok(Math.abs(doc.clips.c43.offset - 13.1) < 0.15, String(doc.clips.c43.offset)); assert.equal(doc.clips.c43.against, "cues"); assert.equal(doc.clips.c44.status, "ok"); assert.equal(doc.clips.c99.status, "no-quote"); assert.match(lines.join("\n"), /c43 +moved +\+13\.\d\d s heard 2132\.\d\d/); assert.deepEqual(await readQuoteCheck(p.dir), doc); // The gate: c43 refused, c44 not; ruled, c43 renders; a changed cut is stale and says nothing. assert.deepEqual(quoteGate(p.manifest.timeline, doc).map((g) => [g.id, g.status]), [["c43", "moved"]]); const ruled = p.manifest.timeline.map((e) => (e.id === "c43" ? { ...e, quoteRuled: true } : e)); assert.deepEqual(quoteGate(ruled, doc), []); const moved = p.manifest.timeline.map((e) => (e.id === "c43" ? { ...e, cutStart: 2120, cutEnd: 2127 } : e)); assert.equal(rowIsCurrent(doc.clips.c43, moved[0]), false); assert.deepEqual(quoteGate(moved, doc), []); // --align: the cut on the words as heard, the extent widened to hold it. const patch = alignPatch(p.manifest.timeline[0], doc.clips.c43); assert.equal(patch.lockCut, true); assert.ok(Math.abs(patch.cutStart - (doc.clips.c43.heard.from - 0.15)) < 0.011); assert.ok(patch.cutStart > 2132 && patch.cutEnd > 2140, JSON.stringify(patch)); assert.equal(patch.start, undefined); assert.equal(patch.end, patch.cutEnd); assert.throws(() => alignPatch(moved[0], doc.clips.c43), /stale/); assert.throws(() => alignPatch(p.manifest.timeline[0], null), /no quote check/); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("judge: not heard is not-found; with no cues the cut is what the offset is measured from", () => { const row = { quote: C43.quote, cut: { from: 2119.34, to: 2127.44 }, span: { from: 2104.34, to: 2142.44 } }; const words = C43.words.map((w) => ({ ...w, start: w.start + C43.window[0], end: w.end + C43.window[0] })); const noCues = judgeClip(row, words, null); assert.equal(noCues.against, "cut"); // Against the cut only: moved when the words are heard outside the cut (the cut does not play them). assert.equal(noCues.status, "moved"); const inside = judgeClip({ ...row, cut: { from: 2130, to: 2142 } }, words, null); assert.equal(inside.status, "ok"); const elsewhere = judgeClip({ ...row, quote: "I promise you by this time next year I will be down 100 pounds" }, words, C43.cues); assert.equal(elsewhere.status, "not-found"); }); test("the CLI: a project's clips checked through the injected editor, --status after", async () => { const p = await project(); try { const out = []; const code = await quoteCheckCommand(["diet", "--clips", "c43"], { reportsRoot: p.root, log: (l) => out.push(l), err: (l) => out.push(`ERR ${l}`), transcribe: fakeTranscribe, cuesOf: fakeCues, }); assert.equal(code, 0, out.join("\n")); assert.match(out.join("\n"), /0 ok, 1 moved, 0 not found/); const st = []; assert.equal(await quoteCheckCommand(["diet", "--status"], { reportsRoot: p.root, log: (l) => st.push(l), err: (l) => st.push(l) }), 0); assert.match(st.join("\n"), /c43 +moved/); assert.match(st.join("\n"), /c44 +unchecked/); } finally { await rm(p.root, { recursive: true, force: true }); } }); test("umtool check: a moved quote and a missing one are blocking, with the fix; ruled or stale, nothing", async () => { const { decisionsFor } = await import("../projects/core.mjs"); const { walkProjects } = await import("../projects/walk.mjs"); const root = await mkdtemp(path.join(os.tmpdir(), "quote-decisions-")); try { const channelsDir = path.join(root, "channels"); const vdir = path.join(channelsDir, "chan", "data", "vid"); await mkdir(vdir, { recursive: true }); await writeFile(path.join(vdir, "transcript.cues.json"), JSON.stringify({ cues: Array.from({ length: 200 }, (_, i) => ({ start: i, end: i + 1, text: `Line ${i}.` })) })); const dir = path.join(root, "reports", "cut"); await mkdir(dir, { recursive: true }); const clip = (id, start, extra = {}) => ({ type: "clip", id, video: "vid", start, end: start + 5, cite: start, lock: true, quote: `Line ${start}`, ...extra }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "cut", title: "Cut", provenance: { channelSlug: "chan", channelsDir: path.relative(dir, channelsDir), siteOrigin: "https://example.test" }, render: { width: 1920, height: 1080, fps: 30 }, timeline: [clip("a", 10), clip("b", 20), clip("c", 30, { quoteRuled: true }), clip("d", 40)], })); const row = (start, status, extra = {}) => ({ status, cut: { from: start, to: start + 5 }, quote: `Line ${start}`, match: 0.3, ...extra }); await writeFile(path.join(dir, "quote-check.json"), JSON.stringify({ version: 1, clips: { a: row(10, "moved", { offset: 13.06, match: 1, heard: { from: 23.06, to: 25 } }), b: row(20, "not-found"), c: row(30, "not-found"), d: { ...row(40, "not-found"), cut: { from: 41, to: 45 } }, }, })); const [ref] = (await walkProjects(path.join(root, "reports"))).filter((p) => p.id === "cut"); const ds = (await decisionsFor(ref, path.join(root, "reports"))).filter((d) => d.kind.startsWith("clip-quote")); assert.deepEqual(ds.map((d) => [d.kind, d.target, d.severity]), [ ["clip-quote-moved", "a", "blocking"], ["clip-quote-missing", "b", "blocking"], ]); assert.match(ds[0].why, /a: its quote is heard \+13\.06 s from its cut — umtool window cut a --align/); assert.equal(ds[0].fix, "umtool window cut a --align # or --quote-ruled, having heard it"); assert.match(ds[1].fix, /umtool fetch cut --refetch b/); // The one press (L4): a moved quote's cut snapped to the words as heard (alignPatch); a missing one checked again. assert.deepEqual(ds[0].settle, { kind: "patch", clip: "a", patch: alignPatch({ ...clip("a", 10), type: "clip" }, { ...row(10, "moved", { heard: { from: 23.06, to: 25 } }) }), label: "Snap the cut to the words" }); assert.equal(ds[0].settle.patch.lockCut, true); assert.deepEqual(ds[1].settle, { kind: "quote-check", clips: ["b"], label: "Run quote-check" }); } finally { await rm(root, { recursive: true, force: true }); } }); test("prep --status says the quote check's verdicts on the clips as they are now", async () => { const { quoteLine } = await import("./prepCli.mjs"); const p = await project(); try { assert.equal(await quoteLine(p.dir), null); const plan = await planQuoteCheck(p.dir, p.manifest, { channelsDirs: [p.channels] }); await runQuoteCheck(p.dir, p.manifest, plan, { transcribe: fakeTranscribe, cuesOf: fakeCues }); const q = await quoteLine(p.dir); assert.equal(q.line, " quotes: 1 ok, 1 moved (c43) — umtool window