// post-progress.mjs: A POST ITEM's PROGRESS BAR -- which items draw it (on, off, auto: a read of 60 s or more; the // item's own over the cut's), its timing (the read's REAL seconds: linear, so a part read faster by a tempo step moves // it faster and it never jumps at a part's edge), its ticks, its fade, the validators, the segment key (an item with // no bar keeps its key), and the build's filters (ffmpeg: the fill clipped at the read's progress frame by frame). import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdtemp, readFile, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { postItemPlan, validatePostItem } from "./post-item.mjs"; import { validatePostsRender } from "./post-redact.mjs"; import { postFxKeyFields } from "./post-render.mjs"; import { PROGRESS_AUTO_SECONDS, progressAt, progressFilters, progressModeOf, progressOpacityAt, progressPlan } from "./post-progress.mjs"; const DIR = await mkdtemp(path.join(tmpdir(), "umtool-post-progress-")); after(() => rm(DIR, { recursive: true, force: true })); // A read of three parts; the middle one read 1.35x (a tempo step): shorter in the take, so its share of the bar is too. const take = (seconds = 90) => ({ file: "narration/posts/1/take-2.tempo-take.wav", engine: "derived", voice: "zac", seconds, parts: [{ n: 1, para: 0, text: "a", start: 0, seconds: seconds * 0.4 }, { n: 2, para: 1, text: "b", start: seconds * 0.45, seconds: seconds * 0.2, tempo: 1.35 }, { n: 3, para: 2, text: "c", start: seconds * 0.7, seconds: seconds * 0.3 }], }); const item = (o = {}, seconds = 90) => ({ type: "post", id: "p-1", post: "subj-x/1", platform: "x", author: "S", handle: "s", date: "2026-10-30T10:00:00.000Z", text: "Words.", narration: { script: "a\n\nb\n\nc", takes: [take(seconds)], pick: 0 }, ...o }); test("WHICH ITEMS: the item's own mode over the cut's (absent: off); auto on for a read of 60 s or more as it plays (its rate counted)", () => { const render = { postNarration: { lead: 0.4 } }; assert.equal(progressModeOf(item(), render), "off"); assert.equal(progressModeOf(item(), { posts: { progress: "auto" } }), "auto"); assert.equal(progressModeOf(item({ progress: "off" }), { posts: { progress: "on" } }), "off"); const plan = (e, r) => postItemPlan(e, r); assert.equal(progressPlan(item(), render, plan(item(), render)), null, "the cut's default: off"); const auto = { posts: { progress: "auto" } }; assert.ok(progressPlan(item(), auto, plan(item(), auto)), "90 s of read: on"); assert.equal(progressPlan(item({}, 40), auto, plan(item({}, 40), auto)), null, "40 s: off"); // A 62 s take read at 1.08: 57.4 s on the item -- under the line. const fast = { posts: { progress: "auto" }, postNarration: { rate: 1.08 } }; assert.equal(progressPlan(item({}, 62), fast, plan(item({}, 62), fast)), null); assert.ok(PROGRESS_AUTO_SECONDS === 60); // on: any read; an item with no read fills over its own length. const bare = { ...item({ progress: "on" }), narration: undefined }; const pb = progressPlan(bare, {}, plan(bare, {})); assert.deepEqual([pb.from, pb.to, pb.ticks], [0, plan(bare, {}).total, []]); }); test("ITS TIMING: the read's real seconds, linear -- no jump at a part read faster; the ticks at each part's start; faded in to the read's start; gone at a stack card's media", () => { const render = { posts: { progress: "on" }, postNarration: { lead: 0.4 } }; const plan = postItemPlan(item(), render); const pp = progressPlan(item(), render, plan); assert.deepEqual([pp.from, pp.to], [0.4, 90.4]); // The ticks: parts 2 and 3 start at 45% and 70% of the read. assert.deepEqual(pp.ticks, [0.45, 0.7]); // Linear: the same step of item seconds is the same step of the bar, inside the stretched part and across its edges. const step = (s) => progressAt(pp, s + 0.1) - progressAt(pp, s); for (const s of [10, 40.9, 41, 50, 58.9, 59, 63.4, 80]) assert.ok(Math.abs(step(s) - 0.1 / 90) < 1e-9, `at ${s}`); assert.deepEqual([progressAt(pp, 0), progressAt(pp, 45.4), progressAt(pp, 95)], [0, 0.5, 1]); // Faded in over 0.35 s ending at the read's start (here 0.4: from 0.05). assert.equal(progressOpacityAt(pp, 0.05), 0); assert.ok(Math.abs(progressOpacityAt(pp, 0.225) - 0.5) < 1e-9); assert.equal(progressOpacityAt(pp, 0.4), 1); assert.equal(progressOpacityAt({ ...pp, until: 50 }, 50), 0, "gone at its until"); const stack = progressPlan(item(), render, { ...plan, steps: [{ at: 91, seconds: 3, items: [] }] }, { stack: true }); assert.equal(stack.until, 91); }); test("THE VALIDATORS and THE KEY: an item's progress and the cut's are on, off or auto; an item with no bar keeps its key, one with a bar adds its plan", () => { assert.deepEqual(validatePostItem(item({ progress: "on" })), []); assert.match(validatePostItem(item({ progress: "yes" })).join(" "), /progress must be auto, on, off/); assert.deepEqual(validatePostsRender({ posts: { progress: "auto" } }), []); assert.match(validatePostsRender({ posts: { progress: 1 } }).join(" "), /render.posts.progress must be auto, on, off/); const render = { postNarration: { lead: 0.4 } }; const plan = postItemPlan(item(), render); assert.deepEqual(postFxKeyFields(item(), render, null, { plan }), {}, "no bar: nothing added"); assert.deepEqual(postFxKeyFields(item(), render, null), {}, "no plan given: nothing added"); const on = { ...render, posts: { progress: "auto" } }; const k = postFxKeyFields(item(), on, null, { plan: postItemPlan(item(), on) }); assert.deepEqual(Object.keys(k), ["progress"]); assert.deepEqual([k.progress.v, k.progress.from, k.progress.to, k.progress.ticks], [1, 0.4, 90.4, [0.45, 0.7]]); }); test("THE BUILD's FILTERS: the track laid, the fill clipped at the read's progress frame by frame, faded in, gone at until (ffmpeg)", async () => { const track = path.join(DIR, "track.png"); const fill = path.join(DIR, "fill.png"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=0x808080:s=200x6", "-frames:v", "1", track]); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=0xff8000:s=200x6", "-frames:v", "1", fill]); const pp = { from: 1, to: 3, ticks: [], until: 3.6, mode: "on" }; const f = progressFilters({ ti: 1, fi: 2, inLabel: "0:v", outLabel: "o", x: 20, y: 50, pp, T: 4 }); const out = path.join(DIR, "out.rgb"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=black:s=240x100:r=10:d=4", "-loop", "1", "-framerate", "10", "-t", "4", "-i", track, "-loop", "1", "-framerate", "10", "-t", "4", "-i", fill, "-filter_complex", f.chains.join(";"), "-map", "[o]", "-pix_fmt", "rgb24", "-f", "rawvideo", out]); const buf = await readFile(out); const px = (t, x) => { const i = Math.round(t * 10) * 240 * 100 * 3 + (52 * 240 + 20 + x) * 3; return [buf[i], buf[i + 1], buf[i + 2]]; }; assert.deepEqual(px(0.5, 100), [0, 0, 0], "before its fade: nothing"); // At 1.5 s a quarter full (50 px of 200): the fill to its edge, the track after. assert.ok(px(1.5, 40)[0] > 240 && px(1.5, 40)[2] < 10, `fill ${px(1.5, 40)}`); assert.deepEqual(px(1.5, 55), [128, 128, 128]); assert.ok(px(2, 90)[0] > 240, "half: the fill at 90 px"); assert.deepEqual(px(2, 105), [128, 128, 128]); assert.ok(px(3.2, 190)[0] > 240, "full"); assert.deepEqual(px(3.7, 100), [0, 0, 0], "gone at its until"); });