// The clip editor's live preview clock (liveClock.mjs): source playhead → cut // clock, the tally at a moment, the stamp on screen, the finale. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { estimateSchedule } from "umtool-report-to-video/deck"; import { tallyVerdicts } from "umtool-report-to-video/factcheck"; import { clipStamps, clockText, cutTimeAt, finaleUp, liveClock, livePlayWindow, sourceTimeAt, stampAt, tallyAt, } from "./liveClock.mjs"; /** A deck cut of four clips: two claims (one stamped at a source second), a finale last. */ const manifest = () => ({ slug: "t", provenance: { channelSlug: "c", siteOrigin: "https://archive.example" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5, palette: { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {}, factcheck: { verdicts: { CONTRADICTED: { label: "Grift" }, CORROBORATED: { label: "Episodes" } }, stamp: { seconds: 3 } }, }, }, timeline: [ { type: "clip", id: "a", video: "v1", start: 10, end: 30 }, // Cut 104–120 inside its extent: the play window starts 0.4 s before the cut. { type: "clip", id: "b", video: "v2", start: 100, end: 130, cutStart: 104, cutEnd: 120, claim: { id: "k1", verdict: "CORROBORATED", at: 110 } }, { type: "clip", id: "c", video: "v3", start: 50, end: 62, claim: { id: "k2", verdict: "CONTRADICTED", count: 2 } }, { type: "clip", id: "d", video: "v4", start: 0, end: 20, layout: "finale" }, ], }); test("livePlayWindow: the cut with its lead-in, clamped into the extent; else the extent", () => { assert.deepEqual(livePlayWindow({ start: 100, end: 130, cutStart: 104, cutEnd: 120 }), { from: 103.6, to: 120 }); assert.deepEqual(livePlayWindow({ start: 100, end: 130, cutStart: 100.1, cutEnd: 140 }), { from: 100, to: 130 }); assert.deepEqual(livePlayWindow({ start: 10, end: 30 }), { from: 10, to: 30 }); assert.deepEqual(livePlayWindow({ start: 10, end: 30, cutStart: 12, cutEnd: 20 }, 1), { from: 11, to: 20 }); }); test("source playhead → cut clock: the segment's start plays the play window's first second", () => { const m = manifest(); const s = estimateSchedule(m); const b = m.timeline[1]; const clock = liveClock(s, b); const seg = s.segments.find((x) => x.id === "b"); assert.equal(clock.start, seg.start); assert.equal(clock.sourceFrom, 103.6); assert.equal(clock.sourceTo, 120); assert.equal(clock.index, 1); assert.equal(clock.of, 4); // Second for second inside the window; the clip's own frames outside it. assert.deepEqual(cutTimeAt(clock, 103.6), { t: seg.start, where: "inside" }); assert.deepEqual(cutTimeAt(clock, 110), { t: Math.round((seg.start + 6.4) * 1000) / 1000, where: "inside" }); assert.deepEqual(cutTimeAt(clock, 90), { t: seg.start, where: "before" }); assert.equal(cutTimeAt(clock, 125).where, "after"); assert.equal(cutTimeAt(clock, 125).t, clock.playEnd); // The estimate's segment is the play window long: its end is the playEnd. assert.equal(clock.playEnd, Math.round((seg.start + seg.duration) * 1000) / 1000); assert.equal(sourceTimeAt(clock, cutTimeAt(clock, 111.25).t), 111.25); assert.equal(sourceTimeAt(clock, clock.start - 1), null); // A draft cut moves the clock at once (the schedule is the saved one). const nudged = liveClock(s, { ...b, cutStart: 105 }); assert.equal(nudged.sourceFrom, 104.6); assert.equal(cutTimeAt(nudged, 104.6).t, seg.start); // A clip the cut does not play has no clock. assert.equal(liveClock(s, { id: "nope", start: 0, end: 1 }), null); }); test("the stamp lands on its source second, and the tally steps there", () => { const m = manifest(); const s = estimateSchedule(m); const order = tallyVerdicts(s.factcheck.stamps); const clock = liveClock(s, m.timeline[1]); const landing = cutTimeAt(clock, 110).t; const st = s.factcheck.stamps.find((x) => x.claim === "k1"); assert.ok(Math.abs(st.landed - landing) < 0.002, `landed ${st.landed} vs the source's 110 at ${landing}`); const [cs] = clipStamps(s, clock); assert.equal(cs.claim, "k1"); assert.ok(Math.abs(cs.sourceLanded - 110) < 0.002); assert.equal(cs.drawn, true); // Just before it lands: slamming in, not counted; once landed: counted, on. assert.equal(stampAt(s, landing - 0.1)?.phase, "in"); assert.deepEqual(tallyAt(s, landing - 0.01, order), [{ verdict: "CORROBORATED", count: 0 }, { verdict: "CONTRADICTED", count: 0 }]); assert.equal(stampAt(s, landing + 0.5)?.phase, "on"); assert.equal(stampAt(s, landing + 0.5)?.claim, "k1"); assert.deepEqual(tallyAt(s, landing, order), [{ verdict: "CORROBORATED", count: 1 }, { verdict: "CONTRADICTED", count: 0 }]); // Gone after its hold and leave; still counted. assert.equal(stampAt(s, st.out[1] + 0.01), null); assert.equal(stampAt(s, st.at - 0.01), null); // The next claim counts 2 at once (its `count`). const k2 = s.factcheck.stamps.find((x) => x.claim === "k2"); assert.deepEqual(tallyAt(s, k2.landed, order), [{ verdict: "CORROBORATED", count: 1 }, { verdict: "CONTRADICTED", count: 2 }]); // No order given: the stamps' own. assert.deepEqual(tallyAt(s, 0).map((r) => r.verdict), ["CORROBORATED", "CONTRADICTED"]); }); test("the finale: up over its window; a stamp there is counted, not drawn", () => { const m = manifest(); m.timeline[3].claim = { id: "k3", verdict: "CONTRADICTED" }; const s = estimateSchedule(m); assert.ok(s.finale, "the cut has a finale"); assert.equal(finaleUp(s, s.finale.from - 0.01), false); assert.equal(finaleUp(s, s.finale.from), true); assert.equal(finaleUp(s, s.total), true); const k3 = s.factcheck.stamps.find((x) => x.claim === "k3"); // The clip before it may still be leaving its own stamp; the finale's is never drawn. for (const t of [k3.at, k3.at + 0.5, s.total - 0.01]) assert.notEqual(stampAt(s, t)?.claim, "k3"); assert.equal(stampAt(s, s.total - 0.01), null); assert.equal(clipStamps(s, liveClock(s, m.timeline[3]))[0].drawn, false); assert.equal(tallyAt(s, s.total).find((r) => r.verdict === "CONTRADICTED").count, 3); assert.equal(finaleUp(estimateSchedule(manifest()), 0), false); }); test("a clip with two marks: each its own stamp at its own source second, each its own step of the tally", () => { const m = manifest(); m.timeline[1] = { ...m.timeline[1], claim: undefined, claims: [{ id: "k1", verdict: "CORROBORATED", at: 106, hold: 2 }, { id: "k1b", verdict: "CONTRADICTED", at: 112 }], }; delete m.timeline[1].claim; const s = estimateSchedule(m); const order = tallyVerdicts(s.factcheck.stamps); const clock = liveClock(s, m.timeline[1]); const marks = clipStamps(s, clock); assert.deepEqual(marks.map((x) => x.claim), ["k1", "k1b"]); assert.ok(Math.abs(marks[0].sourceLanded - 106) < 0.002); assert.ok(Math.abs(marks[1].sourceLanded - 112) < 0.002); const at = (src) => cutTimeAt(clock, src).t; assert.equal(stampAt(s, at(106.5))?.claim, "k1"); assert.equal(stampAt(s, at(112.5))?.claim, "k1b"); const count = (src, v) => tallyAt(s, at(src), order).find((r) => r.verdict === v).count; assert.deepEqual([count(105.9, "CORROBORATED"), count(106, "CORROBORATED"), count(111.9, "CONTRADICTED"), count(112, "CONTRADICTED")], [0, 1, 0, 1]); }); test("a held clip: its hold after its footage's last frame", () => { const s = { segments: [{ id: "a", start: 0, duration: 12, hold: 2 }] }; const clock = liveClock(s, { id: "a", start: 0, end: 10 }); assert.equal(clock.playEnd, 10); assert.equal(clock.end, 12); assert.equal(clock.hold, 2); }); test("clockText", () => { assert.equal(clockText(0), "0:00.0"); assert.equal(clockText(83.87), "1:23.9"); assert.equal(clockText(-3), "0:00.0"); });