// post-conversation.mjs: THE THREAD WALK -- the post items of one conversation // played back to back are one scene. The fixture is the operator's tree // (p-2116779173746491646's, 4 Nov): his root post, his own reply under it (A), // a stranger's answer to A with his reply (C), and a second stranger's answer // to the root with his reply (B) -- the sibling branch. import assert from "node:assert/strict"; import { test } from "node:test"; import { postCardHtml, postItemPlan } from "./post-item.mjs"; import { followPath, followStateAt } from "./read-follow.mjs"; import { THREAD_DEFAULTS, threadGeoAt, threadLayout, threadReadErrors, validateThreadReads } from "./post-thread.mjs"; import { ancestorsOf, chainOf, conversationRuns, conversations, gatheredOrder, planContext, treeOf, treeRank } from "./post-conversation.mjs"; const ROOT = { id: "771", author: "Subject", handle: "subject", date: "2026-11-01T06:00:00.000Z", text: "Many have questioned my methods in dealing with the whole mess.\n\nThey are all jumping for joy because the moment they have it, it is already too late." }; const A_TEXT = "I can't help but feel like I've failed, everyone gave them exactly what they wanted.\n\nRegardless of what happens now, I know I'm going to blame myself."; const FOX = { id: "834", author: "Fox", handle: "fox_one", date: "2026-11-01T07:00:00.000Z", text: "Far as people should be concerned you are no better than the people you criticised." }; const C_TEXT = "As far as people are concerned you're nobody at all.\n\nIn short, go away you worthless loser."; const DRAC = { id: "775", author: "Drac", handle: "drac_two", date: "2026-11-01T06:10:00.000Z", text: "Oh please you were not going to affect anything on this.\n\nYou don't fight them by drawing attention to it, that is the last thing you want to do." }; const B_TEXT = "I have fought them though.\n\nWho are you to speak about what fighting them is like."; const post = (id, text, o = {}) => ({ type: "post", id: `p-${id}`, post: `acct-x/${id}`, platform: "x", author: "Subject", handle: "subject", date: o.date ?? "2026-11-01T06:20:00.000Z", text, ...o }); /** A take as the batch assembles one: pieces in order (para = piece * 100 + paragraph), each paragraph a part. */ function take(pieces) { const parts = []; let at = 0; pieces.forEach((text, pi) => { text.split(/\n\n/).forEach((para, k) => { const seconds = Math.round((para.split(/\s+/).length / 3) * 100) / 100; at += parts.length ? (k === 0 ? 0.8 : 0.6) : 0; parts.push({ n: parts.length + 1, para: pi * 100 + k, text: para, start: Math.round(at * 100) / 100, seconds }); at += seconds; }); }); return { file: "narration/posts/x/take-1.orpheus-thread.wav", engine: "orpheus", voice: "leo+zac", seconds: Math.round(at * 100) / 100, script: pieces.join("\n\n"), parts }; } const narrate = (e, pieces) => ({ ...e, narration: { script: e.text, takes: [take(pieces)], pick: 0 } }); const A = narrate(post("776", A_TEXT, { date: "2026-11-01T06:20:00.000Z", parents: [ROOT] }), [A_TEXT]); const B = narrate(post("779", B_TEXT, { date: "2026-11-01T06:27:00.000Z", parents: [DRAC, ROOT] }), [DRAC.text, B_TEXT]); const X = narrate(post("831", "Something else entirely, another conversation.", { date: "2026-11-01T06:40:00.000Z", parents: [{ id: "825", handle: "saber", text: "A different question about a different thing." }] }), ["Something else entirely, another conversation."]); const C = narrate(post("848", C_TEXT, { date: "2026-11-01T07:10:00.000Z", parents: [FOX, { id: "776", author: "Subject", handle: "subject", date: A.date, text: A_TEXT }] }), [FOX.text, C_TEXT]); const render = { postNarration: { lead: 0.4 } }; test("identity: an item's chain (oldest first), the tree its chains make, its ancestors and the depth-first order -- by first sight, or by date (a branch's date its earliest item's)", () => { assert.deepEqual(chainOf(C), ["776", "834", "848"]); assert.deepEqual(chainOf({ ...A, originalId: "999" }), ["771", "999"], "a repost draws its original"); const tree = treeOf([A, B, C]); assert.deepEqual(tree.roots, ["771"]); assert.deepEqual(ancestorsOf(tree, "848"), ["834", "776", "771"]); assert.deepEqual([...treeRank(tree, "seen").keys()], ["771", "776", "834", "848", "775", "779"]); // By date: the root's branches by their earliest ITEM -- his own reply (06:20) before the stranger's branch (06:27), // though the stranger's post itself (06:10) is older. assert.deepEqual([...treeRank(tree, "date", ["776", "779", "848"]).keys()], ["771", "776", "834", "848", "775", "779"]); }); test("the runs: consecutive narrated, read-following post items of one tree; another conversation between them splits them", () => { const r = conversationRuns([A, B, X, C], render); assert.deepEqual(r.runOf, [0, 0, 1, 2]); const g = conversationRuns([A, C, B, X], render); assert.deepEqual(g.runOf, [0, 0, 0, 1]); // Un-narrated or follow off: in no run. assert.deepEqual(conversationRuns([A, { ...C, follow: false }], render).runOf, [0, -1]); }); test("THE CUT's CONVERSATIONS: the tree, its items' places, split or one run, tree order, a reply placed before what it answers, a re-read", () => { const [c] = conversations([A, B, X, C], render); assert.equal(c.root, "771"); assert.deepEqual(c.items.map((it) => [it.id, it.at]), [["p-776", 0], ["p-779", 1], ["p-848", 3]]); assert.equal(c.split, true); assert.deepEqual(c.order, ["p-776", "p-848", "p-779"]); assert.deepEqual(c.misplaced, []); // His reply to Fox before his own reply A that Fox answered: misplaced. const [m] = conversations([C, A], render); assert.deepEqual(m.misplaced, [{ id: "p-848", answers: "p-776", at: 0, answersAt: 1 }]); // C reading A again (its parent:1) right after A was read: a re-read, with the fix. const again = narrate(C, [A_TEXT, FOX.text, C_TEXT]); const [rr] = conversations([A, again], render); assert.equal(rr.rereads.length, 1); assert.deepEqual([rr.rereads[0].id, rr.rereads[0].scope, rr.rereads[0].post, rr.rereads[0].by], ["p-848", "parent:1", "776", "p-776"]); assert.match(rr.rereads[0].fix, /set-item

