// post-render.mjs: a post item's encode, read as argv (no encoder runs here). import assert from "node:assert/strict"; import { test } from "node:test"; import { postItemPlan, postLayout } from "./post-item.mjs"; import { postSegmentArgs, stepCaptions } from "./post-render.mjs"; const render = { fps: 30, audioRate: 48000 }; const box = { width: 1440, height: 810 }; const entry = (o = {}) => ({ type: "post", id: "p1", post: "c-x/1", platform: "x", author: "A", handle: "a", date: "2026-10-30T04:00:00Z", text: "six words of a short post", media: [ { src: "m/v.mp4", kind: "video", seconds: 6, width: 1920, height: 1080, audio: true, start: 1, end: 4 }, { src: "m/i.jpg", kind: "image", width: 1000, height: 1000 }, ], ...o, }); const argsOf = (e) => { const plan = postItemPlan(e); const layout = postLayout(e, box); const argv = postSegmentArgs({ entry: e, box, plan, layout, cards: { read: "/c/read.png", media: "/c/media.png" }, files: ["/m/v.mp4", "/m/i.jpg"], render, place: ["pad=1920:1080:240:10:color=#000", "setsar=1", "fps=30"], encode: ["-c:v", "libx264"], outPath: "/o/p1.mp4", }); return { plan, layout, argv, graph: argv[argv.indexOf("-filter_complex") + 1] }; }; test("the render plan: the card the whole time, the video from its trim at once with its sound, the image after it, the deck's framing", () => { const { plan, argv, graph } = argsOf(entry()); // A 3 s read: the video's 3 s trim, then the image at the minimum. assert.equal(plan.total, 3 + 1.5); assert.deepEqual(argv.slice(argv.indexOf("-i") - 6, argv.indexOf("-i") + 2), ["-loop", "1", "-framerate", "30", "-t", "4.5", "-i", "/c/read.png"]); const v = argv.indexOf("/m/v.mp4"); assert.deepEqual(argv.slice(v - 5, v + 1), ["-ss", "1", "-t", "3", "-i", "/m/v.mp4"]); assert.ok(argv.includes("/m/i.jpg")); // The video plays from the first frame, beside the card. assert.match(graph, /tpad=start_duration=0:start_mode=clone/); assert.match(graph, /overlay=x=\d+:y=\d+:enable='between\(t,0,3\)'/); assert.match(graph, /overlay=x=\d+:y=\d+:enable='between\(t,3,4.5\)'/); assert.match(graph, /adelay=0\|0\[a0\]/); assert.match(graph, /amix=inputs=2:normalize=0:duration=first\[a\]/); assert.match(graph, /pad=1920:1080:240:10:color=#000,setsar=1,fps=30\[v\];/); assert.deepEqual(argv.slice(-3), ["-t", "4.5", "/o/p1.mp4"]); }); test("a muted video plays silent; a text-only post is the card alone; a long post keeps its words beside the media; pictures crossfade", () => { const muted = argsOf(entry({ media: [{ src: "m/v.mp4", kind: "video", seconds: 6, width: 1920, height: 1080, mute: true }] })); assert.match(muted.graph, /\[\d+:a\]anull\[a\]/); const text = argsOf(entry({ media: undefined })); assert.equal(text.argv.filter((a) => a === "-i").length, 2); // the card and the silence const long = Array.from({ length: 480 }, () => "word").join(" "); const side = argsOf(entry({ text: long })); assert.equal(side.layout.mode, "side"); assert.ok(!side.argv.includes("/c/media.png")); // Four pictures cycle: each after the first fades in over the one before, which stays up under it. const img = (n) => ({ src: `m/${n}.jpg`, kind: "image", width: 1000, height: 1000 }); const e = entry({ media: [img(1), img(2), img(3), img(4)] }); const plan = postItemPlan(e); const argv = postSegmentArgs({ entry: e, box, plan, layout: postLayout(e, box), cards: { read: "/c/read.png" }, files: ["/m/1.jpg", "/m/2.jpg", "/m/3.jpg", "/m/4.jpg"], render, outPath: "/o.mp4" }); const g = argv[argv.indexOf("-filter_complex") + 1]; assert.equal((g.match(/fade=t=in/g) ?? []).length, 3); assert.match(g, /fade=t=in:st=1.5:d=0.3:alpha=1/); assert.match(g, /enable='between\(t,0,1.8\)'/); }); test("a video attachment's transcript, on the item's clock", () => { const plan = postItemPlan(entry()); const caps = stepCaptions(plan.steps[0], [{ start: 0, end: 1.5, text: "before" }, { start: 1.5, end: 2.5, text: "said" }, { start: 5, end: 6, text: "after" }]); assert.deepEqual(caps.map((c) => [c.text, c.from, c.to]), [["before", 0, 0.5], ["said", 0.5, 1.5]]); });