// The module contract and registry (modules/contract.mjs, modules/index.mjs). import { test } from "node:test"; import assert from "node:assert/strict"; import { assertRegion, defineModule, MODULE_KINDS } from "./contract.mjs"; import { deckOverlays, MODULES, moduleById, moduleOfRegion, partsOf, placementOf, regionCaching, regionModule, } from "./index.mjs"; import { slotRect } from "./layout.mjs"; import { chromeOverlayChain, chromeRegions, feedRegion, finaleRegions, postsRegions, stampRegion, threadsRegion } from "../build-video.mjs"; import { deckGeometry, feedGeometry, postsGeometry, stampGeometry, threadsGeometry } from "../deck.mjs"; const RENDER = { width: 1920, height: 1080, fps: 30, palette: { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} }, }; test("defineModule refuses a definition the registry should not hold, by name", () => { const base = { id: "x", version: 1, z: 1, kind: "overlay-whole", slot: "footer", title: "X", region: "x" }; assert.equal(defineModule(base).id, "x"); assert.ok(Object.isFrozen(defineModule(base))); assert.throws(() => defineModule({ ...base, colour: 1 }), /colour is not a module key/); assert.throws(() => defineModule({ ...base, id: "Bad Id" }), /id must be/); assert.throws(() => defineModule({ ...base, kind: "overlay" }), /kind must be one of/); assert.throws(() => defineModule({ ...base, slot: "middle" }), /slot must be/); assert.throws(() => defineModule({ ...base, region: undefined }), /an overlay names its region/); assert.throws(() => defineModule({ ...base, kind: "part", host: "shell" }), /a part's slot is inline/); assert.throws(() => defineModule({ ...base, kind: "part", slot: "inline", host: "shell" }), /no region or page/); assert.throws(() => defineModule({ ...base, provides: ["other.thing"] }), /not its own/); assert.throws(() => defineModule({ ...base, validate: [] }), /validate must be a function/); assert.deepEqual(MODULE_KINDS, ["overlay-whole", "overlay-window", "segment", "input", "post", "part"]); }); test("assertRegion: a window has an offset and a frame count; a malformed region is refused", () => { const r = { name: "deck", frames: "/f", x: 0, y: 890, width: 1920, height: 190 }; assert.equal(assertRegion(r), r); assert.throws(() => assertRegion({ ...r, x: "0" }), /x must be a number/); assert.throws(() => assertRegion(r, "window"), /offset/); assert.throws(() => assertRegion({ ...r, offset: 1 }, "window"), /frame count/); assert.ok(assertRegion({ ...r, offset: 1, frameCount: 30 }, "window")); assert.throws(() => assertRegion({ ...r, startNumber: 0 }), /startNumber/); }); test("the registry: unique ids and regions, sorted by z, the deck's overlays in today's order", () => { const ids = MODULES.map((m) => m.id); assert.equal(new Set(ids).size, ids.length); for (let i = 1; i < MODULES.length; i += 1) assert.ok(MODULES[i - 1].z <= MODULES[i].z); // The order the build has always laid them in (the gap mark's pages over the posts, under the stamps), the measure's instruments beside the side panel; the // finale over all of them but the dial's finale and the promise clock, drawn over it (z 95 both); the shelf's // note page under the measure extras (it never meets them: they stand in slots of their own beside it). assert.deepEqual(deckOverlays().map((m) => m.region), ["deck", "feed", "posts", "gap-cue", "stamp", "chat", "stats", "pair", "status", "fx", "shelf", "streak", "diet", "peak", "goals", "threads", "dial", "odometer", "trail", "flips", "chapter", "finale", "runs-finale", "dial-finale", "promise"]); assert.equal(moduleOfRegion("deck").id, "shell"); assert.equal(regionModule("posts").id, "popup-posts"); assert.throws(() => regionModule("nope"), /unknown chrome region: nope/); assert.equal(moduleById("teaser").kind, "segment"); for (const m of MODULES.filter((x) => x.host)) assert.ok(moduleById(m.host), `${m.id}'s host`); assert.deepEqual(partsOf("feed"), []); }); test("placement and caching come from the module's kind", () => { for (const r of ["deck", "feed", "stamp", "chat", "stats", "fx", "threads", "flips", "dial", "odometer", "trail"]) assert.equal(placementOf(r), "whole", r); for (const r of ["posts", "gap-cue", "finale", "dial-finale"]) assert.equal(placementOf(r), "window", r); assert.equal(placementOf("chart"), null); assert.equal(placementOf("teaser"), null); assert.deepEqual(regionCaching("deck"), { keyed: true, wholeCut: true }); assert.deepEqual(regionCaching("posts"), { keyed: true, wholeCut: false }); assert.deepEqual(regionCaching("teaser"), { keyed: true, wholeCut: false }); assert.deepEqual(regionCaching("chart"), { keyed: false, wholeCut: false }); }); test("the layout engine's slots are the deck geometry's rects", () => { const feedRender = { ...RENDER, chrome: { ...RENDER.chrome, deck: { posts: { layout: "feed" } } } }; assert.deepEqual(slotRect("footer", RENDER), deckGeometry(RENDER).deck); assert.deepEqual(slotRect("footage", RENDER), deckGeometry(RENDER).footage); assert.deepEqual(slotRect("footage", feedRender, { feed: true }), feedGeometry(feedRender).footage); assert.deepEqual(slotRect("column", feedRender), feedGeometry(feedRender).column); assert.deepEqual(slotRect("popup", RENDER), postsGeometry(RENDER)); assert.deepEqual(slotRect("stamp", RENDER), stampGeometry(RENDER)); const { cards: _c, ...side } = threadsGeometry(RENDER); assert.deepEqual(slotRect("side", RENDER), side); assert.deepEqual(slotRect("full", RENDER), { x: 0, y: 0, width: 1920, height: 1080 }); assert.throws(() => slotRect("inline", RENDER), /not a slot/); }); test("the build's region helpers are the modules' regions, shaped as they always were", () => { const deck = chromeRegions(RENDER, "/o"); assert.deepEqual(deck, [{ name: "deck", frames: "/o/chrome/deck-frames", ...deckGeometry(RENDER).deck }]); assert.deepEqual(Object.keys(deck[0]), ["name", "frames", "x", "y", "width", "height"]); const feedRender = { ...RENDER, chrome: { ...RENDER.chrome, deck: { posts: { layout: "feed" } } } }; assert.deepEqual(feedRegion(feedRender, "/f"), { name: "feed", frames: "/f", ...feedGeometry(feedRender).column }); assert.deepEqual(stampRegion(RENDER, "/s", { layout: "deck" }), { name: "stamp", frames: "/s", ...stampGeometry(RENDER) }); assert.equal(threadsRegion(RENDER, "/t", "flips").name, "flips"); const schedule = { fps: 30, total: 20, posts: [{ segment: "a", appear: 2, out: [5, 5.5], slot: 0 }, { segment: "b", appear: 9, out: [12, 12.5], slot: 0 }], finale: { from: 15, to: 20 }, }; const posts = postsRegions(RENDER, "/o", schedule); assert.deepEqual(posts.map((p) => [p.segment, p.offset, p.frameCount]), [["a", 2, 105], ["b", 9, 105]]); assert.deepEqual(postsRegions(RENDER, "/o", schedule, { shift: 8, clip: { at: 8, dur: 2 } }).map((p) => [p.segment, p.offset]), [["b", 1]]); const fin = finaleRegions(RENDER, "/o", schedule); assert.deepEqual(fin.map((f) => [f.name, f.offset, f.frameCount, f.width]), [["finale", 15, 150, 1920]]); assert.deepEqual(finaleRegions(RENDER, "/o", { ...schedule, finale: undefined }), []); }); test("the overlay chain lays a module's region by its placement", () => { const regions = [ { name: "deck", frames: "/d", x: 0, y: 890, width: 1920, height: 190 }, { name: "posts", frames: "/p", x: 1, y: 2, width: 3, height: 4, offset: 2.5, frameCount: 10 }, ]; const c = chromeOverlayChain({ fps: 30 }, regions, "[v]", 1); assert.deepEqual(c.inputs.slice(0, 2), ["-reinit_filter", "0"]); assert.ok(c.inputs.includes("-itsoffset")); assert.match(c.chain, /overlay=x=0:y=890:format=yuv444:shortest=1\[hf0\]/); assert.match(c.chain, /overlay=x=1:y=2:format=yuv444:eof_action=pass\[hf1\]/); const legacy = chromeOverlayChain({ fps: 30 }, [{ name: "chart", frames: "/c", x: 0, y: 880, width: 1920, height: 200 }], "[v]", 1); assert.equal(legacy.inputs[0], "-framerate"); assert.doesNotMatch(legacy.chain, /format=rgba/); }); test("scheduleSlices: each module's slice of the one schedule; moduleProblems: each module's own sentences", async () => { const { scheduleSlices, moduleProblems } = await import("./index.mjs"); const doc = { kind: "deck", total: 20, fps: 30, transition: 0.5, segments: [{ id: "a" }, { id: "b", noPip: true }], factcheck: { stamps: [{ claim: "k", verdict: "CORROBORATED", segment: "a", stamp: false }, { claim: "j", verdict: "PARTLY", segment: "a" }] }, layout: "feed", posts: [{ id: "p" }], finale: { from: 15, to: 20 }, }; const s = scheduleSlices(doc); assert.deepEqual(Object.keys(s).sort(), ["counter", "feed", "finale", "rail", "shell", "stamp"]); assert.deepEqual(s.rail.pips, ["a"]); assert.equal(s.counter.stamps.length, 2); assert.equal(s.stamp.stamps.length, 1, "the stamps drawn"); assert.deepEqual(scheduleSlices({ kind: "chart" }), {}); const manifest = { render: { width: 1920, height: 1080, intro: { style: "boot", brand: "x-kit" }, chrome: { engine: "hyperframes", layout: "deck", deck: { qr: { links: "nowhere" }, pip: { spacing: "pitch", pitch: 1 } } } }, timeline: [], }; const p = moduleProblems(manifest); assert.match(p.qr[0], /qr\.links must be/); assert.match(p.rail[0], /pip\.pitch must be from 8 to 200/); assert.match(p.intro[0], /x-kit" is not a brand kit/); assert.deepEqual(moduleProblems({ render: {}, timeline: [] }), {}); }); test("play: a live-preview contract only an overlay with a page and regions declares", async () => { const { playModules } = await import("./index.mjs"); const base = { id: "x", version: 1, z: 1, kind: "overlay-whole", slot: "footer", title: "X", region: "x", page: async () => "", regions: () => [] }; assert.equal(defineModule({ ...base, play: { clock: "cut", ready: "x:ready" } }).play.clock, "cut"); assert.throws(() => defineModule({ ...base, play: { clock: "source", ready: "x:ready" } }), /play must be/); assert.throws(() => defineModule({ ...base, play: { clock: "cut", ready: "x" } }), /play must be/); assert.throws(() => defineModule({ ...base, page: undefined, play: { clock: "cut", ready: "x:ready" } }), /only an overlay with a page/); assert.throws(() => defineModule({ ...base, kind: "segment", play: { clock: "cut", ready: "x:ready" } }), /only an overlay with a page/); assert.deepEqual(playModules().map((m) => m.id), ["gap-cue", "stats", "pair", "status", "fx", "shelf", "streak", "diet", "peak", "goals", "threads", "dial", "odometer", "trail", "flips", "chapter", "runs-finale", "dial-finale", "promise"]); for (const m of playModules()) assert.equal(m.play.ready, `${m.region}:ready`, m.id); }); test("chat: a whole-cut overlay beside the footage, drawn only for a schedule that carries chat", () => { const chat = moduleById("chat"); assert.equal(chat.kind, "overlay-whole"); assert.equal(chat.region, "chat"); assert.equal(moduleOfRegion("chat"), chat); assert.equal(placementOf("chat"), "whole"); assert.deepEqual(regionCaching("chat"), { keyed: true, wholeCut: true }); assert.equal(chat.configKey, "render.chrome.chat"); assert.equal(chat.on({ chat: { clips: [{ segment: "a" }] } }), true); assert.equal(chat.on({ segments: [] }), false); const [r] = chat.regions({}, { render: RENDER, outDir: "/o" }); assert.deepEqual([r.name, r.frames], ["chat", "/o/chrome/chat-frames"]); assert.ok(r.x > 1920 / 2 && r.width <= 640, "a strip at the footage box's right edge"); });