// `umtool trailer`: the command line over report-to-video/trailer.mjs, on a // fixture project (the encode and the slot stubbed). 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 { PROJECT_KINDS } from "../projects/kinds.mjs"; import { trailerCommand } from "./trailerCli.mjs"; const TAKES_KIND = PROJECT_KINDS.find((k) => k.makesTakes).id; const SCHEDULE = { fps: 30, transition: 0.5, total: 20.167, segments: [ { id: "brand-intro", type: "intro", start: 0, duration: 4.167 }, { id: "c1", type: "clip", start: 3.667, duration: 16.5 }, ], }; async function project() { const root = await mkdtemp(path.join(tmpdir(), "trailercli-")); const reportsRoot = path.join(root, "reports"); const dir = path.join(reportsRoot, "proj"); const take = path.join(dir, "takes", "final"); await mkdir(path.join(take, "out", "sourced", "segments"), { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "cut", render: { fps: 30 }, timeline: [] })); await writeFile(path.join(dir, "project.json"), JSON.stringify({ kind: TAKES_KIND, title: "Proj" })); await writeFile(path.join(take, "video.manifest.json"), JSON.stringify({ slug: "cut", render: { fps: 30, crf: 21, preset: "slow" } })); await writeFile(path.join(take, "out", "cut.mp4"), "master"); await writeFile(path.join(take, "out", "sourced", "schedule.json"), JSON.stringify(SCHEDULE)); await writeFile(path.join(take, "out", "sourced", "segments", "c1.cut.json"), JSON.stringify({ id: "c1", start: 50, end: 66.5 })); return { root, reportsRoot, take }; } function io() { const out = []; const errs = []; return { out, errs, log: (s) => out.push(s), err: (s) => errs.push(s) }; } const deps = (ran) => ({ slot: () => (fn) => fn(), probe: async () => 20.167, run: async (argv) => { ran.push(argv); await writeFile(argv.at(-1), "trailer"); }, }); test("umtool trailer: cuts, prints the mapping and the total, writes the record", async () => { const { root, reportsRoot, take } = await project(); try { const ran = []; const o = io(); const code = await trailerCommand( ["proj", "--take", "final", "--beats-json", JSON.stringify([{ id: "c1", start: 55, end: 60 }]), "--intro", "--finale", "2"], { reportsRoot, log: o.log, err: o.err, deps: deps(ran) }, ); assert.equal(code, 0, o.errs.join("\n")); assert.equal(ran.length, 1); assert.match(o.out.join("\n"), /c1 +source 55\.00–60\.00/); assert.match(o.out.join("\n"), /^total 0:/m); assert.match(o.out.join("\n"), /wrote .*cut\.trailer\.mp4/); const rec = JSON.parse(await readFile(path.join(take, "out", "cut.trailer.json"), "utf8")); assert.equal(rec.pieces.length, 3); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool trailer --beats FILE --json --dry-run: the plan for an agent, nothing encoded", async () => { const { root, reportsRoot } = await project(); try { const file = path.join(root, "beats.json"); await writeFile(file, JSON.stringify([{ id: "c1", start: 55, end: 60 }, { at: 15, dur: 2 }])); const ran = []; const o = io(); const code = await trailerCommand(["proj", "--take", "final", "--beats", file, "--json", "--dry-run"], { reportsRoot, log: o.log, err: o.err, deps: deps(ran) }); assert.equal(code, 0, o.errs.join("\n")); assert.equal(ran.length, 0); const doc = JSON.parse(o.out.join("\n")); assert.equal(doc.out, null); assert.match(doc.wouldWrite, /cut\.trailer\.mp4$/); assert.deepEqual(doc.pieces.map((p) => p.kind), ["clip", "raw"]); assert.ok(doc.seconds > 6.5 && doc.seconds < 7); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool trailer: refusals exit 1 by name; usage mistakes exit 2", async () => { const { root, reportsRoot } = await project(); try { const run = (args) => { const o = io(); return trailerCommand(args, { reportsRoot, log: o.log, err: o.err, deps: deps([]) }).then((code) => ({ code, err: o.errs.join("\n") })); }; let r = await run(["proj", "--take", "final", "--beats-json", JSON.stringify([{ id: "zz", start: 1, end: 2 }])]); assert.equal(r.code, 1); assert.match(r.err, /no clip "zz"/); r = await run(["proj", "--take", "nope", "--beats-json", JSON.stringify([{ id: "c1", start: 55, end: 56 }])]); assert.equal(r.code, 1); assert.match(r.err, /not a take/); r = await run(["proj", "--beats-json", "[]"]); assert.equal(r.code, 2); assert.match(r.err, /--take /); r = await run(["proj", "--take", "final"]); assert.equal(r.code, 2); assert.match(r.err, /--beats FILE or --beats-json/); r = await run(["proj", "--take", "final", "--beats", path.join(root, "missing.json")]); assert.equal(r.code, 2); r = await run(["proj", "--take", "final", "--bogus"]); assert.equal(r.code, 2); } finally { await rm(root, { recursive: true, force: true }); } }); test("umtool trailer --reel: the reel's own beats, crossfade and intro, written to ..mp4; no --take: the latest master", async () => { const { root, reportsRoot, take } = await project(); try { const dir = path.join(reportsRoot, "proj"); await writeFile(path.join(take, "take.json"), JSON.stringify({ id: "final", group: "takes", order: 1, label: "final", kind: "reference", preview: "preview.mp4" })); await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, "promo", "teaser-beats.json"), JSON.stringify({ xfade: 0, intro: true, beats: [{ id: "c1", start: 55, end: 60 }] })); const ran = []; const o = io(); const code = await trailerCommand(["proj", "--reel", "teaser", "--no-snap"], { reportsRoot, log: o.log, err: o.err, deps: deps(ran) }); assert.equal(code, 0, o.errs.join("\n")); assert.match(o.errs.join("\n"), /the reel teaser from take final \(latest master\)/); assert.match(o.out.join("\n"), /wrote .*cut\.teaser\.mp4/); const rec = JSON.parse(await readFile(path.join(take, "out", "cut.teaser.trailer.json"), "utf8")); assert.equal(rec.xfade, 0); assert.deepEqual(rec.pieces.map((p) => p.kind), ["intro", "clip"]); // The trailer reel keeps its old name; a reel that is not there is said. const o2 = io(); assert.equal(await trailerCommand(["proj", "--reel", "nope", "--take", "final"], { reportsRoot, log: o2.log, err: o2.err, deps: deps([]) }), 2); assert.match(o2.errs.join("\n"), /no reel nope: promo\/nope-beats\.json is not there/); const o3 = io(); assert.equal(await trailerCommand(["proj", "--reel", "teaser", "--beats-json", "[]"], { reportsRoot, log: o3.log, err: o3.err, deps: deps([]) }), 2); await writeFile(path.join(dir, "promo", "trailer-beats.json"), JSON.stringify([{ id: "c1", start: 55, end: 58 }])); const o4 = io(); assert.equal(await trailerCommand(["proj", "--reel", "trailer", "--take", "final", "--no-snap"], { reportsRoot, log: o4.log, err: o4.err, deps: deps([]) }), 0, o4.errs.join("\n")); assert.match(o4.out.join("\n"), /wrote .*cut\.trailer\.mp4/); } finally { await rm(root, { recursive: true, force: true }); } });