// `umtool posts repeats` / `posts combo` (postComboCli.mjs) and their writers // (manifest.mjs updatePostCombo, updatePostComboRender): the repeats listed in // the cut's order with the capture-only posts marked; an item's badge off and // on; the cut's knobs set, refused in the build's words. import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { postsCommand } from "./postsCli.mjs"; import { attachFromCaptures, attachLines, isPathSegment, repeatsView } from "./postComboCli.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-combo-")); after(() => rm(ROOT, { recursive: true, force: true })); process.env.UMTOOL_PRINTS_CACHE = path.join(ROOT, "cache"); const CH = path.join(ROOT, "channels"); const REPORTS = path.join(ROOT, "reports"); const post = (id, media) => ({ type: "post", id: `p-${id}`, post: `obv-x/${id}`, platform: "x", author: "Mr. Obvious", handle: "ObviousRises", date: `2026-11-0${id}T10:00:00.000Z`, text: `post ${id}`, ...(media ? { media: [{ src: `posts-media/obv-x/${id}/${id}_1.mp4`, kind: "gif", seconds: 2, width: 498, height: 280 }] } : {}), }); async function project(name) { const dir = path.join(REPORTS, name); for (const id of ["1", "2", "4"]) { await mkdir(path.join(dir, "posts-media", "obv-x", id), { recursive: true }); await writeFile(path.join(dir, "posts-media", "obv-x", id, `${id}_1.mp4`), id === "4" ? "ANOTHER" : "THE-SAME-GIF"); } // 3 went in before its capture: no attachment in the cut, the same file in the corpus (the replies channel, which 4 comes from). await mkdir(path.join(CH, "obv-x-replies", "posts-media", "3"), { recursive: true }); const { createHash } = await import("node:crypto"); const sha = createHash("sha256").update("THE-SAME-GIF").digest("hex"); await writeFile(path.join(CH, "obv-x-replies", "posts-media", "3", "capture.json"), JSON.stringify({ media: [{ name: "3_1.mp4", bytes: 12, sha256: sha, url: "https://video.twimg.com/tweet_video/C.mp4" }] })); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "", channelSlug: "obv-x", channelsDir: CH }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" } }, timeline: [post("1", true), { type: "clip", id: "c01", video: "v1", start: 1, end: 3 }, post("2", true), { ...post("3", false), post: "obv-x/3" }, { ...post("4", true), post: "obv-x-replies/4" }, { ...post("5", true), media: [{ src: "posts-media/obv-x/1/1_1.mp4", kind: "gif", seconds: 2 }] }], }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const run = async (args) => { const out = []; const err = []; const code = await postsCommand(args, { root: REPORTS, out: (s) => out.push(s), err: (s) => err.push(s) }); return { code, out: out.join("\n"), err: err.join("\n") }; }; test("posts repeats: the attachment's posts in the cut's order, the badges they draw, and the post whose capture has it", async () => { const p = await project("rep"); const v = await repeatsView(p); assert.equal(v.groups.length, 1); assert.deepEqual(v.groups[0].members.map((m) => `${m.id}:${m.attached ? (m.badge ?? 1) : "-"}`), ["p-1:1", "p-2:2", "p-3:-", "p-5:3"]); const three = v.groups[0].members.find((m) => m.id === "p-3"); assert.equal(three.captureSlug, "obv-x-replies"); assert.equal(three.after, "p-2"); const r = await run(["repeats", "rep"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /1 attachment\(s\) played more than once/); assert.match(r.out, /×3\s+p-5/); assert.match(r.out, /NOT ATTACHED: its capture obv-x-replies\/posts-media\/3\/3_1\.mp4/); assert.match(r.out, /umtool posts attach rep p-3/); const j = JSON.parse((await run(["repeats", "rep", "--json", "--match", "id"])).out); assert.equal(j.match, "id"); assert.equal(j.groups.length, 0, "the GIF's ids differ: by id alone, no repeat"); assert.equal((await run(["repeats", "rep", "--match", "pixels"])).code, 2); }); test("posts combo: an item's badge off (it still counts) and back; the cut's knobs set and refused in the build's words", async () => { const p = await project("knobs"); let r = await run(["combo", "knobs", "p-2", "off"]); assert.equal(r.code, 0, r.err); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-2").combo, false); let v = await repeatsView(p); assert.deepEqual(v.groups[0].members.filter((m) => m.attached).map((m) => `${m.id}:${m.badge}${m.show ? "!" : ""}`), ["p-1:1", "p-2:2", "p-5:3!"]); r = await run(["combo", "knobs", "p-2", "on"]); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-2").combo, undefined); r = await run(["combo", "knobs", "p-5", "label=HIT", "color=#22ccff"]); assert.deepEqual((await manifest(p)).timeline.find((e) => e.id === "p-5").combo, { label: "HIT", color: "#22ccff" }); r = await run(["combo", "knobs", "p-5", "label="]); assert.deepEqual((await manifest(p)).timeline.find((e) => e.id === "p-5").combo, { color: "#22ccff" }); // The cut's. r = await run(["combo", "knobs", "minCount=3", "sfx=hit", "scale=1.5"]); assert.equal(r.code, 0, r.err); assert.deepEqual((await manifest(p)).render.posts, { combo: { minCount: 3, sfx: "hit", scale: 1.5 } }); v = await repeatsView(p); assert.deepEqual(v.groups[0].members.filter((m) => m.attached).map((m) => m.show), [false, false, true]); r = await run(["combo", "knobs", "sfx=boom"]); assert.equal(r.code, 1); assert.match(r.err, /render\.posts\.combo\.sfx must be one of off, hit, coin/); r = await run(["combo", "knobs", "minCount=", "sfx=", "scale="]); assert.equal((await manifest(p)).render.posts, undefined, "back to the defaults: no key left"); assert.equal((await run(["combo", "knobs", "p-9", "off"])).code, 2); assert.equal((await run(["combo", "knobs", "p-5", "minCount=3"])).code, 2, "minCount is the cut's"); r = await run(["combo", "knobs"]); assert.match(r.out, /label=COMBO/); }); test("posts attach: the capture's media onto the item that carries none, nothing else of it changed; then it counts", async () => { const p = await project("att"); // A real GIF in the corpus capture (it must measure), the same bytes as the cut's p-1/p-2. const { execFileSync } = await import("node:child_process"); const { createHash } = await import("node:crypto"); const capDir = path.join(CH, "obv-x-replies", "posts-media", "3"); const gif = path.join(capDir, "3_1.mp4"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=1", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-an", gif]); const body = await readFile(gif); for (const id of ["1", "2"]) await writeFile(path.join(p.dir, "posts-media", "obv-x", id, `${id}_1.mp4`), body); await writeFile(path.join(capDir, "capture.json"), JSON.stringify({ media: [{ name: "3_1.mp4", bytes: body.length, sha256: createHash("sha256").update(body).digest("hex"), url: "https://video.twimg.com/tweet_video/C.mp4" }] })); const m0 = await manifest(p); m0.timeline.find((e) => e.id === "p-3").quoted = { text: "kept", paraphrase: "kept too" }; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m0, null, 2)); const dry = JSON.parse((await run(["attach", "att", "--all", "--dry-run", "--json"])).out); assert.deepEqual(dry.attached.map((a) => a.id), ["p-3"]); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-3").media, undefined, "a dry run writes nothing"); const r = await run(["attach", "att", "p-3"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /attached the capture's media to 1 post item/); const e = (await manifest(p)).timeline.find((x) => x.id === "p-3"); assert.equal(e.media[0].src, "posts-media/obv-x-replies/3/3_1.mp4"); assert.equal(e.media[0].kind, "gif", "a tweet_video is a GIF X serves as a silent mp4"); assert.deepEqual(e.quoted, { text: "kept", paraphrase: "kept too" }); const v = await repeatsView(p); assert.deepEqual(v.groups[0].members.map((m) => `${m.id}:${m.attached ? m.badge : "-"}`), ["p-1:1", "p-2:2", "p-3:3", "p-5:4"]); assert.equal((await run(["attach", "att"])).code, 2, "names the items, or --all"); }); test("posts attach reads every channels tree: the project's first, then the archive's -- read only; a dry run says which tree each file came from", async () => { const { execFileSync } = await import("node:child_process"); const { readdir, stat } = await import("node:fs/promises"); const p = await project("trees"); // The project's own tree (a .shadow-channels) holds no capture of post 3; the ARCHIVE's does. const shadow = path.join(p.dir, ".shadow-channels"); await mkdir(path.join(shadow, "obv-x"), { recursive: true }); const archive = path.join(ROOT, "archive-channels"); const capDir = path.join(archive, "obv-x", "posts-media", "3"); await mkdir(capDir, { recursive: true }); const gif = path.join(capDir, "3_1.mp4"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=1", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-an", gif]); await writeFile(path.join(capDir, "capture.json"), JSON.stringify({ media: [{ name: "3_1.mp4", url: "https://video.twimg.com/tweet_video/C.mp4" }] })); const listing = async () => (await readdir(archive, { recursive: true })).sort().join("\n"); const archiveBefore = await listing(); const mtimeBefore = (await stat(gif)).mtimeMs; // Only the project's tree, as before: nothing to attach. assert.deepEqual((await attachFromCaptures(p, { channelsDirs: [shadow], dryRun: true })).attached, []); // Both: found in the archive, and said so. const dry = await attachFromCaptures(p, { channelsDirs: [shadow, archive], dryRun: true }); assert.deepEqual(dry.attached.map((a) => [a.id, a.treeKind, a.tree]), [["p-3", "archive", archive]]); assert.equal(dry.attached[0].media[0].from, gif); const lines = attachLines(dry, "trees"); assert.match(lines[0], /would attach the capture's media to 1 post item\(s\) in trees/); assert.ok(lines.includes(` from the archive's channels tree: ${gif}`), lines.join("\n")); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-3").media, undefined, "a dry run writes nothing"); // The project's own capture wins when both hold one. const own = path.join(shadow, "obv-x", "posts-media", "3"); await mkdir(own, { recursive: true }); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=160x90:rate=10:duration=1", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-an", path.join(own, "3_9.mp4")]); await writeFile(path.join(own, "capture.json"), JSON.stringify({ media: [{ name: "3_9.mp4", url: "https://video.twimg.com/tweet_video/D.mp4" }] })); const both = await attachFromCaptures(p, { channelsDirs: [shadow, archive], dryRun: true }); assert.deepEqual(both.attached.map((a) => [a.treeKind, a.media[0].src]), [["project", "posts-media/obv-x/3/3_9.mp4"]]); assert.ok(attachLines(both, "trees").some((l) => l.startsWith(" from the project's channels tree: "))); await rm(own, { recursive: true, force: true }); // The write: copied INTO the project; the archive is exactly as it was. const done = await attachFromCaptures(p, { channelsDirs: [shadow, archive] }); assert.deepEqual(done.attached.map((a) => a.id), ["p-3"]); assert.ok(attachLines(done, "trees").includes(` from the archive's channels tree (${archive})`)); const e = (await manifest(p)).timeline.find((x) => x.id === "p-3"); assert.equal(e.media[0].src, "posts-media/obv-x/3/3_1.mp4"); const copied = await stat(path.join(p.dir, "posts-media", "obv-x", "3", "3_1.mp4")); assert.ok(copied.isFile() && !copied.isSymbolicLink()); assert.equal(await listing(), archiveBefore, "nothing added to the archive"); assert.equal((await stat(gif)).mtimeMs, mtimeBefore, "its file untouched"); }); test("posts attach refuses a capture whose file name is not a plain name, before any file is touched -- in either tree", async () => { const { readdir } = await import("node:fs/promises"); for (const bad of ["../../x", "a/b", "..", ".", "a\\b"]) assert.equal(isPathSegment(bad), false, bad); assert.equal(isPathSegment(""), false); assert.equal(isPathSegment("3_1.mp4"), true); let n = 0; for (const bad of ["../../x", "a/b", ".."]) { for (const where of ["project", "archive"]) { n += 1; const p = await project(`bad${n}`); const shadow = path.join(p.dir, ".shadow-channels"); const archive = path.join(ROOT, `bad-archive-${n}`); const tree = where === "project" ? shadow : archive; const capDir = path.join(tree, "obv-x", "posts-media", "3"); await mkdir(capDir, { recursive: true }); await mkdir(shadow, { recursive: true }); await mkdir(archive, { recursive: true }); // A file where the name points, so only the check stands between it and a read. await writeFile(path.join(tree, "x"), "OUTSIDE"); await writeFile(path.join(capDir, "capture.json"), JSON.stringify({ media: [{ name: bad, url: "https://video.twimg.com/tweet_video/C.mp4" }] })); const before = (await readdir(p.dir, { recursive: true })).sort().join("\n"); const manifestBefore = await readFile(path.join(p.dir, "video.manifest.json"), "utf8"); for (const dryRun of [true, false]) { await assert.rejects(attachFromCaptures(p, { channelsDirs: [shadow, archive], dryRun }), (e) => { assert.match(e.message, /p-3: its capture's file name .* is not a plain name/, `${bad} in the ${where}'s tree`); return true; }); } assert.equal((await readdir(p.dir, { recursive: true })).sort().join("\n"), before, "no directory made in the project"); assert.equal(await readFile(path.join(p.dir, "video.manifest.json"), "utf8"), manifestBefore); // The CLI says it and exits 2. if (where === "project") { const m = JSON.parse(manifestBefore); m.provenance.channelsDir = shadow; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); const r = await run(["attach", `bad${n}`, "--all", "--dry-run"]); assert.equal(r.code, 2, r.out + r.err); assert.match(r.err, /is not a plain name/); } } } });