// read-follow.mjs: a post item's read-follow -- the read's parts matched to the // card's words (a long paragraph split into parts, a script that reads "this // slash that" and lower-cases a shout), the schedule on the item's clock (a // take's part starts divided by the read's rate; un-narrated, the reading // rule), the pan that keeps the block being read in view, and the card page. import assert from "node:assert/strict"; import { test } from "node:test"; import { postCardHtml, postItemPlan, validatePostItem } from "./post-item.mjs"; import { postSegmentArgs } from "./post-render.mjs"; import { speakableText, validatePostNarrationRender } from "./post-narration.mjs"; import { FOLLOW_DEFAULTS, FOLLOW_PAN, followBlockAt, followBlocks, followKnobs, followOffsetAt, followPath, followSchedule, followStateAt, followStateKey, } from "./read-follow.mjs"; import { cardWordsOf } from "./word-zoom.mjs"; const LONG_PARA = "Believe it or not, two people can read the same thing and come to a different conclusion. That doesn't make me a leftist, nor does it mean I'm pretending not to get it. " + "At first I didn't have much context but looking into things, Strawb appears to be a malicious actor and Maria appears to be malicious as well. " + "I think the entire thing was blown WAY out of proportion and a lot of people are being really obtuse/vindictive about it. " + "For example Ember did not infiltrate this group chat, it was actually Strawb who was invited later to the chat, and Ember was one of the three original members."; const TEXT = `Normally I don't respond to stuff like this.\n\nKirsche you have lost the plot.\n\n${LONG_PARA}\n\nThat is all.`; const base = (o = {}) => ({ type: "post", id: "p1", post: "obvious-x/2115851166383063324", platform: "x", author: "MR. OBVIOUS", handle: "ObviousRises", date: "2026-10-29T17:00:03.000Z", text: TEXT, ...o, }); /** A take read part by part, as the orchestrator's generator makes one: the script's parts, each timed. */ function narrated(entry, { rate } = {}) { const script = speakableText(entry.text); const paras = script.split(/\n\n/); const parts = []; let at = 0; paras.forEach((p, para) => { // The long paragraph read in two runs of sentences (as scriptParts splits one over 60 words). const runs = p.split(/ (?=At first|For example)/); for (const text of runs) { const seconds = text.split(/\s+/).length / 3; parts.push({ n: parts.length + 1, para, text, start: Math.round(at * 100) / 100, seconds: Math.round(seconds * 100) / 100 }); at += seconds + 0.5; } }); return { ...entry, narration: { script, takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: Math.round(at * 100) / 100, script, parts }], ...(rate ? { rate } : {}) }, }; } const wordsOf = (entry, b) => cardWordsOf(entry.text).slice(b.from, b.to + 1).join(" "); test("the knobs: on by default, dim 0.65, ease 0.25; the cut's, then the post's; false is off", () => { assert.deepEqual(followKnobs(base()), FOLLOW_DEFAULTS); assert.deepEqual(followKnobs(base(), { postNarration: { follow: { dim: 0.5 } } }), { on: true, dim: 0.5, ease: 0.25 }); assert.equal(followKnobs(base({ follow: false }), { postNarration: { follow: { dim: 0.5 } } }).on, false); assert.equal(followKnobs(base({ follow: { on: true } }), { postNarration: { follow: { on: false } } }).on, true); assert.deepEqual(validatePostItem(base({ follow: { dim: 0.1 } })), ["timeline p1.follow.dim must be 0.2–1 (the other blocks' opacity)"]); assert.deepEqual(validatePostItem(base({ follow: "yes" })), ["timeline p1.follow must be false (off) or { on?, dim?, ease? }"]); assert.deepEqual(validatePostItem(base({ follow: false })), []); assert.deepEqual(validatePostNarrationRender({ postNarration: { follow: { ease: 0.4, dim: 0.7 } } }), []); assert.match(validatePostNarrationRender({ postNarration: { follow: { pan: 1 } } }).join(), /follow.pan is not a follow knob/); assert.equal(postItemPlan(base({ follow: false })).follow, undefined); }); test("parts -> blocks: each part's words found on the card in order, a long paragraph split where its parts split", () => { const e = narrated(base()); const { blocks, source } = followBlocks(e); assert.equal(source, "take"); assert.equal(blocks.length, 6); assert.deepEqual(blocks.map((b) => b.part), [0, 1, 2, 3, 4, 5]); assert.equal(wordsOf(e, blocks[0]), "Normally I don't respond to stuff like this."); assert.equal(wordsOf(e, blocks[1]), "Kirsche you have lost the plot."); // The long paragraph: three blocks, each a run of its sentences, covering it without a gap. assert.match(wordsOf(e, blocks[2]), /^Believe it or not, .* pretending not to get it\.$/); assert.match(wordsOf(e, blocks[3]), /^At first .* malicious as well\. I think .*obtuse\/vindictive about it\.$/); assert.match(wordsOf(e, blocks[4]), /^For example .* original members\.$/); assert.equal(blocks[3].from, blocks[2].to + 1); assert.equal(blocks[4].from, blocks[3].to + 1); assert.equal(wordsOf(e, blocks[5]), "That is all."); assert.equal(blocks[5].to, cardWordsOf(e.text).length - 1); }); test("parts -> blocks: a parent read first (para 0) then the post (para 100+), matched by words, not by index; an unread quote is a block never lit", () => { const parent = { handle: "someone", text: "I think she's just super paranoid" }; const e = base({ text: "And that's fair.\n\nJUST keep in mind this/that is a lot.", parents: [parent], quoted: { handle: "q", text: "A quoted post nobody reads aloud." } }); e.narration = { script: "x", takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", seconds: 9, parts: [ { n: 1, para: 0, text: "I think she's just super paranoid", start: 0, seconds: 2 }, { n: 2, para: 100, text: "And that's fair.", start: 2.6, seconds: 1.5 }, { n: 3, para: 101, text: "just keep in mind this slash that is a lot!", start: 4.7, seconds: 3 }, ] }], }; const { blocks } = followBlocks(e); const by = (scope) => blocks.filter((b) => b.scope === scope); assert.deepEqual(by("parent:0").map((b) => [b.from, b.to, b.part]), [[0, 5, 0]]); assert.deepEqual(by("post").map((b) => [b.part, b.from, b.to]), [[1, 0, 2], [2, 3, 10]]); assert.deepEqual(by("quoted").map((b) => b.part), [null]); const sch = followSchedule(e); assert.deepEqual(sch.blocks.filter((b) => b.start !== null).map((b) => b.start), [0.4, 3, 5.1]); assert.equal(sch.blocks.find((b) => b.scope === "quoted").start, null); }); test("the schedule: a part starts at lead + its start in the take ÷ the read's rate", () => { const e = narrated(base(), { rate: 1.25 }); const plan = postItemPlan(e); const parts = e.narration.takes[0].parts; const f = plan.follow; assert.equal(f.blocks.length, parts.length); parts.forEach((p, i) => assert.equal(f.blocks[i].start, Math.round((0.4 + p.start / 1.25) * 1000) / 1000)); assert.equal(f.readEnd, Math.round((0.4 + e.narration.takes[0].seconds / 1.25) * 1000) / 1000); // Each block ends where the next starts, the last at the read's end. assert.equal(f.blocks[0].end, f.blocks[1].start); assert.equal(f.blocks[5].end, f.readEnd); // The cut's rate does the same. const g = postItemPlan(narrated(base()), { postNarration: { rate: 1.25 } }).follow; assert.deepEqual(g.blocks.map((b) => b.start), f.blocks.map((b) => b.start)); assert.equal(followBlockAt(f, f.blocks[3].start + 0.01), 3); assert.equal(followBlockAt(f, f.blocks[3].start - 0.2), 2); }); test("un-narrated: the card's own paragraphs and sentence runs, paced by the reading rule", () => { const e = base(); const f = postItemPlan(e).follow; assert.equal(f.source, "card"); assert.ok(f.blocks.length >= 4); assert.equal(f.blocks[0].start, 0); // The post's reading time (clamped 3-15 s) is shared by words; the read ends there. assert.equal(f.readEnd, 15); for (let i = 1; i < f.blocks.length; i += 1) assert.ok(f.blocks[i].start > f.blocks[i - 1].start); // A hold longer than the reading spreads it, less a second. assert.equal(postItemPlan(base({ hold: 61 })).follow.readEnd, 60); }); test("the state: the block being read at full, the others dimmed, eased; back to full after the read", () => { const f = postItemPlan(narrated(base())).follow; const at = (s) => followStateAt(f, null, s); const s3 = f.blocks[3].start; const mid = at((s3 + f.blocks[4].start) / 2); assert.equal(mid.current, 3); assert.equal(mid.op[3], 1); assert.equal(mid.op[2], 0.65); assert.equal(mid.op[0], 0.65); // Halfway through the ease, the two meet. const half = at(s3); assert.ok(half.op[3] > 0.65 && half.op[3] < 1 && half.op[2] > 0.65 && half.op[2] < 1); assert.equal(at(s3 + f.ease / 2 + 0.001).op[3], 1); // Before the first block's start the first is lit; after the read everything is back. assert.equal(at(0).op[0], 1); assert.equal(at(0).op[1], 0.65); const after = at(f.readEnd + f.ease + 0.01); assert.ok(Object.values(after.op).every((v) => v === 1)); // Seek-safe: the same second gives the same state, whichever came before. assert.equal(followStateKey(at(s3 + 0.1)), followStateKey(at(s3 + 0.1))); // One block read: nothing dims. const one = postItemPlan(base({ text: "Just one short line." })).follow; assert.deepEqual(followStateAt(one, null, 1).op, {}); }); test("the pan: short cards never move; a tall one keeps the block being read in view, starting before its read", () => { const f = postItemPlan(narrated(base())).follow; // Six blocks stacked 200 px apart in a flow of 1200 px. const blocks = Object.fromEntries(f.blocks.map((b, i) => [b.k, { top: i * 200, bottom: i * 200 + 180 }])); assert.deepEqual(followPath(f, { H: 1300, C: 1200, blocks }).segments, []); const geo = { H: 500, C: 1200, blocks }; const path = followPath(f, geo); assert.equal(path.max, 700); assert.ok(path.segments.length >= 2); const m = Math.round(500 * FOLLOW_PAN.margin); for (const b of f.blocks) { // Once its read is under way (past the pan), the block is inside the window. const s = Math.min(b.end - 0.01, b.start + FOLLOW_PAN.seconds); const off = followOffsetAt(path, s); const { top, bottom } = blocks[b.k]; assert.ok(top >= off - 0.5 && bottom <= off + 500 + 0.5, `block ${b.k} in view at ${s}: ${top}-${bottom} in ${off}-${off + 500}`); } // A pan starts before the block it brings into view is read. const first = path.segments[0]; const target = f.blocks.find((b) => blocks[b.k].bottom > 500 - m / 2); assert.ok(first.t0 < target.start && first.t0 >= target.start - FOLLOW_PAN.lead - 0.001); // Never past the end of the words. assert.ok(path.segments.every((g) => g.to <= 700 && g.to >= 0)); // The fading edges: more below at the start, more above at the end. const s0 = followStateAt(f, path, 0); assert.equal(s0.fadeTop, 0); assert.equal(s0.fadeBottom, 1); const end = followStateAt(f, path, f.readEnd + 1); assert.equal(end.offset, 700); assert.equal(end.fadeBottom, 0); assert.equal(end.fadeTop, 1); }); test("the pan: a block taller than the window pans on through it while it is read", () => { const f = postItemPlan(narrated(base())).follow; const blocks = Object.fromEntries(f.blocks.map((b, i) => [b.k, i === 2 ? { top: 100, bottom: 1300 } : i < 2 ? { top: i * 50, bottom: i * 50 + 40 } : { top: 1300 + (i - 2) * 50, bottom: 1340 + (i - 2) * 50 }])); const path = followPath(f, { H: 400, C: 1550, blocks }); const b = f.blocks[2]; const inside = path.segments.filter((g) => g.t0 >= b.start - FOLLOW_PAN.lead - 0.001 && g.t1 <= b.end + 0.001); assert.ok(inside.length >= 3, `${inside.length} pans through the tall block`); assert.ok(followOffsetAt(path, b.end - 0.01) >= 1300 - 400); }); test("the card page: the blocks wrapped on the card, the post's words in a flow that pans, never shrunk below the readable size", () => { const e = narrated(base({ text: `${TEXT}\n\n${LONG_PARA}\n\n${LONG_PARA}` })); const html = postCardHtml(e, { width: 1600, height: 900, follow: true }); assert.match(html, /data-post-flow=""/); assert.match(html, /data-post-fade="bottom"/); assert.match(html, /Normally<\/span>/); assert.match(html, /data-post-follow="\d+"/); // The fit script's floor: FOLLOW_MIN_SIZE (28) at 900/1080, not the old 18. assert.match(html, /var min = 23;/); const off = postCardHtml(e, { width: 1600, height: 900 }); assert.doesNotMatch(off, /data-post-block|data-post-flow/); assert.match(off, /var min = 15;/); }); test("the encode: the follow frames are a concat list laid over the card's region, before the attachments", () => { const e = base({ text: "Hello there." }); const plan = postItemPlan(e); const argv = postSegmentArgs({ entry: e, box: { width: 1600, height: 900 }, plan, layout: { mode: "text", card: { x: 0, y: 0, width: 100, height: 100 }, slot: null }, cards: { read: "/c/read.png" }, files: [], render: { fps: 30 }, outPath: "/o.mp4", follow: { list: "/c/follow/frames.ffconcat", clip: { x: 210, y: 40, width: 1180, height: 820 } }, }); const fc = argv[argv.indexOf("-filter_complex") + 1]; assert.ok(argv.join(" ").includes("-f concat -safe 0 -i /c/follow/frames.ffconcat")); assert.match(fc, /\[1:v\]fps=30,setsar=1,setpts=PTS-STARTPTS\[rf\];\[b0\]\[rf\]overlay=x=210:y=40:eof_action=repeat\[b1\]/); });