// A take's render: the deck over HARD CUTS (`transition: 0`), built in keyed // chunks (build-video chunkedConcat), the deck's frames drawn only where a // chunk to encode reads them (deckWant / compose-chrome `want`): // // - the first build encodes every chunk and draws every frame, then prunes them; // - the same cut again draws NO frame and encodes no chunk -- the deck's // frame keys say every chunk on disk is still the cut's -- and verify-build // still reads the deck as whole; // - one still made a second longer: its chunk alone is encoded, the deck's // frames drawn only over it, the chunk after it moved and reused. // // Real encodes, kept tiny; the deck's renderer is a stub that logs how many // frames each render drew. Skipped without ffmpeg/ffprobe. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } from "./brand.mjs"; import { buildVideo, setProgressMode } from "./build-video.mjs"; import { CHUNKS_DIR, CHUNKS_RECORD } from "./chunked-encode.mjs"; import { PRUNED_RECORD } from "./prune-frames.mjs"; import { verifyBuild } from "./verify-build.mjs"; const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0); const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; const RENDER = { width: 1920, height: 1080, fps: 10, transition: 0, preset: "ultrafast", audioRate: 48000, audioChannels: 2, palette: PALETTE, fontRegular: MONO_FONT_FILE, fontBold: MONO_BOLD_FONT_FILE, chrome: { engine: "hyperframes", layout: "deck", deck: {} }, }; const STUB = `#!/usr/bin/env node import { spawnSync } from "node:child_process"; import { appendFileSync, copyFileSync, mkdirSync, readFileSync } 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 w = Number((/data-width="(\\d+)"/.exec(html) ?? [0, 640])[1]); const h = Number((/data-height="(\\d+)"/.exec(html) ?? [0, 360])[1]); mkdirSync(out, { recursive: true }); const one = out + "/.one.png"; spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=0x112233:size=" + w + "x" + h, "-frames:v", "1", one]); const n = Math.round(dur * fps); for (let i = 1; i <= n; i += 1) copyFileSync(one, out + "/frame_" + String(i).padStart(6, "0") + ".png"); appendFileSync(process.env.HF_LOG, n + "\\n"); `; test("a hard-cut deck build: keyed chunks, and the deck drawn only where a chunk to encode reads it", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-hcdeck-")); const env = { bin: process.env.HYPERFRAMES_BIN, log: process.env.HF_LOG }; t.after(() => { for (const [k, v] of [["HYPERFRAMES_BIN", env.bin], ["HF_LOG", env.log]]) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } setProgressMode("human"); rmSync(root, { recursive: true, force: true }); }); const stub = path.join(root, "hf-stub.mjs"); writeFileSync(stub, STUB); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; const log = path.join(root, "renders.log"); process.env.HF_LOG = log; const drawn = () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map(Number) : []); mkdirSync(path.join(root, "stills"), { recursive: true }); for (const [n, c] of [["one", "334455"], ["two", "554433"], ["three", "445533"]]) { const r = spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=0x${c}:size=320x180`, "-frames:v", "1", path.join(root, "stills", `${n}.png`)]); assert.equal(r.status, 0, String(r.stderr)); } const manifestPath = path.join(root, "video.manifest.json"); const write = (seconds2) => writeFileSync(manifestPath, JSON.stringify({ slug: "hc", title: "Hard cuts", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: RENDER, timeline: [ { id: "i1", type: "image", src: "stills/one.png", seconds: 2 }, { id: "i2", type: "image", src: "stills/two.png", seconds: seconds2 }, { id: "i3", type: "image", src: "stills/three.png", seconds: 2 }, ], })); const v = path.join(root, "out", "sourced"); const notes = []; const events = []; setProgressMode((e) => { events.push(e); if (e.ev === "note") notes.push(e.message); }); const record = () => JSON.parse(readFileSync(path.join(v, CHUNKS_DIR, CHUNKS_RECORD), "utf8")); // First: every chunk encoded, every frame drawn, the frames pruned after. write(2); await buildVideo({ manifestPath, opts: {} }); const rec1 = record(); assert.equal(rec1.frames, 60); assert.deepEqual(rec1.chunks.map((c) => c.anchor), ["i1.mp4@0", "i2.mp4@0", "i3.mp4@0"], "one chunk a segment"); assert.equal(drawn().reduce((a, b) => a + b, 0) > 0, true); assert.ok(!existsSync(path.join(v, "chrome", "deck-frames")), "pruned after the mux"); assert.equal((await verifyBuild(manifestPath, {})).problems.length, 0); // The same cut: no chunk encoded, no deck frame drawn. const before = drawn().length; notes.length = 0; await buildVideo({ manifestPath, opts: {} }); assert.ok(notes.some((m) => /^final encode: 3\/3 chunk\(s\) unchanged/.test(m)), notes.join("\n")); assert.ok(notes.some((m) => /0 of 3 chunk\(s\) to encode read 0 of 60 deck-frames frames/.test(m)), notes.join("\n")); assert.equal(drawn().length, before, "the renderer was not run"); assert.equal(JSON.parse(readFileSync(path.join(v, "chrome", PRUNED_RECORD), "utf8")).dirs["deck-frames"].frames, 60, "the sparse deck recorded as the cut's length"); assert.deepEqual((await verifyBuild(manifestPath, {})).problems, []); // i2 a second longer: its chunk alone, the deck drawn only over it. notes.length = 0; write(3); await buildVideo({ manifestPath, opts: {} }); const rec3 = record(); assert.equal(rec3.frames, 70); const encoded = rec3.chunks.filter((c) => !rec1.chunks.some((o) => o.key === c.key)).map((c) => c.anchor); assert.deepEqual(encoded, ["i2.mp4@0"], "only the grown still's chunk"); const read = notes.find((m) => /chunk\(s\) to encode read/.test(m)); assert.match(read, /1 of 3 chunk\(s\) to encode read (\d+) of 70/); assert.ok(Number(/read (\d+) of/.exec(read)[1]) <= 32, read); assert.ok(drawn().slice(before).every((n) => n <= 32), `drawn ${drawn().slice(before)}`); assert.deepEqual((await verifyBuild(manifestPath, {})).problems, []); // --chrome-only over the same segments: the same chunks, nothing drawn or encoded. const runs = drawn().length; notes.length = 0; await buildVideo({ manifestPath, opts: { chromeOnly: true } }); assert.ok(notes.some((m) => /^final encode: 3\/3 chunk\(s\) unchanged/.test(m)), notes.join("\n")); assert.equal(drawn().length, runs); assert.deepEqual(record().chunks.map((c) => c.key), rec3.chunks.map((c) => c.key)); }); test("the render pool: a cut built before it is filed by its next build; another project's build of the same cut takes every chunk and the sound from it", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-hcpool-")); const env = { bin: process.env.HYPERFRAMES_BIN, log: process.env.HF_LOG, pool: process.env.UMTOOL_RENDER_POOL }; t.after(() => { for (const [k, v] of [["HYPERFRAMES_BIN", env.bin], ["HF_LOG", env.log], ["UMTOOL_RENDER_POOL", env.pool]]) { if (v === undefined) delete process.env[k]; else process.env[k] = v; } setProgressMode("human"); rmSync(root, { recursive: true, force: true }); }); const stub = path.join(root, "hf-stub.mjs"); writeFileSync(stub, STUB); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; const log = path.join(root, "renders.log"); process.env.HF_LOG = log; const pool = path.join(root, "pool"); process.env.UMTOOL_RENDER_POOL = pool; const drawn = () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map(Number) : []); const project = (name) => { const dir = path.join(root, name); mkdirSync(path.join(dir, "stills"), { recursive: true }); for (const [n, c] of [["one", "334455"], ["two", "554433"]]) { const r = spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", `color=c=0x${c}:size=320x180`, "-frames:v", "1", path.join(dir, "stills", `${n}.png`)]); assert.equal(r.status, 0, String(r.stderr)); } const manifestPath = path.join(dir, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify({ slug: "hc", title: "Pooled", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: RENDER, timeline: [ { id: "i1", type: "image", src: "stills/one.png", seconds: 2 }, { id: "i2", type: "image", src: "stills/two.png", seconds: 2 }, ], })); return manifestPath; }; const notes = []; setProgressMode((e) => { if (e.ev === "note") notes.push(e.message); }); // Built before the pool (off), then again with it: what it reused of its own is filed. process.env.UMTOOL_RENDER_POOL = "0"; const ma = project("a"); await buildVideo({ manifestPath: ma, opts: {} }); process.env.UMTOOL_RENDER_POOL = pool; await buildVideo({ manifestPath: ma, opts: {} }); const runs = drawn().length; const { poolStats } = await import("./render-pool.mjs"); const s1 = await poolStats(pool); assert.equal(s1.kinds.chunk.objects, 2, "its chunks filed"); assert.equal(s1.kinds.sound.objects, 1, "its sound filed"); // Project b: the same cut, built anew -- from the pool. notes.length = 0; const mb = project("b"); await buildVideo({ manifestPath: mb, opts: {} }); assert.ok(notes.some((m) => /^final encode: 2\/2 chunk\(s\) unchanged \(.*2 from the render pool\)/.test(m)), notes.join("\n")); assert.ok(notes.some((m) => /sound from the render pool/.test(m)), notes.join("\n")); assert.equal(drawn().length, runs, "no deck frame drawn"); assert.deepEqual((await verifyBuild(mb, {})).problems, []); const s2 = await poolStats(pool); assert.ok(s2.kinds.chunk.hits >= 2 && s2.kinds.sound.hits >= 1); assert.equal(s2.kinds.chunk.objects, 2, "nothing stored twice"); // A build stopped mid-encode, run again: what it encoded is on disk or in // the pool; only the rest is encoded (here: one chunk lost, taken back). const chunksB = path.join(root, "b", "out", "sourced", CHUNKS_DIR); const rec = JSON.parse(readFileSync(path.join(chunksB, CHUNKS_RECORD), "utf8")); rmSync(path.join(chunksB, rec.chunks[1].file)); notes.length = 0; await buildVideo({ manifestPath: mb, opts: {} }); assert.ok(notes.some((m) => /^final encode: 2\/2 chunk\(s\) unchanged \(.*1 from the render pool\)/.test(m)), notes.join("\n")); });