// A take that re-lays its chrome over ANOTHER take's segments: it hard-links // `out/sourced/segments/*` into its own out dir and runs `--chrome-only`. // // - `--chrome-only` builds a segment that is MISSING (from what is cached) // and never writes one that EXISTS: the other take's file keeps its inode, // its bytes, its mtime and its second name; // - `--only ` and a teaser re-encoded for changed words write a file of // this take's own, and leave the other take's alone; // - detachSegmentFiles unlinks only links (a hard link, a symlink), never a // file with one name, and only an entry's own files. // // Real encodes, kept tiny; the deck's renderer is a stub. Skipped without // ffmpeg/ffprobe. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { createHash } from "node:crypto"; import { chmodSync, existsSync, linkSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { buildTeaserSegment, buildVideo, detachSegmentFiles, segmentFraming, setProgressMode } from "./build-video.mjs"; import { MONO_BOLD_FONT_FILE, MONO_FONT_FILE } from "./brand.mjs"; const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0); const PALETTE = { bg: "#12101a", fg: "#f4f1ea", muted: "#9a93ad", accent: "#a97bff", amber: "#ffc860" }; // The deck is drawn on a 1920x1080 frame (what its default panel fits). 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 PROV = { siteOrigin: "https://example.test", channelSlug: "chan" }; const ff = (args) => { const r = spawnSync("ffmpeg", ["-nostdin", "-v", "error", "-y", ...args]); assert.equal(r.status, 0, String(r.stderr)); }; const sha = (p) => createHash("sha256").update(readFileSync(p)).digest("hex"); const fingerprint = (p) => { const st = statSync(p); return { ino: st.ino, nlink: st.nlink, size: st.size, mtimeMs: st.mtimeMs, sha: sha(p) }; }; /** * Two takes. A: a built image segment `i1` (and its framing record). B: the * same manifest plus a second still `i2`, with A's `i1` files hard-linked into * its segments/ and `i2` never built. */ function twoTakes(root) { const a = path.join(root, "take-a"); const b = path.join(root, "take-b"); const aSegs = path.join(a, "out", "sourced", "segments"); const bSegs = path.join(b, "out", "sourced", "segments"); for (const d of [aSegs, bSegs, path.join(b, "stills")]) mkdirSync(d, { recursive: true }); ff(["-f", "lavfi", "-i", "color=c=0x334455:size=320x180", "-frames:v", "1", path.join(b, "stills", "one.png")]); ff(["-f", "lavfi", "-i", "color=c=0x554433:size=320x180", "-frames:v", "1", path.join(b, "stills", "two.png")]); // A's segment: a second of picture and silence, as a still's segment is. ff([ "-f", "lavfi", "-i", "color=c=0x223344:size=320x180:rate=10:duration=1", "-f", "lavfi", "-i", "anullsrc=channel_layout=stereo:sample_rate=48000", "-t", "1", "-c:v", "libx264", "-preset", "ultrafast", "-c:a", "aac", "-shortest", path.join(aSegs, "i1.mp4"), ]); writeFileSync(path.join(aSegs, "i1.cut.json"), JSON.stringify({ version: 1, id: "i1", framing: segmentFraming(RENDER) }) + "\n"); for (const f of ["i1.mp4", "i1.cut.json"]) linkSync(path.join(aSegs, f), path.join(bSegs, f)); const manifestPath = path.join(b, "video.manifest.json"); writeFileSync(manifestPath, JSON.stringify({ slug: "links", title: "Links", provenance: PROV, render: RENDER, timeline: [ { id: "i1", type: "image", src: "stills/one.png", seconds: 1 }, { id: "i2", type: "image", src: "stills/two.png", seconds: 1 }, ], })); return { a, b, aSegs, bSegs, manifestPath }; } // The build's events, read off its progress hook; a failure is returned, not thrown. async function quietly(fn) { const events = []; setProgressMode((e) => events.push(e)); try { return { result: await fn().catch((err) => ({ err })), events }; } finally { setProgressMode("human"); } } test("--chrome-only builds a MISSING segment from the cache and never writes an EXISTING hard-linked one", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-links-")); const env = process.env.HYPERFRAMES_BIN; t.after(() => { if (env === undefined) delete process.env.HYPERFRAMES_BIN; else process.env.HYPERFRAMES_BIN = env; rmSync(root, { recursive: true, force: true }); }); const { aSegs, bSegs, manifestPath } = twoTakes(root); // The renderer refuses: the run stops at the deck's render, after every // segment it was going to build is built -- which is all this test reads. const stub = path.join(root, "hf-refuses.sh"); writeFileSync(stub, "#!/bin/sh\nexit 1\n"); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; const before = fingerprint(path.join(aSegs, "i1.mp4")); const beforeRec = fingerprint(path.join(aSegs, "i1.cut.json")); assert.equal(before.nlink, 2, "A's segment has B's second name"); const { result, events } = await quietly(() => buildVideo({ manifestPath, opts: { chromeOnly: true } })); assert.ok(result?.err, "the stub renderer stops the run"); assert.ok(events.some((e) => e.ev === "chrome" && e.phase === "schedule"), `stopped past the schedule: ${result.err.message}`); // The missing one was built, as a file of B's own, for this cut's layout. assert.ok(events.some((e) => e.ev === "note" && /--chrome-only: i2's segment is missing — building it from what is cached/.test(e.message))); assert.ok(events.some((e) => e.ev === "segment" && e.id === "i2")); const built = statSync(path.join(bSegs, "i2.mp4")); assert.ok(built.size > 0); assert.equal(built.nlink, 1); assert.deepEqual(JSON.parse(readFileSync(path.join(bSegs, "i2.cut.json"), "utf8")).framing, segmentFraming(RENDER)); assert.equal(existsSync(path.join(aSegs, "i2.mp4")), false); // The existing one: not a byte, not a name, not a timestamp changed. assert.deepEqual(fingerprint(path.join(aSegs, "i1.mp4")), before); assert.deepEqual(fingerprint(path.join(aSegs, "i1.cut.json")), beforeRec); assert.equal(statSync(path.join(bSegs, "i1.mp4")).ino, before.ino, "B still names A's file: nothing was rebuilt in its place"); // Nothing was built for i1. assert.equal(events.some((e) => e.ev === "segment" && e.id === "i1"), false); }); test("--chrome-preview still refuses a missing segment, by name", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-links-pv-")); t.after(() => rmSync(root, { recursive: true, force: true })); const { bSegs, manifestPath } = twoTakes(root); const { result } = await quietly(() => buildVideo({ manifestPath, opts: { chromePreview: { at: 0, dur: 1 } } })); assert.match(String(result?.err?.message), /--chrome-preview needs .*i2\.mp4, which is missing — run a full build first/); assert.equal(existsSync(path.join(bSegs, "i2.mp4")), false); }); test("--only still builds that one segment -- as this take's own file, the other take's untouched", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-links-only-")); t.after(() => rmSync(root, { recursive: true, force: true })); const { aSegs, bSegs, manifestPath } = twoTakes(root); const before = fingerprint(path.join(aSegs, "i1.mp4")); const beforeRec = fingerprint(path.join(aSegs, "i1.cut.json")); const { result } = await quietly(() => buildVideo({ manifestPath, opts: {}, only: "i1" })); assert.equal(result.err, undefined, result.err?.message); assert.equal(result.out, path.join(bSegs, "i1.mp4")); const mine = statSync(path.join(bSegs, "i1.mp4")); assert.notEqual(mine.ino, before.ino); assert.equal(mine.nlink, 1); assert.equal(statSync(path.join(bSegs, "i1.cut.json")).nlink, 1); // A's: the same bytes and inode; one name now, its own. const after = fingerprint(path.join(aSegs, "i1.mp4")); assert.deepEqual({ ...after, nlink: 2 }, before); assert.equal(after.nlink, 1); assert.equal(fingerprint(path.join(aSegs, "i1.cut.json")).sha, beforeRec.sha); }); test("a teaser re-encoded for changed words replaces a hard-linked segment instead of writing through it", { skip: !have && "needs ffmpeg and ffprobe" }, async (t) => { const root = mkdtempSync(path.join(tmpdir(), "rtv-links-teaser-")); const env = process.env.HYPERFRAMES_BIN; t.after(() => { if (env === undefined) delete process.env.HYPERFRAMES_BIN; else process.env.HYPERFRAMES_BIN = env; rmSync(root, { recursive: true, force: true }); }); // A renderer that writes the frames the composition declares (real PNGs, // so the encode can read them), as the e2e fixture's stub does. const stub = path.join(root, "hf-stub.mjs"); writeFileSync(stub, `#!/usr/bin/env node import { spawnSync } from "node:child_process"; import { 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, 1920])[1]); const h = Number((/data-height="(\\d+)"/.exec(html) ?? [0, 1080])[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]); for (let i = 1; i <= Math.round(dur * fps); i += 1) copyFileSync(one, out + "/frame_" + String(i).padStart(6, "0") + ".png"); `); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; const a = path.join(root, "take-a", "out", "sourced", "segments"); const bDir = path.join(root, "take-b"); const b = path.join(bDir, "out", "sourced", "segments"); mkdirSync(a, { recursive: true }); mkdirSync(b, { recursive: true }); // A's teaser segment and its record, linked into B. B's words differ, so // its key does not match A's record and the teaser is encoded again. writeFileSync(path.join(a, "t1.mp4"), "take A's teaser"); writeFileSync(path.join(a, "t1.teaser.json"), JSON.stringify({ key: "a-key", frames: "a-frames", hits: 0, transition: 0 }) + "\n"); for (const f of ["t1.mp4", "t1.teaser.json"]) linkSync(path.join(a, f), path.join(b, f)); const manifestPath = path.join(bDir, "video.manifest.json"); const entry = { id: "t1", type: "teaser", lines: ["Take B", "Soon"] }; const render = { ...RENDER, chrome: undefined }; writeFileSync(manifestPath, JSON.stringify({ slug: "teaser", title: "T", provenance: PROV, render, timeline: [entry] })); const { result } = await quietly(() => buildTeaserSegment(entry, { manifestPath, render, outDir: path.join(bDir, "out", "sourced"), variant: "sourced", transition: 0 }), ); assert.equal(result?.err, undefined, result?.err?.message); // A's files: as they were, one name each now. assert.equal(readFileSync(path.join(a, "t1.mp4"), "utf8"), "take A's teaser"); assert.equal(JSON.parse(readFileSync(path.join(a, "t1.teaser.json"), "utf8")).key, "a-key"); assert.equal(statSync(path.join(a, "t1.mp4")).nlink, 1); // B's: a new segment and record of its own, and no temp file left beside them. assert.notEqual(statSync(path.join(b, "t1.mp4")).ino, statSync(path.join(a, "t1.mp4")).ino); assert.ok(statSync(path.join(b, "t1.mp4")).size > 1000); assert.notEqual(JSON.parse(readFileSync(path.join(b, "t1.teaser.json"), "utf8")).key, "a-key"); const { readdirSync } = await import("node:fs"); assert.deepEqual(readdirSync(b).sort(), ["t1.mp4", "t1.teaser.json"]); }); test("detachSegmentFiles: unlinks an entry's linked files (hard or symbolic), never a file with one name", () => { const root = mkdtempSync(path.join(tmpdir(), "rtv-detach-")); try { const other = path.join(root, "other"); const segs = path.join(root, "segs"); mkdirSync(other); mkdirSync(segs); writeFileSync(path.join(other, "c1.mp4"), "other take's picture"); writeFileSync(path.join(other, "c1.attrib.txt"), "other take's line"); linkSync(path.join(other, "c1.mp4"), path.join(segs, "c1.mp4")); symlinkSync(path.join(other, "c1.attrib.txt"), path.join(segs, "c1.attrib.txt")); writeFileSync(path.join(segs, "c1.cut.json"), "mine"); writeFileSync(path.join(other, "c10.mp4"), "x"); linkSync(path.join(other, "c10.mp4"), path.join(segs, "c10.mp4")); return detachSegmentFiles(segs, "c1").then((gone) => { assert.deepEqual(gone.sort(), ["c1.attrib.txt", "c1.mp4"]); assert.equal(existsSync(path.join(segs, "c1.mp4")), false); assert.equal(existsSync(path.join(segs, "c1.attrib.txt")), false); assert.equal(readFileSync(path.join(segs, "c1.cut.json"), "utf8"), "mine", "a file with one name stays"); assert.equal(readFileSync(path.join(other, "c1.mp4"), "utf8"), "other take's picture"); assert.equal(readFileSync(path.join(other, "c1.attrib.txt"), "utf8"), "other take's line"); assert.equal(statSync(path.join(segs, "c10.mp4")).nlink, 2, "another entry's files are not this entry's"); }).finally(() => rmSync(root, { recursive: true, force: true })); } catch (err) { rmSync(root, { recursive: true, force: true }); throw err; } });