// The video knobs' writers (plans/video-knobs-ui.md): a claim's stamp and // count, a clip's sound, the fact-check's stamp, the deck's markers, the // encode ladder -- each token-guarded, each refusal leaving the file as it was, // and the variants/sync read-back. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MARKER_LIMITS } from "umtool-report-to-video/marker-limits"; // The read-back resolves files inside the reports root: this suite's own. const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-knobs-")); process.env.REPORTS_DIR = ROOT; const { MANIFEST_NAME, StaleToken, StructureRefused, manifestToken, moveEntry, updateClaimKnobs, updateClip, updateDeckMarkers, updateFactcheck, updateOnscreen, updateVariants, } = await import("./manifest.mjs"); const knobs = await import("./knobs.mjs"); const { buildFileOf, variantsState, variantFile } = await import("./variantsView.mjs"); const base = () => ({ slug: "t", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5, chrome: { engine: "hyperframes", layout: "deck" } }, timeline: [ { type: "clip", id: "c01", video: "v1", start: 10, end: 20, claim: { id: "k1", verdict: "CONTRADICTED" } }, { type: "clip", id: "c02", video: "v2", start: 30, end: 41, claim: { id: "k1", verdict: "CONTRADICTED" } }, { type: "clip", id: "c03", video: "v3", start: 50, end: 55 }, ], }); let n = 0; async function project(manifest = base()) { const dir = path.join(ROOT, `p${(n += 1)}`); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); return dir; } const readRaw = (dir) => readFile(path.join(dir, MANIFEST_NAME), "utf8"); const read = async (dir) => JSON.parse(await readRaw(dir)); const clip = (m, id) => m.timeline.find((e) => e.id === id); test.after(() => rm(ROOT, { recursive: true, force: true })); test("updateClaimKnobs: sets each knob, says which entry stamps, clears with null and with the default", async () => { const dir = await project(); const r = await updateClaimKnobs(dir, "c02", { at: 35.123, hold: 4, stamp: false, count: 2, label: " 2 SEIZURES " }, { token: await manifestToken(dir) }); assert.equal(r.stamps, true); assert.equal(r.stampsOn, "c02"); assert.deepEqual(clip(await read(dir), "c02").claim, { id: "k1", verdict: "CONTRADICTED", at: 35.12, hold: 4, stamp: false, count: 2, label: "2 SEIZURES" }); const r2 = await updateClaimKnobs(dir, "c02", { at: null, stamp: true, count: 1, label: "" }); assert.deepEqual(r2.claim, { id: "k1", verdict: "CONTRADICTED", hold: 4 }); // c01 carries the claim but does not stamp it: its knobs are inert, and the answer says so. const r3 = await updateClaimKnobs(dir, "c01", { count: 3 }); assert.equal(r3.stamps, false); assert.equal(r3.stampsOn, "c02"); }); test("updateClaimKnobs: refuses a bad value, an at outside the window, an entry with no claim, a stale token", async () => { const dir = await project(); const before = await readRaw(dir); await assert.rejects(updateClaimKnobs(dir, "c02", { at: 50 }), /claim\.at 50 is outside the clip's window 30–41/); await assert.rejects(updateClaimKnobs(dir, "c02", { hold: 0.2 }), /claim\.hold must be from 0\.5 to 10 seconds/); await assert.rejects(updateClaimKnobs(dir, "c02", { count: 1.5 }), /whole number from 1 to 99/); await assert.rejects(updateClaimKnobs(dir, "c02", { label: "x".repeat(21) }), /at most 20/); await assert.rejects(updateClaimKnobs(dir, "c02", { colour: 1 }), /not a claim knob/); await assert.rejects(updateClaimKnobs(dir, "c03", { count: 2 }), /carries no claim/); await assert.rejects(updateClaimKnobs(dir, "nope", { count: 2 }), /no timeline entry/); await assert.rejects(updateClaimKnobs(dir, "c02", { count: 2 }, { token: "1" }), StaleToken); assert.equal(await readRaw(dir), before); }); test("updateClaimKnobs: a variant's twin gets the same knobs", async () => { const m = base(); m.timeline.push({ type: "clip", id: "c02", variant: "full", video: "v2", start: 30, end: 41, claim: { id: "k1", verdict: "CONTRADICTED" } }); const dir = await project(m); await updateClaimKnobs(dir, "c02", { count: 2 }); assert.deepEqual((await read(dir)).timeline.filter((e) => e.id === "c02").map((e) => e.claim.count), [2, 2]); }); test("updateOnscreen: a row naming only id and verdict keeps the knobs; a new claim id drops them; a row with knobs replaces them", async () => { const dir = await project(); await updateClaimKnobs(dir, "c02", { count: 2, at: 33 }); await updateOnscreen(dir, { c02: { title: "T", claim: { id: "k1", verdict: "CONTRADICTED" } } }); assert.deepEqual(clip(await read(dir), "c02").claim, { id: "k1", verdict: "CONTRADICTED", count: 2, at: 33 }); await updateOnscreen(dir, { c02: { title: "T", claim: { id: "k1", verdict: "CONTRADICTED", hold: 2 } } }); assert.deepEqual(clip(await read(dir), "c02").claim, { id: "k1", verdict: "CONTRADICTED", hold: 2 }); await updateOnscreen(dir, { c01: { claim: { id: "k9", verdict: "CONTRADICTED" } }, c02: { title: "T", claim: { id: "k9", verdict: "CONTRADICTED" } } }); assert.deepEqual(clip(await read(dir), "c02").claim, { id: "k9", verdict: "CONTRADICTED" }); await assert.rejects(updateOnscreen(dir, { c02: { claim: { id: "k9", verdict: "CONTRADICTED", count: 0 } } }), /claim\.count/); }); test("updateClip audio: merged per key, rounded, a 0 or null removes a key, null removes all; out of range is refused", async () => { const dir = await project(); await updateClip(dir, "c03", { audio: { offset: -68.4, gain: 1.26 } }); assert.deepEqual(clip(await read(dir), "c03").audio, { offset: -68, gain: 1.3 }); await updateClip(dir, "c03", { audio: { fadeIn: 0.333, gain: null } }); assert.deepEqual(clip(await read(dir), "c03").audio, { offset: -68, fadeIn: 0.33 }); await updateClip(dir, "c03", { audio: { offset: 0, fadeIn: 0 } }); assert.equal("audio" in clip(await read(dir), "c03"), false); await updateClip(dir, "c03", { audio: { offset: 40 } }); await updateClip(dir, "c03", { audio: null }); assert.equal("audio" in clip(await read(dir), "c03"), false); const before = await readRaw(dir); await assert.rejects(updateClip(dir, "c03", { audio: { offset: 2500 } }), /offset must be a number from -2000 to 2000/); await assert.rejects(updateClip(dir, "c03", { audio: { pan: 1 } }), /not an audio setting/); await assert.rejects(updateClip(dir, "c03", { audio: 3 }), /audio must be/); assert.equal(await readRaw(dir), before); }); test("updateClip: a window that leaves claim.at outside is refused", async () => { const dir = await project(); await updateClaimKnobs(dir, "c02", { at: 31 }); await assert.rejects(updateClip(dir, "c02", { start: 32 }), /claim\.at 31\) must lie inside the window 32–41/); await updateClip(dir, "c02", { start: 30.5 }); }); test("updateClip: the refusal carries the fixes, and markFix / muteFix apply one in the same write", async () => { const dir = await project(); await updateClaimKnobs(dir, "c02", { at: 31 }); // The refusal: a MarksOutside naming the second and where each fix would put it. const err = await updateClip(dir, "c02", { start: 32.5, end: 39 }).catch((e) => e); assert.equal(err.name, "MarksOutside"); assert.equal(err.kind, "mark"); assert.deepEqual(err.window, { start: 32.5, end: 39 }); assert.deepEqual(err.marks, [{ index: null, id: err.marks[0].id, at: 31, first: 32.5, nearest: 32.5 }]); // Moved to the first second that plays, in the one write that also trims. const before = await readRaw(dir); const r = await updateClip(dir, "c02", { start: 32.5, end: 39, markFix: "first" }); assert.equal(r.entry.start, 32.5); assert.equal(clip(await read(dir), "c02").claim.at, 32.5); assert.notEqual(await readRaw(dir), before); // A named second; then clear: the stamp keeps its verdict, loses its second. await updateClaimKnobs(dir, "c02", { at: 33 }); await updateClip(dir, "c02", { start: 34, markFix: 35.25 }); assert.equal(clip(await read(dir), "c02").claim.at, 35.25); await updateClip(dir, "c02", { start: 36, markFix: "clear" }); assert.equal("at" in clip(await read(dir), "c02").claim, false); assert.ok(clip(await read(dir), "c02").claim.verdict, "the stamp is still there"); // A bad fix is refused in words. await updateClaimKnobs(dir, "c02", { at: 36.5 }); await assert.rejects(updateClip(dir, "c02", { start: 37, markFix: "later" }), /markFix is first, nearest, clear or a source second/); // The mute mark: refused with its fixes, then moved to the nearest second the clip plays. await updateClip(dir, "c02", { start: 36, muteFrom: 38.5, markFix: "nearest" }); const m = await updateClip(dir, "c02", { end: 37.5 }).catch((e) => e); assert.equal(m.name, "MarksOutside"); assert.equal(m.kind, "mute"); await updateClip(dir, "c02", { end: 37.5, muteFix: "nearest" }); assert.equal(clip(await read(dir), "c02").muteFrom, 37.5); }); test("updateFactcheck: stamp scale, motion, opacity and a verdict's stampLabel; refused out of range", async () => { const dir = await project(); const r = await updateFactcheck(dir, { stamp: { scale: 1.4, motion: "pop", opacity: 0.8 }, verdicts: { CONTRADICTED: { label: "Seizures", stampLabel: "SEIZURE" } } }, { token: await manifestToken(dir) }); assert.deepEqual(r.factcheck.stamp, { scale: 1.4, motion: "pop", opacity: 0.8 }); assert.equal((await read(dir)).render.chrome.factcheck.verdicts.CONTRADICTED.stampLabel, "SEIZURE"); const before = await readRaw(dir); await assert.rejects(updateFactcheck(dir, { stamp: { motion: "spin" } }), (e) => e instanceof StructureRefused && /stamp\.motion/.test(e.message)); await assert.rejects(updateFactcheck(dir, { stamp: { scale: 3 } }), /stamp\.scale must be from 0\.5 to 2/); await assert.rejects(updateFactcheck(dir, { stamp: { opacity: 0.1 } }), /stamp\.opacity must be from 0\.3 to 1/); await assert.rejects(updateFactcheck(dir, { verdicts: { CONTRADICTED: { stampLabel: "x".repeat(30) } } }), /stampLabel is 30 characters/); assert.equal(await readRaw(dir), before); }); test("updateDeckMarkers: sets the list (align auto dropped) and the layout, leaves the rest of the deck, refuses bad ones", async () => { const m = base(); m.render.chrome.deck = { height: 300 }; const dir = await project(m); const r = await updateDeckMarkers(dir, { markers: [{ after: "c01", label: " Arrival ", color: "", align: "auto" }, { after: "c02", label: "Rescue", color: "accent", align: "right" }], markerLayout: "truncate" }, { token: await manifestToken(dir) }); assert.deepEqual(r.markers, [{ after: "c01", label: "Arrival" }, { after: "c02", label: "Rescue", color: "accent", align: "right" }]); const deck = (await read(dir)).render.chrome.deck; assert.equal(deck.height, 300); assert.equal(deck.markerLayout, "truncate"); const before = await readRaw(dir); await assert.rejects(updateDeckMarkers(dir, { markers: [{ after: "c01", label: "x", align: "middle" }] }), /align must be/); await assert.rejects(updateDeckMarkers(dir, { markerLayout: "wrap" }), /markerLayout must be/); await assert.rejects(updateDeckMarkers(dir, { markers: [{ after: "zz", label: "x" }] }), /zz/); await assert.rejects(updateDeckMarkers(dir, { markers: [{ after: "c01", label: "x".repeat(25) }] }), /at most 24/); await assert.rejects(updateDeckMarkers(dir, { markers: Array.from({ length: MARKER_LIMITS.count + 1 }, () => ({ after: "c01", label: "x" })) }), /at most 32/); assert.equal(await readRaw(dir), before); await updateDeckMarkers(dir, { markers: [], markerLayout: null }); const after = (await read(dir)).render.chrome.deck; assert.equal("markers" in after, false); assert.equal("markerLayout" in after, false); }); test("a structural edit works on a manifest carrying knobs", async () => { const m = base(); m.render.chrome.deck = { markers: [{ after: "c01", label: "A", align: "left" }], markerLayout: "stagger" }; m.timeline[1].claim = { id: "k1", verdict: "CONTRADICTED", count: 2, at: 33 }; const dir = await project(m); await moveEntry(dir, "c03", 0, { token: await manifestToken(dir) }); assert.deepEqual((await read(dir)).timeline.map((e) => e.id), ["c03", "c01", "c02"]); }); test("updateVariants: sets the ladder, null restores the default, a bad ladder is refused", async () => { const dir = await project(); await updateVariants(dir, [{ name: "master" }, { name: "4k", height: 2160, crf: 18 }, { name: "fit8", maxMiB: 8 }]); assert.equal((await read(dir)).render.variants.length, 3); await assert.rejects(updateVariants(dir, [{ name: "x", maxMiB: 8, crf: 20 }]), /takes no crf/); await assert.rejects(updateVariants(dir, [{ name: "a", height: 720 }, { name: "a", height: 480 }]), /used twice/); await assert.rejects(updateVariants(dir, [{ name: "A b" }]), /name must be/); await assert.rejects(updateVariants(dir, [{ name: "master", crf: 20 }]), /master/); await updateVariants(dir, null); assert.equal("variants" in (await read(dir)).render, false); }); test("knobs: normalizeClaimKnobPatch and the problems lists", () => { assert.deepEqual(knobs.normalizeClaimKnobPatch({ at: "12.345", stamp: "false", count: "2" }), { at: 12.35, stamp: false, count: 2 }); assert.deepEqual(knobs.normalizeClaimKnobPatch({ stamp: true, count: 1 }), { stamp: null, count: null }); assert.throws(() => knobs.normalizeClaimKnobPatch({}), /nothing to change/); assert.deepEqual(knobs.variantsProblems(knobs.VARIANTS_DEFAULT), []); assert.equal(knobs.stampingEntryId({ timeline: [{ id: "a", claim: { id: "k" } }, { id: "b", claim: { id: "k" } }, { id: "c", variant: "full", claim: { id: "k" } }] }, "k", "sourced"), "b"); }); test("variantsState: the build, the variants made, the sync report with the lags past a frame flagged", async () => { const dir = await project(); const out = path.join(dir, "out"); await mkdir(path.join(out, "sourced"), { recursive: true }); await writeFile(path.join(out, "t.mp4"), "x".repeat(100)); await writeFile(path.join(out, "t.720p.mp4"), "x".repeat(40)); await writeFile(path.join(out, "variants.json"), JSON.stringify({ at: new Date(Date.now() + 5000).toISOString(), source: "t.mp4", items: [{ name: "720p", path: "t.720p.mp4", width: 1280, height: 720, bytes: 40, kbps: 900 }, { name: "../evil", path: "x" }] })); await writeFile(path.join(out, "sourced", "sync.json"), JSON.stringify({ fps: 30, items: [{ id: "c01", start: 0, lagMs: 0, peak: 0.9 }, { id: "c02", start: 9.5, lagMs: -469, peak: 0.8 }] })); const s = await variantsState({ id: "p", dir }, await read(dir)); assert.equal(s.built.size, 100); assert.equal(s.ladderIsDefault, true); assert.deepEqual(s.made.items.map((i) => [i.name, i.exists, i.bytes]), [["720p", true, 40]]); assert.equal(s.stale, false); assert.deepEqual(s.sync.items.map((i) => [i.id, i.flagged, i.clip]), [["c01", false, true], ["c02", true, true]]); assert.equal((await variantFile({ dir }, { slug: "t" }, "720p"))?.size, 40); assert.equal(await variantFile({ dir }, { slug: "t" }, "../t"), null); await assert.rejects(variantsState({ id: "p", dir }, await read(dir), "../x"), /not a take id/); }); test("variantsState: the `master` item is the build itself (.mp4), never a .master.mp4", async () => { const dir = await project(); const out = path.join(dir, "out"); await mkdir(out, { recursive: true }); await writeFile(path.join(out, "t.mp4"), "m".repeat(120)); await writeFile(path.join(out, "t.720p.mp4"), "x".repeat(40)); await writeFile(path.join(out, "variants.json"), JSON.stringify({ at: new Date(Date.now() + 5000).toISOString(), source: path.join(out, "t.mp4"), items: [ { name: "master", path: path.join(out, "t.mp4"), width: 1920, height: 1080, bytes: 120, kbps: 1919 }, { name: "720p", path: path.join(out, "t.720p.mp4"), width: 1280, height: 720, bytes: 40, kbps: 899 }, ], })); let s = await variantsState({ id: "p", dir }, await read(dir)); assert.deepEqual(s.made.items.map((i) => [i.name, i.exists, i.bytes]), [["master", true, 120], ["720p", true, 40]]); // The build gone: the master is gone with it. await rm(path.join(out, "t.mp4")); s = await variantsState({ id: "p", dir }, await read(dir)); assert.deepEqual(s.made.items.map((i) => [i.name, i.exists]), [["master", false], ["720p", true]]); }); test("variantsState on a take: its out/.mp4, else its finished preview.mp4 (the encoder pointed at it)", async () => { const dir = await project(); const takeDir = path.join(dir, "takes", "final"); await mkdir(path.join(takeDir, "out"), { recursive: true }); // A work dir with a preview but no take.json is not a build. await writeFile(path.join(takeDir, "preview.mp4"), "p".repeat(70)); assert.equal((await variantsState({ id: "p", dir }, await read(dir), "final")).built, null); await writeFile(path.join(takeDir, "take.json"), JSON.stringify({ id: "final", group: "takes", order: 1, label: "Final", kind: "different", preview: "preview.mp4" })); let s = await variantsState({ id: "p", dir }, await read(dir), "final"); assert.deepEqual([s.built?.size, s.built?.from], [70, "preview"]); assert.equal((await buildFileOf({ dir }, "t", "final"))?.abs, path.join(takeDir, "preview.mp4")); assert.deepEqual(s.takes, ["final"]); // The build's own name wins when it is there. await writeFile(path.join(takeDir, "out", "t.mp4"), "b".repeat(90)); s = await variantsState({ id: "p", dir }, await read(dir), "final"); assert.deepEqual([s.built?.size, s.built?.from], [90, "build"]); });