import { test } from "node:test"; import assert from "node:assert/strict"; import { VERTICAL_DEFAULTS, captionGroups, clipGeometry, clipVerticalLayout, hookText, karaokeText, landscapeSideGeometry, safeArea, sectionTitlesOf, sideErrors, sidePostOf, sideSpan, sourceTagText, validateClipSides, validateVerticalRender, verticalAss, verticalClipGraph, verticalKnobs, verticalNotes, verticalOn, verticalPostBox, verticalPostEntry, verticalRender, } from "./vertical.mjs"; import { postLayout } from "./post-item.mjs"; import { applyLookPatch, validateLook } from "yt-dlp-transcript-common/lib/report/look.mjs"; import { beatsOf, cutClip, layoutTag, piecesWhole, planVerticalCut, renderForPart } from "./vertical-plan.mjs"; import { masterSpans, reframeGraph, spanRegions } from "./reframe.mjs"; const W = 1080; const H = 1920; test("the knobs: absent is landscape, the defaults are the shorts conventions", () => { assert.equal(verticalOn({}), false); assert.equal(verticalOn({ format: "vertical" }), true); const k = verticalKnobs({}); assert.equal(k.clip, "full"); assert.equal(k.loudness, -14); assert.equal(k.maxSeconds, 60); assert.deepEqual(k.safe, { top: 0.08, bottom: 0.2, right: 0.12 }); assert.deepEqual(verticalKnobs({ vertical: { clip: "quote", safe: { right: 0.1 } } }).safe, { top: 0.08, bottom: 0.2, right: 0.1 }); assert.equal(VERTICAL_DEFAULTS.captions.lines, 2); }); test("validateVerticalRender refuses what cannot be built, and says why", () => { assert.deepEqual(validateVerticalRender({ render: {} }), []); assert.deepEqual(validateVerticalRender({ render: { format: "vertical", vertical: { clip: "framed", post: "media", side: "overlay", hook: "section" } } }), []); const e = validateVerticalRender({ render: { format: "square", vertical: { clip: "wide", captions: { lines: 3 }, hook: "make it pop", safe: { left: 0.1 }, loudness: -2 } } }); assert.ok(e.some((x) => x.includes("render.format"))); assert.ok(e.some((x) => x.includes("vertical.clip"))); assert.ok(e.some((x) => x.includes("at most two lines"))); assert.ok(e.some((x) => x.includes("vertical.hook"))); assert.ok(e.some((x) => x.includes("safe.left"))); assert.ok(e.some((x) => x.includes("loudness"))); // The hook says the cut's own copy: a line, never longer than the cap. assert.deepEqual(validateVerticalRender({ render: { vertical: { hook: { text: "In her own words" } } } }), []); }); test("the look's checks carry the vertical ones; a look patch writes format and vertical", () => { const m = { render: {}, timeline: [{ type: "clip", id: "a", vertical: { layout: "quote" } }] }; assert.deepEqual(validateLook(m), []); assert.ok(validateLook({ ...m, timeline: [{ type: "clip", id: "a", vertical: { layout: "tall" } }] }).length); const r = applyLookPatch({ render: {} }, { format: "vertical", vertical: { clip: "framed" } }); assert.deepEqual(r.errors, []); assert.deepEqual(r.render, { format: "vertical", vertical: { clip: "framed" } }); assert.deepEqual(applyLookPatch({ render: { format: "vertical" } }, { format: "landscape" }).render, {}); }); test("a side post: one of item or post, a span inside the clip, a known place", () => { assert.deepEqual(sideErrors({ item: "p1", from: 1, to: 5 }, "side", { itemIds: new Set(["p1"]) }), []); assert.ok(sideErrors({ item: "p9", from: 1, to: 5 }, "side", { itemIds: new Set(["p1"]) })[0].includes("no post item")); assert.ok(sideErrors({ from: 1, to: 5 }).some((x) => x.includes("exactly one"))); assert.ok(sideErrors({ post: { text: "x", handle: "h" }, from: 3, to: 3.5 }).some((x) => x.includes("at least"))); assert.ok(sideErrors({ post: { text: "x", handle: "h" }, from: 0, to: 4, place: "top" }).some((x) => x.includes("place"))); const tl = [{ type: "clip", id: "c", side: { item: "p", from: 0, to: 4 } }, { type: "post", id: "p", text: "hi" }, { type: "post", id: "q", side: {} }]; const errs = validateClipSides({ timeline: tl }); assert.equal(errs.length, 1); assert.ok(errs[0].includes("only a clip")); assert.equal(sidePostOf(tl[0], tl).id, "p"); assert.equal(sidePostOf({ id: "c", side: { post: { text: "t", handle: "h" }, from: 0, to: 2 } }).id, "c-side"); assert.deepEqual(sideSpan({ from: 2, to: 30 }, 10), { from: 2, to: 10 }); assert.equal(sideSpan({ from: 12, to: 30 }, 10), null); }); test("verticalRender: 1080x1920 at the cut's fps, no chrome, rail, QR or intro; the short's loudness", () => { const r = verticalRender({ width: 1920, height: 1080, fps: 25, chrome: { deck: {} }, rail: {}, intro: "boot-dark", qr: { scale: 4 }, posts: { slurs: {} }, vertical: { loudness: -15 } }); assert.equal(r.width, W); assert.equal(r.height, H); assert.equal(r.fps, 25); assert.equal(r.chrome, undefined); assert.equal(r.rail, undefined); assert.equal(r.intro, undefined); assert.equal(r.qr, false); assert.deepEqual(r.posts, { slurs: {} }); assert.deepEqual(r.loudness, { target: -15 }); assert.equal(r.format, "vertical"); }); test("every layout keeps its text inside the safe area", () => { const render = { format: "vertical" }; const a = safeArea(render); assert.deepEqual(a, { x0: 59, x1: 950, y0: 154, y1: 1536 }); for (const layout of ["full", "framed", "quote", "split", "overlay"]) { const g = clipGeometry(layout, render); const c = g.captions; // A caption's block: its margins keep it off the right 12 % and out of the bottom 20 %. assert.ok(W - c.marginR <= a.x1 + 1, `${layout}: captions past the right edge`); if (c.align <= 3) assert.ok(H - c.marginV <= a.y1, `${layout}: captions in the bottom fifth`); if (g.tag) assert.ok(g.tag.y >= a.y0, `${layout}: tag in the top band`); if (g.card) { assert.ok(g.card.y + g.card.h <= a.y1, `${layout}: card in the bottom fifth`); assert.ok(g.card.x + g.card.w <= a.x1 + 1, `${layout}: card past the right edge`); } if (g.cite) assert.ok(g.cite.bottom <= a.y1); } // T2: the picture in the middle third. const f = clipGeometry("framed", render).video; assert.ok(f.y >= H / 3 - 60 && f.y + f.h <= (2 * H) / 3 + 60); // T3: the picture in the top 40 %. assert.equal(clipGeometry("quote", render).video.h, 768); }); test("a clip's layout: its own, else the cut's; a side post makes it split or overlay", () => { const render = { vertical: { clip: "framed", side: "overlay" } }; assert.equal(clipVerticalLayout({ id: "a" }, render), "framed"); assert.equal(clipVerticalLayout({ id: "a", vertical: { layout: "quote" } }, render), "quote"); assert.equal(clipVerticalLayout({ id: "a", side: { item: "p", from: 0, to: 3 } }, render), "overlay"); assert.equal(clipVerticalLayout({ id: "a", side: { item: "p", from: 0, to: 3, place: "split" } }, render), "split"); }); test("the picture's graph: one input, the blur first, the layout; a side card rides in and out", () => { const g = verticalClipGraph({ geo: clipGeometry("full", {}), srcW: 1920, srcH: 1080, crop: [0.64, 0, 0.3164, 1], blur: ["gblur=sigma=5"] }); assert.ok(g.startsWith("gblur=sigma=5,setsar=1,crop=608:1080:")); assert.ok(!g.includes(";")); const fr = verticalClipGraph({ geo: clipGeometry("framed", {}), srcW: 1920, srcH: 1080 }); assert.match(fr, /split=2\[vbgs\]\[vfgs\]/); assert.match(fr, /gblur=sigma=38/); const sp = verticalClipGraph({ geo: clipGeometry("split", {}), srcW: 1920, srcH: 1080, side: { idx: 2, from: 1, to: 6 } }); assert.match(sp, /\[2:v\]format=rgba/); assert.match(sp, /overlay=x=\d+:y='if\(lt\(t,1\.000\),1920/); }); test("captions: groups of at most two lines, each word lit as it is said", () => { const words = "and I used a voice changer from the beginning to sound like a girl".split(" ").map((t, i) => ({ text: t, start: i * 0.3, end: i * 0.3 + 0.25 })); const groups = captionGroups(words, { maxChars: 14, lines: 2 }); assert.ok(groups.length >= 2); for (const g of groups) assert.ok(g.words.map((w) => w.text).join(" ").length <= 28 + 10); const k = karaokeText(groups[0]); assert.match(k, /^\{\\k30\}and \{\\k30\}I/); // A pause breaks a group. const paused = captionGroups([{ text: "one", start: 0, end: 0.2 }, { text: "two", start: 0.3, end: 0.5 }, { text: "three", start: 2, end: 2.2 }]); assert.equal(paused.length, 2); }); test("the ASS document: captions, the tag and a quote's citation; captions move above an overlay card", () => { const words = [{ text: "hello", start: 0.5, end: 0.9 }, { text: "there", start: 1, end: 1.4 }]; const doc = verticalAss({ geo: clipGeometry("quote", {}), render: {}, words, seconds: 3, tag: sourceTagText({ channel: "EmberParadox", date: "2026-10-27" }) }); assert.match(doc, /PlayResX: 1080/); assert.match(doc, /— EmberParadox · 27 Oct 2026/); assert.match(doc, /\{\\k50\}hello/); const ov = verticalAss({ geo: clipGeometry("overlay", {}), render: {}, words, seconds: 3, side: { from: 0, to: 2 } }); assert.match(ov, /\\an2\\pos\(/); const hook = verticalAss({ geo: clipGeometry("framed", {}), render: {}, words, seconds: 3, hook: "Why the voice changer" }); assert.match(hook, /hook,,0,0,0,,Why the voice changer/); }); test("the hook says only the cut's own copy", () => { assert.equal(hookText(false, { entry: { title: "x" } }), null); assert.equal(hookText("title", { entry: { title: "We need to talk" } }), "We need to talk"); assert.equal(hookText("section", { sectionTitle: "Why the voice changer" }), "Why the voice changer"); assert.equal(hookText({ text: "In her own words" }), "In her own words"); const m = { render: { chrome: { deck: { sectionTitle: { first: "Life is good" }, markers: [{ after: "b", label: "Second" }] } } }, timeline: [{ id: "a" }, { id: "b" }, { id: "c" }] }; const s = sectionTitlesOf(m); assert.equal(s.get("a"), "Life is good"); assert.equal(s.get("b"), "Life is good"); assert.equal(s.get("c"), "Second"); }); test("post items: a portrait box inside the safe area, laid card / stack / media", () => { const { box, place } = verticalPostBox({ fps: 30, palette: { bg: "#12100c" } }); assert.ok(box.x + box.width <= 950 + 2); assert.equal(box.y, 154); assert.ok(box.y + box.height <= 1536); assert.match(place[0], /^pad=1080:1920:\d+:154:color=0x12100c$/); const e = { type: "post", id: "p", text: "words", media: [{ src: "a.gif", kind: "gif", width: 480, height: 270 }] }; assert.equal(verticalPostEntry(e, { vertical: { post: "card" } }).panel, "bottom"); assert.equal(verticalPostEntry(e, { vertical: { post: "card" } }).follow, false); assert.equal(verticalPostEntry(e, { vertical: { post: "stack" } }).panel, "top"); assert.equal(verticalPostEntry(e, { vertical: { post: "media" } }).panelShare, 0.6); assert.equal(verticalPostEntry({ type: "clip", id: "c" }, {}).panel, undefined); }); test("postLayout: a portrait box never lays the attachment beside the words; a landscape box is as it was", () => { const e = { type: "post", id: "p", text: "a few words here", media: [{ src: "a.png", kind: "image", width: 800, height: 800 }] }; assert.equal(postLayout(e, { width: 1574, height: 886 }).panel, "side"); const tall = postLayout(e, { width: 910, height: 1382 }); assert.equal(tall.panel, "bottom"); assert.ok(tall.slot.y > tall.card.y); const media = postLayout({ ...e, panel: "top", panelShare: 0.6 }, { width: 910, height: 1382 }); assert.ok(media.slot.height > tall.slot.height); }); test("notes: a short over its length is said so", () => { assert.deepEqual(verticalNotes({}, 90), []); assert.equal(verticalNotes({ format: "vertical" }, 59.9).length, 0); assert.match(verticalNotes({ format: "vertical" }, 72.4)[0], /72\.4 s, over its 60 s/); }); test("the plan: clip spans cut to their beats, master spans to their items, a whole item", () => { const timeline = [ { type: "clip", id: "c1", start: 100, end: 130, gaps: [{ from: 105, to: 106 }, { from: 125, to: 127 }], cutStart: 101 }, { type: "post", id: "p1", text: "x" }, { type: "post", id: "p2", text: "y" }, ]; const beats = beatsOf({ beats: [{ id: "c1", start: 110, end: 128 }, { at: 40, dur: 12 }, { id: "p2" }] }); assert.deepEqual(beats.map((b) => b.kind), ["clip", "master", "item"]); const schedule = { segments: [{ id: "c1", start: 0, end: 30 }, { id: "p1", start: 30, end: 50 }, { id: "p2", start: 50, end: 60 }] }; const plan = planVerticalCut({ timeline, beats, schedule, project: [{ type: "clip", id: "c1", vertical: { crop: [0.6, 0, 0.3164, 1] } }] }); assert.deepEqual(plan.timeline.map((e) => e.id), ["c1", "p1", "p2", "p2~2"]); assert.deepEqual(plan.timeline[0].gaps, [{ from: 125, to: 127 }]); assert.equal(plan.timeline[0].cutStart, undefined); assert.deepEqual(plan.timeline[0].vertical, { crop: [0.6, 0, 0.3164, 1] }); assert.deepEqual(plan.pieces[1], { id: "p1", from: 10, to: 20 }); assert.deepEqual(plan.pieces[2], { id: "p2", from: 0, to: 2 }); assert.equal(piecesWhole(plan.pieces), false); assert.throws(() => beatsOf([{ at: "x" }]), /beat 1/); assert.equal(layoutTag({ clip: "framed" }), "framed"); assert.equal(layoutTag({}, { reframe: true }), "reframe"); }); test("cutClip moves a side post's span with the cut's new start", () => { const e = { type: "clip", id: "x", start: 100, end: 120, side: { item: "p", from: 6, to: 15 } }; assert.deepEqual(cutClip(e, 104, 118).side, { item: "p", from: 2, to: 11 }); assert.equal(cutClip(e, 116, 120).side, undefined); }); test("R: master spans from beats; a post's card over its attachment; the graph stacks them", () => { const schedule = { segments: [{ id: "c1", type: "clip", start: 0, end: 10 }, { id: "p1", type: "post", start: 10, end: 30 }] }; const spans = masterSpans({ beats: beatsOf([{ id: "c1", start: 52, end: 56 }, { at: 12, dur: 5 }]), schedule, cuts: new Map([["c1", { start: 50 }]]) }); assert.deepEqual(spans.map((s) => [s.id, s.a, s.b]), [["c1", 2, 6], ["p1", 12, 17]]); const box = { x: 173, y: 2, width: 1574, height: 886 }; const regions = spanRegions({ type: "post", box, entry: { type: "post", id: "p", text: "hi there", media: [{ src: "a.png", kind: "image", width: 800, height: 800 }] } }); assert.equal(regions.length, 2); assert.equal(spanRegions({ type: "clip", box }).length, 1); const g = reframeGraph({ regions, safe: safeArea({}) }); assert.match(g, /^\[0:v\]split=3\[rbg\]\[r0\]\[r1\]/); assert.match(g, /\[v\]$/); }); test("landscape side panel: a third of the picture, at its right or left", () => { const g = landscapeSideGeometry({ x: 0, y: 56, w: 1920, h: 1024 }, "right"); assert.ok(g.x + g.w <= 1920); assert.equal(landscapeSideGeometry({ x: 0, y: 56, w: 1920, h: 1024 }, "left").x < 100, true); }); test("a part of the timeline keeps only the deck's markers after entries it carries", () => { const render = { fps: 30, chrome: { deck: { markers: [{ after: "a", label: "A" }, { after: "z", label: "Z" }] } } }; assert.deepEqual(renderForPart(render, [{ id: "a" }, { id: "b~2" }]).chrome.deck.markers, [{ after: "a", label: "A" }]); assert.equal(renderForPart({ fps: 30 }, []).fps, 30); });