// `umtool posts add` (2026-10-25): a post card from the corpus's posts archive, // its screenshot copied beside the manifest, in through upsertPost. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, readdir, rm, stat, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-posts-cli-")); const REPORTS = path.join(ROOT, "reports"); const CHANNELS = path.join(ROOT, "channels"); process.env.REPORTS_DIR = REPORTS; const { deckPlatformOf, findPostRow, postEntryOf, postsCommand, shotHome, snowflakeMonth } = await import("./postsCli.mjs"); // 2110480074398969914 was posted 2026-10-14 (its snowflake says so). const XID = "2110480074398969914"; const ROW = { id: XID, channelSlug: "acct-X", author: "Acct", authorName: "Acct", createdAt: "2026-10-14T21:17:15.000Z", uploadDate: "20261014", text: "Adjusted his meds", url: `https://x.com/Acct/status/${XID}`, platform: "twitter", }; async function setup() { const posts = path.join(CHANNELS, "acct-X", "posts"); await mkdir(posts, { recursive: true }); // An older copy of the row in the same shard: the last line wins. await writeFile(path.join(posts, "2026-10.jsonl"), [JSON.stringify({ ...ROW, text: "old" }), "{torn", JSON.stringify(ROW), ""].join("\n")); await writeFile(path.join(posts, "2026-09.jsonl"), JSON.stringify({ ...ROW, id: "2100000000000000000", text: "no shot" }) + "\n"); const media = path.join(CHANNELS, "acct-X", "posts-media", XID); await mkdir(media, { recursive: true }); await writeFile(path.join(media, "shot.png"), "PNG"); await writeFile(path.join(media, "capture.json"), '{"version":1}'); await writeFile(path.join(media, `${XID}_1.jpg`), "JPG"); await writeFile(path.join(media, `${XID}_2.mp4`), "MP4"); const bsky = path.join(CHANNELS, "pirate-bsky"); await mkdir(path.join(bsky, "posts"), { recursive: true }); await writeFile(path.join(bsky, "posts", "2026-09.jsonl"), JSON.stringify({ id: "3mwrc2zzvdk2u", author: "pirate.live", authorName: "Pirate", createdAt: "2026-09-30T21:30:27.487Z", text: "update", url: "https://bsky.app/profile/pirate.live/post/3mwrc2zzvdk2u", platform: "bluesky" }) + "\n"); await mkdir(path.join(bsky, "posts-media", "3mwrc2zzvdk2u"), { recursive: true }); await writeFile(path.join(bsky, "posts-media", "3mwrc2zzvdk2u", "shot.png"), "PNG"); } 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, provenance: { siteOrigin: "https://example.test", channelSlug: "chan", channelsDir: CHANNELS }, render: { width: 1920, height: 1080, fps: 30 }, timeline: [{ type: "clip", id: "c01", video: "v1", start: 10, end: 20 }], posts: [{ id: "x-1", platform: "x", author: "Real Name", handle: "Acct", date: "2026-08-24", text: "earlier", url: "https://x.com/Acct/status/1" }], }, null, 2) + "\n"); return dir; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const runner = () => { const lines = []; return { lines, run: (...args) => postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(`ERR ${s}`) }) }; }; await setup(); test.after(() => rm(ROOT, { recursive: true, force: true })); test("snowflakeMonth: an X id names its month; nothing else does", () => { assert.equal(snowflakeMonth(XID), "2026-10"); assert.equal(snowflakeMonth("3mwrc2zzvdk2u"), null); assert.equal(snowflakeMonth("12"), null); assert.equal(deckPlatformOf("twitter"), "x"); assert.equal(deckPlatformOf("bluesky"), "bluesky"); assert.equal(deckPlatformOf("forum"), "web"); }); test("findPostRow: the last line with the id; a torn line is skipped; a missing post is null", async () => { const hit = await findPostRow(CHANNELS, "acct-X", XID); assert.equal(hit.shard, "2026-10.jsonl"); assert.equal(hit.row.text, "Adjusted his meds"); const miss = await findPostRow(CHANNELS, "acct-X", "999"); assert.equal(miss.row, null); assert.equal(miss.shards, 2); }); test("postEntryOf: the deck's shape; the author an earlier post by the handle carries", () => { const e = postEntryOf(ROW, { slug: "acct-X", postId: XID, manifest: { posts: [{ platform: "x", handle: "acct", author: "Real Name" }] }, attach: "c01", flag: "Same day" }); assert.deepEqual(e, { id: `x-${XID}`, platform: "x", author: "Real Name", handle: "Acct", date: "2026-10-14T21:17:15.000Z", url: ROW.url, attachTo: "c01", text: "Adjusted his meds", shot: `posts-media/acct-X/${XID}/shot.png`, flag: "Same day", }); assert.equal(postEntryOf(ROW, { slug: "acct-X", postId: XID }).author, "Acct"); assert.equal(postEntryOf(ROW, { slug: "acct-X", postId: XID, author: "Given" }).author, "Given"); }); test("posts add: the entry through upsertPost, the capture's pictures copied (not its video); replace keeps attachTo", async () => { const dir = await project("add"); const { lines, run } = runner(); assert.equal(await run("add", "add", `acct-X/${XID}`, "--attach", "c01", "--flag", "Same day"), 0); const m = await read(dir); const p = m.posts.find((x) => x.id === `x-${XID}`); assert.equal(p.author, "Real Name"); assert.equal(p.attachTo, "c01"); assert.equal(p.shot, `posts-media/acct-X/${XID}/shot.png`); assert.deepEqual((await readdir(path.join(dir, "posts-media", "acct-X", XID))).sort(), [`${XID}_1.jpg`, "capture.json", "shot.png"]); assert.ok((await readdir(path.join(dir, "revisions"))).some((n) => n.endsWith("-auto-before-post.manifest.json"))); // Again, no --attach: replaced, its clip kept; nothing to copy. assert.equal(await run("add", "add", `acct-X/${XID}`, "--json"), 0); const res = JSON.parse(lines.at(-1)); assert.equal(res.created, false); assert.deepEqual(res.copied, []); assert.equal(res.post.attachTo, "c01"); // A bluesky post: bsky-. assert.equal(await run("add", "add", "pirate-bsky/3mwrc2zzvdk2u"), 0); assert.ok((await read(dir)).posts.some((x) => x.id === "bsky-3mwrc2zzvdk2u" && x.platform === "bluesky" && x.author === "Pirate")); }); test("posts add refuses: no screenshot (naming the capture), no such post, no such clip, a bad ref", async () => { const dir = await project("refuse"); const before = await read(dir); const { lines, run } = runner(); assert.equal(await run("add", "refuse", "acct-X/2100000000000000000"), 1); assert.match(lines.at(-1), /no screenshot/); assert.match(lines.at(-1), /pnpm ops capture-posts --json '\{"slug":"acct-X","ids":\["2100000000000000000"\]\}'/); assert.equal(await run("add", "refuse", "acct-X/42"), 1); assert.match(lines.at(-1), /has no post 42/); assert.equal(await run("add", "refuse", `acct-X/${XID}`, "--attach", "nope"), 2); assert.match(lines.at(-1), /no clip with that id/); assert.equal(await run("add", "refuse", "nonsense"), 2); assert.equal(await run("add"), 2); assert.deepEqual(await read(dir), before); assert.equal(await stat(path.join(dir, "posts-media")).catch(() => null), null); }); test("posts add never copies through a link out of the project: linked captures are used, anything else refused", async () => { const dir = await project("linked"); await mkdir(path.join(dir, "posts-media"), { recursive: true }); await symlink(path.join(CHANNELS, "acct-X", "posts-media"), path.join(dir, "posts-media", "acct-X")); const { lines, run } = runner(); assert.equal(await run("add", "linked", `acct-X/${XID}`, "--json"), 0); const res = JSON.parse(lines.at(-1)); assert.equal(res.shot.via, "linked"); assert.deepEqual(res.copied, []); // The channel's capture dir is untouched (nothing written through the link). assert.deepEqual((await readdir(path.join(CHANNELS, "acct-X", "posts-media", XID))).sort(), [`${XID}_1.jpg`, `${XID}_2.mp4`, "capture.json", "shot.png"]); const other = await project("elsewhere"); const away = path.join(ROOT, "away"); await mkdir(away, { recursive: true }); await mkdir(path.join(other, "posts-media"), { recursive: true }); await symlink(away, path.join(other, "posts-media", "acct-X")); assert.equal(await run("add", "elsewhere", `acct-X/${XID}`), 1); assert.match(lines.at(-1), /leads out of elsewhere/); assert.deepEqual(await readdir(away), []); assert.equal(await shotHome(other, path.join(other, "posts-media", "fresh", "1"), path.join(CHANNELS, "acct-X", "posts-media")), "project"); }); test("posts add --thread: the entry asks for the thread shot, copied beside its shot; with none yet it says which capture takes it", async () => { // A reply with a thread shot, and one without. const RID = "2114214000653414579"; const NID = "2114580970087108886"; const posts = path.join(CHANNELS, "acct-R", "posts"); await mkdir(posts, { recursive: true }); const row = (id, text) => JSON.stringify({ ...ROW, id, channelSlug: "acct-R", text, url: `https://x.com/Acct/status/${id}`, createdAt: "2026-10-25T04:34:32.000Z" }); await writeFile(path.join(posts, "2026-10.jsonl"), [row(RID, "@someone Well.."), row(NID, "@other He eats raw"), ""].join("\n")); for (const id of [RID, NID]) { await mkdir(path.join(CHANNELS, "acct-R", "posts-media", id), { recursive: true }); await writeFile(path.join(CHANNELS, "acct-R", "posts-media", id, "shot.png"), "PNG"); } await writeFile(path.join(CHANNELS, "acct-R", "posts-media", RID, "shot-thread.png"), "THREAD"); const dir = await project("thread"); const { lines, run } = runner(); assert.equal(await run("add", "thread", `acct-R/${RID}`, "--thread", "--json"), 0); const res = JSON.parse(lines.at(-1)); assert.equal(res.post.thread, true); assert.equal(res.threadShot, true); assert.ok(res.copied.includes("shot-thread.png"), "the thread shot is copied in with the shot"); assert.equal(await readFile(path.join(dir, "posts-media", "acct-R", RID, "shot-thread.png"), "utf8"), "THREAD"); assert.equal(await run("add", "thread", `acct-R/${NID}`, "--thread"), 0); assert.ok(lines.some((l) => /no shot-thread\.png yet/.test(l))); assert.ok(lines.some((l) => l.includes(`"thread":true`) && l.includes("capture-posts"))); const m = await read(dir); assert.equal(m.posts.find((p) => p.id === `x-${NID}`).thread, true, "asked for: drawn once a capture takes it"); // Replaced without --thread: the choice is kept, as attachTo is. assert.equal(await run("add", "thread", `acct-R/${RID}`, "--json"), 0); assert.equal(JSON.parse(lines.at(-1)).post.thread, true); }); test("posts set: the Posts table's writer -- thread on/off by the post's id or its bare id; refusals in words", async () => { const dir = await project("set"); const { lines, run } = runner(); assert.equal(await run("add", "set", `acct-X/${XID}`), 0); assert.equal(await run("set", "set", XID, "thread=on", "--json"), 0); const on = JSON.parse(lines.at(-1)); assert.equal(on.id, `x-${XID}`); assert.deepEqual(on.post, { attachTo: null, hide: false, thread: true, shotMaxHeight: null }); assert.equal(on.threadShot, false, "no thread shot beside its shot: its own is drawn"); assert.equal((await read(dir)).posts.find((p) => p.id === `x-${XID}`).thread, true); assert.equal(await run("set", "set", `x-${XID}`, "thread=off", "hide=on", "attach=c01"), 0); const off = (await read(dir)).posts.find((p) => p.id === `x-${XID}`); assert.equal("thread" in off, false, "off removes the key"); assert.equal(off.hide, true); assert.equal(off.attachTo, "c01"); assert.equal(await run("set", "set", XID, "thread=maybe"), 2); assert.match(lines.at(-1), /thread= takes on or off/); assert.equal(await run("set", "set", "nope", "thread=on"), 2); assert.match(lines.at(-1), /no post nope/); assert.equal(await run("set", "set", XID, "attach=c99"), 1); assert.match(lines.at(-1), /refused/); // The build's check: a post with no shot cannot ask for a thread shot. assert.equal(await run("set", "set", "x-1", "thread=on"), 1); assert.match(lines.at(-1), /thread draws the thread shot beside the post's shot/); });