// word-zoom.mjs: a post item's word zoom -- the validator, the match, the // alignment of the words heard to the script, the time on the item's clock and // the curve (the render's expressions and Play's CSS read the same one). import assert from "node:assert/strict"; import { test } from "node:test"; import { postCardHtml, validatePostItem } from "./post-item.mjs"; import { postSegmentArgs, postZoomPlan } from "./post-render.mjs"; import { postItemPlan, postLayout } from "./post-item.mjs"; import { WORD_ZOOM_OUT, alignWords, cardWordsOf, easeOf, findMatch, overlappingZooms, progressExpr, scriptWordTimes, wordSimilarity, wordZoomTimes, zoomAt, zoomProgressAt, zoomSpan, zoomWindow, zoompanExprs, } from "./word-zoom.mjs"; const TEXT = "If no one does stop me and save me from myself, barbecue if no one does, I'm going after him."; const base = (o = {}) => ({ type: "post", id: "p1", post: "obvious-x/2117488972809330822", platform: "x", author: "MR. OBVIOUS", handle: "ObviousRises", date: "2026-11-03T04:00:00.000Z", text: TEXT, ...o, }); const zoom = (o = {}) => ({ kind: "word-zoom", match: "barbecue", ...o }); const take = (o = {}) => ({ file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 6, script: TEXT, ...o }); test("the validator: unknown keys refused, the match must be in the target's words, the knobs in their bounds", () => { assert.deepEqual(validatePostItem(base({ fx: [zoom()] })), []); assert.deepEqual(validatePostItem(base({ fx: [zoom({ match: "Barbecue,", occurrence: 1, at: 2, lead: 0.3, hold: 1, scale: 3, ease: "snap", highlight: false })] })), []); // "if no one does" is said twice: its second time is there, a third is not. assert.deepEqual(validatePostItem(base({ fx: [zoom({ match: "if no one does", occurrence: 2 })] })), []); const errs = (fx) => validatePostItem(base({ fx: [fx] })).join("\n"); assert.match(errs(zoom({ match: "if no one does", occurrence: 3 })), /is in the post's words 2 times: occurrence 3 is not there/); assert.match(errs(zoom({ match: "because" })), /fx\[0\]\.match "because" is not in the post's words/); assert.match(errs(zoom({ zoomy: 1 })), /fx\[0\]\.zoomy is not a word-zoom key/); assert.match(errs(zoom({ scale: 9 })), /scale must be 1.2–4/); assert.match(errs(zoom({ ease: "bounce" })), /ease must be one of out, inOut, snap/); assert.match(errs(zoom({ highlight: "yellow" })), /highlight must be a colour/); assert.match(errs(zoom({ target: "quoted" })), /target quoted: the card draws no such post \(it quotes none\)/); assert.match(errs({ kind: "sparkle" }), /kind must be one of word-zoom/); assert.match(validatePostItem(base({ fx: { kind: "word-zoom" } })).join(), /fx must be a list/); // A context post's words: its paraphrase when it has one (what the card shows). const q = { quoted: { handle: "someone", text: "the original words", paraphrase: "a short retelling" } }; assert.deepEqual(validatePostItem(base({ ...q, fx: [zoom({ target: "quoted", match: "retelling" })] })), []); assert.match(validatePostItem(base({ ...q, fx: [zoom({ target: "quoted", match: "original" })] })).join(), /not in the quoted post's words/); }); test("the match: the card's words, case and edge punctuation aside, the n-th run", () => { assert.deepEqual(findMatch(TEXT, "BARBECUE"), { from: 11, to: 11, count: 1 }); assert.equal(cardWordsOf(TEXT)[11], "barbecue"); assert.deepEqual(findMatch(TEXT, "no one does", 2), { from: 13, to: 15, count: 2 }); assert.equal(findMatch(TEXT, "zzz").error, "not in"); }); test("the card: each word a span the build measures; the highlight marks the zoom's words", () => { const html = postCardHtml(base(), { width: 1280, height: 720 }); assert.match(html, /barbecue<\/span>/); const lit = postCardHtml(base(), { width: 1280, height: 720, highlight: { scope: "post", from: 11, to: 11, color: "#ffd84d" } }); assert.match(lit, /barbecue<\/span>/); assert.match(lit, /\[data-w-hl\] \{ background: rgba\(255, 216, 77, 0.42\)/); }); test("the alignment: in order, fuzzy -- a typo read as the word it means, a word misheard, a word not heard", () => { assert.ok(wordSimilarity("barbecue", "barbeque") > 0.8); assert.ok(wordSimilarity("barbecue,", "Barbecue") === 1); assert.ok(wordSimilarity("2", "two") === 1); assert.ok(wordSimilarity("because", "barbecue") < 0.5); // The script has the typo; the voice said it, the transcription heard "barbeque"; "myself" was not heard. const script = "save me from myself, barbecue if no one does"; const heard = [ ["save", 0.1, 0.4], ["me", 0.4, 0.5], ["from", 0.5, 0.7], ["barbeque", 1.6, 2.1], ["if", 2.2, 2.3], ["know", 2.3, 2.5], ["one", 2.5, 2.7], ["does", 2.7, 3.0], ].map(([w, start, end]) => ({ w, start, end })); const r = scriptWordTimes(script, heard); assert.equal(r.of, 9); assert.equal(r.heard, 8); // "no" was heard as "know": fuzzy (2 letters of 4 apart is half: a pair) assert.deepEqual(r.words[4], { w: "barbecue", start: 1.6, end: 2.1 }); // "myself," not heard: placed between "from" (ends 0.7) and "barbecue" (starts 1.6). assert.deepEqual(r.words[3], { w: "myself,", start: 0.7, end: 1.6 }); // Tags are not words; order holds (a word heard later never pairs with an earlier one). assert.equal(scriptWordTimes(" fine then", [{ w: "fine", start: 0, end: 0.3 }, { w: "then", start: 0.3, end: 0.6 }]).of, 2); assert.deepEqual(alignWords(["a", "b", "c"], ["c", "a", "b"]), [1, 2, -1]); }); test("the time: at when set; else the word's start in the take ÷ the read's rate, after its lead", () => { const words = scriptWordTimes(TEXT, cardWordsOf(TEXT).map((w, i) => ({ w, start: i * 0.3, end: i * 0.3 + 0.25 }))).words; const narrated = base({ narration: { script: TEXT, takes: [take({ words })], lead: 0.4 }, fx: [zoom()] }); // "barbecue" is word 11: 3.3 s into the take, read from 0.4 s. assert.deepEqual(wordZoomTimes(narrated)[0].t, 3.7); assert.equal(wordZoomTimes(narrated)[0].source, "words"); // At rate 1.25 the take's 3.3 s are 2.64 s of the item. const fast = base({ narration: { script: TEXT, takes: [take({ words })], lead: 0.4, rate: 1.25 }, fx: [zoom()] }); assert.equal(wordZoomTimes(fast)[0].t, 3.04); // The cut's rate too. assert.equal(wordZoomTimes(narrated, { postNarration: { rate: 1.5 } })[0].t, 2.6); // at wins. assert.deepEqual(wordZoomTimes(base({ ...narrated, fx: [zoom({ at: 1.25 })] }))[0], { i: 0, t: 1.25, source: "at" }); // No words yet, or not narrated: no time, said how to give it one. assert.match(wordZoomTimes(base({ narration: { script: TEXT, takes: [take()] }, fx: [zoom()] }))[0].error, /no word timings yet \(umtool posts narrate

