// postReel.mjs: a posts reel from a synthesized archive -- the selection, the // entries (a repost as its original, a reply with its parents, the media // copied in and measured), one write, and a re-run that adds only what is new. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; 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 { postsReel, selectPosts, mediaKindOf } from "./postReel.mjs"; import { postsCommand } from "./postsCli.mjs"; import { removeEntry, undoStructural, updatePostItem, moveEntry } from "./manifest.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-reel-")); after(() => rm(ROOT, { recursive: true, force: true })); const CH = path.join(ROOT, "channels"); const REPORTS = path.join(ROOT, "reports"); const row = (id, o = {}) => ({ id, slug: `subj-x/${id}`, channelSlug: "subj-x", author: "Subject", authorName: "THE SUBJECT", createdAt: "2026-10-30T10:00:00.000Z", text: `post ${id}`, url: `https://x.com/Subject/status/${id}`, platform: "twitter", isReply: false, isRepost: false, engagement: { likes: 1, reposts: 0, replies: 0, quotes: 0 }, ...o, }); async function archive(slug, rows, handle = "Subject") { await mkdir(path.join(CH, slug, "posts"), { recursive: true }); await writeFile(path.join(CH, slug, "config.json"), JSON.stringify({ socialHandle: handle, platform: "twitter" })); await writeFile(path.join(CH, slug, "posts", "2026-10.jsonl"), rows.map((r) => JSON.stringify({ ...r, channelSlug: slug })).join("\n") + "\n"); } async function capture(slug, id, files) { const dir = path.join(CH, slug, "posts-media", id); await mkdir(dir, { recursive: true }); for (const [name, make] of files) make(path.join(dir, name)); await writeFile(path.join(dir, "capture.json"), JSON.stringify({ version: 1, id, media: files.map(([name]) => ({ name, url: name.endsWith(".mp4") ? "https://video.twimg.com/amplify_video/1/vid/x.mp4" : "https://pbs.twimg.com/media/x" })) })); } const ff = (args) => execFileSync("ffmpeg", ["-v", "error", "-y", ...args]); const png = (f) => ff(["-f", "lavfi", "-i", "color=c=red:s=64x48", "-frames:v", "1", f]); const mp4 = (f) => ff(["-f", "lavfi", "-i", "testsrc=s=96x64:d=1.5", "-f", "lavfi", "-i", "sine=d=1.5", "-shortest", "-c:v", "libx264", "-c:a", "aac", f]); async function project(name) { const dir = path.join(REPORTS, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "", channelSlug: "subj-x", channelsDir: CH }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" }, chrome: { engine: "hyperframes", layout: "deck", deck: { qr: { links: "original" } } } }, timeline: [{ type: "clip", id: "c01", video: "v1", start: 1, end: 3 }], }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); await archive("subj-x", [ row("100", { createdAt: "2026-10-29T10:00:00.000Z" }), row("101", { createdAt: "2026-10-30T09:00:00.000Z", mediaCount: 1 }), row("102", { createdAt: "2026-10-30T12:00:00.000Z", text: "RT @Subject: post 101", isRepost: true, repostOf: { id: "101", author: "Subject" } }), row("103", { createdAt: "2026-10-31T08:00:00.000Z", mediaCount: 1 }), row("104", { createdAt: "2026-11-02T08:00:00.000Z" }), ]); await archive("subj-x-replies", [ row("200", { author: "someone_private", authorName: "Private Person", createdAt: "2026-10-31T07:00:00.000Z", text: "is this true?" }), row("201", { createdAt: "2026-10-31T07:30:00.000Z", text: "@someone_private yes it is", isReply: true, replyTo: { id: "200", author: "someone_private" } }), row("202", { createdAt: "2026-10-31T09:00:00.000Z", text: "@gone no", isReply: true, replyTo: { id: "999", author: "gone_person" } }), ]); await capture("subj-x", "101", [["101_1.jpg", png]]); await capture("subj-x", "103", [["103_1.mp4", mp4]]); test("the selection: days inclusive, own, replies, with media, merged and de-duplicated, a repost of a selected post dropped", () => { const rows = [row("1", { createdAt: "2026-10-29T23:59:00Z" }), row("2", { createdAt: "2026-10-30T00:00:00Z", isReply: true }), row("3", { author: "other", createdAt: "2026-10-31T00:00:00Z" }), row("4", { createdAt: "2026-10-31T05:00:00Z", isRepost: true, repostOf: { id: "2" } })]; assert.deepEqual(selectPosts(rows, { since: "2026-10-30" }).map((r) => r.id), ["2", "3"]); assert.deepEqual(selectPosts(rows, { own: true, handles: ["subject"] }).map((r) => r.id), ["1", "2"]); assert.deepEqual(selectPosts(rows, { replies: "only" }).map((r) => r.id), ["2"]); assert.deepEqual(selectPosts(rows, { replies: "exclude", order: "reverse" }).map((r) => r.id), ["4", "3", "1"]); // 4 reposts 2, which is not selected assert.deepEqual(selectPosts(rows, { withMedia: true, hasMedia: (r) => r.id === "3" }).map((r) => r.id), ["3"]); assert.deepEqual(selectPosts([...rows, row("1")], { limit: 2 }).map((r) => r.id), ["1", "2"]); assert.equal(mediaKindOf("a.mp4", "https://video.twimg.com/tweet_video/x.mp4"), "gif"); assert.equal(mediaKindOf("a.jpg", null), "image"); assert.equal(mediaKindOf("a.mp4", "https://video.twimg.com/amplify_video/1/x.mp4", { audio: true, seconds: 3 }), "video"); }); test("a reel from two channels: one write after the cut's clip; media copied in and measured; a reply with its parent, blurred", async () => { const p = await project("reel-a"); const r = await postsReel(p, { channels: ["subj-x", "subj-x-replies"], since: "2026-10-30", until: "2026-10-31", own: true }); assert.deepEqual(r.entries.map((e) => e.id), ["p-101", "p-201", "p-103", "p-202"]); const m = await manifest(p); assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "p-101", "p-201", "p-103", "p-202"]); const [, e101, e201, e103, e202] = m.timeline; assert.deepEqual(e101.media.map((x) => [x.src, x.kind, x.width]), [["posts-media/subj-x/101/101_1.jpg", "image", 64]]); assert.equal(e103.media[0].kind, "video"); assert.equal(e103.media[0].audio, true); assert.ok(Math.abs(e103.media[0].seconds - 1.5) < 0.1); assert.ok((await readFile(path.join(p.dir, "posts-media/subj-x/103/103_1.mp4"))).length > 0); assert.deepEqual(e201.parents, [{ id: "200", author: "Private Person", handle: "someone_private", date: "2026-10-31T07:00:00.000Z", text: "is this true?", url: "https://x.com/Subject/status/200" }]); assert.deepEqual(e201.subjects, ["Subject"]); assert.equal(e202.replyingTo, "gone_person"); assert.equal(e101.engagement.likes, 1); // One undo point for the whole reel. assert.ok(r.result.snapshot && !r.result.snapshot.skipped); }); test("re-running adds only what is new; a repost of a post in the cut is the same post", async () => { const p = await project("reel-b"); const first = await postsReel(p, { channels: ["subj-x"], until: "2026-10-30" }); assert.deepEqual(first.entries.map((e) => e.id), ["p-100", "p-101"]); const again = await postsReel(p, { channels: ["subj-x"], until: "2026-10-30" }); assert.equal(again.added, 0); assert.equal(again.already, 2); const more = await postsReel(p, { channels: ["subj-x"] }); assert.deepEqual(more.entries.map((e) => e.id), ["p-103", "p-104"]); // A repost alone (its original not selected) is drawn as the original, reposted by the subject. const q = await project("reel-c"); const rep = await postsReel(q, { channels: ["subj-x"], since: "2026-10-30", until: "2026-10-30", limit: 1, order: "reverse" }); assert.equal(rep.entries.length, 1); assert.equal(rep.entries[0].id, "p-101"); // the repost of 101 is 101's }); test("a mixed cut's structural writes take a post item like a clip: move, remove, its knobs, undo; the CLI", async () => { const p = await project("reel-d"); await postsReel(p, { channels: ["subj-x"], since: "2026-10-31", until: "2026-10-31" }); await moveEntry(p.dir, "p-103", 0); assert.deepEqual((await manifest(p)).timeline.map((e) => e.id), ["p-103", "c01"]); await updatePostItem(p.dir, "p-103", { hold: 4, media: { 0: { start: 0.2, end: 1.2, mute: true } } }); const e = (await manifest(p)).timeline[0]; assert.equal(e.hold, 4); assert.deepEqual([e.media[0].start, e.media[0].end, e.media[0].mute], [0.2, 1.2, true]); await assert.rejects(updatePostItem(p.dir, "p-103", { media: { 0: { start: 1.1 } } }), /at least 0.5 s after start/); await assert.rejects(updatePostItem(p.dir, "c01", { hold: 3 }), /not a post item/); await removeEntry(p.dir, "p-103"); assert.deepEqual((await manifest(p)).timeline.map((x) => x.id), ["c01"]); await undoStructural(p.dir); assert.deepEqual((await manifest(p)).timeline.map((x) => x.id), ["p-103", "c01"]); // The CLI: set-item, and reel --dry-run --json. const out = []; assert.equal(await postsCommand(["set-item", "reel-d", "103", "hold=off", "media.0.trim=off"], { root: REPORTS, out: (s) => out.push(s), err: (s) => out.push(s) }), 0, out.join("\n")); const after2 = (await manifest(p)).timeline[0]; assert.equal(after2.hold, undefined); assert.equal(after2.media[0].start, undefined); const lines = []; assert.equal(await postsCommand(["reel", "reel-d", "--channel", "subj-x", "--dry-run", "--json"], { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(s) }), 0, lines.join("\n")); const j = JSON.parse(lines.join("\n")); assert.equal(j.dryRun, true); assert.deepEqual(j.entries.map((x) => x.id), ["p-100", "p-101", "p-104"]); }); test("a take's window times a post item by the duration rule, not a card's 4 s", async () => { const { windowEntries } = await import("./takeMaker.mjs"); const post = (id, words) => ({ type: "post", id, post: `subj-x/${id}`, platform: "x", date: "2026-10-30T00:00:00Z", text: Array.from({ length: words }, () => "w").join(" ") }); // 42 words: 1 + 14 = 15 s each; a 20 s window from 0 touches the first two. const tl = [post("a", 42), post("b", 42), post("c", 42), { type: "clip", id: "c01", video: "v", start: 0, end: 3 }]; assert.deepEqual(windowEntries(tl, { at: 0, dur: 20 }).ids, ["a", "b"]); assert.deepEqual(windowEntries(tl, { at: 44, dur: 2 }).ids, ["c", "c01"]); }); test("a context post's paraphrase: set and cleared by updatePostItem and posts set-item, its text never touched", async () => { const p = await project("reel-para"); const m0 = await manifest(p); const quoted = { id: "900", author: "Someone Else", handle: "someone_else", badge: "blue", date: "2026-11-02T16:21:59.000Z", text: "the long original words of the quoted post" }; const parent = { id: "901", author: "A Parent", handle: "a_parent", date: "2026-11-02T15:00:00.000Z", text: "the parent's own words" }; m0.timeline.push({ type: "post", id: "p-1", post: "subj-x/1", platform: "x", author: "Subject", handle: "Subject", date: "2026-11-02T21:19:53.000Z", text: "the post", quoted, parents: [parent] }); m0.timeline.push({ type: "post", id: "p-2", post: "subj-x/2", platform: "x", author: "Subject", handle: "Subject", date: "2026-11-02T21:19:53.000Z", text: "no context" }); await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m0, null, 2)); const item = async (id) => (await manifest(p)).timeline.find((e) => e.id === id); await updatePostItem(p.dir, "p-1", { quoted: { paraphrase: " He reviewed three videos. " }, parents: { 0: { paraphrase: "A question." } } }); let e = await item("p-1"); assert.equal(e.quoted.paraphrase, "He reviewed three videos."); assert.equal(e.quoted.text, quoted.text); assert.equal(e.parents[0].paraphrase, "A question."); assert.equal(e.parents[0].text, parent.text); // Refused in words: too long, no such context, not a knob. await assert.rejects(updatePostItem(p.dir, "p-1", { quoted: { paraphrase: "x".repeat(601) } }), /quoted\.paraphrase is longer than 600/); await assert.rejects(updatePostItem(p.dir, "p-2", { quoted: { paraphrase: "x" } }), /quotes no post/); await assert.rejects(updatePostItem(p.dir, "p-1", { parents: { 1: { paraphrase: "x" } } }), /no parent 1/); await assert.rejects(updatePostItem(p.dir, "p-1", { quoted: { text: "rewritten" } }), /not a context knob/); // An empty paraphrase (or null) clears it: the original again. await updatePostItem(p.dir, "p-1", { parents: { 0: { paraphrase: "" } } }); e = await item("p-1"); assert.equal("paraphrase" in e.parents[0], false); assert.equal(e.quoted.paraphrase, "He reviewed three videos."); // The CLI: quoted.paraphrase="…" and parent..paraphrase="…", off clears; one write each. const run = async (...pairs) => { const out = []; const code = await postsCommand(["set-item", "reel-para", "p-1", ...pairs], { root: REPORTS, out: (s) => out.push(s), err: (s) => out.push(s) }); return { code, out: out.join("\n") }; }; let r = await run("parent.0.paraphrase=Is it true? No."); assert.equal(r.code, 0, r.out); assert.match(r.out, /parent 0 \(@a_parent\): paraphrased -- "Is it true\? No\."/); assert.equal((await item("p-1")).parents[0].paraphrase, "Is it true? No."); r = await run("quoted.paraphrase=off"); assert.equal(r.code, 0, r.out); e = await item("p-1"); assert.equal("paraphrase" in e.quoted, false); assert.equal(e.quoted.text, quoted.text); // "no" is a paraphrase, not off. r = await run("quoted.paraphrase=no"); assert.equal((await item("p-1")).quoted.paraphrase, "no"); const { postItemPatchOf } = await import("./postsCli.mjs"); assert.deepEqual(postItemPatchOf(["quoted.paraphrase=a = b", "parent.1.paraphrase=", "parents.0.paraphrase=OFF"]), { quoted: { paraphrase: "a = b" }, parents: { 1: { paraphrase: null }, 0: { paraphrase: null } } }); assert.throws(() => postItemPatchOf(["quoted.text=x"]), /not a post item knob/); });