// `umtool trailer-beats ... gap` (lane/reel-bench): a beat's gaps from a // terminal, through the beats' one writer -- the page's twin. 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 { trailerBeatsCommand } from "./trailerBeatsCli.mjs"; async function root() { const r = await mkdtemp(path.join(tmpdir(), "umtool-tbcli-")); const dir = path.join(r, "p1"); await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ slug: "p1", title: "P1", timeline: [{ type: "clip", id: "a", video: "v", start: 10, end: 30 }] })); await writeFile(path.join(dir, "promo", "trailer-beats.json"), JSON.stringify([{ id: "a", start: 11, end: 29 }, { at: 3, dur: 2 }])); return { r, dir }; } test("gap add / remove / clear: written through the writer, said, refused by name", async () => { const { r, dir } = await root(); const out = []; const err = []; const run = (...a) => trailerBeatsCommand(["p1", ...a], { root: r, out: (s) => out.push(s), err: (s) => err.push(s) }); try { assert.equal(await run("gap", "1", "add", "14-17"), 0); assert.match(out.join("\n"), /beat 1: gaps 14–17/); assert.match(out.join("\n"), /gaps 14–17/); assert.equal(await run("gap", "1", "add", "20-22"), 0); const beats = () => readFile(path.join(dir, "promo", "trailer-beats.json"), "utf8").then(JSON.parse); assert.deepEqual((await beats())[0].gaps, [{ from: 14, to: 17 }, { from: 20, to: 22 }]); assert.equal(await run("gap", "1", "remove", "15"), 0); assert.deepEqual((await beats())[0].gaps, [{ from: 20, to: 22 }]); assert.equal(await run("gap", "1", "clear"), 0); assert.equal((await beats())[0].gaps, undefined); assert.equal(await run("gap", "1", "add", "11.1-12"), 1); assert.match(err.join("\n"), /keep at least 0.5 s/); assert.equal(await run("gap", "2", "add", "3.5-4"), 2); assert.match(err.join("\n"), /only a clip beat has gaps/); } finally { await rm(r, { recursive: true, force: true }); } }); test("status / anchor / clamp: a reel anchored to one take, read against another that moved a clip's end; the clipped beat clamped", async () => { const r = await mkdtemp(path.join(tmpdir(), "umtool-tbcli-st-")); const dir = path.join(r, "p1"); const takeOf = async (id, aEnd, builtAt) => { const t = path.join(dir, "takes", id); await mkdir(path.join(t, "out", "sourced", "segments"), { recursive: true }); await writeFile(path.join(t, "video.manifest.json"), JSON.stringify({ slug: "p1", render: { fps: 30 }, timeline: [] })); await writeFile(path.join(t, "take.json"), JSON.stringify({ id, group: "takes", order: 1, label: id, kind: "reference", summary: "", changes: [], preview: "preview.mp4", seconds: 10, builtAt, style: null, patch: null, window: "full" })); await writeFile(path.join(t, "out", "p1.mp4"), "x"); await writeFile(path.join(t, "out", "sourced", "schedule.json"), JSON.stringify({ version: 1, fps: 30, transition: 0, total: aEnd - 100 + 10, segments: [ { id: "a", type: "clip", start: 0, duration: aEnd - 100, end: aEnd - 100 }, { id: "b", type: "clip", start: aEnd - 100, duration: 10, end: aEnd - 90 }, ] })); await writeFile(path.join(t, "out", "sourced", "segments", "a.cut.json"), JSON.stringify({ id: "a", start: 100, end: aEnd })); await writeFile(path.join(t, "out", "sourced", "segments", "b.cut.json"), JSON.stringify({ id: "b", start: 200, end: 210 })); }; await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ slug: "p1", title: "P1", timeline: [{ type: "clip", id: "a", video: "v", start: 100, end: 110 }, { type: "clip", id: "b", video: "v", start: 200, end: 210 }] })); await writeFile(path.join(dir, "promo", "trailer-beats.json"), JSON.stringify([{ id: "a", start: 101, end: 109 }, { id: "b", start: 201, end: 205 }])); await takeOf("t1", 110, "2026-11-01T00:00:00Z"); await takeOf("t2", 107, "2026-11-02T00:00:00Z"); const out = []; const err = []; const run = (...a) => trailerBeatsCommand(["p1", ...a], { root: r, out: (s) => out.push(s), err: (s) => err.push(s) }); try { assert.equal(await run("anchor", "--take", "t1"), 0, err.join("\n")); out.length = 0; assert.equal(await run("status", "--take", "t2", "--json"), 0, err.join("\n")); const st = JSON.parse(out.join("\n")); assert.equal(st.line, "cut against t1; take t2 changes beat 1"); assert.deepEqual(st.rows.map((x) => x.state), ["clipped", "rerendered"]); assert.deepEqual(st.rows[0].now, { start: 100, end: 107, gaps: [] }); assert.equal(await run("clamp", "1", "--take", "t2"), 0, err.join("\n")); const beats = JSON.parse(await readFile(path.join(dir, "promo", "trailer-beats.json"), "utf8")); assert.equal(beats[0].end, 107); assert.equal(beats[0].anchor.take, "t2"); out.length = 0; await run("history"); assert.match(out.join("\n"), /beat 1: start, end \(take t2\): 1\.end 109 → 107; 1\.anchor "t1" → "t2"/); assert.equal(await run("clamp", "2", "--take", "t2"), 2); assert.match(err.join("\n"), /only a clipped beat is clamped/); } finally { await rm(r, { recursive: true, force: true }); } }); test("outro on|off|none|JSON and intro JSON: written through the writer; ls says the card and counts it", async () => { const { r, dir } = await root(); const out = []; const err = []; const run = (...a) => trailerBeatsCommand(["p1", ...a], { root: r, out: (s) => out.push(s), err: (s) => err.push(s) }); try { await writeFile(path.join(dir, "promo", "thumb.jpg"), "jpg"); assert.equal(await run("outro", '{"title":"The Full Title","image":"promo/thumb.jpg"}'), 0, err.join("\n")); assert.match(out.join("\n"), /outro on/); assert.match(out.join("\n"), /outro card {4}3\.50s {2}"The Full Title" · "Full video on the channel" · over promo\/thumb\.jpg/); assert.match(out.join("\n"), /· outro 3\.5 s/); out.length = 0; assert.equal(await run("outro", "off"), 0); assert.match(out.join("\n"), /outro off \(its words kept/); assert.doesNotMatch(out.join("\n"), /· outro 3\.5 s/); assert.equal(await run("outro", "on"), 0); const doc = () => readFile(path.join(dir, "promo", "trailer-beats.json"), "utf8").then(JSON.parse); assert.deepEqual((await doc()).outro, { on: true, title: "The Full Title", image: "promo/thumb.jpg" }); assert.equal(await run("outro", "none"), 0); assert.equal((await doc()).outro, undefined); assert.equal(await run("outro", "maybe"), 2); assert.equal(await run("outro", '{"seconds":99}'), 1); assert.match(err.join("\n"), /outro\.seconds/); out.length = 0; assert.equal(await run("intro", '{"style":"boot-dark","mark":"chime","seconds":4.75}'), 0, err.join("\n")); assert.match(out.join("\n"), /intro 4\.75 s \(the reel's own\)/); assert.equal((await doc()).intro.style, "boot-dark"); } finally { await rm(r, { recursive: true, force: true }); } });