// The combo badge's pass (post-combo-render.mjs): when and where it is laid // (comboPlan), the encode (comboPassArgs), and the files -- the plain segment // kept beside it, put back when the badge goes. import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync, statSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { comboKnobs } from "./post-combo.mjs"; import { applyPostCombo, comboPassArgs, comboPlainPath, comboPlan, comboRecordPath, comboSfxFile } from "./post-combo-render.mjs"; const render = { width: 1920, height: 1080, fps: 30, palette: {} }; const entry = (media, extra = {}) => ({ type: "post", id: "p", post: "c/1", platform: "x", text: "a few words here to read", date: "2026-11-01T00:00:00Z", media, ...extra }); const gif = { src: "m/a.mp4", kind: "gif", seconds: 2, width: 498, height: 280 }; const combo = (count, extra = {}) => ({ count, media: 0, show: count >= 2, knobs: comboKnobs({}), ...extra }); test("the badge pops when its attachment starts, on its corner, for its 2.6 s (cut at the item's end)", () => { const box = { x: 160, y: 40, width: 1600, height: 900 }; const one = comboPlan(entry([gif]), combo(2), { box, render }); assert.equal(one.at, 0, "beside the card, the attachment plays from the first frame"); assert.equal(one.frames, 78); assert.ok(one.place.region.x + one.place.region.width <= box.width && one.place.region.x > box.width / 2, "top-right, inside the box"); // Narrated with the attachments after the read: it pops when the attachment does. const after = entry([gif], { narration: { script: "x", takes: [{ file: "t.wav", seconds: 4 }], pick: 0, attach: "after" } }); const late = comboPlan(after, combo(3), { box, render }); assert.ok(late && late.at > 3, `after the read (${late?.at})`); // Its second attachment: the step that plays it. const two = comboPlan(entry([{ ...gif, src: "m/b.mp4" }, gif]), combo(2, { media: 1 }), { box, render }); assert.ok(two.at > 0); assert.equal(comboPlan(entry([gif]), combo(1), { box, render }), null, "the first use draws none"); assert.equal(comboPlan(entry([gif]), { ...combo(2), show: false }, { box, render }), null); // A short item cuts the badge's life. const short = comboPlan(entry([gif], { hold: 1.5 }), combo(2), { box, render }); assert.equal(short.frames, 45); }); test("the pass: the frames over the plain segment from the attachment's start, the sound into its mix or its audio copied", () => { const a = comboPassArgs({ plain: "/s/p.plain.mp4", frames: "/c/f%04d.png", fps: 30, at: 1.5, x: 1500, y: 60, encode: ["-c:v", "libx264", "-c:a", "aac"], outPath: "/s/p.tmp.mp4" }); const fc = a[a.indexOf("-filter_complex") + 1]; assert.match(fc, /setpts=PTS-STARTPTS\+1\.5\/TB/); assert.match(fc, /overlay=x=1500:y=60:eof_action=pass/); assert.match(fc, /between\(t,1\.5,4\.1\)/); assert.deepEqual(a.slice(a.indexOf("-map"), a.indexOf("-map") + 4), ["-map", "[v]", "-map", "0:a"]); assert.deepEqual(a.slice(-3), ["-c:a", "copy", "/s/p.tmp.mp4"]); const s = comboPassArgs({ plain: "p", frames: "f", fps: 30, at: 2, x: 0, y: 0, sfx: comboSfxFile("hit"), outPath: "o" }); const fs2 = s[s.indexOf("-filter_complex") + 1]; assert.match(fs2, /adelay=2000\|2000/); assert.match(fs2, /amix=inputs=2:normalize=0:duration=first\[a\]/); assert.ok(s.includes("[a]") && !s.includes("copy")); assert.ok(existsSync(comboSfxFile("hit")) && existsSync(comboSfxFile("coin")), "the bundled sounds"); assert.equal(comboSfxFile("off"), null); }); test("a badge that goes (the count fell under minCount, turned off) puts the plain segment back", async () => { const dir = mkdtempSync(path.join(os.tmpdir(), "umtool-combo-pass-")); try { const seg = path.join(dir, "p.mp4"); writeFileSync(seg, "WITH-BADGE"); writeFileSync(comboPlainPath(seg), "PLAIN"); const st = statSync(seg); writeFileSync(comboRecordPath(seg), JSON.stringify({ key: "k", out: `${st.size}:${Math.round(st.mtimeMs)}` })); const notes = []; await applyPostCombo(seg, entry([gif]), { combo: { ...combo(2), show: false }, render, outDir: dir, box: { width: 1920, height: 1080 }, exec: async () => assert.fail("no encode"), emit: (_, f) => notes.push(f.message) }); assert.equal(readFileSync(seg, "utf8"), "PLAIN"); assert.equal(existsSync(comboRecordPath(seg)), false); assert.match(notes[0], /plain segment back/); // A fresh plain segment (post-render's) with no badge to lay is left alone. await applyPostCombo(seg, entry([gif]), { combo: combo(1), render, outDir: dir, box: { width: 1920, height: 1080 }, exec: async () => assert.fail("no encode") }); assert.equal(readFileSync(seg, "utf8"), "PLAIN"); } finally { rmSync(dir, { recursive: true, force: true }); } });