// A clip's `fx` through the one writer (updateClip / patchClipEntry): whole, // per field, cleared; refused in validateFxEntries's words; a screenshot is a // file in the project (or under its shots/); a take's walk writes it too; and // `umtool set fx.*` is the same writer from a terminal. // // 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 { fxSchedule, validateFxEntries, FX_MOTION } from "umtool-report-to-video/fx"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-clip-fx-")); process.env.REPORTS_DIR = ROOT; const { MANIFEST_NAME, manifestToken, patchClipEntry, updateClip } = await import("./manifest.mjs"); const { setCommand } = await import("./knobsCli.mjs"); const base = () => ({ slug: "t", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5 }, timeline: [ { type: "card", id: "k1", heading: "Opening" }, { type: "clip", id: "c01", video: "v1", start: 10, end: 20 }, { type: "clip", id: "c02", video: "v2", start: 30, end: 41, fx: { punch: "kept" } }, ], }); async function project(name, manifest = base()) { const dir = path.join(ROOT, name); await mkdir(path.join(dir, "shots"), { recursive: true }); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); await writeFile(path.join(dir, "shots", "doc.png"), "png"); await writeFile(path.join(dir, "top.png"), "png"); 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("updateClip fx.zoom: written, rounded, its shot checked in the project; a bare name found under shots/", async () => { const dir = await project("zoom"); const res = await updateClip(dir, "c01", { "fx.zoom": { shot: "shots/doc.png", focus: [0.1, 0.20001, 0.9, 0.4], at: 5.904, hold: 4 } }, { token: await manifestToken(dir) }); assert.deepEqual(res.entry.fx, { zoom: { shot: "shots/doc.png", focus: [0.1, 0.2, 0.9, 0.4], at: 5.9, hold: 4 } }); assert.deepEqual(clip(await read(dir), "c01").fx, res.entry.fx); // A bare name only under shots/ is written as that path; one beside the manifest as itself. await updateClip(dir, "c01", { "fx.zoom": { shot: "doc.png", focus: [0, 0, 1, 1] } }); assert.equal(clip(await read(dir), "c01").fx.zoom.shot, "shots/doc.png"); await updateClip(dir, "c01", { "fx.zoom": { shot: "top.png", focus: [0, 0, 1, 1] } }); assert.equal(clip(await read(dir), "c01").fx.zoom.shot, "top.png"); // The manifest still validates as the build reads it. assert.deepEqual(validateFxEntries(await read(dir)), []); }); test("updateClip fx: a missing or escaping shot, or a shape the build refuses, is refused in its words and nothing is written", async () => { const dir = await project("refuse"); const before = await readRaw(dir); await assert.rejects(updateClip(dir, "c01", { "fx.zoom": { shot: "shots/nope.png", focus: [0, 0, 1, 1] } }), /fx\.zoom\.shot shots\/nope\.png is not a file in the project/); await assert.rejects(updateClip(dir, "c01", { "fx.zoom": { shot: "../x.png", focus: [0, 0, 1, 1] } }), /not outside it/); await assert.rejects(updateClip(dir, "c01", { "fx.zoom": { shot: "shots/doc.png", focus: [0.5, 0, 0.4, 1] } }), /timeline entry c01: fx\.zoom\.focus must be \[left, top, right, bottom\]/); await assert.rejects(updateClip(dir, "c01", { "fx.zoom": { shot: "shots/doc.png", focus: [0, 0, 1, 1], hold: 30 } }), /fx\.zoom\.hold must be from 0\.5 to 10 seconds/); await assert.rejects(updateClip(dir, "c01", { "fx.zoom": { shot: "shots/doc.png", focus: [0, 0, 1, 1], spin: 1 } }), /fx\.zoom\.spin is not a zoom field/); await assert.rejects(updateClip(dir, "c01", { "fx.punchIn": [5] }), /fx\.punchIn has a second outside the clip \(10–20\)/); await assert.rejects(updateClip(dir, "c01", { "fx.quote": "yes" }), /fx\.quote must be true or false/); await assert.rejects(updateClip(dir, "c01", { fx: [1] }), /^Error: fx must be \{/); await assert.rejects(updateClip(dir, "c01", { "fx.receipts": [{ shot: "shots/nope.png" }] }), /fx\.receipts\[0\]\.shot shots\/nope\.png is not a file/); assert.equal(await readRaw(dir), before); }); test("updateClip fx: a field patch keeps the rest; null removes a field; fx whole replaces; fx null (or \"\") clears", async () => { const dir = await project("fields"); await updateClip(dir, "c02", { "fx.punchIn": [31.234], "fx.quote": false, "fx.receipts": [{ shot: "doc.png", mark: [0, 0.4, 1, 0.5] }] }); assert.deepEqual(clip(await read(dir), "c02").fx, { punch: "kept", punchIn: [31.23], quote: false, receipts: [{ shot: "shots/doc.png", mark: [0, 0.4, 1, 0.5] }], }); await updateClip(dir, "c02", { "fx.punch": null, "fx.quote": "" }); assert.deepEqual(clip(await read(dir), "c02").fx, { punchIn: [31.23], receipts: [{ shot: "shots/doc.png", mark: [0, 0.4, 1, 0.5] }] }); await updateClip(dir, "c02", { fx: { punch: " the words ", speakers: [{ at: 32, who: 1 }] } }); assert.deepEqual(clip(await read(dir), "c02").fx, { punch: "the words", speakers: [{ at: 32, who: 1 }] }); await updateClip(dir, "c02", { fx: null }); assert.equal("fx" in clip(await read(dir), "c02"), false); // The last field removed removes fx. await updateClip(dir, "c02", { "fx.punch": "x" }); await updateClip(dir, "c02", { "fx.punch": null }); assert.equal("fx" in clip(await read(dir), "c02"), false); }); test("patchClipEntry: a window save that strands a punch-in is refused; one under an fx already bad is not blamed for it", () => { const m = base(); clip(m, "c01").fx = { punchIn: [12] }; assert.throws(() => patchClipEntry(m, "c01", { start: 13 }), /punchIn has a second outside the clip \(13–20\)/); const bad = base(); clip(bad, "c01").fx = { quote: "nope" }; const { entry } = patchClipEntry(bad, "c01", { end: 19 }); assert.equal(entry.end, 19); }); test("fx.zoom.hold: absent, the zoom stays to its clip's end (as it was); set, it leaves that long after the push lands", () => { const render = { width: 1920, height: 1080, fps: 30, chrome: { engine: "hyperframes", layout: "deck", fx: { zoom: {} } } }; const segments = [{ id: "c01", start: 0, duration: 20 }]; const sched = (zoom) => fxSchedule({ segments, entries: [{ type: "clip", id: "c01", start: 0, end: 20, fx: { zoom } }], render, D: 0.5, total: 20, sourceFrom: () => 0, hidden: () => false, }).zoom.clips[0]; const plain = sched({ shot: "s.png", focus: [0, 0, 1, 1], at: 3 }); assert.equal(plain.out, 19.7); // the clip's clean span less 0.3 const held = sched({ shot: "s.png", focus: [0, 0, 1, 1], at: 3, hold: 4 }); assert.equal(held.push, plain.push); assert.equal(held.out, Math.round((plain.push + FX_MOTION.push + 4 + 0.3) * 1000) / 1000); // Never past the clip's own leave. assert.equal(sched({ shot: "s.png", focus: [0, 0, 1, 1], at: 12, hold: 10 }).out, 19.7); }); test("umtool set fx.*: the zoom as JSON, the punch as words, fx= clears every field; a bad one refused", async () => { const dir = await project("cli"); const lines = []; const run = (...args) => setCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }); assert.equal(await run("cli", "c01", 'fx.zoom={"shot":"shots/doc.png","focus":[0.1,0.2,0.9,0.4],"at":5.9}', "fx.punch=2020"), 0, lines.join("\n")); assert.deepEqual(clip(await read(dir), "c01").fx, { zoom: { shot: "shots/doc.png", focus: [0.1, 0.2, 0.9, 0.4], at: 5.9 }, punch: "2020" }); assert.match(lines.at(-1), /^c01: fx \{.*"zoom":\{"shot":"shots\/doc.png"/); assert.equal(await run("cli", "c01", "fx.zoom.hold=4"), 2); assert.match(lines.at(-1), /not a clip knob/); assert.equal(await run("cli", "c01", 'fx.zoom={"shot":"shots/gone.png","focus":[0,0,1,1]}'), 1); assert.match(lines.at(-1), /is not a file in the project/); assert.equal(await run("cli", "c01", "fx="), 0); assert.equal("fx" in clip(await read(dir), "c01"), false); assert.match(lines.at(-1), /^c01: fx none/); });