// color-range.mjs (operator, 5 Nov: "the background brightens basically exactly at the 37m mark during the // transition"): a post item with a JPEG attachment came out FULL range (yuvj420p) while every other segment is // limited, so the page -- #12100c in both -- decoded a shade apart in the master. The pin converts; the probe finds // the old ones to rebuild. import assert from "node:assert/strict"; import { execFile, execFileSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { promisify } from "node:util"; import { after, test } from "node:test"; import { LIMITED_RANGE, isFullRange } from "./color-range.mjs"; import { comboPassArgs } from "./post-combo-render.mjs"; import { postItemPlan, postLayout } from "./post-item.mjs"; import { postSegmentArgs } from "./post-render.mjs"; const exec = promisify(execFile); const ROOT = mkdtempSync(path.join(tmpdir(), "color-range-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const box = { width: 640, height: 360 }; const BG = "0x12100c"; const encode = ["-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-r", "30"]; const post = (media) => ({ type: "post", id: "p1", post: "c-x/1", platform: "x", author: "A", handle: "a", date: "2026-10-30T04:00:00Z", text: "I should shower", ...(media ? { media } : {}) }); /** The raw (unconverted) luma and chroma at the frame's top-left corner: the page. */ const cornerYuv = (file) => { const d = execFileSync("ffmpeg", ["-v", "error", "-ss", "0.5", "-i", file, "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-"], { maxBuffer: 1 << 24 }); const W = box.width, H = box.height; return [d[2 * W + 2], d[W * H + 1], d[W * H + (W * H) / 4 + 1]]; }; const segment = (e, files, name, { pin = true } = {}) => { const out = path.join(ROOT, `${name}.mp4`); let argv = postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout: postLayout(e, box), cards: { read: path.join(ROOT, "card.png") }, files, render: { fps: 30 }, outPath: out, encode }); if (!pin) argv = argv.filter((a, i) => !(a === "-color_range" || argv[i - 1] === "-color_range")); execFileSync("ffmpeg", argv); return out; }; test("the pin rides after the encode args (not a key term) in the post item's encode and the combo pass", () => { const e = post(); const argv = postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout: postLayout(e, box), cards: { read: "/c.png" }, files: [], render: { fps: 30 }, outPath: "/o.mp4", encode }); const at = argv.indexOf("-color_range"); assert.ok(at > argv.indexOf("libx264"), "after the encode"); assert.deepEqual(argv.slice(at, at + 2), [...LIMITED_RANGE]); const combo = comboPassArgs({ plain: "/p.mp4", frames: "/f%04d.png", fps: 30, at: 1, x: 0, y: 0, encode, outPath: "/o.mp4" }); assert.deepEqual(combo.slice(combo.indexOf("-color_range"), combo.indexOf("-color_range") + 2), [...LIMITED_RANGE]); }); test("a lone text card and a card with a JPEG attachment share the page colour: both limited range (ffmpeg)", async () => { execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", `color=c=${BG}:s=${box.width}x${box.height}`, "-frames:v", "1", path.join(ROOT, "card.png")]); const jpg = path.join(ROOT, "pic.jpg"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=s=320x180", "-frames:v", "1", jpg]); const pic = [{ src: "m/pic.jpg", kind: "image", width: 320, height: 180 }]; const lone = segment(post(), [], "lone"); const side = segment(post(pic), [jpg], "side"); const before = segment(post(pic), [jpg], "side-unpinned", { pin: false }); // What the bug was: without the pin, ffmpeg makes the JPEG item full range -- its page Y 16, the lone card's 30. // (ffmpeg 7.1+'s negotiation; an ffmpeg that keeps it limited has nothing to show here.) if (await isFullRange(before, { exec })) assert.notEqual(cornerYuv(before)[0], cornerYuv(lone)[0]); // Pinned: both limited, the page the same bytes (within a level of the encoder's rounding). assert.equal(await isFullRange(lone, { exec }), false); assert.equal(await isFullRange(side, { exec }), false); const [a, b] = [cornerYuv(lone), cornerYuv(side)]; for (let k = 0; k < 3; k++) assert.ok(Math.abs(a[k] - b[k]) <= 1, `page yuv: lone ${a}, with a JPEG ${b}`); assert.ok(Math.abs(a[0] - 29) <= 1, `#12100c in limited range is Y≈29 (got ${a[0]})`); }); test("the probe: full range only for pc / yuvj; a failed probe is not full range", async () => { assert.equal(await isFullRange("/nope.mp4", { exec }), false); const fake = (stdout) => async () => ({ stdout }); assert.equal(await isFullRange("x", { exec: fake(JSON.stringify({ streams: [{ pix_fmt: "yuvj420p" }] })) }), true); assert.equal(await isFullRange("x", { exec: fake(JSON.stringify({ streams: [{ pix_fmt: "yuv420p", color_range: "tv" }] })) }), false); assert.equal(await isFullRange("x", { exec: fake(JSON.stringify({ streams: [{ pix_fmt: "yuv420p" }] })) }), false); });