// The trailer's beats (trailerBeats.mjs): the one writer of promo/trailer-beats.json, // and the reader that flags a beat outside its clip and keeps the running length. // // 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 { BeatsStale, TRAILER_BEATS_FILE, addTrailerBeat, beatsToken, introSecondsOf, moveTrailerBeat, orderTrailerBeats, readTrailerBeats, removeTrailerBeat, setTrailerSettings, trailerCandidatesOf, trailerView, updateTrailerBeat, } from "./trailerBeats.mjs"; const manifest = () => ({ timeline: [ { type: "clip", id: "a", video: "v", start: 10, end: 20 }, { type: "card", id: "k", seconds: 3 }, { type: "clip", id: "b", video: "v", start: 30, end: 50, gaps: [{ from: 35, to: 40 }], star: true }, { type: "clip", id: "c", video: "v", start: 60, end: 70, cutStart: 61, cutEnd: 69, star: true, verdict: "confirmed" }, ], }); async function project(beats) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-beats-")); if (beats !== undefined) { await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, TRAILER_BEATS_FILE), typeof beats === "string" ? beats : JSON.stringify(beats, null, 1) + "\n"); } return dir; } const fileOf = async (dir) => readFile(path.join(dir, TRAILER_BEATS_FILE), "utf8"); test("the view: each beat's length (gaps out), the running length, the limit, beats outside their clip", () => { const v = trailerView(manifest(), { beats: [ { id: "a", start: 12, end: 18 }, { id: "b", start: 30, end: 45 }, { at: 100, dur: 4 }, { id: "c", start: 60, end: 66 }, { id: "gone", start: 1, end: 3 }, ], settings: { xfade: 0.5, intro: true, limit: 20 }, }, { introSeconds: 4 }); assert.deepEqual(v.rows.map((r) => r.seconds), [6, 10, 4, 5, 2]); // 4 (intro) + 6 + 10 + 4 + 5 + 2 = 31, six pieces, five crossfades of 0.5 assert.equal(v.total, 28.5); assert.equal(v.over, true); assert.equal(v.rows[1].running, 19, "intro + a + b, two crossfades"); assert.equal(v.rows[1].over, false); assert.equal(v.rows[2].over, true); assert.deepEqual(v.rows[3].problems, ["starts 1.00 s before c's window (61–69)"]); assert.deepEqual(v.rows[4].problems, ["gone is not in the cut any more"]); assert.equal(v.outside, 2); assert.equal(v.inOrder, true); }); test("the view says when the beats are out of the cut's order; X's 2:20 is the default limit", () => { const v = trailerView(manifest(), { beats: [{ id: "c", start: 62, end: 64 }, { id: "a", start: 12, end: 14 }] }); assert.equal(v.inOrder, false); assert.equal(v.limit, 140); assert.equal(v.total, 3.7, "two beats, one 0.3 s crossfade"); }); test("add, edit, move, remove: the file keeps its shape and indent, checked by parseBeats", async () => { const dir = await project([{ id: "a", start: 12, end: 14 }]); try { const t0 = await beatsToken(dir); const r = await addTrailerBeat(dir, { id: "c", start: 62, end: 65.555 }, { token: t0, manifest: manifest() }); assert.equal(r.index, 1); const r2 = await addTrailerBeat(dir, { id: "b", start: 31, end: 34 }, { manifest: manifest() }); assert.equal(r2.index, 1, "in the cut's order: before c"); let text = await fileOf(dir); assert.match(text, /^\[\n \{/, "still an array, still a 1-space indent"); assert.deepEqual(JSON.parse(text)[2], { id: "c", start: 62, end: 65.56 }); await updateTrailerBeat(dir, 2, { end: 64, label: "the line" }); await moveTrailerBeat(dir, 2, 0); assert.deepEqual(JSON.parse(await fileOf(dir)).map((b) => b.id), ["c", "a", "b"]); await removeTrailerBeat(dir, 1); assert.deepEqual(JSON.parse(await fileOf(dir)).map((b) => b.id), ["c", "b"]); await assert.rejects(updateTrailerBeat(dir, 0, { end: 50 }).then(() => updateTrailerBeat(dir, 0, { start: 60, end: 59 })), /not after start/); await assert.rejects(addTrailerBeat(dir, { id: "a", start: 1, end: 2 }, { token: "1" }), (e) => e instanceof BeatsStale); await orderTrailerBeats(dir, manifest()); assert.deepEqual(JSON.parse(await fileOf(dir)).map((b) => b.id), ["b", "c"]); // A setting turns the file into { limit, …, beats }, which --beats reads too. await setTrailerSettings(dir, { limit: 90, intro: true }); text = await fileOf(dir); const doc = JSON.parse(text); assert.equal(doc.limit, 90); assert.equal(doc.intro, true); assert.equal(doc.beats.length, 2); const back = await readTrailerBeats(dir); assert.equal(back.shape, "object"); assert.deepEqual(back.settings, { limit: 90, intro: true }); await setTrailerSettings(dir, { limit: null }); assert.equal(JSON.parse(await fileOf(dir)).limit, undefined); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a beats file that does not parse is never written over; none at all starts one", async () => { const bad = await project("[{ not json"); const none = await project(); try { await assert.rejects(addTrailerBeat(bad, { id: "a", start: 1, end: 2 }), /does not parse/); assert.equal(await fileOf(bad), "[{ not json"); await addTrailerBeat(none, { id: "a", start: 12, end: 14 }); assert.deepEqual(JSON.parse(await fileOf(none)), [{ id: "a", start: 12, end: 14 }]); } finally { await rm(bad, { recursive: true, force: true }); await rm(none, { recursive: true, force: true }); } }); test("the ★ candidates are the starred clips not yet beats, over what they play", () => { assert.deepEqual(trailerCandidatesOf(manifest(), [{ id: "b", start: 31, end: 33 }]).map((c) => [c.id, c.start, c.end]), [["c", 61, 69]]); }); test("the intro's length comes from the newest built take's schedule", async () => { const dir = await project(); try { assert.equal(await introSecondsOf(dir), null); await mkdir(path.join(dir, "takes", "t1", "out", "sourced"), { recursive: true }); await writeFile(path.join(dir, "takes", "t1", "out", "sourced", "schedule.json"), JSON.stringify({ segments: [{ id: "brand-intro", type: "intro", start: 0, duration: 4.634 }] })); assert.equal(await introSecondsOf(dir), 4.63); } finally { await rm(dir, { recursive: true, force: true }); } }); // ---- beat reels (lane/beat-reels) ------------------------------------------- test("reels: the trailer is promo/trailer-beats.json, any other promo/-beats.json; listed, made empty or as a copy", async () => { const { DEFAULT_REEL, createReel, listReels, reelFileOf, reelOutName } = await import("./trailerBeats.mjs"); assert.equal(reelFileOf(DEFAULT_REEL), TRAILER_BEATS_FILE); assert.equal(reelFileOf("teaser"), path.join("promo", "teaser-beats.json")); assert.throws(() => reelFileOf("Bad Name"), /not a reel name/); assert.equal(reelOutName("kirsche", "trailer"), "kirsche.trailer.mp4"); assert.equal(reelOutName("kirsche", "x-first"), "kirsche.x-first.mp4"); const dir = await project([{ id: "a", start: 12, end: 18, verdict: "confirmed" }]); try { assert.deepEqual((await listReels(dir)).map((r) => [r.name, r.beats]), [["trailer", 1]]); await createReel(dir, "teaser", { title: "The teaser" }); await createReel(dir, "kf", { from: "trailer" }); await assert.rejects(createReel(dir, "kf"), /exists/); await assert.rejects(createReel(dir, "no", { from: "ghost" }), /no reel ghost/); const reels = await listReels(dir); assert.deepEqual(reels.map((r) => [r.name, r.beats, r.title]), [["trailer", 1, null], ["kf", 1, null], ["teaser", 0, "The teaser"]]); // A copy drops the walk's verdicts: the new reel has not been walked. assert.deepEqual((await readTrailerBeats(dir, { reel: "kf" })).beats, [{ id: "a", start: 12, end: 18 }]); // Writes to one reel leave the others alone. await addTrailerBeat(dir, { id: "c", start: 61, end: 64 }, { reel: "teaser" }); assert.equal((await readTrailerBeats(dir)).beats.length, 1); assert.equal((await readTrailerBeats(dir, { reel: "teaser" })).beats.length, 1); const v = trailerView(manifest(), await readTrailerBeats(dir, { reel: "teaser" }), { reel: "teaser" }); assert.equal(v.reel, "teaser"); assert.equal(v.file, path.join("promo", "teaser-beats.json")); assert.equal(v.title, "The teaser"); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a beat's verdict (confirmed / note) is kept, cleared with null, refused when it is anything else; title and take are settings", async () => { const dir = await project([{ id: "a", start: 12, end: 18 }]); try { await updateTrailerBeat(dir, 0, { verdict: "confirmed", start: 12.5 }); assert.deepEqual((await readTrailerBeats(dir)).beats[0], { id: "a", start: 12.5, end: 18, verdict: "confirmed" }); assert.equal(trailerView(manifest(), await readTrailerBeats(dir)).confirmed, 1); await assert.rejects(updateTrailerBeat(dir, 0, { verdict: "great" }), /verdict is one of confirmed, note/); await updateTrailerBeat(dir, 0, { verdict: null }); assert.deepEqual((await readTrailerBeats(dir)).beats[0], { id: "a", start: 12.5, end: 18 }); await setTrailerSettings(dir, { title: " X cut ", take: "draft-6" }); const d = await readTrailerBeats(dir); assert.deepEqual(d.settings, { title: "X cut", take: "draft-6" }); await assert.rejects(setTrailerSettings(dir, { take: "Not A Take" }), /take is a take id/); await setTrailerSettings(dir, { title: null, take: null }); assert.deepEqual((await readTrailerBeats(dir)).settings, {}); } finally { await rm(dir, { recursive: true, force: true }); } }); test("every write is an undo point: undo puts the file back as it was, one write at a time, and the token guards it", async () => { const { readReelUndo, undoTrailerBeats } = await import("./trailerBeats.mjs"); const dir = await project([{ id: "a", start: 12, end: 18 }]); try { const before = await fileOf(dir); await updateTrailerBeat(dir, 0, { start: 13 }); await addTrailerBeat(dir, { id: "c", start: 61, end: 64 }); assert.deepEqual((await readReelUndo(dir)).map((p) => p.what), ["beat 1: start", "add c"]); const tok = await beatsToken(dir); await assert.rejects(undoTrailerBeats(dir, { token: "0" }), BeatsStale); const u = await undoTrailerBeats(dir, { token: tok }); assert.equal(u.undone, "add c"); assert.equal(u.left, 1); assert.deepEqual((await readTrailerBeats(dir)).beats, [{ id: "a", start: 13, end: 18 }]); await undoTrailerBeats(dir); assert.equal(await fileOf(dir), before); await assert.rejects(undoTrailerBeats(dir), /nothing to undo/); // A reel made here: its first undo takes the file away again. const { createReel } = await import("./trailerBeats.mjs"); await createReel(dir, "teaser"); await undoTrailerBeats(dir, { reel: "teaser" }); assert.equal(await beatsToken(dir, "teaser"), null); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a beat's gaps (lane/reel-bench): written as a list through the one writer, refused by the clip gap rule, counted out of its length; undo puts them back", async () => { const dir = await project([{ id: "a", start: 11, end: 19 }]); try { let tok = await beatsToken(dir); const r = await updateTrailerBeat(dir, 0, { gaps: [{ from: 15.004, to: 17 }, { from: 12, to: 13 }] }, { token: tok }); assert.deepEqual(r.beat.gaps, [{ from: 12, to: 13 }, { from: 15, to: 17 }]); assert.deepEqual(JSON.parse(await fileOf(dir))[0], { id: "a", start: 11, end: 19, gaps: [{ from: 12, to: 13 }, { from: 15, to: 17 }] }); // 8 s of beat, 3 s cut: the view counts 5; and the clip's own gaps with them on another beat. const v = trailerView(manifest(), await readTrailerBeats(dir)); assert.equal(v.rows[0].seconds, 5); assert.deepEqual(v.rows[0].gaps, [{ from: 12, to: 13 }, { from: 15, to: 17 }]); const both = trailerView(manifest(), { beats: [{ id: "b", start: 30, end: 50, gaps: [{ from: 39, to: 44 }] }] }); // b's own gap 35–40 and the beat's 39–44: 9 s out of 20. assert.equal(both.rows[0].seconds, 11); tok = await beatsToken(dir); await assert.rejects(updateTrailerBeat(dir, 0, { gaps: [{ from: 11.2, to: 12 }] }, { token: tok }), /keep at least 0.5 s/); await assert.rejects(updateTrailerBeat(dir, 0, { gaps: [{ from: 14, to: 14.1 }] }, { token: tok }), /under 0.2 s/); // Moving the edges inside a gap is refused too: the edges and the gaps are one rule. await assert.rejects(updateTrailerBeat(dir, 0, { end: 17.2 }, { token: tok }), /before the beat's end/); await updateTrailerBeat(dir, 0, { gaps: [] }, { token: tok }); assert.deepEqual(JSON.parse(await fileOf(dir))[0], { id: "a", start: 11, end: 19 }); const { undoTrailerBeats } = await import("./trailerBeats.mjs"); await undoTrailerBeats(dir); assert.equal(JSON.parse(await fileOf(dir))[0].gaps.length, 2); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the outro: on/off keeps its words, an object merges (null drops a key), the image must be the project's; the view counts it", async () => { const dir = await project({ beats: [{ id: "a", start: 12, end: 18 }], limit: 20, title: "Reel" }); try { await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, "promo", "thumb.jpg"), "jpg"); await setTrailerSettings(dir, { outro: true }); let doc = await readTrailerBeats(dir); assert.deepEqual(doc.settings.outro, { on: true }); await setTrailerSettings(dir, { outro: { title: "Full title", image: "promo/thumb.jpg", seconds: 4 } }); await setTrailerSettings(dir, { outro: false }); doc = await readTrailerBeats(dir); assert.deepEqual(doc.settings.outro, { on: false, title: "Full title", image: "promo/thumb.jpg", seconds: 4 }); assert.equal(trailerView(manifest(), doc).outro, null); await setTrailerSettings(dir, { outro: { seconds: null } }); doc = await readTrailerBeats(dir); assert.deepEqual(doc.settings.outro, { on: true, title: "Full title", image: "promo/thumb.jpg" }); const v = trailerView(manifest(), doc); // 6 s of beat, then the card's 3.5 s less its 0.5 s fade assert.equal(v.total, 9); assert.deepEqual(v.outro, { title: "Full title", text: "Full video on the channel", image: "promo/thumb.jpg", seconds: 3.5 }); await assert.rejects(setTrailerSettings(dir, { outro: { image: "promo/none.jpg" } }), /not a file of the project/); await assert.rejects(setTrailerSettings(dir, { outro: { seconds: 40 } }), /outro\.seconds/); await setTrailerSettings(dir, { outro: null }); doc = await readTrailerBeats(dir); assert.equal(doc.settings.outro, undefined); assert.equal(JSON.parse(await fileOf(dir)).outro, undefined); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the reel's own intro: render.intro's shape, its seconds counted; a reel with no outro keeps its version", async () => { const { reelVersion } = await import("./trailerBeats.mjs"); const dir = await project([{ id: "a", start: 12, end: 18 }]); try { await setTrailerSettings(dir, { intro: { style: "boot-dark", mark: "chime", seconds: 4.75, site: "anilyzer" } }); const doc = await readTrailerBeats(dir); assert.deepEqual(doc.settings.intro, { style: "boot-dark", mark: "chime", seconds: 4.75, site: "anilyzer" }); const v = trailerView(manifest(), doc); assert.equal(v.introSeconds, 4.75); assert.equal(v.total, Number((4.75 + 6 - 0.3).toFixed(2))); await assert.rejects(setTrailerSettings(dir, { intro: { style: "nope" } }), /not an intro style/); const base = { beats: [{ id: "a", start: 1, end: 2 }], settings: { xfade: 0.3 } }; // the version as it was computed before the outro: { beats, xfade, intro } only const { createHash } = await import("node:crypto"); const before = createHash("sha256").update(JSON.stringify({ beats: [["a", 1, 2, null]], xfade: 0.3, intro: null })).digest("hex").slice(0, 16); assert.equal(reelVersion(base), before); assert.notEqual(reelVersion({ ...base, settings: { xfade: 0.3, outro: { on: true } } }), reelVersion(base)); } finally { await rm(dir, { recursive: true, force: true }); } });