// The story modules -- pair, chapter, status: their settings, schedules, // pages and frame keys, over a synthetic deck schedule. import { test } from "node:test"; import assert from "node:assert/strict"; import { frameKeys } from "../frame-keys.mjs"; import { chapterCover, chapterHtml, chapterPlacement, chapterWindows, CHAPTER_SIDE_MIN, pairHtml, pairSchedule, statusHtml, statusRows, statusStates } from "../chrome-story.mjs"; import { validateChrome } from "../deck.mjs"; import { moduleById, moduleProblems } from "./index.mjs"; import { storyClipErrors, validateChapter, validatePair, validateStatus } from "./story-settings.mjs"; const FPS = 30; const segs = [["a", 0, 10], ["b", 10, 20], ["c", 20, 30], ["d", 30, 45]]; const schedule = { kind: "deck", fps: FPS, total: 45, transition: 0.5, segments: segs.map(([id, start, end]) => ({ id, type: "clip", start, end, duration: end - start, title: id, subtitle: "" })), }; const palette = { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }; const render = (chrome) => ({ width: 1920, height: 1080, fps: FPS, palette, chrome: { engine: "hyperframes", layout: "deck", ...chrome } }); const PAIR = { pairs: [ { then: { clip: "a", date: "2012-09-29" }, now: { clip: "b", date: "2026-02-27" }, caption: "The same opening, renamed.", topic: "How he opens" }, { then: { clip: "c", date: "2019" }, now: { clip: "zz", date: "2026-03" }, caption: "Only the then-clip is in this cut." }, ] }; const CHAPTER = { list: [{ at: "a", title: "Indy", subtitle: "Labrador" }, { at: "c", title: "Huck", date: "2020-10" }, { at: "nope", title: "Gone" }] }; const STATUS = { items: [ { item: "Wizards of the Coast", announced: { clip: "a", date: "2017-12-08" }, outcome: { clip: "c", date: "2018-01-04", text: "decided not to sue" }, evidence: ["a", "b", "c"] }, { item: "CNET", announced: { clip: "b", date: "2019-06-03" }, outcome: "no follow-up found in the archive", evidence: ["b"] }, ] }; test("settings: the shapes the module configs use are accepted, mistakes refused", () => { assert.deepEqual(validatePair(PAIR), []); assert.deepEqual(validateChapter(CHAPTER), []); assert.deepEqual(validateStatus(STATUS), []); assert.match(validatePair({ pairs: [{ then: { clip: "a", date: "Sept 2012" }, now: { clip: "b" } }] }).join(), /date/); assert.match(validatePair({ style: "diagonal" }).join(), /style/); assert.match(validateChapter({ list: [{ at: "a" }] }).join(), /title is required/); assert.match(validateChapter({ list: [{ at: "a", title: "x" }, { at: "a", title: "y" }] }).join(), /already opens/); assert.match(validateStatus({ items: [{ item: "x", announced: "words only" }] }).join(), /names no clip/); assert.match(validateStatus({ items: [], rows: 12 }).join(), /rows/); }); test("validateChrome takes the three keys, and refuses through them", () => { assert.deepEqual(validateChrome(render({ pair: PAIR, chapter: CHAPTER, status: STATUS }).chrome, render({})), []); assert.match(validateChrome(render({ chapter: { list: [{ at: "a" }] } }).chrome, render({})).join(), /chapter\.list\[0\]\.title/); const problems = moduleProblems({ render: render({ status: { items: [{ item: "x" }] } }) }); assert.ok(problems.status?.length); }); test("storyClipErrors names clips the timeline does not hold", () => { const m = { timeline: [{ id: "a" }, { id: "b" }, { id: "c" }], render: render({ pair: PAIR, chapter: CHAPTER }) }; const errs = storyClipErrors(m); assert.ok(errs.some((e) => e.includes("zz"))); assert.ok(errs.some((e) => e.includes("nope"))); assert.equal(errs.length, 2); }); test("pair: a pair is up over its clips, each side lit while its clip plays", () => { const r = render({ pair: PAIR }); const ps = pairSchedule(schedule, r); assert.equal(ps.length, 2); assert.deepEqual(ps[0].spans, [{ in: 0, out: 20 }]); assert.deepEqual(ps[0].phases.map((p) => p.side), ["then", "now"]); assert.deepEqual(ps[1].phases.map((p) => p.side), ["then"]); const { html, items } = pairHtml(schedule, r, {}); assert.match(html, /29 Sep 2012/); assert.match(html, /Feb 2026/); assert.equal(items.length, 2); for (const it of items) assert.ok(html.includes(it.html)); assert.ok(moduleById("pair").on(schedule, r)); assert.ok(!moduleById("pair").on(schedule, render({}))); }); test("pair: then→now draws a card over the pair's first seconds", () => { const r = render({ pair: { ...PAIR, style: "then→now", seconds: 3 } }); const { html } = pairHtml(schedule, r, {}); assert.match(html, /class="arrow"/); }); test("pair frame keys: frames off a pair's span share a key; a caption changed recaptures only its pair's frames", () => { const r = render({ pair: PAIR }); const frames = 45 * FPS; const keysOf = (rr) => { const { html, items } = pairHtml(schedule, rr, {}); return frameKeys({ html, region: "pair", fps: FPS, frames, version: "t", items }); }; const a = keysOf(r); assert.ok(a, "the page is modelled"); const changed = structuredClone(PAIR); changed.pairs[1].caption = "Changed."; const b = keysOf(render({ pair: changed })); const diff = a.map((k, i) => (k !== b[i] ? i : -1)).filter((i) => i >= 0); assert.ok(diff.length > 0); // Pair 1 is up over clip c only (20–30 s): nothing before 20 s changes. assert.ok(diff.every((i) => i / FPS >= 19.9 && i / FPS <= 30.1), `frames ${diff[0]}..${diff[diff.length - 1]}`); }); test("chapter: a window per chapter the cut plays, its page keyed from its offset", () => { const r = render({ chapter: CHAPTER }); const w = chapterWindows(schedule, r); assert.deepEqual(w.map((x) => [x.segment, x.from, x.to]), [["a", 0, 3], ["c", 20, 23]]); const mod = moduleById("chapter"); const regions = mod.regions(schedule, { render: r, outDir: "/tmp/x" }); assert.equal(regions.length, 2); assert.equal(regions[1].offset, 20); assert.equal(regions[1].frameCount, 90); const win = mod.window(schedule, { segment: "c", fps: FPS, total: 45, render: r }); const html = chapterHtml(schedule, r, win, {}); assert.match(html, /Huck/); assert.match(html, /Oct 2020/); const fk = frameKeys({ html, region: "chapter", fps: FPS, frames: 90, version: "t" }); assert.ok(fk && fk.length === 90); // A clip's window from the build: only the ones it intersects. assert.equal(mod.regions(schedule, { render: r, outDir: "/tmp/x", clip: { at: 15, dur: 10 }, shift: 15 })[0].offset, 5); }); test("chapter: a lower band over a post item never covers its card -- it stands in the card's free side margin", () => { // A section that opens on a post item: its card (schedule.covers, deck.mjs postCovers) in the middle of the footage box. const r = render({ chapter: { style: "lower", list: [{ at: "c", title: "The scale video", subtitle: "23 Oct – 3 Nov 2026", date: "2026-10-23" }, { at: "a", title: "Indy" }] } }); const band = chapterCover(r); const card = { x: band.x + 520, y: band.y - 600, width: 767, height: band.height + 560 }; const withCard = (c) => ({ ...schedule, covers: [{ segment: "c", from: 20, to: 30, rects: [c] }] }); const ws = chapterWindows(withCard(card), r); const wa = ws.find((w) => w.segment === "a"), wc = ws.find((w) => w.segment === "c"); assert.equal(wa.place.mode, "band", "a clip's section: the band, as always"); assert.equal(wc.place.mode, "side"); const touches = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; assert.ok(!touches(wc.place.rect, card), "the bumper never covers the card"); assert.equal(wc.place.side, "left", "the wider margin"); assert.equal(wc.place.rect.x + wc.place.rect.width + 16, card.x); // Its page: the band laid in that margin (504 px: its words at full size). const mod = moduleById("chapter"); const page = (sch, rr) => chapterHtml(sch, rr, mod.window(sch, { segment: "c", fps: FPS, total: 45, render: rr }), {}); const title = (h) => Number(/\.title \{[^}]*font-size: (\d+)px/.exec(h)[1]); const html = page(withCard(card), r); assert.match(html, new RegExp(`\\.chapter \\{[^}]*left: 0px; top: 0px; width: ${wc.place.rect.width}px;`)); assert.equal(title(html), 54); // A narrow margin (220 px) and a long word: the words scaled down until it fits, then wrapped. const rl = render({ chapter: { style: "lower", list: [{ at: "c", title: "Unrefundable", date: "2026-10-23" }] } }); const narrow = { ...card, x: band.x + 236, width: band.width - 336 }; assert.equal(chapterWindows(withCard(narrow), rl)[0].place.rect.width, 220); const t = title(page(withCard(narrow), rl)); assert.ok(t < 54 && t >= Math.round(54 * 0.45) && 12 * 0.58 * t <= 220 - 40 + 1, `title ${t}px`); // The measure extras move clear of where it stands, not of the band it left. // A card on the right: the left margin; a card too wide for either margin to hold it: no bumper over it. assert.equal(chapterPlacement(withCard({ ...card, x: band.x + band.width - 900 }), r, wc).side, "left"); const wide = { ...card, x: band.x + 100, width: band.width - 200 }; assert.ok(100 - 16 < CHAPTER_SIDE_MIN); assert.deepEqual(chapterWindows(withCard(wide), r).map((w) => w.segment), ["a"]); // A card clear of the band's rows (a short card up top): the band, as always. assert.equal(chapterPlacement(withCard({ ...card, height: 200 }), r, wc).mode, "band"); }); test("status: rows come in on their first clip, outcomes fill on theirs", () => { const r = render({ status: STATUS }); const rows = statusRows(schedule, r); assert.deepEqual(rows.map((x) => [x.item, x.in, x.done]), [["Wizards of the Coast", 0, 20], ["CNET", 10, 20]]); const states = statusStates(schedule, r); assert.deepEqual(states.map((s) => [s.at, s.rows.length, s.rows.filter((x) => x.filled).length]), [[0, 1, 0], [10, 2, 0], [20, 2, 2]]); const each = statusStates(schedule, render({ status: { ...STATUS, mode: "each" } })); assert.ok(each.every((s) => s.rows.length === 1)); const { html, items } = statusHtml(schedule, r, {}); assert.match(html, /decided not to sue/); assert.match(html, /no follow-up found/); const fk = frameKeys({ html, region: "status", fps: FPS, frames: 45 * FPS, version: "t", items }); assert.ok(fk); // Nothing changes between two events: frames 31..299 share frame 31's key (after the fade in). assert.equal(new Set(fk.slice(31, 299)).size, 1); }); test("the three modules are registered, documented and carry a generic-panel descriptor", () => { for (const id of ["pair", "chapter", "status"]) { const m = moduleById(id); assert.ok(m, id); assert.ok(Array.isArray(m.ui) && m.ui.length >= 3, `${id} ui`); assert.ok(m.docs?.rows?.length, `${id} docs`); assert.equal(typeof m.preview, "function"); assert.ok(m.frameKeys); } });