// Popup posts that PERSIST (`render.chrome.deck.posts.persist`): a card stays // up on its own clock across clip joins, the footage playing on; close posts // share one bar (a later clip's swap in), and the bar leaves when none follows // soon. Absent, every schedule and page is what it was. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { postsCues, postsHtml, snapWindow, windowPosts } from "./chrome-posts.mjs"; import { POSTS_PERSIST_LEAVE, deckSchedule, footageMoves, postHolds, postSchedule, postWindows, postsPersist, shiftedFootage, deckGeometry, validateChrome, } from "./deck.mjs"; import { asidesOf } from "./threads.mjs"; import { footageAt } from "../lib/report/footage-move.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const renderOf = (posts = {}) => ({ width: 1920, height: 1080, fps: 30, transition: 0.5, palette: PALETTE, chrome: { engine: "hyperframes", layout: "deck", deck: { posts } }, }); const POST = (id, attachTo, extra = {}) => ({ id, platform: "bluesky", author: "A", handle: "a.example", date: "2024-10-01", attachTo, text: `words of ${id}`, url: `https://bsky.app/profile/a.example/post/${id}`, ...extra, }); // Four 20 s clips under 0.5 s dissolves: starts 0, 19.5, 39, 58.5; the cut is 78.5 s. const CLIPS = ["c1", "c2", "c3", "c4"].map((id) => ({ id, type: "clip", date: "2024-09-01" })); const SEGS = CLIPS.map((c, i) => ({ id: c.id, start: i * 19.5, duration: 20 })); const TOTAL = 78.5; const place = (posts, persist, { entries = CLIPS, segments = SEGS, total = TOTAL } = {}) => postSchedule({ posts, entries, segments, D: 0.5, total, render: renderOf(persist === undefined ? {} : { persist }) }); const byId = (placed) => Object.fromEntries(placed.map((p) => [p.id, p])); test("validation: persist is true, false/null, or { seconds 8–60, gap 0–30 }", () => { const ok = (persist) => validateChrome(renderOf({ persist }).chrome, renderOf({ persist })); for (const v of [true, false, null, {}, { seconds: 8 }, { seconds: 60, gap: 0 }, { gap: 30 }]) assert.deepEqual(ok(v), [], JSON.stringify(v)); assert.match(ok({ seconds: 5 }).join(), /posts\.persist\.seconds must be from 8 to 60 seconds/); assert.match(ok({ gap: 31 }).join(), /posts\.persist\.gap must be from 0 to 30 seconds/); assert.match(ok({ hold: 2 }).join(), /posts\.persist\.hold is not a deck setting/); assert.match(ok("yes").join(), /posts\.persist must be true, false or \{ seconds, gap \}/); assert.deepEqual(postsPersist(renderOf({ persist: true })), { seconds: 12, gap: 6 }); assert.deepEqual(postsPersist(renderOf({ persist: { seconds: 20 } })), { seconds: 20, gap: 6 }); assert.equal(postsPersist(renderOf({ persist: false })), null); assert.equal(postsPersist(renderOf({})), null); // The feed keeps every post up anyway: persist is not its. assert.equal(postsPersist(renderOf({ persist: true, layout: "feed" })), null); }); test("absent persist: the schedule, the windows, the holds and the page are what they were (false and null alike)", () => { const posts = [POST("a", "c1"), POST("b", "c2"), POST("b2", "c2")]; const plain = place(posts); for (const v of [false, null]) assert.deepEqual(place(posts, v), plain); for (const p of plain) assert.ok(!("span" in p) && !("fresh" in p), "no persist keys"); assert.deepEqual(postWindows({ posts: plain }).map((w) => w.segment), ["c1", "c2"]); const entries = CLIPS; assert.equal(postHolds({ posts, entries, render: renderOf({}) }).get("c1"), 2.5); const sched = { fps: 30, total: TOTAL, posts: plain }; const win = snapWindow(postWindows(sched)[1], { fps: 30, total: TOTAL }); const html = postsHtml(sched, renderOf({}), win, {}); assert.equal(postsHtml({ ...sched, posts: place(posts, false) }, renderOf({ persist: false }), win, {}), html); assert.doesNotMatch(html, /"fresh"/); }); test("persist: a card stays up across the join, the bar runs to `seconds` after the last post, and nothing is held", () => { const posts = [POST("a", "c1")]; const p = byId(place(posts, { seconds: 12, gap: 6 })).a; // At its clip's end as ever (A = c2's start, 19.5, less posts.seconds 4)... assert.equal(p.appear, 15.5); // ...and up for 12 s on its own clock: past the join at 19.5–20, out over POSTS_PERSIST_LEAVE. assert.deepEqual(p.out, [27.5, 27.5 + POSTS_PERSIST_LEAVE]); assert.equal(p.span, "c1"); const w = postWindows({ posts: [p] }); assert.deepEqual(w, [{ segment: "c1", from: 15.5, to: 27.95 }]); assert.ok(w[0].to > SEGS[1].start + 0.5, "the window runs over the join into c2"); // The footage never freezes. assert.equal(postHolds({ posts, entries: CLIPS, render: renderOf({ persist: true }) }).size, 0); const s = deckSchedule({ entries: CLIPS, durs: SEGS.map((x) => x.duration), D: 0.5, render: renderOf({ persist: true }), posts }); assert.ok(s.segments.every((x) => !("hold" in x))); assert.equal(s.total, TOTAL); }); test("persist: a later clip's post within the bar (or the gap after it) joins it and swaps in; one past the gap opens a new bar", () => { const posts = [POST("a", "c1"), POST("b", "c2"), POST("b2", "c2"), POST("d", "c4")]; const near = byId(place(posts, { seconds: 12, gap: 8 })); // c2's two posts come up a posts.seconds apart before its dissolve out (39): 31, 35. assert.equal(near.b.appear, 31); assert.equal(near.b2.appear, 35); // a's bar would go at 27.5; b's moment, 31, is within its gap of 8: one bar. assert.equal(near.b.span, "c1"); assert.equal(near.b2.span, "c1"); assert.equal(near.b.fresh, true, "the first of a later clip's posts swaps the bar's cards out"); assert.ok(!("fresh" in near.b2), "the clip's own next one stacks"); assert.ok(!("fresh" in near.a)); // The bar runs to 12 s after the last post on it, every card out together. for (const id of ["a", "b", "b2"]) assert.deepEqual(near[id].out, [47, 47 + POSTS_PERSIST_LEAVE]); // d (c4: appear 74.2) is past 47 + 8: a bar of its own, ending with the cut. assert.equal(near.d.span, "c4"); assert.deepEqual(near.d.out, [TOTAL - 0.3, TOTAL]); assert.deepEqual(postWindows({ posts: Object.values(near) }).map((w) => [w.segment, w.from, w.to]), [["c1", 15.5, 47.45], ["c4", 74.2, 78.5]]); // The page for the span: every post of it, in the order they come up. assert.deepEqual(windowPosts({ posts: Object.values(near) }, "c1").map((p) => p.id), ["a", "b", "b2"]); // A gap too short: a's bar goes at 27.5 and b's opens at 31. const far = byId(place(posts, { seconds: 12, gap: 2 })); assert.equal(far.a.span, "c1"); assert.deepEqual(far.a.out, [27.5, 27.95]); assert.equal(far.b.span, "c2"); assert.ok(!("fresh" in far.b), "the first post of a bar is not a swap"); }); test("persist: the bar is over clips only -- gone by the dissolve into a card (or the finale), and a post after it starts anew", () => { const entries = [CLIPS[0], { id: "k1", type: "card" }, CLIPS[1], CLIPS[2]]; const segments = entries.map((e, i) => ({ id: e.id, start: i * 19.5, duration: 20 })); const posts = [POST("a", "c1"), POST("b", "c2")]; const p = byId(place(posts, { seconds: 30, gap: 30 }, { entries, segments })); assert.deepEqual(p.a.out, [19.5, 20], "out with the dissolve into the card, as a clip's posts always left"); assert.equal(p.b.span, "c2"); // The finale: its clips are the end of any bar. const fin = [CLIPS[0], CLIPS[1], { ...CLIPS[2], layout: "finale" }]; const segs3 = fin.map((e, i) => ({ id: e.id, start: i * 19.5, duration: 20 })); const q = byId(place([POST("a", "c1")], { seconds: 60, gap: 0 }, { entries: fin, segments: segs3, total: 59 })); assert.deepEqual(q.a.out, [39, 39.5]); }); test("persist + shift: the footage is aside across the joins the bar spans, on the cut's clock, and glides back as it leaves", () => { const render = renderOf({ persist: { seconds: 12, gap: 8 } }); const posts = [POST("a", "c1"), POST("b", "c2")]; const placed = postSchedule({ posts, entries: CLIPS, segments: SEGS, D: 0.5, total: TOTAL, render }); const moves = footageMoves({ posts: placed, segments: SEGS, render }); // a: 15.5; b: 35 (its clip's end); the bar leaves at 47 -- inside c3. assert.deepEqual(moves.map((m) => m.segment), ["c1", "c2", "c3"]); const phase = { at: 15.5, until: 47 }; for (const m of moves) { const s = SEGS.find((x) => x.id === m.segment); assert.deepEqual(m.phases, [{ ...phase, segmentAt: Math.round((15.5 - s.start) * 1000) / 1000, segmentUntil: Math.round((47 - s.start) * 1000) / 1000 }]); assert.equal(m.at, 15.5); assert.deepEqual(m.to, shiftedFootage(render)); assert.deepEqual(m.from, deckGeometry(render).footage); } // The preview's box: aside after the glide, on both sides of the join and through it; back after the bar. const sched = { segments: SEGS, moves }; assert.equal(footageAt(sched, 15.4).progress, 0); for (const t of [16.2, 19.4, 19.6, 20.2, 30, 46.9]) assert.equal(footageAt(sched, t).progress, 1, `aside at ${t}`); assert.ok(footageAt(sched, 47.3).progress > 0 && footageAt(sched, 47.3).progress < 1, "gliding back"); assert.equal(footageAt(sched, 47.7).progress, 0); assert.equal(footageAt(sched, 60), null, "c4 never moves"); // The left panel steps aside once for the whole bar. assert.deepEqual(asidesOf(moves, () => 0), [{ from: 15.5, to: 47 }]); }); test("postsCues: a fresh card swaps the bar -- every card up slides up and out as it lands; without fresh, as before", () => { const posts = [ { id: "a", appear: 1, out: [20, 20.45] }, { id: "a2", appear: 3, out: [20, 20.45] }, { id: "b", appear: 9, out: [20, 20.45], fresh: true }, ]; const plan = postsCues({ posts, heights: [200, 200, 200], column: 900 }); const at9 = plan.cues.filter((c) => c.at === 9); assert.deepEqual(at9.filter((c) => c.k === "stack").map((c) => c.to), [{ y: -428 }], "the stack moves the new card to the top"); assert.deepEqual(at9.filter((c) => c.why === "slide for b").map((c) => c.k).sort(), ["c0", "c1", "stack"]); assert.ok(plan.cues.some((c) => c.k === "c2" && c.why === "enter b" && c.at === 9)); assert.ok(!plan.cues.some((c) => c.why === "leave a"), "a swapped card does not leave again"); assert.ok(plan.cues.some((c) => c.why === "leave b" && c.at === 20)); // The same posts without the flag stack as they always did: nobody slides out. const stacked = postsCues({ posts: posts.map(({ fresh: _f, ...p }) => p), heights: [200, 200, 200], column: 900 }); assert.ok(!stacked.cues.some((c) => c.k === "stack")); }); test("ffmpeg: a persisting move's phases -- aside before the segment starts, back inside it -- parse and move the picture", { skip: !have("ffmpeg", ["-version"]) }, async () => { const { joinInputChain } = await import("./build-video.mjs"); const dir = mkdtempSync(path.join(tmpdir(), "persist-ff-")); try { const render = renderOf({ persist: true }); const move = { segment: "c2", at: 15.5, segmentAt: -4, seconds: 0.6, from: deckGeometry(render).footage, to: shiftedFootage(render), phases: [{ at: 15.5, segmentAt: -4, until: 21, segmentUntil: 1.5 }], }; const runChain = (m, name) => { const c = joinInputChain(0, { hold: 0, move: m }, render); const out = path.join(dir, name); const res = spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=1920x1080:r=30:d=3", "-filter_complex", `${c.parts.join(";")};${c.v}format=rgb24[o]`, "-map", "[o]", "-f", "rawvideo", out], { maxBuffer: 1 << 28 }); assert.equal(res.status, 0, String(res.stderr)); return readFileSync(out); }; const a = runChain(move, "o.rgb"); // The same chain with the bar never up: the picture in its box (the // move's edge pinning included), what "back" must equal. const b = runChain({ ...move, phases: [{ at: 99, segmentAt: 99, until: 100, segmentUntil: 100 }] }, "p.rgb"); const F = 1920 * 1080 * 3; const frame = (buf, n) => buf.subarray(n * F, (n + 1) * F); assert.ok(!frame(a, 0).equals(frame(b, 0)), "aside from its first frame (the bar came up before it)"); assert.ok(!frame(a, 30).equals(frame(b, 30)), "still aside at 1 s"); assert.ok(frame(a, 75).equals(frame(b, 75)), "back in its box once the bar has gone (2.5 s)"); } finally { rmSync(dir, { recursive: true, force: true }); } }); // --------------------------------------------------------------------------- // The pixels: the page a persisting bar is drawn by, seeked as the renderer seeks. // --------------------------------------------------------------------------- function have(b, a) { return spawnSync(b, a, { stdio: "ignore" }).status === 0; } const haveChromium = have("qrencode", ["-V"]) && have("magick", ["-version"]) && have(process.env.CHROME ?? "/usr/bin/chromium", ["--version"]); function inked(png, x, y, w, h) { const r = spawnSync("magick", [png, "-crop", `${w}x${h}+${x}+${y}`, "+repage", "-depth", "8", "rgba:-"], { maxBuffer: 1 << 26 }); let n = 0; for (let i = 3; i < r.stdout.length; i += 4) if (r.stdout[i] > 128) n += 1; return n / (r.stdout.length / 4); } test("pixels: the card is up across the join and after it, swapped for the next clip's, and gone when no post follows", { skip: !haveChromium }, async () => { const { composeChrome } = await import("./compose-chrome.mjs"); const dir = mkdtempSync(path.join(tmpdir(), "persist-px-")); try { const render = { ...renderOf({ persist: { seconds: 12, gap: 8 } }), fontRegular: path.join(HERE, "fonts", "IBMPlexMono-Regular.ttf"), fontBold: path.join(HERE, "fonts", "IBMPlexMono-Bold.ttf"), }; const manifestPath = path.join(dir, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify({ slug: "t", title: "t", provenance: {}, render, timeline: [] })); const posts = postSchedule({ posts: [POST("a", "c1"), POST("b", "c2")], entries: CLIPS, segments: SEGS, D: 0.5, total: TOTAL, render }); const sched = { version: 1, kind: "deck", estimated: false, fps: 30, transition: 0.5, total: TOTAL, multiChannel: false, segments: SEGS.map((s) => ({ ...s, type: "clip", end: s.start + s.duration, title: "", subtitle: "", qrUrl: null, hideDeck: false })), posts, }; const [window] = postWindows(sched); assert.deepEqual([window.segment, window.from, window.to], ["c1", 15.5, 47.45]); const still = async (t) => { const png = path.join(dir, `still-${t}.png`); await composeChrome({ manifestPath, region: "posts", schedule: sched, window, still: t, png }); return png; }; // The top card's band of the column (the region is the posts column). const band = [40, 20, 500, 120]; assert.ok(inked(await still(15.3), ...band) < 0.01, "nothing before the first post"); for (const t of [17, 19.75, 21, 29]) assert.ok(inked(await still(t), ...band) > 0.3, `the card is up at ${t} (the join is 19.5–20)`); // b swaps in at 35 and stays to 47; the bar leaves by 47.45. const html = readFileSync(path.join(dir, "out", "sourced", "chrome", "posts-c1", "index.html"), "utf8"); assert.match(html, /"id":"b","appear":35,"out":\[47,47\.45\],"fresh":true/); assert.ok(inked(await still(40), ...band) > 0.3, "the next clip's card is up"); assert.ok(inked(await still(47.44), ...band) < 0.05, "gone as the bar leaves"); } finally { rmSync(dir, { recursive: true, force: true }); } });