p-848 thread\.parent\.1=context/); }); test("GATHERED in tree order (threads --write): at the first item's place, a post after what it answers, branches by date; another section's item is left", () => { const g = gatheredOrder([A, B, X, C], render); assert.deepEqual(g.ids, ["p-776", "p-848", "p-779", "p-831"]); assert.deepEqual(g.moved, [{ root: "771", from: ["p-776", "p-779", "p-848"], to: ["p-776", "p-848", "p-779"] }]); assert.deepEqual(gatheredOrder([A, C, B, X], render).moved, [], "already gathered"); const sec = new Map([["p-776", 0], ["p-779", 0], ["p-831", 0], ["p-848", 1]]); const left = gatheredOrder([A, B, X, C], render, { sectionOf: sec }); assert.deepEqual(left.left, [{ id: "p-848", root: "771", section: 1, first: "p-776" }]); assert.deepEqual(left.ids, ["p-776", "p-779", "p-831", "p-848"]); // A conversation across a boundary is gathered within each section, never across it. const two = new Map([["p-776", 0], ["p-779", 1], ["p-831", 1], ["p-848", 1]]); const within = gatheredOrder([A, B, X, C], render, { sectionOf: two }); assert.deepEqual(within.ids, ["p-776", "p-848", "p-779", "p-831"]); assert.deepEqual(within.moved, [{ root: "771", section: 1, from: ["p-779", "p-848"], to: ["p-848", "p-779"] }]); }); test("THE SCENE: each item opens on what its run left on screen -- an ancestor kept on top, the branch before collapsing away, the post the item before ended on in full", () => { const tl = [A, C, B, X]; assert.equal(planContext(tl, 0, render).scene, null); // C after A: A's root stays on top (an ancestor of C); A's post is C's own parent (carried), not a scene tile. const c = planContext(tl, 1, render); assert.equal(c.prev, A); assert.deepEqual(c.scene.tiles.map((t) => [t.id, t.action, t.from]), [["771", "keep", "small"], ["776", "keep", "full"]]); const Lc = threadLayout(C, render, c); assert.deepEqual(Lc.tiles.map((t) => [t.scope, t.mode]), [["conv:771", "scene"], ["parent:1", "carry"], ["parent:0", "full"], ["post", "full"]]); // B after C: the root is B's own (context); C's branch (A, Fox, C) collapses -- in the tree's order, between the root and B's branch. const b = planContext(tl, 2, render); assert.deepEqual(b.scene.tiles.map((t) => [t.id, t.action, t.from]), [["771", "keep", "small"], ["776", "collapse", "small"], ["834", "collapse", "small"], ["848", "collapse", "full"]]); const Lb = threadLayout(B, render, b); assert.deepEqual(Lb.tiles.map((t) => [t.scope, t.mode, t.action ?? null]), [["parent:1", "context", null], ["conv:776", "scene", "collapse"], ["conv:834", "scene", "collapse"], ["conv:848", "scene", "collapse"], ["parent:0", "full", null], ["post", "full", null]]); // Another conversation: no scene. assert.equal(planContext(tl, 3, render).scene, null); // The schedule: the branch collapses (scale 0, opacity 0) as the stranger's post is revealed, all at the first read's lead. const plan = postItemPlan(B, render, b); const th = plan.follow.thread; const row = (sc) => th.tiles.find((t) => t.scope === sc); assert.deepEqual([row("conv:848").from, row("conv:848").to], [[1, 1], [0, 0]]); assert.deepEqual([row("conv:776").from, row("conv:776").to], [[THREAD_DEFAULTS.shrink, THREAD_DEFAULTS.dim], [0, 0]]); assert.deepEqual([row("parent:0").from, row("parent:0").to], [[1, 0], [1, 1]]); assert.equal(row("conv:848").t0, row("parent:0").t0); assert.equal(row("conv:848").ref.handle, "subject"); assert.deepEqual(threadReadErrors(B, render, b), []); assert.deepEqual(validateThreadReads({ render, timeline: tl }), []); }); /** A page's geometry for a plan: each tile a head and its blocks (scene tiles: a head and a paragraph). */ function fakeGeo(plan, { H = 520, line = 44 } = {}) { const blocks = {}; const tiles = {}; let y = 0; for (const t of plan.follow.thread.tiles) { const top = y; y += 70; const mine = plan.follow.blocks.filter((x) => x.scope === t.scope); if (!mine.length) y += 3 * line; for (const b of mine) { const h = Math.max(1, Math.ceil((b.to - b.from + 1) / 6)) * line; blocks[b.k] = { top: y, bottom: y + h }; y += h + line; } tiles[t.scope] = { top, height: y - top, ...(t.scope.startsWith("conv:") ? { gap: 22 } : {}) }; y += 22; } return { H, C: y, blocks, tiles }; } test("THE INVARIANT, in a scene: every part of each item's read is on screen while it is said -- its tile in full and lit, its block in the window -- and the scene's posts are never read", () => { const tl = [A, C, B]; for (let i = 1; i < tl.length; i += 1) { const e = tl[i]; const ctx = planContext(tl, i, render); const plan = postItemPlan(e, render, ctx); const geo = fakeGeo(plan); const path = followPath(plan.follow, geo); const t = e.narration.takes[0]; const settle = Math.max(THREAD_DEFAULTS.pan, THREAD_DEFAULTS.seconds) + plan.follow.ease / 2 + 0.02; t.parts.forEach((p, k) => { const blk = plan.follow.blocks.find((b) => b.part === k); assert.ok(blk && !blk.scope.startsWith("conv:"), `${e.id} part ${k + 1}: a block of its own`); const to = (t.parts[k + 1] ? plan.follow.blocks.find((b) => b.part === k + 1).start : plan.follow.readEnd) - 0.45; for (let s = blk.start + settle; s <= to; s += 0.1) { const st = followStateAt(plan.follow, path, s); assert.deepEqual(st.tiles[blk.scope].slice(0, 2), [1, 1], `${e.id} part ${k + 1} at ${s.toFixed(2)} s: its tile in full`); assert.equal(st.op[blk.k] ?? 1, 1); const g = threadGeoAt(geo, Object.fromEntries(Object.entries(st.tiles).map(([x, v]) => [x, v[0]]))); const bb = g.blocks[blk.k]; assert.ok(bb.top - st.offset >= -1 && bb.bottom - st.offset <= geo.H + 1, `${e.id} part ${k + 1} at ${s.toFixed(2)} s: in the window (${(bb.top - st.offset).toFixed(0)}–${(bb.bottom - st.offset).toFixed(0)})`); // A collapsed branch is gone (scale 0), its gap closed. for (const [sc, v] of Object.entries(st.tiles)) if (sc.startsWith("conv:") && plan.follow.thread.tiles.find((x) => x.scope === sc).action === "collapse") assert.equal(v[0], 0, `${sc} collapsed`); } }); // The opening frame is the one the item before ended on: the post it ended on in full, at the top of the window. const st0 = followStateAt(plan.follow, path, 0); const endedOn = plan.follow.thread.tiles.find((x) => x.wasPost || x.mode === "carry"); assert.ok(endedOn, `${e.id}: opens on the post the item before ended on`); assert.deepEqual(st0.tiles[endedOn.scope].slice(0, 2), [1, 1]); const g0 = threadGeoAt(geo, Object.fromEntries(Object.entries(st0.tiles).map(([x, v]) => [x, v[0]]))); const want = Math.min(Math.max(0, g0.tiles[endedOn.scope].top - Math.round(geo.H * 0.1)), Math.max(0, g0.C - geo.H)); assert.ok(Math.abs(path.start - want) < 1, `${e.id}: framed on it (${path.start} vs ${want})`); } }); test("the card page draws the scene's posts as they were -- in the tree's order, never read (no blocks, no word spans), their words redacted by their own item's terms, their authors blurred by its rule", () => { const tl = [A, C, B]; const ctx = planContext(tl, 2, render); const plan = postItemPlan(B, render, ctx); const html = postCardHtml(B, { width: 1600, height: 900, follow: true, thread: plan.follow.thread }); const at = (sc) => html.indexOf(`data-thread-slot="${sc}"`); for (const sc of ["parent:1", "conv:776", "conv:834", "conv:848", "parent:0", "post"]) assert.ok(at(sc) > 0, sc); assert.ok(at("parent:1") < at("conv:776") && at("conv:776") < at("conv:834") && at("conv:834") < at("conv:848") && at("conv:848") < at("parent:0") && at("parent:0") < at("post"), "the tree's order"); assert.match(html, /data-thread-slot="conv:848" data-thread-scene="collapse"/); const scene = html.slice(at("conv:848"), at("parent:0")); assert.doesNotMatch(scene, /data-w=|data-post-block=/, "never read"); assert.match(scene, /worthless loser/); // Redacted as published: a term of the project's, and a stranger (no badge) blurred by C's rule. const red = postCardHtml(B, { width: 1600, height: 900, follow: true, thread: plan.follow.thread, redaction: { terms: [{ term: "worthless", wholeWord: true }] } }); assert.doesNotMatch(red.slice(red.indexOf('data-thread-slot="conv:848"'), red.indexOf('data-thread-slot="parent:0"')), /worthless/); const fox = red.slice(red.indexOf('data-thread-slot="conv:834"'), red.indexOf('data-thread-slot="conv:848"')); assert.match(fox, /data-redacted=""/); assert.doesNotMatch(fox, /fox_one/); }); test("A THREAD SCENE AND A MEDIA SCENE on one item: the conversation's scene wins the card; the media scene keeps only the attachment (post-media-scene.mjs's header)", async () => { const { repeatGroups } = await import("./post-combo.mjs"); const { mediaSceneOf, sceneEntry } = await import("./post-media-scene.mjs"); const gif = (id) => [{ src: `posts-media/acct-x/${id}/${id}_1.mp4`, kind: "gif", seconds: 0.8, width: 500, height: 280 }]; // A and C, one conversation (its id snapshotted on both; C's card shows only the stranger it answers), carrying // the same GIF (the same bytes): both scenes want C's card. const a = { ...A, conversation: "771", media: gif("776") }; const c = { ...C, conversation: "771", parents: [FOX], media: gif("848") }; const tl = [a, c]; const groups = repeatGroups(tl, () => ({ sha256: "same" })); const ms = mediaSceneOf(tl, 1, groups, render); assert.ok(ms, "C is in a media scene with A"); const ctx = planContext(tl, 1, render); assert.ok(ctx.scene, "and in A's conversation's run"); const shown = sceneEntry(c, ms); assert.equal(shown.media[0].src, a.media[0].src, "the media scene keeps the attachment: the anchor's"); const plan = postItemPlan(shown, render, { ...ctx, mediaScene: ms }); const scopes = plan.follow.thread.tiles.map((t) => t.scope); assert.ok(scopes.includes("conv:776"), "the conversation's scene draws the card (A's post, collapsing)"); assert.ok(!scopes.some((sc) => sc.startsWith("scene:")), "no media-scene tile on it"); // With no conversation scene, the media scene's column is drawn as before. const alone = postItemPlan(shown, render, { prev: a, scene: null, mediaScene: ms }); assert.ok(alone.follow.thread.tiles.some((t) => t.scope.startsWith("scene:")), "the media scene's earlier message above it"); });