p1 --words/); assert.match(wordZoomTimes(base({ fx: [zoom()] }))[0].error, /not narrated, so set its second \(at/); assert.throws(() => postZoomPlan(base({ fx: [zoom()] })), /not narrated/); assert.throws(() => postZoomPlan(base({ fx: [zoom({ at: 1, hold: 3 }), zoom({ match: "him", at: 2 })] })), /word zooms 1 and 2 are up at once/); assert.throws(() => postZoomPlan(base({ fx: [zoom({ at: 9 })] }), undefined, 5), /starts at 8.5 s, after the post's 5 s/); }); test("the curve: in over lead on the ease, held, out over WORD_ZOOM_OUT; the window is region ÷ z, landing on the word", () => { const span = zoomSpan(zoom({ lead: 0.5, hold: 1, scale: 2.5 }), 3); assert.deepEqual(span, { t0: 2.5, t1: 3, t2: 4, t3: 4 + WORD_ZOOM_OUT, scale: 2.5, ease: "inOut" }); assert.equal(zoomProgressAt(span, 2.4), 0); assert.equal(zoomProgressAt(span, 2.75), 0.5); // inOut is half way at half time assert.equal(zoomProgressAt(span, 3.5), 1); assert.ok(zoomProgressAt(span, 4.2) > 0 && zoomProgressAt(span, 4.2) < 1); assert.equal(zoomProgressAt(span, 4.4), 0); assert.equal(easeOf("out", 0.5), 0.875); assert.ok(easeOf("snap", 0.3) > 0.85); const region = { x: 40, y: 40, width: 800, height: 600 }; const word = { x: 700, y: 500, width: 100, height: 40 }; assert.deepEqual(zoomWindow(region, word, 2.5, 1), region); const full = zoomWindow(region, word, 2.5, 2.5); assert.equal(full.width, 320); assert.equal(full.height, 240); // Centred on the word, clamped inside the region (the word is near its corner). assert.equal(full.x, 40 + 800 - 320); assert.equal(full.y, 40 + 600 - 240); const mid = zoomWindow(region, word, 2.5, zoomAt(span, 0.5)); assert.ok(Math.abs(mid.width - 800 / 1.75) < 1e-9); // The ffmpeg expression is the same curve: evaluated at a few seconds. const S = (s) => progressExpr(span, String(s)); const evalExpr = (ex) => Function("lt", "gte", "clip", "pow", "if_", `return ${ex.replace(/\bif\(/g, "if_(")};`)( (a, b) => (a < b ? 1 : 0), (a, b) => (a >= b ? 1 : 0), (v, lo, hi) => Math.min(hi, Math.max(lo, v)), Math.pow, (c, a, b) => (c ? a : b), ); for (const s of [2, 2.6, 2.75, 2.9, 3.5, 4.1, 4.3, 5]) assert.ok(Math.abs(evalExpr(S(s)) - zoomProgressAt(span, s)) < 1e-3, `at ${s}`); const ex = zoompanExprs(span, { x: 480, y: 360 }, 3, "S"); assert.match(ex.x, /^1440\*\(1-1\/zoom\)\/0\.6$/); assert.deepEqual(overlappingZooms([zoomSpan({}, 1), zoomSpan({}, 1.5)]), [0, 1]); assert.equal(overlappingZooms([zoomSpan({}, 1), zoomSpan({}, 5)]), null); }); test("the render: the zoom's region shot zoomed on the curve, laid over the card before the attachments, only while up", () => { const e = base({ fx: [zoom({ at: 1.5 })], media: [{ src: "m/i.jpg", kind: "image", width: 1000, height: 1000 }] }); const box = { width: 1440, height: 810 }; const span = zoomSpan(e.fx[0], 1.5); const zooms = [{ i: 0, k: 2, span, region: { x: 30, y: 30, width: 500, height: 740 }, target: { x: 100, y: 200 }, plain: "/c/zoom-0.png", hl: "/c/zoom-hl-0.png", t: 1.5 }]; const argv = postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout: postLayout(e, box), cards: { read: "/c/read.png" }, files: ["/m/i.jpg"], render: { fps: 30 }, outPath: "/o.mp4", zooms }); const graph = argv[argv.indexOf("-filter_complex") + 1]; assert.ok(argv.includes("/c/zoom-0.png") && argv.includes("/c/zoom-hl-0.png")); assert.match(graph, /\[1:v\]scale=1000:1480,setsar=1,format=rgba\[wzp0\]/); assert.match(graph, /fade=t=in:st=0:d=0.5:alpha=1,fade=t=out:st=1.7:d=0.4:alpha=1\[wzh0\]/); assert.match(graph, /zoompan=z='1\+1\*if\(lt\(\(1\+on\/30\),1\)/); assert.match(graph, /d=1:s=500x740:fps=30,setsar=1,setpts=PTS-STARTPTS\+1\/TB\[wz0\]/); assert.match(graph, /\[b0\]\[wz0\]overlay=x=30:y=30:eof_action=pass:enable='between\(t,1,3.1\)'\[b1\]/); // The attachment is laid after it: on top. assert.ok(graph.indexOf("[wz0]overlay") < graph.indexOf("[m0_0]")); });