// postContext.mjs: a post item's quoted post and parent from its capture's // `context` when the archive row lacks them -- the fill rule (pure), the // snapshot when an entry is made (`posts reel`, `timeline insert post:`), and // the refresh of every entry (`umtool posts context`): one write, an undo // point, a re-run that does nothing, the posts with no context named with the // capture call that reads it. 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 { cardAuthors, validatePostItem } from "umtool-report-to-video/post-item"; import { applyCaptureContext, contextCaptureCall, contextRefOf, postsContext, readCaptureContext, refFromRow, sameRef, statusLinksOf } from "./postContext.mjs"; import { postItemFromArchive, postsReel } from "./postReel.mjs"; import { postsCommand } from "./postsCli.mjs"; import { setPostItemsContext, undoStructural } from "./manifest.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-context-")); after(() => rm(ROOT, { recursive: true, force: true })); const CH = path.join(ROOT, "channels"); const REPORTS = path.join(ROOT, "reports"); // The operator's case: his reply to Kirsche, archived with no replyTo, no // quoted and no links -- the capture's page showed both. const KIRSCHE = { id: "1983200000000000001", url: "https://x.com/Kirsche/status/1983200000000000001", handle: "Kirsche", author: "Kirsche", verified: true, badge: "blue", date: "2026-10-29T12:00:00.000Z", text: "Mr. Obvious is a leftist in disguise.", media: [], from: "above", }; const QUOTED = { id: "1983100000000000003", url: "https://x.com/quoted_person/status/1983100000000000003", handle: "quoted_person", author: "Quoted Person", date: "2026-10-28T09:00:00.000Z", text: "The receipt itself.", media: [{ kind: "image", url: "https://pbs.twimg.com/media/Q2.jpg" }], }; const CONTEXT = { readAt: "2026-11-02T20:00:00.000Z", quoted: QUOTED, parent: KIRSCHE }; const row = (id, o = {}) => ({ id, slug: `obv-x/${id}`, channelSlug: "obv-x", author: "ObviousRises", authorName: "Mr. Obvious", createdAt: "2026-10-29T14:05:00.000Z", text: `post ${id}`, url: `https://x.com/ObviousRises/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], ...o, }); async function archive(slug, rows) { await mkdir(path.join(CH, slug, "posts"), { recursive: true }); await writeFile(path.join(CH, slug, "config.json"), JSON.stringify({ socialHandle: "ObviousRises", 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, context) { const dir = path.join(CH, slug, "posts-media", id); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "capture.json"), JSON.stringify({ version: 1, id, state: "captured", mediaState: "skipped", media: [], ...(context ? { context } : {}) })); } 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: "obv-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("obv-x", [ row("300", { text: "you called me a Leftist, so here is what you said" }), // The archive names the quote but has no words for it (not archived). row("301", { createdAt: "2026-10-29T15:00:00.000Z", text: "the receipt", quoted: { platform: "twitter", id: QUOTED.id, author: "quoted_person", url: QUOTED.url } }), row("302", { createdAt: "2026-10-29T16:00:00.000Z", text: "no context captured yet" }), // The archive has this reply's parent: its own wins. row("303", { createdAt: "2026-10-29T17:00:00.000Z", author: "someone_private", authorName: "Private", text: "really?" }), row("304", { createdAt: "2026-10-29T18:00:00.000Z", text: "@someone_private yes", isReply: true, replyTo: { platform: "twitter", id: "303", author: "someone_private" } }), ]); test("the fill rule: only what is missing, a named ref only completed by the same post; the card's blur rule still decides", () => { assert.deepEqual(contextRefOf(KIRSCHE), { id: KIRSCHE.id, author: "Kirsche", handle: "Kirsche", badge: "blue", date: KIRSCHE.date, text: KIRSCHE.text, url: KIRSCHE.url, }); assert.equal(contextRefOf({ handle: "no spaces allowed", text: " " }), null); assert.equal(contextRefOf({ handle: "a", text: "x".repeat(30000) }).text.length, 10000); const bare = { type: "post", id: "p-1", handle: "ObviousRises", subjects: ["ObviousRises"] }; assert.deepEqual(applyCaptureContext(bare, CONTEXT), ["quoted", "parents"]); assert.equal(bare.parents[0].handle, "Kirsche"); assert.equal(bare.quoted.text, "The receipt itself."); // Blurred by the card's rule: neither author is the subject, and a blue check is bought. assert.deepEqual(cardAuthors(bare).map((a) => [a.where, a.redact, a.reason]), [ ["author", false, "subject"], ["parents[0]", true, "blue-check"], ["quoted", true, "private"], ]); // The archive's own: kept; completed only by the same post. const named = { quoted: { id: QUOTED.id, handle: "quoted_person", url: QUOTED.url }, parents: [{ id: "303", handle: "someone_private", text: "really?" }] }; assert.deepEqual(applyCaptureContext(named, CONTEXT), ["quoted"]); assert.equal(named.quoted.text, "The receipt itself."); assert.equal(named.quoted.author, "Quoted Person"); assert.equal(named.parents[0].id, "303", "the archive's parent stands"); const other = { quoted: { id: "1", handle: "elsewhere" } }; assert.deepEqual(applyCaptureContext(other, CONTEXT), ["parents"]); assert.deepEqual(other.quoted, { id: "1", handle: "elsewhere" }); assert.equal(sameRef({ handle: "Quoted_Person" }, { handle: "quoted_person" }), true); assert.equal(sameRef({ id: "1", handle: "a" }, { id: "2", handle: "a" }), false); // A context that read neither fills nothing. assert.deepEqual(applyCaptureContext({}, { readAt: "t" }), []); assert.deepEqual(applyCaptureContext({}, null), []); assert.equal(contextCaptureCall("obv-x", ["302"]), `pnpm ops capture-posts --json '{"slug":"obv-x","ids":["302"],"contextOnly":true}' --wait`); }); test("made with it: posts reel and timeline insert snapshot the capture's context where the archive lacks it", async () => { await capture("obv-x", "300", CONTEXT); await capture("obv-x", "301", { readAt: "t", quoted: { ...QUOTED, id: undefined } }); const p = await project("ctx-reel"); const r = await postsReel(p, { channels: ["obv-x"], until: "2026-10-29" }); assert.equal(r.added, 5); const by = new Map((await manifest(p)).timeline.filter((e) => e.type === "post").map((e) => [e.id, e])); const e300 = by.get("p-300"); assert.equal(e300.parents?.[0]?.handle, "Kirsche"); assert.equal(e300.parents?.[0]?.text, KIRSCHE.text); assert.equal(e300.quoted?.handle, "quoted_person"); assert.deepEqual(validatePostItem(e300), []); // The archive named 301's quote: completed with the words, its id kept. assert.equal(by.get("p-301").quoted.id, QUOTED.id); assert.equal(by.get("p-301").quoted.text, "The receipt itself."); assert.equal(by.get("p-301").parents, undefined); assert.equal(by.get("p-302").quoted, undefined); assert.equal(by.get("p-304").parents[0].id, "303"); const one = await postItemFromArchive(p, await manifest(p), "obv-x", "300", { id: "again-300" }); assert.equal(one.parents[0].handle, "Kirsche"); }); test("refreshed: posts context fills every entry in one write, an undo point; a re-run does nothing; no context named with its call", async () => { // A reel made before any capture read a context. await archive("obv-y", [row("400", { channelSlug: "obv-y", slug: "obv-y/400" }), row("401", { channelSlug: "obv-y", slug: "obv-y/401", createdAt: "2026-10-29T15:00:00.000Z" })]); const p = await project("ctx-refresh"); await postsReel(p, { channels: ["obv-y"] }); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-400").parents, undefined); await capture("obv-y", "400", CONTEXT); const dry = await postsContext(p, { dryRun: true }); assert.equal(dry.filled, 1); assert.equal((await manifest(p)).timeline.find((e) => e.id === "p-400").parents, undefined, "a dry run writes nothing"); const r = await postsContext(p); assert.deepEqual([r.items, r.filled, r.complete, r.noContext], [2, 1, 0, 1]); assert.deepEqual(r.report.find((x) => x.id === "p-400").filled, ["quoted", "parents"]); assert.deepEqual(r.missing, [{ slug: "obv-y", ids: ["401"], call: contextCaptureCall("obv-y", ["401"]) }]); assert.ok(r.result.snapshot?.rel, "an undo point"); const e = (await manifest(p)).timeline.find((x) => x.id === "p-400"); assert.equal(e.parents[0].handle, "Kirsche"); assert.equal(e.quoted.handle, "quoted_person"); const again = await postsContext(p); assert.deepEqual([again.filled, again.complete], [0, 1]); assert.equal(again.result, null); await undoStructural(p.dir); assert.equal((await manifest(p)).timeline.find((x) => x.id === "p-400").parents, undefined, "undone"); // The CLI. const lines = []; const code = await postsCommand(["context", p.dir], { root: REPORTS, out: (s) => lines.push(s), err: (s) => lines.push(s) }); assert.equal(code, 0, lines.join("\n")); assert.match(lines[0], /^filled the context of 1 of 2 post item\(s\)/); assert.ok(lines.some((l) => /p-400 {2}quotes @quoted_person, answers @Kirsche/.test(l)), lines.join("\n")); assert.ok(lines.some((l) => l.includes('"contextOnly":true')), lines.join("\n")); }); test("the writer: only quoted and parents, refused whole in the build's words; the reader refuses what is no post", async () => { const p = await project("ctx-writer"); await postsReel(p, { channels: ["obv-x"], until: "2026-10-29" }); await assert.rejects(setPostItemsContext(p.dir, { "p-300": { text: "rewritten" } }), /text is not a post item's context/); await assert.rejects(setPostItemsContext(p.dir, { c01: { quoted: QUOTED } }), /not a post item/); await assert.rejects(setPostItemsContext(p.dir, { "p-302": { parents: [{ handle: "bad handle!" }] } }), (e) => /handle must be a handle/.test(String(e.errors ?? e.message))); assert.equal((await manifest(p)).timeline.find((x) => x.id === "p-302").parents, undefined, "refused whole"); assert.equal(await readCaptureContext(CH, "obv-x", "../../etc"), null); assert.equal(await readCaptureContext(CH, "obv-x", "302"), null); assert.equal((await readCaptureContext(CH, "obv-x", "300")).parent.handle, "Kirsche"); }); test("A STATUS LINK IS A QUOTE: a post with no quoted post whose words link another (X's split link put back) takes it from any channel's archive (via: link); the row's threadId is its conversation", async () => { assert.deepEqual(statusLinksOf("look\nhttps://\nx.com/OtherPerson/status/7770000000000001\n…", ["https://twitter.com/a_b/status/123456789"]), [{ handle: "OtherPerson", id: "7770000000000001" }, { handle: "a_b", id: "123456789" }]); assert.deepEqual(statusLinksOf("no links here"), []); assert.equal(refFromRow({ id: "1", author: "x_y", authorName: "X Y", createdAt: "2026-10-01T00:00:00.000Z", text: "hi", url: "https://x.com/x_y/status/1" }).via, "link"); await archive("other-x", [row("7770000000000001", { channelSlug: "other-x", slug: "other-x/7770000000000001", author: "OtherPerson", authorName: "Other Person", text: "The post he linked to." })]); await archive("obv-z", [ row("500", { channelSlug: "obv-z", slug: "obv-z/500", text: "Read this\nhttps://\nx.com/OtherPerson/status/7770000000000001\n…", threadId: "499" }), row("501", { channelSlug: "obv-z", slug: "obv-z/501", createdAt: "2026-10-29T15:00:00.000Z" }), ]); const p = await project("ctx-link"); await postsReel(p, { channels: ["obv-z"] }); const r = await postsContext(p); const x = r.report.find((y) => y.id === "p-500"); assert.deepEqual(x.filled, ["quoted", "conversation"]); const e = (await manifest(p)).timeline.find((y) => y.id === "p-500"); assert.deepEqual([e.quoted.handle, e.quoted.text, e.quoted.via, e.conversation], ["OtherPerson", "The post he linked to.", "link", "499"]); assert.deepEqual(validatePostItem(e), []); assert.equal(r.report.find((y) => y.id === "p-501").state, "no-context"); // Re-run: nothing more to fill. assert.equal((await postsContext(p)).filled, 0); assert.match(validatePostItem({ ...e, conversation: 5 }).join(" "), /conversation must be/); assert.match(validatePostItem({ ...e, quoted: { ...e.quoted, via: "guess" } }).join(" "), /via must be "link"/); });