// A post item's CLOCK (post-clock.mjs) and the PACE knob (post-narration.mjs // POST_PACES): which items show it, its plan from the item before (forward, // rewind, none), its face in the zone, the run's states, the validation. import assert from "node:assert/strict"; import { test } from "node:test"; import { CLOCK_TICK, clockFace, clockFrameTimes, clockOn, clockPlan, clockStateAt, gapLabel, validateClockRender } from "./post-clock.mjs"; import { validatePostFx } from "./word-zoom.mjs"; import { narrationSchedule, postNarrationKnobs, validatePostNarration } from "./post-narration.mjs"; import { validatePostsRender } from "./post-redact.mjs"; const post = (id, date, o = {}) => ({ type: "post", id, post: `s/${id}`, platform: "x", author: "S", handle: "s", date, text: "do you want to die?", ...o }); const CLK = { fx: [{ kind: "clock" }] }; test("which items show it: an item's own clock fx, else the cut's render.posts.clock.on; never one without a date", () => { assert.equal(clockOn(post("a", "2026-11-02T22:15:00Z"), {}), false); assert.equal(clockOn(post("a", "2026-11-02T22:15:00Z", CLK), {}), true); assert.equal(clockOn(post("a", "2026-11-02T22:15:00Z"), { posts: { clock: { on: true } } }), true); assert.equal(clockOn(post("a", "2026-11-02T22:15:00Z", { fx: [{ kind: "clock", on: false }] }), { posts: { clock: { on: true } } }), false); assert.equal(clockOn(post("a", "", CLK), {}), false); }); test("its face: the post's time in the operator's zone (ET), the date as a day", () => { assert.deepEqual( (({ time, zone, date }) => ({ time, zone, date }))(clockFace(Date.parse("2026-11-04T09:10:00Z"))), { time: "4:10 AM", zone: "ET", date: "Wed 4 Nov" }, ); assert.equal(clockFace(Date.parse("2026-11-04T09:10:00Z"), "Europe/London").time, "9:10 AM"); }); test("the plan: forward from the clock item before, a rewind when this one is earlier, none after a non-clock item", () => { const a = post("a", "2026-11-02T22:15:00Z", CLK); const b = post("b", "2026-11-02T22:15:39Z", CLK); const fwd = clockPlan(b, {}, a); assert.equal(fwd.mode, "forward"); assert.equal(fwd.gap, 39000); const back = clockPlan(post("c", "2026-11-02T10:10:07Z", CLK), {}, b); assert.equal(back.mode, "rewind"); assert.equal(clockPlan(b, {}, post("x", "2026-11-02T20:00:00Z")).mode, "none"); assert.equal(clockPlan(b, {}, null).mode, "none"); // Forced and suppressed. assert.equal(clockPlan(post("c", "2026-11-02T10:10:07Z", { fx: [{ kind: "clock", rewind: "off" }] }), {}, b).mode, "none"); const forced = clockPlan(post("d", "2026-11-02T23:00:00Z", { fx: [{ kind: "clock", rewind: "on" }] }), {}, b); assert.equal(forced.mode, "rewind"); assert.equal(forced.forced, true); assert.ok(forced.from > forced.to); // A repeat of the same post (the same time): nothing to run. assert.equal(clockPlan(post("e", "2026-11-02T22:15:39Z", CLK), {}, b).mode, "none"); assert.equal(clockPlan(post("f", "2026-11-02T22:15:39Z"), {}, b), null); }); test("the run: from the earlier time to this one over CLOCK_TICK, the gap flashing; a rewind spins the hands backwards", () => { const a = post("a", "2026-11-02T13:30:00Z", CLK); const b = post("b", "2026-11-02T14:08:00Z", CLK); const p = clockPlan(b, {}, a); const s0 = clockStateAt(p, 0); const s1 = clockStateAt(p, CLOCK_TICK); assert.equal(s0.time, "8:30 AM"); assert.equal(s1.time, "9:08 AM"); assert.equal(s0.gap, "+38 min"); assert.ok(s0.running && !s1.running); assert.ok(s1.minuteTurn > s0.minuteTurn); assert.equal(clockStateAt(p, 3).gapOpacity, 0); const r = clockPlan(post("c", "2026-10-30T13:00:00Z", CLK), {}, b); const r0 = clockStateAt(r, 0); const r1 = clockStateAt(r, CLOCK_TICK); assert.ok(r1.minuteTurn < r0.minuteTurn, "the hands run backwards"); assert.match(r0.gap, /^⟲ rewind −3 d 1 h/); assert.equal(r0.date, "Mon 2 Nov"); assert.equal(r1.date, "Fri 30 Oct"); assert.equal(clockFrameTimes(clockPlan(b, {}, null)).length, 1); assert.ok(clockFrameTimes(p, 30).length > 50); }); test("gap labels", () => { assert.equal(gapLabel(39000), "+39 s"); assert.equal(gapLabel(38 * 60000), "+38 min"); assert.equal(gapLabel((2 * 60 + 14) * 60000), "+2 h 14 min"); assert.equal(gapLabel(-(3 * 86400 + 4 * 3600) * 1000), "−3 d 4 h"); }); test("validation: the item's clock fx and the cut's render.posts.clock", () => { assert.deepEqual(validatePostFx(post("a", "2026-11-02T22:15:00Z", { fx: [{ kind: "clock", rewind: "auto" }] })), []); assert.match(validatePostFx(post("a", "2026-11-02T22:15:00Z", { fx: [{ kind: "clock", rewind: "back" }] })).join(), /rewind must be one of auto, on, off/); assert.match(validatePostFx(post("a", "2026-11-02T22:15:00Z", { fx: [{ kind: "clock" }, { kind: "clock" }] })).join(), /one clock per item/); assert.deepEqual(validateClockRender({ on: true, zone: "America/New_York" }), []); assert.match(validatePostsRender({ posts: { clock: { zone: "Mars/Olympus" } } }).join(), /is not a time zone/); }); test("THE PACE: fast plays a post back to back -- its gap knobs, unless it sets one; the cut's pace too; normal is the cut's", () => { const take = { file: "n/t.wav", engine: "orpheus", voice: "zac", seconds: 5, script: "x" }; const e = post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", takes: [take], pace: "fast" } }); const k = postNarrationKnobs(e, {}); assert.deepEqual([k.lead, k.tail, k.ramp, k.linger], [0.1, 0.15, 0.3, 0.45]); assert.equal(narrationSchedule(e, {}).at, 0.1); const own = post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", takes: [take], pace: "fast", linger: 1 } }); assert.equal(postNarrationKnobs(own, {}).linger, 1); const cut = { postNarration: { pace: "fast" } }; const plain = post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", takes: [take] } }); assert.equal(postNarrationKnobs(plain, cut).linger, 0.45); const normal = post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", takes: [take], pace: "normal" } }); assert.equal(postNarrationKnobs(normal, cut).linger, 1.8); assert.match(validatePostNarration(post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", pace: "slow" } })).join(), /pace must be fast or normal/); }); test("THE SEGMENT KEY (post-render.mjs postFxKeyFields): nothing for a plain item (its old key, its segment reused); the caption, the clock (the time it runs from, the rewind, the zone) and the pace each change it", async () => { const { postFxKeyFields } = await import("./post-render.mjs"); const take = { file: "n/t.wav", engine: "orpheus", voice: "zac", seconds: 5, script: "x" }; const plain = post("a", "2026-11-02T22:15:00Z", { narration: { script: "x", takes: [take] } }); assert.deepEqual(postFxKeyFields(plain, {}, post("z", "2026-11-02T22:00:00Z")), {}); assert.deepEqual(postFxKeyFields(plain, { postNarration: { rate: 1.08 } }, null), {}); // A caption: its drawing's version (the entry, in the key, carries its text and knobs). assert.deepEqual(postFxKeyFields({ ...plain, fx: [{ kind: "caption", match: "x", text: "y" }] }, {}), { captionV: 2 }); // The clock: from the item before -- its time, its being a clock item, the zone, the rewind. const clk = { ...plain, fx: [{ kind: "clock" }] }; const before = post("z", "2026-11-02T22:00:00Z", CLK); const k1 = JSON.stringify(postFxKeyFields(clk, {}, before)); assert.match(k1, /"clockV":1/); assert.notEqual(JSON.stringify(postFxKeyFields(clk, {}, { ...before, date: "2026-11-02T21:00:00Z" })), k1); assert.notEqual(JSON.stringify(postFxKeyFields(clk, {}, { ...before, fx: [] })), k1); assert.notEqual(JSON.stringify(postFxKeyFields(clk, { posts: { clock: { zone: "Europe/London" } } }, before)), k1); assert.notEqual(JSON.stringify(postFxKeyFields({ ...clk, fx: [{ kind: "clock", rewind: "on" }] }, {}, before)), k1); // The cut's clock on: every dated item keys its clock. assert.ok(postFxKeyFields(plain, { posts: { clock: { on: true } } }, null).clock); // The pace: the item's, or the cut's (which the entry does not carry). assert.deepEqual(postFxKeyFields(plain, { postNarration: { pace: "fast" } }).pace, { pace: "fast", lead: 0.1, tail: 0.15, ramp: 0.3, linger: 0.45 }); assert.equal(postFxKeyFields({ ...plain, narration: { ...plain.narration, pace: "fast", linger: 1 } }, {}).pace.linger, 1); }); test("a take --from another carries a post item's record (its key) and combo files with its segment, so a matching item is reused", async () => { const { SEGMENT_FILE_SUFFIXES } = await import("./build-video.mjs"); for (const s of [".mp4", ".post.json", ".combo.json", ".plain.mp4"]) assert.ok(SEGMENT_FILE_SUFFIXES.includes(s), s); });