// A post item's CAPTION (post-caption.mjs): its validation, its time (at, the // take's words, the reading estimate), its span (waiting for a word zoom), and // its place (never over a word read while it is up). import assert from "node:assert/strict"; import { test } from "node:test"; import { CAPTION_IN, CAPTION_OUT, captionInnerHtml, captionOpacityAt, captionPlan, captionSlideAt, captionSpan, captionTimes, cardWordTimes, placeCaption, } from "./post-caption.mjs"; import { validatePostFx, wordZoomTimes } from "./word-zoom.mjs"; const TEXT = "I'm ignoring my broken bones so my training can continue. I have achieved Fudōshin. Its amazing."; const base = (o = {}) => ({ type: "post", id: "p1", post: "s/1", platform: "x", author: "S", handle: "s", date: "2026-10-20T19:46:00.000Z", text: TEXT, ...o }); const cap = (o = {}) => ({ kind: "caption", match: "Fudōshin", text: "不動心 · fudōshin — \"immovable mind\"", ...o }); const take = (o = {}) => ({ file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 10, script: TEXT, ...o }); test("validation: a caption's keys, its match, its text, its knobs -- in the build's words", () => { assert.deepEqual(validatePostFx(base({ fx: [cap()] })), []); const errs = validatePostFx(base({ fx: [cap({ match: "Fudoshinn", text: "", place: "left", hold: 0.1, scale: 2 })] })); assert.ok(errs.some((e) => /fx\[0\]\.scale is not a caption key/.test(e)), errs.join("\n")); assert.ok(errs.some((e) => /match "Fudoshinn" is not in the post's words/.test(e))); assert.ok(errs.some((e) => /\.text must be what the caption says/.test(e))); assert.ok(errs.some((e) => /\.place must be one of below, above, side/.test(e))); assert.ok(errs.some((e) => /\.hold must be 0.5–10 seconds/.test(e))); assert.ok(validatePostFx(base({ fx: [cap({ text: "x".repeat(161) })] })).some((e) => /longer than 160 characters/.test(e))); }); test("a caption is not a word zoom: wordZoomTimes gives it no time and no error", () => { assert.deepEqual(wordZoomTimes(base({ fx: [cap()] })), [{ i: 0, kind: "caption" }]); }); test("WHEN: at wins; un-narrated, the reading estimate (1 s + 3 words a second)", () => { assert.deepEqual(captionTimes(base({ fx: [cap({ at: 2.25 })] })), [{ i: 0, t: 2.25, source: "at" }]); // "Fudōshin." is the card's 14th word (index 13): 1 + 13/3. assert.deepEqual(captionTimes(base({ fx: [cap()] })), [{ i: 0, t: 5.33, source: "estimate" }]); }); test("WHEN: narrated with no word timings -- the read spread over its script by characters, on the item's clock", () => { const e = base({ narration: { script: TEXT, takes: [take()], lead: 0.5, rate: 1.25 }, fx: [cap()] }); const [t] = captionTimes(e); assert.equal(t.source, "estimate"); // 72 characters of 101 before it, over 10 ÷ 1.25 = 8 s, from 0.5. const before = TEXT.indexOf("Fudōshin"); const n = TEXT.split(/\s+/).reduce((a, w) => a + w.length + 1, 0); assert.equal(t.t, Math.round((0.5 + (before / n) * 8) * 1000) / 1000); // Each card word has a time, in order. const wt = cardWordTimes(e); assert.equal(wt.times.length, TEXT.split(/\s+/).length); for (let k = 1; k < wt.times.length; k += 1) assert.ok(wt.times[k] >= wt.times[k - 1]); }); test("WHEN: narrated and timed -- the matched word's start in the take (lead + start ÷ rate)", () => { const words = TEXT.split(" ").map((w, i) => ({ w, start: i * 0.5, end: i * 0.5 + 0.4 })); const e = base({ narration: { script: TEXT, takes: [take({ words })], lead: 0.4, rate: 1.25 }, fx: [cap()] }); assert.deepEqual(captionTimes(e), [{ i: 0, t: 5.6, source: "words" }]); }); test("the span: in over its lead, held from the word, out; it waits for a word zoom it would be up with; it ends by the item's end", () => { const s = captionSpan(cap(), 4, {}); assert.deepEqual(s, { t0: 3.85, t1: 3.85 + CAPTION_IN, t2: 6.5, t3: 6.5 + CAPTION_OUT, at: 4, waited: null }); const w = captionSpan(cap(), 4, { zooms: [{ i: 0, t0: 3.5, t1: 4, t2: 5.2, t3: 5.6 }] }); assert.equal(w.t0, 5.6); assert.equal(w.waited, 0); assert.equal(Math.round((w.t2 - w.t0) * 100) / 100, 2.65); const short = captionSpan(cap(), 4, { end: 5 }); assert.equal(short.t3, 5); assert.match(captionSpan(cap(), 4.9, { end: 5 }).error, /at or after the item's 5 s/); assert.equal(captionOpacityAt(s, 3.8), 0); assert.equal(captionOpacityAt(s, 5), 1); assert.ok(captionOpacityAt(s, 6.6) > 0 && captionOpacityAt(s, 6.6) < 1); assert.equal(captionSlideAt(s, 3.85), 1); assert.equal(captionSlideAt(s, 4.5), 0); }); test("the plan: the words read while it is up are what it must not cover; two up at once are refused", () => { const plan = captionPlan(base({ fx: [cap({ at: 2 })] })); assert.equal(plan.length, 1); assert.deepEqual([plan[0].scope, plan[0].from, plan[0].to], ["post", 13, 13]); // Up 1.85–4.85 s (± 0.25), the words read at 1 s + 3 a second: words 2..12 -- never the phrase itself. assert.deepEqual(plan[0].avoid, [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); assert.throws(() => captionPlan(base({ fx: [cap({ at: 2 }), cap({ at: 3, match: "amazing." })] })), /captions 1 and 2 are up at once/); }); const overlapArea = (a, b) => Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) * Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y)); /** A card of text: `rows` lines of words, `[{x, y, width, height}]`, from (x0, y0). */ const textBlock = (x0, y0, rows, { words = 8, w = 70, h = 40, gapX = 14, lineH = 52 } = {}) => Array.from({ length: rows }, (_, r) => Array.from({ length: words }, (_, c) => ({ x: x0 + c * (w + gapX), y: y0 + r * lineH, width: w, height: h }))).flat(); test("THE PLACE (operator, 4 Nov: \"a caption chip never covers ANY of the card's words\"): free space only -- below the text, off the card, or in a band -- never over a word, read or not", () => { const card = { x: 40, y: 40, width: 760, height: 1000 }; const bounds = { x: 12, y: 12, width: 1896, height: 1056 }; const words = textBlock(80, 300, 8); // y 300..704: a block of text mid-card const phrase = words[3 * 8 + 3]; const obstacles = words.filter((w) => w !== phrase); const sizes = [{ width: 480, height: 70, max: 480 }, { width: 340, height: 100, max: 340 }]; const p = placeCaption({ word: phrase, sizes, bounds, card, column: { x: 80, y: 300, width: 680, height: 410 }, obstacles }); assert.equal(p.covers, 0); for (const w of words) assert.equal(overlapArea(p, w), 0, `over a word at ${w.x},${w.y}`); assert.equal(p.place, "card", "the card's own free space beats the frame's"); assert.ok(p.y >= 704 || p.y + p.height <= 300, "above or below the block, not inside it"); assert.ok(Array.isArray(p.leader) && p.leader.length >= 2, "a leader to the phrase"); // Its leader runs through free space: past the first leg (in the gap under the phrase's line), it crosses no word. const legs = p.leader.slice(p.leader.length > 2 ? 1 : 0); for (let k = 1; k < legs.length; k += 1) { for (let q = 0; q <= 20; q += 1) { const x = legs[k - 1][0] + ((legs[k][0] - legs[k - 1][0]) * q) / 20; const y = legs[k - 1][1] + ((legs[k][1] - legs[k - 1][1]) * q) / 20; for (const w of obstacles) assert.ok(!(x > w.x + 1 && x < w.x + w.width - 1 && y > w.y + 1 && y < w.y + w.height - 1), `leader through a word at ${x},${y}`); } } // A card full of text: off the card, still over nothing (the attachment's box an obstacle too). const full = textBlock(60, 60, 18, { words: 8 }); const media = { x: 840, y: 240, width: 1060, height: 600 }; const q = placeCaption({ word: full[5 * 8 + 2], sizes, bounds, card, obstacles: [...full.filter((_, i) => i !== 5 * 8 + 2), media] }); assert.equal(q.covers, 0); assert.equal(q.place, "off-card"); for (const w of [...full, media]) assert.equal(overlapArea(q, w), 0); // Below by default when both are free; above when asked. const lone = { x: 300, y: 500, width: 100, height: 40 }; const b = placeCaption({ word: lone, sizes, bounds, card }); assert.ok(b.y >= 540); const a = placeCaption({ word: lone, sizes, bounds, card, place: "above" }); assert.ok(a.y + a.height <= 500); }); test("THE PLACE, at random: over a hundred cards of words, the chip never overlaps a word box when the frame has room", () => { let seed = 7; const rnd = () => ((seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648); for (let n = 0; n < 120; n += 1) { const rows = 2 + Math.floor(rnd() * 12); const words = textBlock(60 + rnd() * 40, 60 + rnd() * 300, rows, { words: 4 + Math.floor(rnd() * 5) }); const k = Math.floor(rnd() * words.length); const p = placeCaption({ word: words[k], sizes: [{ width: 300 + rnd() * 300, height: 60 + rnd() * 50 }], bounds: { x: 0, y: 0, width: 1920, height: 1080 }, card: { x: 40, y: 40, width: 800, height: 1000 }, obstacles: words.filter((_, i) => i !== k) }); assert.equal(p.covers, 0, `card ${n}`); for (const w of words) assert.equal(overlapArea(p, w), 0, `card ${n}: over a word`); } }); test("the chip's markup: the part before the dash bold, every character escaped, CJK kept", () => { assert.equal(captionInnerHtml("不動心 · fudōshin — \"immovable mind\""), "不動心 · fudōshin — "immovable mind""); assert.equal(captionInnerHtml("x"), "<b>x</b>"); });