// Chrome frame keys (frame-keys.mjs) and compose-chrome's frame reuse: a // frame's key is the page less its cue list plus every element's value at // that frame, as GSAP draws it; a cut that moved keys its moved frames as it // did; a changed value changes exactly the frames it shows in; a page this // does not model has no keys. Then compose-chrome with the renderer stubbed: // a re-render after a shift captures only the new keys -- in one frame-list // render -- and assembles the rest from the old frames. // // Run with: node --test umtool/report-to-video/frame-keys.test.mjs import assert from "node:assert/strict"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { deckHtml } from "./chrome-deck.mjs"; import { easeOf, frameKeys, staticKey } from "./frame-keys.mjs"; import { stampHtml } from "./chrome-stamp.mjs"; const RENDER = { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} }, }; const FONTS = { regular: "assets/DeckSans.ttf", bold: "assets/DeckSansBold.ttf" }; /** The chrome-deck fixture, its opening card `grow` seconds longer. */ function schedule(grow = 0, titles = {}) { const seg = (id, type, start, duration, extra = {}) => ({ id, type, start, duration, end: start + duration, title: titles[id] ?? "", subtitle: "", qrUrl: null, hideDeck: false, ...extra, }); const g = grow; return { version: 1, kind: "deck", estimated: true, fps: 30, transition: 0.5, total: 40 + g, multiChannel: false, segments: [ seg("t00", "card", 0, 5 + g, { title: "Opening", subtitle: "a card", hideDeck: true }), seg("c01", "clip", 4.5 + g, 10, { title: titles.c01 ?? "He said", subtitle: "Ferret Rescue · Sep 5, 2024" }), seg("c02", "clip", 14 + g, 8, { title: titles.c02 ?? "A sanctuary", subtitle: "Ferret Rescue · Sep 5, 2024" }), seg("c03", "clip", 21.5 + g, 13, { subtitle: "Untitled source · Sep 29, 2024" }), seg("end", "card", 34 + g, 6, { title: "Close", subtitle: "", hideDeck: true }), ], }; } const keysOf = (s, render = RENDER) => frameKeys({ html: deckHtml(s, render, { fonts: FONTS }), region: "deck", fps: 30, frames: Math.round(s.total * 30), version: "v", }); test("easeOf: GSAP's own eases", () => { assert.equal(easeOf("none")(0.3), 0.3); assert.ok(Math.abs(easeOf("power1.out")(0.5) - 0.75) < 1e-12); assert.ok(easeOf("back.out(2.2)")(0.6) > 1, "an overshoot"); }); test("frame keys: the moved frames of a longer opening key as they did", () => { const A = keysOf(schedule(0)); const B = keysOf(schedule(1 / 3)); // 10 frames longer assert.equal(A.length, 1200); assert.equal(B.length, 1210); // From the end of the first handover on (6 s), every frame is the old one 10 frames on. let same = 0; for (let n = 180; n < A.length; n += 1) if (B[n + 10] === A[n]) same += 1; assert.equal(same, A.length - 180); // The opening itself is the card's, hidden deck; while it is longer nothing moves either. assert.equal(new Set(A.slice(0, 100)).size, 1, "a still deck keys as one frame"); // Frames that differ are not the same key by accident. assert.notEqual(A[400], A[700]); }); test("frame keys: a cut moved by frames keys as it did when its times are stored to the millisecond", () => { // The schedule stores seconds to 3 places (deck.mjs): a move of 59 frames // (not a multiple of 3) moves each start by 59/30 s give or take a millisecond. const r3 = (v) => Math.round(v * 1000) / 1000; const at = (introFrames) => { // Clips of uneven frame counts, so their starts fall on every third of a frame. const frames = [introFrames, 301, 242, 391, 180]; const s = schedule(0); let t = 0; s.segments = s.segments.map((x, i) => { const d = frames[i] / 30; const seg = { ...x, start: r3(t), duration: r3(d), end: r3(t + d) }; t += d - 0.5; return seg; }); s.total = r3(t + 0.5); return s; }; const A = keysOf(at(160)), B = keysOf(at(160 + 59)); let same = 0, n = 0; for (let f = 200; f < A.length; f += 1) { n += 1; if (B[f + 59] === A[f]) same += 1; } assert.equal(same, n, `${n - same} moved frames keyed differently`); }); test("frame keys: a changed title changes every frame (it is in the page); a changed value only where it shows", () => { const A = keysOf(schedule(0)); const T = keysOf(schedule(0, { c02: "Another sanctuary" })); assert.ok(A.every((k, n) => k !== T[n]), "the page's text is part of every frame's key"); // The tally steps at its stamp's landing: frames before it are the same, after it not. const stamped = (landed) => { const s = schedule(0); return { ...s, segments: s.segments.map((x) => (x.id === "c02" ? { ...x, claim: { id: "k1", verdict: "CORROBORATED" } } : x)), factcheck: { stamps: [{ claim: "k1", verdict: "CORROBORATED", segment: "c02", at: landed - 0.3, landed, out: [21.5, 22] }] }, }; }; const P = keysOf(stamped(18)); const Q = keysOf(stamped(19)); const differ = P.map((k, n) => (k !== Q[n] ? n : null)).filter((n) => n != null); assert.ok(differ.length > 0); assert.ok(differ[0] >= 18 * 30 - 1, `nothing differs before the earlier landing (first at ${differ[0]})`); assert.ok(differ.at(-1) <= 20 * 30, `the counts agree again once both have landed (last at ${differ.at(-1)})`); }); test("frame keys: a window of the cut keys each frame as the whole cut keys it there", () => { const s = schedule(0); const whole = frameKeys({ html: deckHtml(s, RENDER, { fonts: FONTS }), region: "deck", fps: 30, frames: 1200, version: "v" }); const win = frameKeys({ html: deckHtml(s, RENDER, { fonts: FONTS, from: 5, duration: 10 }), region: "deck", fps: 30, frames: 300, version: "v" }); assert.equal(win.length, 300); assert.deepEqual(win, whole.slice(150, 450)); }); test("frame keys: refused for a page this does not model", () => { const s = schedule(0); // A window not as long as its frames. assert.equal(frameKeys({ html: deckHtml(s, RENDER, { fonts: FONTS, from: 5, duration: 10 }), region: "deck", fps: 30, frames: 600, version: "v" }), null); // A region it does not know. assert.equal(frameKeys({ html: deckHtml(s, RENDER, { fonts: FONTS }), region: "feed", fps: 30, frames: 1200, version: "v" }), null); // A cue whose from is not what its element holds. const html = deckHtml(s, RENDER, { fonts: FONTS }); const broken = html.replace(/"from":\{"xPercent":0\}/, '"from":{"xPercent":7}'); assert.notEqual(broken, html); assert.equal(frameKeys({ html: broken, region: "deck", fps: 30, frames: 1200, version: "v" }), null); // The assets, the frame rate and the renderer are in the key. const k = (o) => staticKey({ html, region: "deck", assets: [["a.png", "1"]], fps: 30, version: "v", ...o }); assert.notEqual(k({ assets: [["a.png", "2"]] }), k({})); assert.notEqual(k({ version: "w" }), k({})); assert.notEqual(k({ fps: 25 }), k({})); }); // ---- compose-chrome's frame reuse, the renderer stubbed -------------------------- const STUB = `#!/usr/bin/env node // A renderer that writes, for each frame, the cut frame it shows and the page's cue list's hash. import { createHash } from "node:crypto"; import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; const a = process.argv.slice(2); const out = a[a.indexOf("--output") + 1]; const fps = Number(a[a.indexOf("--fps") + 1]); const html = readFileSync(a.at(-1) + "/index.html", "utf8"); const dur = Number(/data-composition-id="[^"]*"[^>]*data-duration="([\\d.]+)"/.exec(html)[1]); const data = /