// THE SEGMENT CACHE KEY (post-render.mjs postSegmentKeyOf; operator, 4 Nov: "make sure you handle caching well, we may // have a couple rerenders before shipping"): every thread-scenes input that changes a post segment's pixels or timing // changes its key; an item none of them touch keeps the key it had on main (645dafb7), so `umtool take --from` reuses it. import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { test } from "node:test"; import { postItemPlan, postLayout } from "./post-item.mjs"; import { planContext } from "./post-conversation.mjs"; import { POST_SEGMENT_RV, postSegmentKey, postSegmentKeyOf } from "./post-render.mjs"; const render = { fps: 30, palette: { bg: "#111111" }, fontRegular: "/f/r.ttf", fontBold: "/f/b.ttf", postNarration: { lead: 0.25 } }; const box = { width: 1610, height: 906 }; const at = (h) => `2026-11-02T${String(h).padStart(2, "0")}:00:00.000Z`; const take = (text, role) => ({ file: "narration/posts/1/take-1.wav", engine: "orpheus", voice: "zac", seconds: 4, script: text, parts: [{ n: 1, para: 0, text, start: 0, seconds: 3.5, ...(role ? { role } : {}) }] }); const post = (id, text, o = {}) => ({ type: "post", id: `p-${id}`, post: `acct-x/${id}`, platform: "x", author: "Subject", handle: "subject", date: at(10), text, ...o }); const narrated = (e, pieces) => ({ ...e, narration: { script: e.text, takes: [{ ...take(e.text), parts: pieces.map(([t, role], k) => ({ n: k + 1, para: k * 100, text: t, start: k * 4, seconds: 3.5, role })), seconds: pieces.length * 4 }], pick: 0 } }); /** Main's key, verbatim (645dafb7 post-render.mjs buildPostSegment), for an item with no redaction, bleeps or slurs. */ function mainKey({ entry, plan, take: tk, stamps = [] }) { return createHash("sha256") .update(JSON.stringify({ v: 1, entry, box, place: [], palette: render.palette, fonts: [render.fontRegular, render.fontBold], fps: render.fps, encode: [], stamps, ...(tk ? { narration: plan.narration, postNarration: render.postNarration ?? null } : {}), ...(plan.follow ? { follow: plan.follow } : {}) })) .digest("hex"); } const keyOf = (entry, ctx = {}, extra = {}) => { const plan = postItemPlan(entry, render, ctx); return { plan, key: postSegmentKey(postSegmentKeyOf({ entry, box, render, plan, take: entry.narration ? entry.narration.takes[0] : null, layout: postLayout(entry, box), ...extra })) }; }; test("a PLAIN item (no thread, no join, no wide attachment) keeps main's key: --from reuses its segment", () => { assert.equal(POST_SEGMENT_RV, null, "no renderer salt: unchanged plans draw what they drew"); for (const e of [ post("1", "Just a post with words."), narrated(post("2", "A narrated post read on its own."), [["A narrated post read on its own.", "post"]]), post("3", "A post with its picture.", { media: [{ src: "posts-media/a/3.png", kind: "image", width: 800, height: 600 }] }), ]) { const { plan, key } = keyOf(e); assert.equal(key, mainKey({ entry: e, plan, take: e.narration ? e.narration.takes[0] : null }), e.id); } }); test("every thread-scenes input changes the key: the join, context media, a wide layout, a manual thread and its relations, the branch map, link visits and linked posts, whole-thread pieces", () => { const root = narrated(post("10", "You all have low testosterone, I become bestial.", { date: at(10) }), [["You all have low testosterone, I become bestial.", "post"]]); const reply = narrated(post("11", "We high T guys have to watch ourselves.", { date: at(12), parents: [{ id: "5", handle: "ryobi", author: "Ryobi", date: at(11), text: "Three days is enough." }, { id: "10", handle: "subject", author: "Subject", date: at(10), text: root.text }] }), [["Three days is enough.", "parent:0"], ["We high T guys have to watch ourselves.", "post"]]); const tl = [root, reply]; const base = keyOf(reply, { ...planContext(tl, 1, render), join: null }).key; const keys = new Map(); // The join: its seconds in the plan (follow.join), the item before's end in joinFrom. const j = keyOf(reply, planContext(tl, 1, render)); keys.set("join (plan)", j.key); keys.set("join (the item before's end)", keyOf(reply, planContext(tl, 1, render), { from: { entry: root, plan: postItemPlan(root, render), media: [], fadeIn: false } }).key); // A context post's media (the plan's panel, the entry's ref media). const withMedia = { ...reply, parents: [{ ...reply.parents[0], media: [{ src: "posts-media/r/5.jpg", kind: "image", width: 600, height: 800 }] }, reply.parents[1]] }; keys.set("context media", keyOf(withMedia, { ...planContext([root, withMedia], 1, render), join: null }).key); // A wide attachment: the top layout. keys.set("wide", keyOf({ ...reply, media: [{ src: "posts-media/w/1.png", kind: "image", width: 692, height: 141 }] }, { ...planContext(tl, 1, render), join: null }).key); // A manual thread: its group's stack and relations. const g = (e) => ({ ...e, thread: { group: "g" } }); const lone = narrated(post("20", "Ready for more lessons?", { handle: "movail", date: at(9) }), [["Ready for more lessons?", "post"]]); const gtl = [g(lone), g(reply)]; keys.set("manual thread", keyOf(gtl[1], { ...planContext(gtl, 1, render), join: null }).key); // The branch map off. keys.set("branch map knob", keyOf({ ...reply, thread: { map: false } }, { ...planContext(tl, 1, render), join: null }).key); // A linked post (a link visit) and its transcript. const linked = { ...reply, links: [{ id: "77", handle: "iv", author: "IV", date: at(8), text: "Ya try to be civil" }] }; keys.set("linked post", keyOf(linked, { ...planContext([root, linked], 1, render), join: null }).key); keys.set("linked transcript", keyOf({ ...linked, links: [{ ...linked.links[0], transcript: "the messages" }] }, { ...planContext([root, linked], 1, render), join: null }).key); // A whole-thread read: a third ancestor. const deeper = { ...reply, parents: [...reply.parents, { id: "3", handle: "x", author: "X", date: at(9), text: "An older post." }] }; keys.set("whole thread", keyOf(deeper, { ...planContext([root, deeper], 1, render), join: null }).key); for (const [what, k] of keys) assert.notEqual(k, base, what); assert.equal(new Set(keys.values()).size, keys.size, "each input its own key"); });