// outro.mjs (the reel's outro card) and the trailer's reel extras: the // setting's validation and defaults, the card's layout inside the vertical's // safe area, the plan with a reel's own intro and the card, the graph, and // the run with both (the renders stubbed). // // 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 { OUTRO_FADE, OUTRO_SECONDS_DEFAULT, OUTRO_TEXT_DEFAULT, VERTICAL_SAFE, normalizeOutro, outroAdds, outroErrors, outroLayout, outroOn, resolveOutro, } from "./outro.mjs"; import { REEL_INTRO_DEFAULT, outroBrand, reelIntroOf, scheduleHasIntro } from "./reel-intro.mjs"; import { makeTrailer, parseBeats, planTrailer, trailerArgs, withReelExtras } from "./trailer.mjs"; test("outro: on|off or an object of known keys; each wrong key refused by name", () => { assert.deepEqual(outroErrors(true), []); assert.deepEqual(outroErrors(false), []); assert.deepEqual(outroErrors({ title: "T", text: "go", image: "promo/t.jpg", seconds: 3.5 }), []); assert.match(outroErrors({ colour: "red" }).join(), /outro\.colour is not a key/); assert.match(outroErrors({ seconds: 0.5 }).join(), /outro\.seconds is 1\.5–10/); assert.match(outroErrors({ image: "../x.jpg" }).join(), /outro\.image/); assert.match(outroErrors({ image: "/abs/x.jpg" }).join(), /outro\.image/); assert.match(outroErrors({ image: "promo/x.gif" }).join(), /outro\.image/); assert.match(outroErrors("yes").join(), /on\|off/); assert.throws(() => normalizeOutro({ seconds: 99 }), /outro\.seconds/); assert.deepEqual(normalizeOutro(true), { on: true }); assert.deepEqual(normalizeOutro({ title: " The title ", image: "./promo/t.jpg" }), { on: true, title: "The title", image: "promo/t.jpg" }); assert.equal(outroOn({ on: false, title: "x" }), false); assert.equal(outroOn({ title: "x" }), true); }); test("outro: the defaults -- the reel's title, else the project's; the call to action; 3.5 s; no image", () => { assert.equal(resolveOutro(undefined), null); assert.equal(resolveOutro({ on: false, title: "x" }), null); assert.deepEqual(resolveOutro(true, { reelTitle: "Reel", projectTitle: "Project" }), { title: "Reel", text: OUTRO_TEXT_DEFAULT, image: null, seconds: OUTRO_SECONDS_DEFAULT }); assert.equal(resolveOutro({ on: true }, { projectTitle: "Project" }).title, "Project"); assert.deepEqual(resolveOutro({ on: true, title: "T", text: "Watch it", image: "promo/t.jpg", seconds: 4 }, { reelTitle: "Reel" }), { title: "T", text: "Watch it", image: "promo/t.jpg", seconds: 4 }); assert.equal(outroAdds(3.5), 3.5 - OUTRO_FADE); assert.equal(outroAdds(0), 0); }); test("the card's layout: a vertical's content inside the safe area (top 8 %, bottom 20 %, right 12 %); a landscape's centred", () => { const v = outroLayout({ width: 1080, height: 1920 }); assert.equal(v.vertical, true); assert.ok(v.box.y0 >= 1920 * VERTICAL_SAFE.top); assert.ok(v.box.y1 <= 1920 * (1 - 0.2)); assert.ok(v.box.x1 <= 1080 * (1 - 0.12)); assert.ok(v.box.x0 >= 0 && v.box.x0 < v.box.x1); // the title's wrap is the box's width, and the brand's room sits inside it assert.ok(v.box.y1 - v.brandRoom > v.box.y0); const l = outroLayout({ width: 1920, height: 1080 }); assert.equal(l.vertical, false); assert.equal(l.box.x0 + l.box.x1, 1920); assert.equal(l.box.y0 + l.box.y1, 1080); }); // A master of a brand intro and two clips, 30 fps, 0.5 s dissolves. const SCHEDULE = { version: 1, kind: "deck", fps: 30, transition: 0.5, total: 26.167, segments: [ { id: "c1", type: "clip", start: 0, duration: 12, end: 12 }, { id: "c2", type: "clip", start: 11.5, duration: 14.667, end: 26.167 }, ], }; const RECORDS = new Map([ ["c1", { version: 1, id: "c1", start: 100, end: 112 }], ["c2", { version: 1, id: "c2", start: 500, end: 514.667 }], ]); const beats = () => parseBeats([{ id: "c1", start: 102, end: 106 }, { id: "c2", start: 503, end: 507 }]); test("withReelExtras: a reel's own intro first (crossfaded in), the card after the last beat (faded in over 0.5 s), the total counts both", () => { const plan = planTrailer({ schedule: SCHEDULE, records: RECORDS, beats: beats(), xfade: 0.3 }); const bare = plan.total; const p = withReelExtras(plan, { intro: { file: null, seconds: 4.75 }, outro: { seconds: 3.5, title: "T" } }); assert.equal(p.pieces[0].kind, "intro"); assert.equal(p.pieces[0].master, null); assert.equal(p.pieces[0].frames.count, 143); assert.equal(p.pieces[1].at, Number((143 / 30 - 0.3).toFixed(3))); // intro 143 frames, less one crossfade, plus the card less its fade assert.ok(Math.abs(p.total - (bare + 143 / 30 - 0.3 + 3.5 - OUTRO_FADE)) < 1e-3, `${p.total}`); assert.equal(p.outro.fade, OUTRO_FADE); assert.ok(Math.abs(p.outro.at - (bare + 143 / 30 - 0.3 - OUTRO_FADE)) < 1e-3); // nothing asked: the plan as it was const same = withReelExtras(plan, {}); assert.equal(same.total, bare); assert.equal(same.outro, undefined); }); test("trailerArgs: no outro and no file run is the graph it always was (no timebase moved); with them, the card and the intro's own file", () => { const plan = planTrailer({ schedule: SCHEDULE, records: RECORDS, beats: beats(), xfade: 0.3 }); const render = { fps: 30, crf: 20, preset: "medium", audioBitrate: "160k" }; const bare = trailerArgs({ plan, master: "/m/cut.mp4", render, outPath: "/o.mp4" }); const graph = bare[bare.indexOf("-filter_complex") + 1]; assert.doesNotMatch(graph, /settb|outro|aevalsrc/); const p = withReelExtras(plan, { intro: { file: "/cache/intro.mp4", seconds: 4.75 }, outro: { seconds: 3.5 } }); p.pieces[0].file = "/cache/intro.mp4"; const argv = trailerArgs({ plan: p, master: "/m/cut.mp4", render, outPath: "/o.mp4", outro: { png: "/card.png", seconds: 3.5, fade: 0.5 } }); const g = argv[argv.indexOf("-filter_complex") + 1]; assert.match(g, /movie=filename=\/cache\/intro\.mp4:seek_point=0\.000000/); assert.match(g, /movie=filename=\/card\.png,loop=loop=104:size=1/); assert.match(g, /xfade=transition=fade:duration=0\.500000/); assert.match(g, /acrossfade=d=0\.500000/); assert.equal(argv[argv.indexOf("-map") + 1], "[ov]"); }); async function takeFixture({ render = {} } = {}) { const root = await mkdtemp(path.join(tmpdir(), "outro-")); const take = path.join(root, "takes", "final"); const seg = path.join(take, "out", "sourced", "segments"); await mkdir(seg, { recursive: true }); await writeFile(path.join(take, "video.manifest.json"), JSON.stringify({ slug: "cut", render: { fps: 30, width: 1920, height: 1080, crf: 21, preset: "slow", audioBitrate: "160k", audioRate: 48000, audioChannels: 2, ...render } })); await writeFile(path.join(take, "out", "cut.mp4"), "master"); await writeFile(path.join(take, "out", "sourced", "schedule.json"), JSON.stringify(SCHEDULE)); for (const [id, rec] of RECORDS) await writeFile(path.join(seg, `${id}.cut.json`), JSON.stringify(rec)); return { root, take }; } test("reelIntroOf: the reel's own, else the master's, else the take's render.intro, else the default -- rendered for the reel", async () => { const sitesDir = await mkdtemp(path.join(tmpdir(), "outro-sites-")); await mkdir(path.join(sitesDir, "anilyzer")); await writeFile(path.join(sitesDir, "anilyzer", "site.json"), JSON.stringify({ siteTitle: "Anilyzer", wordmarkLead: "Ani", accent: "sakura", siteUrl: "https://anilyzer.pages.dev" })); try { const build = { schedule: SCHEDULE, render: { fps: 30 }, manifest: {} }; const own = reelIntroOf({ build, introSpec: { style: "boot-dark", mark: "chime", site: "anilyzer" }, sitesDir }); assert.equal(own.from, "reel"); assert.equal(own.entry.site.name, "Anilyzer"); assert.equal(reelIntroOf({ build, sitesDir }).from, "default"); assert.equal(reelIntroOf({ build, sitesDir }).entry.style, REEL_INTRO_DEFAULT.style); assert.equal(reelIntroOf({ build: { ...build, render: { fps: 30, intro: "boot" } }, sitesDir }).from, "take"); const withIntro = { ...SCHEDULE, segments: [{ id: "brand-intro", type: "intro", start: 0, duration: 4 }, ...SCHEDULE.segments] }; assert.equal(scheduleHasIntro(withIntro), true); assert.equal(reelIntroOf({ build: { ...build, schedule: withIntro }, sitesDir }).from, "master"); assert.throws(() => reelIntroOf({ build, introSpec: { style: "nope" }, sitesDir }), /not an intro style/); const brand = await outroBrand(own.entry, { sitesDir }); assert.equal(brand.name, "Anilyzer"); assert.equal(brand.lead, "Ani"); assert.match(brand.ink, /^#[0-9a-f]{6}$/i); assert.equal((await outroBrand(null, { sitesDir })).name, "Archilyzer"); } finally { await rm(sitesDir, { recursive: true, force: true }); } }); test("makeTrailer: a reel's own intro and the card, made inside the slot; the record names both", async () => { const { root, take } = await takeFixture(); try { let ran = null; const made = []; const res = await makeTrailer({ takeDir: take, beats: beats(), introSpec: { style: "boot-dark", mark: "chime" }, outro: { title: "The Title", text: OUTRO_TEXT_DEFAULT, image: null, imageFile: null, seconds: 3.5 }, probe: async () => 26.167, measure: async () => new Map(), slot: async (fn) => { made.push("slot"); return fn(); }, makeIntro: async ({ entry }) => { made.push(`intro ${entry.style}`); return "/cache/intro.mp4"; }, makeOutro: async (o) => { made.push(`outro ${o.width}x${o.height} ${o.brand.name}`); return { file: "/cards/outro.png" }; }, run: async (argv) => { ran = argv; await writeFile(argv.at(-1), "trailer"); }, }); assert.deepEqual(made, ["slot", "intro boot-dark", "outro 1920x1080 Archilyzer"]); const g = ran[ran.indexOf("-filter_complex") + 1]; assert.match(g, /\/cache\/intro\.mp4/); assert.match(g, /\/cards\/outro\.png/); const rec = JSON.parse(await readFile(res.recordPath, "utf8")); assert.equal(rec.pieces[0].kind, "intro"); assert.equal(rec.pieces[0].file, "/cache/intro.mp4"); assert.equal(rec.outro.title, "The Title"); assert.equal(rec.outro.card, "/cards/outro.png"); assert.equal(rec.seconds, res.plan.total); // intro on with no intro in the master: one rendered for the reel (the default), not refused const dry = await makeTrailer({ takeDir: take, beats: beats(), intro: true, dryRun: true, probe: async () => 26.167, measure: async () => new Map() }); assert.equal(dry.plan.pieces[0].kind, "intro"); assert.match(dry.plan.notes[0], /the default intro/); } finally { await rm(root, { recursive: true, force: true }); } }); test("the portrait intro: the drawing centred in the Shorts safe area (top 8 %, bottom 20 %, 12 % either side); landscape unchanged", async () => { const { introHtml, lockupParts, PORTRAIT_SAFE_CENTRE } = await import("./chrome-intro.mjs"); const { brandKit } = await import("./brand.mjs"); const kit = brandKit("archilyzer-media"); const parts = lockupParts(kit); for (const mark of ["none", "chime"]) { const W = 1080; const H = 1920; const html = introHtml({ style: "boot-dark", ground: "dark", mark, seconds: 4.75, width: W, height: H, fps: 30, kit, site: { name: "Anilyzer", lead: "Ani", accent: "sakura", url: "anilyzer.pages.dev" } }); const m = /\s*/.exec(html); assert.ok(m, "the stage's transform"); const [ox, oy, k] = m.slice(1).map(Number); const withMark = mark !== "none"; const x0 = withMark ? parts.mark.x : 0; const y0 = Math.min(parts.top, withMark ? parts.mark.y : Infinity); const y1 = Math.max(parts.bottom, withMark ? parts.mark.y + parts.mark.size : -Infinity); assert.ok(ox + x0 * k >= W * 0.12 - 1, `left ${ox + x0 * k}`); assert.ok(ox + parts.width * k <= W * 0.88 + 1, `right ${ox + parts.width * k}`); assert.ok(oy + y0 * k >= H * 0.08, `top ${oy + y0 * k}`); assert.ok(oy + y1 * k <= H * 0.8, `bottom ${oy + y1 * k}`); assert.ok(Math.abs(oy + ((y0 + y1) / 2) * k - H * PORTRAIT_SAFE_CENTRE) < 1); } // landscape: centred on the frame, as it always was const l = introHtml({ style: "boot-dark", ground: "dark", mark: "none", seconds: 4.18, width: 1920, height: 1080, fps: 30, kit }); const [lx, ly, lk] = /\s*/.exec(l).slice(1).map(Number); assert.ok(Math.abs(ly + ((parts.top + parts.bottom) / 2) * lk - 540) < 1); assert.ok(Math.abs(lx + (parts.width / 2) * lk - 960) < 1); }); test("wrapShortArgs: the intro crossfaded into the short, the card after it; a hard cut with xfade 0", async () => { const { wrapShortArgs } = await import("./reel-intro.mjs"); const { filterPath } = await import("./trailer.mjs"); const { outroFilterParts } = await import("./outro.mjs"); const base = { short: "/s.mp4", shortSeconds: 40, fps: 30, encode: ["-c:v", "libx264"], out: "/o.mp4", escape: filterPath, outroParts: outroFilterParts }; const argv = wrapShortArgs({ ...base, intro: { file: "/i.mp4", seconds: 4.75 }, outro: { png: "/c.png", seconds: 3.5, fade: 0.5 }, xfade: 0.3 }); const g = argv[argv.indexOf("-filter_complex") + 1]; assert.match(g, /\[iv\]trim=end_frame=143/); assert.match(g, /\[ivt\]\[sv\]xfade=transition=fade:duration=0\.300000:offset=4\.466667\[wv\]/); // the card fades in over the last 0.5 s of intro + short − crossfade assert.match(g, /xfade=transition=fade:duration=0\.500000:offset=43\.966667\[ov\]/); assert.deepEqual(argv.slice(argv.indexOf("-map"), argv.indexOf("-map") + 4), ["-map", "[ov]", "-map", "[oa]"]); const hard = wrapShortArgs({ ...base, intro: { file: "/i.mp4", seconds: 4.75 }, xfade: 0 }); assert.match(hard[hard.indexOf("-filter_complex") + 1], /concat=n=2:v=1:a=1\[wv\]\[wa\]/); const onlyOutro = wrapShortArgs({ ...base, outro: { png: "/c.png", seconds: 3.5, fade: 0.5 } }); assert.match(onlyOutro[onlyOutro.indexOf("-filter_complex") + 1], /offset=39\.500000\[ov\]/); }); // ---- the 9:16 end card (lane vertical-deck) ----------------------------------------------------------------- test("the end card's sources: each clip once, its date, channel and time; the schedule's source parts first", async () => { const { endCardSources, endCardDate, endCardHost } = await import("./outro.mjs"); const timeline = [ { type: "clip", id: "a", video: "v1", start: 5104.2, channelTitle: "TheQuartering (Rumble)", date: "2026-01-12" }, { type: "clip", id: "b", video: "v1", start: 20 }, { type: "post", id: "p" }, { type: "clip", id: "c~1", video: "v2", start: 61 }, ]; const schedule = { segments: [{ id: "c", vsource: { channel: "Chan", date: "Feb 3, 2026", clock: "1:01" } }] }; assert.deepEqual(endCardSources(timeline, schedule), [ { date: "Jan 12", label: "TheQuartering (Rumble) · 1:25:04" }, { date: "Feb 3", label: "Chan · 1:01" }, ]); assert.equal(endCardDate("whenever"), "whenever"); assert.equal(endCardHost("https://jeralyzer.pages.dev/?q=1"), "jeralyzer.pages.dev"); assert.equal(endCardHost(null), ""); }); test("the end card's layout keeps inside the safe area; a landscape outro is not one", async () => { const { endCardLayout } = await import("./outro.mjs"); const E = endCardLayout({ width: 1080, height: 1920 }); assert.ok(E.box.x0 >= 0 && E.box.x1 <= 1080 * (1 - VERTICAL_SAFE.right)); assert.ok(E.box.y0 >= 1920 * VERTICAL_SAFE.top && E.box.y1 <= 1920 * (1 - VERTICAL_SAFE.bottom)); assert.ok(E.qr.size <= E.box.x1 - E.box.x0); }); test("the end card drawn: the QR, the sources and the brand inside the safe area", async (t) => { const { execFile } = await import("node:child_process"); const { promisify } = await import("node:util"); const run = promisify(execFile); const have = await run("magick", ["-version"]).then(() => run("qrencode", ["-V"]).then(() => true)).catch(() => false); if (!have) return t.skip("needs ImageMagick and qrencode"); const { renderOutroCard } = await import("./outro.mjs"); const dir = await mkdtemp(path.join(tmpdir(), "endcard-")); try { const sources = Array.from({ length: 5 }, (_, i) => ({ date: `Jan ${i + 1}`, label: `Channel ${i} · 1:0${i}:00` })); const r = await renderOutroCard({ width: 1080, height: 1920, title: "Every weight he said, in his own words", text: "Full video on the channel", brand: { name: "Jeralyzer", lead: "Jer", ink: "#5fc4a6" }, dir, qrUrl: "https://jeralyzer.pages.dev/?q=diet", sources, }); const { stdout } = await run("magick", [r.file, "-format", "%w %h", "info:"]); assert.equal(stdout.trim(), "1080 1920"); for (const k of ["kicker", "title", "qr", "host", "cta", "more", "brand"]) assert.ok(r.placed[k], k); assert.equal(r.placed.sources.length, 3); const boxes = [r.placed.kicker, r.placed.title, r.placed.qr, r.placed.host, r.placed.cta, r.placed.more, r.placed.brand, ...r.placed.sources]; for (const b of boxes) { assert.ok(b.y >= 1920 * VERTICAL_SAFE.top - 1 && b.y + b.h <= 1920 * (1 - VERTICAL_SAFE.bottom) + 1, JSON.stringify(b)); assert.ok(b.x + b.w <= 1080 * (1 - VERTICAL_SAFE.right) + 1, JSON.stringify(b)); } // Drawn again: the same key, kept. const again = await renderOutroCard({ width: 1080, height: 1920, title: "Every weight he said, in his own words", text: "Full video on the channel", brand: { name: "Jeralyzer", lead: "Jer", ink: "#5fc4a6" }, dir, qrUrl: "https://jeralyzer.pages.dev/?q=diet", sources, }); assert.equal(again.cached, true); if (process.env.END_CARD_OUT) await writeFile(process.env.END_CARD_OUT, await readFile(r.file)); } finally { await rm(dir, { recursive: true, force: true }); } });