// The take maker: the merge patch, the words for what a take changes, the // window, a take made with a stub renderer, recipes, and the pick with its // base-hash guard. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { lstat, mkdir, mkdtemp, readFile, readlink, readdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { TakeStale, allStylesRecipe, changeLines, loadMontageStyles, makeTake, makeTakes, mergePatch, parseRecipe, patchFromSets, pickTake, renderManifestPath, lastGroup, takeIsStale, takeSteps, uniqueTakeId, windowBasisLine, windowEntries, windowManifest, withPatchEntries, } from "./takeMaker.mjs"; import { listTakes, readVerdicts, setTakeVerdict } from "./takes.mjs"; import { validateDeckClaims, validateDeckMarkers } from "umtool-report-to-video/deck"; import { validateFxEntries } from "umtool-report-to-video/fx"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; // The kind takes are made of, from the registry (never its id spelled here). const TAKES_KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; import { takeCommand, takesCommand } from "./takeCli.mjs"; const clip = (id, start, end, extra = {}) => ({ type: "clip", id, video: `v-${id}`, start, end, cite: start, section: 0, ...extra }); const MANIFEST = { schemaVersion: 1, slug: "fixture", title: "Fixture", render: { width: 640, height: 360, fps: 15, transition: 0, crf: 30 }, timeline: [clip("s01", 0, 10), clip("s02", 20, 25), clip("s03", 40, 50), { type: "card", id: "k1", seconds: 3 }, clip("s04", 0, 5)], }; const STYLES = { flash: { label: "Flash count", summary: "Flash cuts and a big counter.", patch: { render: { montage: { cut: "flash", counter: { style: "big" } } } } }, straight: { label: "Straight", summary: "Tight hard cuts.", patch: { render: { montage: { cut: "hard" } } } }, }; async function project(manifest = MANIFEST) { const root = await mkdtemp(path.join(tmpdir(), "takemaker-")); const dir = path.join(root, "reports", "proj"); await mkdir(path.join(dir, "out", "clips-raw"), { recursive: true }); await writeFile(path.join(dir, "out", "clips-raw", "v-s01_0.00-13.00.mp4"), "media"); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); return { root, dir, reportsRoot: path.join(root, "reports") }; } /** A renderer that writes a file where build-video would, and remembers what it was given. */ function stubRender({ fail = null } = {}) { const calls = []; const render = async (job) => { const doc = JSON.parse(await readFile(job.manifestPath, "utf8")); calls.push({ ...job, doc, linked: await readdir(job.outDir) }); if (fail) throw new Error(fail); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, `render of ${doc.timeline.map((e) => e.id).join(",")}`); return { file }; }; return { render, calls, logs: [] }; } const opts = (r, extra = {}) => ({ render: r.render, styles: STYLES, probe: async () => 12.345, now: () => new Date("2026-10-17T12:00:00Z"), log: (l) => r.logs?.push(l), ...extra }); test("mergePatch is RFC 7386: objects merge, null removes, arrays and scalars replace, nothing is mutated", () => { const target = { a: 1, b: { c: 2, d: [1, 2] }, e: "x" }; const patch = { a: null, b: { c: 3, d: [9] }, f: { g: true } }; const before = JSON.stringify(target); assert.deepEqual(mergePatch(target, patch), { b: { c: 3, d: [9] }, e: "x", f: { g: true } }); assert.equal(JSON.stringify(target), before); assert.deepEqual(mergePatch({ a: { b: 1 } }, { a: "flat" }), { a: "flat" }); assert.deepEqual(mergePatch("scalar", { a: 1 }), { a: 1 }); assert.deepEqual(mergePatch({ a: 1 }, [1, 2]), [1, 2]); assert.deepEqual(mergePatch({ a: 1 }, {}), { a: 1 }); }); test("changeLines: one plain line per changed leaf, with the old value when there was one", () => { const base = { render: { crf: 30, montage: { cut: "hard", ramp: { from: 3, to: 1 } } } }; const p1 = { render: { crf: 34, montage: { cut: "flash", counter: { style: "big" } } } }; const p2 = { render: { montage: { ramp: null, cut: "hard" } } }; const after = mergePatch(mergePatch(base, p1), p2); assert.deepEqual(changeLines(base, after, [p1, p2]), [ "render.crf: 34 (was 30)", "render.montage.counter.style: big", "render.montage.ramp: removed", ]); // A value set to what it already was is not a change. assert.deepEqual(changeLines(base, mergePatch(base, { render: { crf: 30 } }), [{ render: { crf: 30 } }]), []); // An array is a value: replaced whole, shown short. const pa = { render: { montage: { stutter: { clips: ["s01", "s02"] } } } }; assert.deepEqual(changeLines(base, mergePatch(base, pa), [pa]), ['render.montage.stutter.clips: ["s01","s02"]']); }); test("patchFromSets: dotted paths, JSON values when they parse, strings otherwise", () => { assert.deepEqual(patchFromSets(["render.montage.cut=flash", "render.crf=34", "render.montage.counter={\"style\":\"big\"}", "render.x=null"]), { render: { montage: { cut: "flash", counter: { style: "big" } }, crf: 34, x: null }, }); assert.throws(() => patchFromSets(["nothing"]), /path=value/); assert.throws(() => patchFromSets(["a..b=1"]), /dotted path/); }); test("windowEntries: by the manifest's own lengths, or by the deck's schedule when it names every entry", () => { // s01 0–10, s02 10–15, s03 15–25, k1 25–28, s04 28–33 assert.deepEqual(windowEntries(MANIFEST.timeline, { at: 12, dur: 5 }), { ids: ["s02", "s03"], basis: "estimate", total: 33, missing: ["s01", "s02", "s03", "k1", "s04"] }); assert.deepEqual(windowEntries(MANIFEST.timeline, { at: 0, dur: 60 }).ids, ["s01", "s02", "s03", "k1", "s04"]); // Past the end: the last entry, never nothing. assert.deepEqual(windowEntries(MANIFEST.timeline, { at: 500, dur: 10 }).ids, ["s04"]); // A clip's cut, not its extent, is what plays. assert.deepEqual(windowEntries([clip("a", 0, 100, { cutStart: 10, cutEnd: 12 }), clip("b", 0, 3)], { at: 2.5, dur: 1 }).ids, ["b"]); // A clip's gaps never play: a window aimed at the clip after a gapped one gets that clip (lane gap-cue: it got the // gapped one, the window that many seconds early). a is 40 s with 27 s of gaps: it plays 13 s; b 13–16. const gapped = [clip("a", 0, 40, { gaps: [{ from: 5, to: 30 }, { from: 33, to: 35 }] }), clip("b", 0, 3)]; assert.deepEqual(windowEntries(gapped, { at: 13.5, dur: 1 }).ids, ["b"]); assert.equal(windowEntries(gapped, { at: 0, dur: 1 }).total, 16); // Under render.montage a clip cuts to its phrase and applies no gaps (review 1, 10): its whole cut, 40 + 3. assert.equal(windowEntries(gapped, { at: 0, dur: 1 }, null, { montage: { style: "wall" } }).total, 43); assert.deepEqual(windowEntries(gapped, { at: 13.5, dur: 1 }, null, { montage: { style: "wall" } }).ids, ["a"]); const schedule = { segments: ["s01", "s02", "s03", "k1", "s04"].map((id, i) => ({ id, start: i * 2, duration: 2 })) }; assert.deepEqual(windowEntries(MANIFEST.timeline, { at: 3, dur: 2 }, schedule), { ids: ["s02", "s03"], basis: "schedule", total: 10, missing: [] }); // A schedule that misses an entry is not trusted -- one built before a clip // was inserted -- and says which it missed. const old = windowEntries(MANIFEST.timeline, { at: 3, dur: 2 }, { segments: schedule.segments.slice(1) }); assert.deepEqual([old.basis, old.missing], ["estimate", ["s01"]]); assert.match(windowBasisLine(old, { schedule: true, scheduleOlder: true }), /estimates.*schedule\.json is older than the manifest and does not time s01 — rebuild/); assert.match(windowBasisLine(windowEntries(MANIFEST.timeline, { at: 3, dur: 2 }, schedule), { schedule: true, scheduleOlder: false }), /covers 2 entries \(s02, s03\), timed by out\/sourced\/schedule\.json$/); assert.match(windowBasisLine(windowEntries(MANIFEST.timeline, { at: 3, dur: 2 }), { schedule: false, scheduleOlder: false }), /there is no out\/sourced\/schedule\.json/); // Only the default cut's entries. assert.deepEqual(windowEntries([clip("a", 0, 5), clip("f", 0, 5, { variant: "full" }), clip("b", 0, 5)], { at: 6, dur: 1 }).ids, ["b"]); }); test("makeTake: the style's patch then the take's, rendered from beside the project's manifest, take.json last", async () => { const { root, dir } = await project(); const r = stubRender(); try { const done = await makeTake(dir, { id: "flash", label: "Flash", group: "montage", style: "flash", patch: { render: { crf: 34 } }, window: { at: 12, dur: 5 } }, opts(r)); assert.equal(done.skipped, false); const takeDir = path.join(dir, "takes", "flash"); // The take's manifest is the WHOLE patched manifest -- what a pick copies. const m = JSON.parse(await readFile(path.join(takeDir, "video.manifest.json"), "utf8")); assert.equal(m.timeline.length, 5); assert.deepEqual(m.render.montage, { cut: "flash", counter: { style: "big" } }); assert.equal(m.render.crf, 34); // The render saw only the window's entries, from a manifest in the PROJECT's directory. assert.equal(r.calls.length, 1); assert.equal(path.dirname(r.calls[0].manifestPath), dir); assert.equal(r.calls[0].manifestPath, renderManifestPath(dir, "flash")); assert.deepEqual(r.calls[0].doc.timeline.map((e) => e.id), ["s02", "s03"]); assert.equal(r.calls[0].full, false); assert.equal(r.calls[0].xfade, false, "hard cuts unless the take sets a transition"); assert.equal(r.calls[0].outDir, path.join(takeDir, "out")); assert.deepEqual(r.calls[0].linked, ["clips-raw"]); // ...and it is gone once the render is done. await assert.rejects(lstat(renderManifestPath(dir, "flash"))); // The fetched media is LINKED from the project, never copied. const link = path.join(takeDir, "out", "clips-raw"); assert.ok((await lstat(link)).isSymbolicLink()); assert.equal(await readlink(link), path.join(dir, "out", "clips-raw")); // The preview was moved into the take; take.json says how it was made. assert.match(await readFile(path.join(takeDir, "preview.mp4"), "utf8"), /render of s02,s03/); // ...and left where the build put it, as a hard link: the take's out/ is a built out/ (variants, Deliver). const [pv, built] = await Promise.all([lstat(path.join(takeDir, "preview.mp4")), lstat(path.join(takeDir, "out", "fixture.mp4"))]); assert.equal(built.ino, pv.ino); // The log says what timed the window. assert.ok(r.logs.some((l) => /^flash: the window covers 2 entries \(s02, s03\), timed by the manifest's own lengths \(estimates\): there is no out\/sourced\/schedule\.json/.test(l)), r.logs.join("\n")); const t = JSON.parse(await readFile(path.join(takeDir, "take.json"), "utf8")); assert.equal(t.kind, "different"); assert.equal(t.group, "montage"); assert.equal(t.order, 1); assert.equal(t.summary, "Flash cuts and a big counter."); assert.deepEqual(t.changes, ["style: Flash count", "render.montage.cut: flash", "render.montage.counter.style: big", "render.crf: 34 (was 30)"]); assert.equal(t.seconds, 12.35); assert.equal(t.builtAt, "2026-10-17T12:00:00.000Z"); assert.equal(t.style, "flash"); assert.deepEqual(t.window, { at: 12, dur: 5 }); assert.deepEqual(t.entries, { ids: ["s02", "s03"], basis: "estimate" }); assert.match(t.baseHash, /^[0-9a-f]{64}$/); assert.equal(await takeIsStale(dir, t), false); assert.equal(t.quick, undefined); assert.equal(r.calls[0].doc.render.preset, undefined); // The listing reads it back. const listing = await listTakes(dir); assert.deepEqual(listing.takes.map((x) => [x.id, x.style, x.baseHash === t.baseHash]), [["flash", "flash", true]]); } finally { await rm(root, { recursive: true, force: true }); } }); test("makeTake: the clip deck's rulings leave their clips out of the render, numbered over the whole cut; the take's own manifest is as written", async () => { // Five montage clips; s02 ruled not-him, s04 cut. A window over s03–s05. const m = (id, start, n) => clip(id, start, start + 4, { channel: "chan", cutStart: start + 1, cutEnd: start + 3, montage: { n, of: 5, match: "slow news" } }); const manifest = { ...MANIFEST, timeline: [m("s01", 0, 1), m("s02", 10, 2), m("s03", 20, 3), m("s04", 30, 4), m("s05", 40, 5)] }; const { root, dir } = await project(manifest); const r = stubRender(); const logs = []; try { await writeFile(path.join(dir, "montage-rulings.json"), JSON.stringify({ version: 1, rulings: { "chan/v-s02@11.00": { verdict: "not-him", reason: "guest", at: "2026-10-18T00:00:00Z" }, "chan/v-s04@31.00": { verdict: "cut", reason: "", at: "2026-10-18T00:00:00Z" }, "chan/v-s05@41.00": { verdict: "keep", reason: "", match: "a slow news week", at: "2026-10-18T00:00:00Z" }, } })); await makeTake(dir, { id: "win", label: "Window", window: { at: 4.5, dur: 5 } }, opts(r, { log: (l) => logs.push(l) })); assert.deepEqual(r.calls[0].doc.timeline.map((e) => [e.id, e.montage.n, e.montage.of]), [["s03", 2, 3], ["s05", 3, 3]]); assert.equal(r.calls[0].doc.timeline[1].montage.match, "a slow news week"); assert.ok(logs.some((l) => /win: montage rulings: 2 clip\(s\) left out — s02 \(not him: guest\), s04 \(cut\)/.test(l)), logs.join("\n")); const take = JSON.parse(await readFile(path.join(dir, "takes", "win", "video.manifest.json"), "utf8")); assert.equal(take.timeline.length, 5, "a pick copies the manifest as written; the rulings stay beside it"); await makeTake(dir, { id: "whole", label: "Whole", window: "full" }, opts(r)); assert.deepEqual(r.calls[1].doc.timeline.map((e) => e.id), ["s01", "s03", "s05"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("makeTake: no change is the reference; a transition turns crossfades on; full renders the whole cut", async () => { const { root, dir } = await project(); const r = stubRender(); try { await makeTake(dir, { id: "as-is", label: "As built", window: "full" }, opts(r)); const ref = JSON.parse(await readFile(path.join(dir, "takes", "as-is", "take.json"), "utf8")); assert.equal(ref.kind, "reference"); assert.deepEqual(ref.changes, []); assert.equal(ref.window, "full"); assert.equal(ref.entries, undefined); assert.equal(r.calls[0].full, true); assert.equal(r.calls[0].doc.timeline.length, 5); await makeTake(dir, { id: "soft", label: "Soft", kind: "similar", patch: { render: { transition: 0.3 } } }, opts(r)); assert.equal(r.calls[1].xfade, true); const soft = JSON.parse(await readFile(path.join(dir, "takes", "soft", "take.json"), "utf8")); assert.equal(soft.kind, "similar"); assert.equal(soft.order, 2); assert.deepEqual(soft.window, { at: 0, dur: 60 }, "the default window"); // Quick: the RENDER is encoded veryfast; the take's manifest -- what a pick uses -- is not touched. await makeTake(dir, { id: "quick", label: "Quick", patch: { render: { crf: 33 } }, quick: true }, opts(r)); assert.equal(r.calls[2].doc.render.preset, "veryfast"); assert.equal(r.calls[2].doc.render.crf, 33); const qm = JSON.parse(await readFile(path.join(dir, "takes", "quick", "video.manifest.json"), "utf8")); assert.equal(qm.render.preset, undefined); const qt = JSON.parse(await readFile(path.join(dir, "takes", "quick", "take.json"), "utf8")); assert.equal(qt.quick, true); assert.deepEqual(qt.changes, ["render.crf: 33 (was 30)"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("makeTake: an existing take is skipped unless forced; a failed rebuild leaves the old take whole", async () => { const { root, dir } = await project(); const r = stubRender(); try { await makeTake(dir, { id: "t", label: "T", patch: { render: { crf: 31 } } }, opts(r)); const takeDir = path.join(dir, "takes", "t"); const before = await Promise.all(["take.json", "video.manifest.json", "preview.mp4"].map((f) => readFile(path.join(takeDir, f), "utf8"))); assert.deepEqual(await makeTake(dir, { id: "t", label: "T", patch: { render: { crf: 99 } } }, opts(r)), { id: "t", skipped: true }); assert.equal(r.calls.length, 1); const bad = stubRender({ fail: "2 entries could not be cut: s02, s03" }); await assert.rejects(makeTake(dir, { id: "t", label: "T", patch: { render: { crf: 99 } } }, opts(bad, { force: true })), /could not be cut/); const after = await Promise.all(["take.json", "video.manifest.json", "preview.mp4"].map((f) => readFile(path.join(takeDir, f), "utf8"))); assert.deepEqual(after, before); // A rebuild whose build writes the cut IN PLACE and then fails: the old // preview's hard link was unlinked first, so the preview is untouched. const inPlace = { render: async (job) => { await writeFile(path.join(job.outDir, `${job.slug}.mp4`), "half a render"); throw new Error("build-video exited 1"); }, logs: [] }; await assert.rejects(makeTake(dir, { id: "t", label: "T", patch: { render: { crf: 99 } } }, opts(inPlace, { force: true })), /exited 1/); assert.equal(await readFile(path.join(takeDir, "preview.mp4"), "utf8"), before[2]); await assert.rejects(lstat(renderManifestPath(dir, "t")), "the render manifest is removed on failure too"); // A first make that fails writes no take.json: the listing shows nothing. await assert.rejects(makeTake(dir, { id: "never", label: "Never" }, opts(bad))); assert.deepEqual((await listTakes(dir)).takes.map((x) => x.id), ["t"]); } finally { await rm(root, { recursive: true, force: true }); } }); test("makeTake refuses an unknown style, a bad id, and a window the patch emptied", async () => { const { root, dir } = await project(); const r = stubRender(); try { await assert.rejects(makeTake(dir, { id: "x", label: "X", style: "nope" }, opts(r)), /unknown style "nope" — one of flash, straight/); await assert.rejects(makeTake(dir, { id: "x", label: "X", style: "nope" }, opts(r, { styles: {} })), /no montage styles are installed/); await assert.rejects(makeTake(dir, { id: "Bad", label: "X" }, opts(r)), /id must be/); await assert.rejects(makeTake(dir, { id: "x", label: "" }, opts(r)), /needs a label/); assert.equal(r.calls.length, 0); // A patch whose timeline holds none of the entries the window covers is // timed over its OWN timeline (walk a take: a tight version's sub-windows), // rather than refused as emptied. const own = await makeTake(dir, { id: "x", label: "X", window: { at: 0, dur: 5 }, patch: { timeline: [clip("s04", 0, 5), clip("s05", 7, 9)] } }, opts(r)); assert.equal(own.skipped, false); assert.deepEqual(r.calls[0].doc.timeline.map((e) => e.id), ["s04"]); assert.ok(r.logs.some((l) => /none of those is in the take's own timeline — the window covers 1 of ITS entries \(s04\)/.test(l)), r.logs.join("\n")); } finally { await rm(root, { recursive: true, force: true }); } }); test("recipes: checked, made one at a time, existing takes skipped unless forced, a failure does not stop the rest", async () => { assert.throws(() => parseRecipe({ takes: [] }), /needs takes/); assert.throws(() => parseRecipe({ takes: [{ id: "a", label: "A" }, { id: "a", label: "B" }] }), /twice/); const parsed = parseRecipe({ group: "g", window: "full", takes: [{ id: "a", label: "A" }, { id: "b", label: "B", group: "own", window: { at: 1, dur: 2 } }] }); assert.deepEqual(parsed.takes.map((t) => [t.id, t.group, t.window]), [["a", "g", "full"], ["b", "own", { at: 1, dur: 2 }]]); assert.deepEqual(parseRecipe({ quick: true, takes: [{ id: "a", label: "A" }, { id: "b", label: "B", quick: false }] }).takes.map((t) => t.quick), [true, false]); const { root, dir } = await project(); const r = stubRender(); try { const recipe = { group: "montage", window: { at: 0, dur: 12 }, takes: [{ id: "flash", label: "Flash", style: "flash" }, { id: "nope", label: "Nope", style: "nope" }, { id: "straight", label: "Straight", style: "straight" }] }; const events = []; const first = await makeTakes(dir, recipe, opts(r, { emit: (e) => events.push(e) })); assert.deepEqual(first.map((x) => x.status), ["made", "failed", "made"]); assert.match(first[1].error, /unknown style/); assert.deepEqual(events.filter((e) => e.phase === "start").map((e) => `${e.id} ${e.i}/${e.n}`), ["flash 1/3", "nope 2/3", "straight 3/3"]); const again = await makeTakes(dir, recipe, opts(r)); assert.deepEqual(again.map((x) => x.status), ["skipped", "failed", "skipped"]); assert.equal(r.calls.length, 2); const forced = await makeTakes(dir, recipe, opts(r, { force: true, only: ["straight"] })); assert.deepEqual(forced, [{ id: "straight", status: "made" }]); await assert.rejects(makeTakes(dir, recipe, opts(r, { only: ["ghost"] })), /not in the recipe: ghost/); } finally { await rm(root, { recursive: true, force: true }); } }); test("allStylesRecipe: one take per style, ids made from the names", () => { assert.deepEqual(allStylesRecipe(STYLES, { group: "m", window: "full" }), { group: "m", window: "full", takes: [ { id: "flash", label: "Flash count", style: "flash" }, { id: "straight", label: "Straight", style: "straight" }, ], }); assert.throws(() => allStylesRecipe({}), /no montage styles/); assert.equal(uniqueTakeId("Wall Finale!"), "wall-finale"); assert.equal(uniqueTakeId("wall", new Set(["wall", "wall-2"])), "wall-3"); assert.equal(uniqueTakeId("§§"), "take"); }); test("pickTake: the take's manifest over the project's, an undo point first, one pick per group", async () => { const { root, dir } = await project(); const r = stubRender(); const proj = { id: "proj", dir, kind: TAKES_KIND }; try { await makeTake(dir, { id: "flash", label: "Flash", group: "m", style: "flash" }, opts(r)); await makeTake(dir, { id: "straight", label: "Straight", group: "m", style: "straight" }, opts(r)); await setTakeVerdict(dir, "flash", { verdict: "like" }); const got = await pickTake(proj, "flash"); const projectText = await readFile(path.join(dir, "video.manifest.json"), "utf8"); assert.equal(projectText, await readFile(path.join(dir, "takes", "flash", "video.manifest.json"), "utf8")); assert.match(got.snapshot.rel, /^revisions\/.*-auto-before-pick-take\.manifest\.json$/); const snap = JSON.parse(await readFile(path.join(dir, got.snapshot.rel), "utf8")); assert.equal(snap.render.montage, undefined, "the snapshot is the manifest as it was"); let v = await readVerdicts(dir); assert.equal(v.flash.picked, true); assert.equal(v.flash.verdict, "like"); // Picking it again is no write and no refusal: it already is the project's manifest. const again = await pickTake(proj, "flash"); assert.equal(again.snapshot, null); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), projectText); // The other take was made from the manifest the pick replaced: refused, then used anyway. await assert.rejects(pickTake(proj, "straight"), (e) => e instanceof TakeStale && /changed since this take was made/.test(e.message)); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), projectText, "a refused pick writes nothing"); await pickTake(proj, "straight", { anyway: true }); v = await readVerdicts(dir); assert.equal(v.straight.picked, true); assert.equal(v.flash.picked, undefined, "one pick per group"); assert.equal(v.flash.verdict, "like"); const m = JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); assert.deepEqual(m.render.montage, { cut: "hard" }); } finally { await rm(root, { recursive: true, force: true }); } }); test("pickTake: a take that does not record its base needs anyway; an unknown take is an error", async () => { const { root, dir } = await project(); const proj = { id: "proj", dir, kind: TAKES_KIND }; try { const takeDir = path.join(dir, "takes", "by-hand"); await mkdir(takeDir, { recursive: true }); await writeFile(path.join(takeDir, "take.json"), JSON.stringify({ id: "by-hand", group: "g", order: 1, label: "By hand", kind: "different", preview: "preview.mp4" })); await writeFile(path.join(takeDir, "video.manifest.json"), JSON.stringify({ ...MANIFEST, title: "By hand" }, null, 2) + "\n"); await assert.rejects(pickTake(proj, "by-hand"), /does not record which manifest/); await pickTake(proj, "by-hand", { anyway: true }); assert.equal(JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")).title, "By hand"); await assert.rejects(pickTake(proj, "ghost"), /no take ghost/); } finally { await rm(root, { recursive: true, force: true }); } }); test("loadMontageStyles: a module of the shape, a missing module is none, a wrong entry is named", async () => { const root = await mkdtemp(path.join(tmpdir(), "styles-")); try { const file = path.join(root, "montage-styles.mjs"); await writeFile( file, `export const MONTAGE_STYLES = { a: { label: "A", summary: "s", patch: { render: { montage: { cut: "flash" } } } }, broken: { label: 3 } };\n`, ); const got = await loadMontageStyles({ file }); assert.deepEqual(Object.keys(got.styles), ["a"]); assert.deepEqual(got.skipped, ["broken"]); assert.equal(got.error, null); const none = await loadMontageStyles({ file: path.join(root, "montage-styles-absent.mjs") }); assert.deepEqual(none, { styles: {}, error: null, skipped: [] }); } finally { await rm(root, { recursive: true, force: true }); } }); test("the CLI: umtool take with --set, umtool takes make with a recipe, umtool takes pick", async () => { const { root, dir, reportsRoot } = await project(); const r = stubRender(); const out = []; const err = []; const io = { reportsRoot, log: (s) => out.push(s), err: (s) => err.push(s), deps: { render: r.render, styles: STYLES, probe: async () => 1 } }; try { assert.equal(await takeCommand(["proj", "--id", "crisp", "--label", "Crisp", "--set", "render.crf=33", "--at", "0", "--dur", "11"], io), 0, err.join("\n")); const t = JSON.parse(await readFile(path.join(dir, "takes", "crisp", "take.json"), "utf8")); assert.deepEqual(t.changes, ["render.crf: 33 (was 30)"]); assert.deepEqual(t.window, { at: 0, dur: 11 }); assert.deepEqual(r.calls[0].doc.timeline.map((e) => e.id), ["s01", "s02"]); // NDJSON progress: events on stdout, nothing else. out.length = 0; assert.equal(await takeCommand(["proj", "--id", "crisp", "--rebuild", "--progress", "ndjson"], io), 0, err.join("\n")); assert.ok(out.length > 0 && out.every((l) => JSON.parse(l))); assert.deepEqual(r.calls[1].doc.render.crf, 33, "a rebuild reapplies the take's own patch"); // Usage errors exit 2. assert.equal(await takeCommand(["proj", "--id", "x", "--label", "X", "--full", "--at", "3"], io), 2); assert.equal(await takeCommand(["proj", "--id", "x", "--label", "X", "--set", "a=1", "--patch-json", "{}"], io), 2); assert.equal(await takeCommand(["nowhere", "--id", "x", "--label", "X"], io), 2); await writeFile(path.join(dir, "takes", "recipe.json"), JSON.stringify({ group: "m", window: "full", takes: [{ id: "flash", label: "Flash", style: "flash" }] })); assert.equal(await takesCommand(["make", "proj"], io), 0, err.join("\n")); assert.equal((await listTakes(dir)).takes.length, 2); assert.equal(await takesCommand(["make", "proj", "--all-styles", "--full"], io), 0, err.join("\n")); const all = JSON.parse(await readFile(path.join(dir, "takes", "all-styles.recipe.json"), "utf8")); assert.deepEqual(all.takes.map((x) => x.id), ["flash", "straight"]); assert.deepEqual((await listTakes(dir)).takes.map((x) => x.id).sort(), ["crisp", "flash", "straight"]); assert.equal(await takesCommand(["pick", "proj", "flash"], io), 0, err.join("\n")); err.length = 0; assert.equal(await takesCommand(["pick", "proj", "crisp"], io), 1); assert.match(err.join("\n"), /changed since this take was made — pass --anyway/); assert.equal(await takesCommand(["pick", "proj", "crisp", "--anyway"], io), 0); } finally { await rm(root, { recursive: true, force: true }); } }); test("the CLI: umtool takes effects lists the cut's effect presets; umtool take --effect applies one before --set", async () => { const { root, dir, reportsRoot } = await project(); const r = stubRender(); const out = []; const err = []; const io = { reportsRoot, log: (s) => out.push(s), err: (s) => err.push(s), deps: { render: r.render, styles: STYLES, probe: async () => 1 } }; try { assert.equal(await takesCommand(["effects", "proj", "--json"], io), 0, err.join("\n")); const { effects } = JSON.parse(out.join("\n")); const names = effects.map((x) => x.name); for (const n of ["kinetic-quote", "redaction", "odometer", "scrubber", "glitch", "everything"]) assert.ok(names.includes(n), n); assert.ok(!names.includes("receipts") && !names.includes("zoom"), "no clip carries a screenshot"); assert.equal(await takeCommand(["proj", "--id", "glitchy", "--label", "Glitch", "--effect", "glitch", "--set", "render.crf=31", "--full"], io), 0, err.join("\n")); const doc = r.calls.at(-1).doc; assert.deepEqual(doc.render.chrome.fx, { glitch: {} }); assert.equal(doc.render.chrome.engine, "hyperframes"); assert.equal(doc.render.crf, 31, "--set merges over the effect's patch"); const t = JSON.parse(await readFile(path.join(dir, "takes", "glitchy", "take.json"), "utf8")); assert.ok(t.changes.some((c) => c.startsWith("render.chrome")), t.changes.join("; ")); err.length = 0; assert.equal(await takeCommand(["proj", "--id", "x", "--label", "X", "--effect", "sparkle"], io), 2); assert.match(err.join("\n"), /unknown effect "sparkle" — this cut offers kinetic-quote/); } finally { await rm(root, { recursive: true, force: true }); } }); test("takeSteps: each button is one job running the CLI, checked before it starts", () => { const proj = { id: "reports/proj" }; const ctx = { styles: STYLES, existing: [{ id: "flash", window: "full" }], clipCount: 75 }; const one = takeSteps(proj, { take: { label: "Crisp cut", group: "m", patch: { render: { crf: 33 } }, window: { at: 5, dur: 20 } } }, ctx); assert.deepEqual(one.takes, ["crisp-cut"]); assert.equal(one.steps.length, 1); const argv = one.steps[0].argv; assert.equal(argv[0], "node"); assert.match(argv[1], /bin\/umtool\.mjs$/); assert.deepEqual(argv.slice(2), [ "take", "reports/proj", "--id", "crisp-cut", "--label", "Crisp cut", "--group", "m", "--patch-json", '{"render":{"crf":33}}', "--at", "5", "--dur", "20", "--progress", "ndjson", ]); assert.equal(one.steps[0].ndjson, true); assert.deepEqual(one.steps[0].env, {}, "nothing in env: jobView shows it to the browser"); assert.throws(() => takeSteps(proj, { take: { label: "Flash", style: "flash", id: "flash" } }, ctx), /exists/); assert.ok(takeSteps(proj, { take: { label: "Flash", style: "flash", id: "flash" }, force: true }, ctx).steps[0].argv.includes("--force")); assert.throws(() => takeSteps(proj, { take: { label: "X", style: "nope" } }, ctx), /unknown style/); assert.throws(() => takeSteps(proj, { take: { label: "" } }, ctx), /label/); assert.ok(takeSteps(proj, { take: { label: "Q", quick: true } }, ctx).steps[0].argv.includes("--quick")); const all = takeSteps(proj, { allStyles: { group: "m", window: "full" } }, ctx); assert.deepEqual(all.takes, ["flash", "straight"]); assert.deepEqual(all.steps[0].argv.slice(2), ["takes", "make", "reports/proj", "--all-styles", "--group", "m", "--full", "--progress", "ndjson"]); assert.equal(all.steps[0].timeoutMs, 2 * 75 * 60_000); const rb = takeSteps(proj, { rebuild: "flash" }, ctx); assert.deepEqual(rb.steps[0].argv.slice(2), ["take", "reports/proj", "--id", "flash", "--rebuild", "--progress", "ndjson"]); assert.throws(() => takeSteps(proj, { rebuild: "ghost" }, ctx), /no take ghost/); assert.throws(() => takeSteps(proj, {}, ctx), /say what to make/); assert.equal(lastGroup([{ group: "a", builtAt: "2026-01-01" }, { group: "b", builtAt: "2026-02-01" }]), "b"); assert.equal(lastGroup([]), "takes"); }); test("windowManifest: markers and claim checks outside the window are dropped; a goalpost straddling its edge is clipped", () => { const manifest = { ...MANIFEST, render: { ...MANIFEST.render, chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [ { after: "s02", label: "inside" }, { after: "s04", label: "outside" }, ], claims: [ { kind: "count", at: "s01", t: 2, said: "“in”", window: { from: "2026-01-01", to: "2026-12-31" }, tally: "CONTRADICTED" }, { kind: "count", at: "s04", t: 1, said: "“out”", window: { from: "2026-01-01", to: "2026-12-31" }, tally: "CONTRADICTED" }, { kind: "goalpost", label: "straddles", steps: [{ at: "s02", t: 21, said: "“3 weeks”", due: ["P3W", "P5W"] }, { at: "s04", t: 2, said: "“6 weeks”", due: ["P6W", "P8W"] }], }, { kind: "goalpost", label: "all outside", steps: [{ at: "s04", t: 2, said: "“6 weeks”", due: ["P6W", "P8W"] }] }, ], }, }, }, }; const before = JSON.stringify(manifest); const m = windowManifest(manifest, ["s01", "s02"]); assert.deepEqual(m.timeline.map((e) => e.id), ["s01", "s02"]); assert.deepEqual(m.render.chrome.deck.markers.map((x) => x.label), ["inside"]); assert.deepEqual(m.render.chrome.deck.claims.map((c) => c.label ?? c.said), ["“in”", "straddles"]); // Clipped to the step inside the window, its seconds as written (the clip renders whole). assert.deepEqual(m.render.chrome.deck.claims[1].steps, [{ at: "s02", t: 21, said: "“3 weeks”", due: ["P3W", "P5W"] }]); assert.equal(JSON.stringify(manifest), before, "the project's manifest is not touched"); // What the build refuses with: the whole manifest windowed as-was names clips it does not hold. const naive = { ...manifest, timeline: m.timeline }; assert.ok(validateDeckMarkers(naive).length + validateDeckClaims(naive).length > 0); assert.deepEqual(validateDeckMarkers(m), []); assert.deepEqual(validateDeckClaims(m), []); // The full window keeps every one. const all = windowManifest(manifest, ["s01", "s02", "s03", "k1", "s04"]); assert.equal(all.render.chrome.deck.markers.length, 2); assert.equal(all.render.chrome.deck.claims.length, 4); assert.equal(all.render.chrome.deck.claims[2].steps.length, 2); }); test("windowManifest: an effect's clip list keeps the window's clips -- a glitch's with a clip before it, a then/now pair with both ends -- and an effect left with none is off", () => { const clipIds = ["k01", "k02", "k03", "k04", "k05", "k06"]; const manifest = { ...MANIFEST, timeline: clipIds.map((id, i) => clip(id, 10 * i, 10 * i + 5)), render: { ...MANIFEST.render, chrome: { engine: "hyperframes", layout: "deck", deck: {}, fx: { glitch: { on: "clips", clips: ["k02", "k03", "k06"] }, thenNow: { pairs: [{ then: "k02", now: "k03" }, { then: "k05", now: "k06" }] }, kineticQuote: { clips: ["k01", "k03"] }, redact: { clips: ["k06"] }, wipe: {}, }, }, }, }; const before = JSON.stringify(manifest); // The whole manifest windowed as-was is what the build refused (rev9-a9: "names k06, which is not a clip of the cut"). const naive = { ...manifest, timeline: manifest.timeline.filter((e) => ["k02", "k03", "k04"].includes(e.id)) }; assert.match(validateFxEntries(naive, { schedule: null }).join("\n"), /render\.chrome\.fx\.glitch\.clips names k06, which is not a clip of the cut/); const m = windowManifest(manifest, ["k02", "k03", "k04"]); const fx = m.render.chrome.fx; // k02 opens the window: no clip before it, no cut to tear at. assert.deepEqual(fx.glitch, { on: "clips", clips: ["k03"] }); assert.deepEqual(fx.thenNow.pairs, [{ then: "k02", now: "k03" }]); assert.deepEqual(fx.kineticQuote.clips, ["k03"]); assert.equal("redact" in fx, false, "it named only a clip outside: off in the window"); assert.deepEqual(fx.wipe, {}, "an effect that names no clip is as it was"); assert.deepEqual(validateFxEntries(m, { schedule: null }), []); assert.equal(JSON.stringify(manifest), before, "the project's manifest is not touched"); // A window with none of them: the effects are off, the rest of the chrome as it was. const none = windowManifest(manifest, ["k04"]); assert.deepEqual(none.render.chrome.fx, { wipe: {} }); assert.deepEqual(validateFxEntries(none, { schedule: null }), []); // The full window keeps every one. assert.deepEqual(windowManifest(manifest, clipIds).render.chrome.fx, manifest.render.chrome.fx); // A glitch in another mode never reads its list (fx.mjs): the window leaves it, and the glitch, as they were. const byYear = { ...manifest, render: { ...manifest.render, chrome: { ...manifest.render.chrome, fx: { glitch: { on: "year", clips: ["k06"] } } } } }; assert.deepEqual(windowManifest(byYear, ["k02", "k03", "k04"]).render.chrome.fx, { glitch: { on: "year", clips: ["k06"] } }); const noMode = { ...manifest, render: { ...manifest.render, chrome: { ...manifest.render.chrome, fx: { glitch: { clips: ["k06"] } } } } }; assert.deepEqual(windowManifest(noMode, ["k02", "k03", "k04"]).render.chrome.fx, { glitch: { clips: ["k06"] } }); // A pair cut by the window's edge is dropped, the other kept. assert.deepEqual(windowManifest(manifest, ["k03", "k04", "k05", "k06"]).render.chrome.fx.thenNow.pairs, [{ then: "k05", now: "k06" }]); }); test("withPatchEntries: a clip only the take's patch adds stays in the window beside the covered clip before it", () => { const base = [{ id: "a" }, { id: "b" }, { id: "c" }, { id: "d" }]; // The patch adds x after b (covered), y after x, z after d (not covered). const take = [{ id: "a" }, { id: "b" }, { id: "x" }, { id: "y" }, { id: "c" }, { id: "d" }, { id: "z" }]; assert.deepEqual(withPatchEntries(take, base, ["b", "c"]), ["b", "x", "y", "c"]); // Nothing added: the window's ids as they were. assert.deepEqual(withPatchEntries(base, base, ["b", "c"]), ["b", "c"]); // An id the take dropped is passed on for the caller's present-check. assert.deepEqual(withPatchEntries([{ id: "a" }, { id: "c" }], base, ["b", "c"]), ["c", "b"]); }); test("makeTake refuses a cut that still holds a clip ruled incorrect, by name and correction; one the take leaves out renders", async () => { const wrong = { ...MANIFEST, timeline: [clip("s01", 0, 10), clip("e01", 20, 25, { verdict: "incorrect", correction: "Other speaker, not Ember." }), clip("s03", 40, 50)] }; const { root, dir } = await project(wrong); const r = stubRender(); try { await assert.rejects( makeTake(dir, { id: "full", label: "Full", window: "full" }, opts(r)), (e) => e.message === "full: e01 is marked incorrect: Other speaker, not Ember — remove it, fix it and re-confirm, or clear the verdict", ); assert.equal(r.calls.length, 0); // Taken out by the take's own timeline: nothing incorrect renders. const ok = await makeTake(dir, { id: "without", label: "Without", window: "full", patch: { timeline: [clip("s01", 0, 10), clip("s03", 40, 50)] } }, opts(r)); assert.equal(ok.skipped, false); } finally { await rm(root, { recursive: true, force: true }); } }); test("makeTake records renderSeconds: from the build's first event (after any wait at the heavy slot) to its end (UI notes #18)", async () => { const { root, dir } = await project(); let t = 0; const r = stubRender(); const render = async (job) => { t = 30_000; // 30 s waiting at the slot's door: not counted job.emit({ ev: "phase", phase: "clips" }); t = 30_000 + 95_400; return r.render(job); }; try { await makeTake(dir, { id: "timed", label: "Timed", group: "g", window: "full" }, opts(r, { render, clock: () => t })); const doc = JSON.parse(await readFile(path.join(dir, "takes", "timed", "take.json"), "utf8")); assert.equal(doc.renderSeconds, 95.4); } finally { await rm(root, { recursive: true, force: true }); } });