// The Play view's clock (playCut.mjs): one cut clock across clips, the hard // cut at the middle of each crossfade, a clip's source second at a cut // second and back, a carrying clip's hold, cards between clips, and the // "Send all open notes" request. // // Run with: node --test lib/report/playCut.test.mjs import assert from "node:assert/strict"; import test from "node:test"; import { estimateSchedule } from "umtool-report-to-video/deck"; import { liveClock, cutTimeAt } from "./liveClock.mjs"; import { chromeTime, counterAt, counterSteps, cutAtSource, cutNotesRequest, cutPlace, dateKeyLabel, dateRamp, inHold, itemIndexAt, playPlan, playTimeOfChrome, rulerTicks, runningTime, seamsOf, shownSeconds, sourceAt, startOf, } from "./playCut.mjs"; const manifest = (transition = 0.5) => ({ slug: "t", provenance: { channelSlug: "c", siteOrigin: "https://archive.example" }, render: { width: 1920, height: 1080, fps: 30, transition, chrome: { engine: "hyperframes", layout: "deck", deck: {} } }, timeline: [ { type: "clip", id: "a", video: "v1", start: 10, end: 14 }, // Cut 104–108 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: 108 }, { type: "card", id: "k", seconds: 3, heading: "A card" }, { type: "clip", id: "c", video: "v3", start: 50, end: 53 }, ], }); test("the plan: one item per segment, midpoint to midpoint, the cut's total", () => { const m = manifest(0.5); const s = estimateSchedule(m); const plan = playPlan(s.segments, m.timeline, { leadIn: 0.4 }); assert.deepEqual(plan.items.map((i) => i.id), ["a", "b", "k", "c"]); // a: 4 s from 0; b: 4.4 s from 3.5 (overlap 0.5); k: 3 s from 7.4; c: 3 s from 9.9. assert.deepEqual(plan.items.map((i) => i.start), [0, 3.5, 7.4, 9.9]); assert.equal(plan.total, 12.9); assert.deepEqual(plan.items.map((i) => [i.from, i.to]), [[0, 3.75], [3.75, 7.65], [7.65, 10.15], [10.15, 12.9]]); // The items tile the cut: each shown from where the last stopped. assert.equal(plan.items.reduce((n, i) => n + shownSeconds(i), 0), plan.total); assert.equal(plan.items[2].clip, null); assert.equal(plan.items[2].type, "card"); assert.deepEqual(plan.items[1].clip, { sourceFrom: 103.6, sourceTo: 108, playEnd: 7.9, hold: 0 }); }); test("the clock across clips: the item at t, its source second, and back", () => { const m = manifest(0.5); const plan = playPlan(estimateSchedule(m).segments, m.timeline); assert.equal(itemIndexAt(plan, 0), 0); assert.equal(itemIndexAt(plan, 3.74), 0); assert.equal(itemIndexAt(plan, 3.75), 1); assert.equal(itemIndexAt(plan, 8), 2); assert.equal(itemIndexAt(plan, 99), 3); assert.equal(itemIndexAt({ items: [] }, 1), -1); const [a, b, k, c] = plan.items; // a plays its window second for second; the hard cut lands 0.25 s into b's window. assert.equal(sourceAt(a, 0), 10); assert.equal(sourceAt(a, 3.7), 13.7); assert.equal(sourceAt(b, 3.75), 103.85); assert.equal(cutAtSource(b, 103.85), 3.75); assert.equal(sourceAt(c, 10.15), 50.25); assert.equal(sourceAt(k, 8), null); // The jump targets are where the page starts showing each item. assert.equal(startOf(plan, 1), 3.75); assert.equal(startOf(plan, 9), 10.15); assert.equal(startOf(plan, -1), 0); }); test("the clock agrees with the live preview's (liveClock) inside every clip", () => { const m = manifest(0.5); const s = estimateSchedule(m); const plan = playPlan(s.segments, m.timeline); for (const item of plan.items.filter((i) => i.clip)) { const e = m.timeline.find((x) => x.id === item.id); const lc = liveClock(s, /** @type {any} */ (e)); assert.ok(lc); for (const t of [item.from, (item.from + item.to) / 2, item.to - 0.01]) { const src = /** @type {number} */ (sourceAt(item, t)); assert.ok(Math.abs(cutTimeAt(lc, src).t - t) < 0.002, `${item.id} at ${t}`); } } }); test("a hard-cut cut (transition 0): items are the segments themselves", () => { const m = manifest(0); const plan = playPlan(estimateSchedule(m).segments, m.timeline); assert.deepEqual(plan.items.map((i) => [i.from, i.to]), [[0, 4], [4, 8.4], [8.4, 11.4], [11.4, 14.4]]); assert.equal(sourceAt(plan.items[1], 4), 103.6); }); test("a carrying clip's hold: its last frame past its footage", () => { const segs = [ { id: "a", start: 0, duration: 6, hold: 2 }, { id: "b", start: 6, duration: 3 }, ]; const entries = [ { type: "clip", id: "a", video: "v", start: 10, end: 14 }, { type: "clip", id: "b", video: "v", start: 20, end: 23 }, ]; const plan = playPlan(segs, entries); const a = plan.items[0]; assert.equal(a.clip?.playEnd, 4); assert.equal(a.clip?.hold, 2); assert.equal(inHold(a, 3.9), false); assert.equal(inHold(a, 5), true); assert.equal(sourceAt(a, 5), 14); }); test("Send all open notes: the open notes on this cut (or take), in cut order, with where each was taken", () => { const notes = [ { status: "open", text: "too long here", anchor: { kind: "cut", t: 61.2, entry: "c04", sourceT: 194.2 } }, { status: "open", text: "start sooner", anchor: { kind: "cut", t: 3.1, entry: "c01", ask: "trim" } }, { status: "resolved", text: "done", anchor: { kind: "cut", t: 5 } }, { status: "open", text: "on the take", anchor: { kind: "cut", t: 9, take: "short-19" } }, { status: "open", text: "whole", anchor: { kind: "whole" } }, ]; assert.equal( cutNotesRequest(notes), "Notes taken while playing the cut, in order:\n1. at 0:03.1, in clip c01 [trim]: start sooner\n2. at 1:01.2, in clip c04 (source 194.2 s): too long here", ); assert.equal(cutNotesRequest(notes, { take: "short-19" }), "Notes taken while playing take short-19, in order:\n1. at 0:09.0: on the take"); assert.equal(cutNotesRequest(notes.slice(2, 3)), null); // Over the cap: what does not fit is counted, not cut mid-sentence. const many = Array.from({ length: 50 }, (_, i) => ({ status: "open", text: "x".repeat(60), anchor: { kind: "cut", t: i } })); const req = /** @type {string} */ (cutNotesRequest(many, { max: 400 })); assert.ok(req.length <= 400); assert.match(req, /more not sent\)$/); assert.equal(cutPlace({ t: 0 }), "at 0:00.0"); }); test("the reel strip: the date ramp, the seams, the counter's staircase, the ruler", () => { const ramp = dateRamp([ { id: "a", dateKey: 20201024 }, { id: "b", dateKey: 20241202 }, { id: "k" }, { id: "c", dateKey: 20221113 }, ]); assert.equal(ramp.get("a"), 0); assert.equal(ramp.get("b"), 1); assert.equal(ramp.get("k"), null); assert.ok(Number(ramp.get("c")) > 0.4 && Number(ramp.get("c")) < 0.6); assert.equal(dateRamp([{ id: "x", dateKey: 20200101 }]).get("x"), 1); const entries = [ { id: "a", type: "clip", section: 0, dateLabel: "24 Oct 2020" }, { id: "b", type: "clip", section: 0, dateLabel: "1 Dec 2020" }, { id: "k", type: "card" }, { id: "c", type: "clip", section: 1, dateLabel: "13 Nov 2022" }, { id: "d", type: "clip", section: 1, dateLabel: "2 Dec 2024" }, ]; assert.deepEqual(seamsOf(entries), [{ before: "c", index: 3, date: "13 Nov 2022", label: null }]); assert.deepEqual(seamsOf(entries, [{ after: "a", label: "Rescue" }, { after: "d", label: "past the end" }]), [ { before: "b", index: 1, date: "1 Dec 2020", label: "Rescue" }, { before: "c", index: 3, date: "13 Nov 2022", label: null }, ]); const stamps = [ { landed: 5, verdict: "E", count: 2 }, { landed: 1, verdict: "E" }, { landed: 5, verdict: "G" }, { landed: 9, verdict: "E" }, ]; assert.deepEqual(counterSteps(stamps, "E"), [{ t: 1, n: 1 }, { t: 5, n: 3 }, { t: 9, n: 4 }]); assert.deepEqual(counterSteps(stamps), [{ t: 1, n: 1 }, { t: 5, n: 4 }, { t: 9, n: 5 }]); assert.equal(counterAt(counterSteps(stamps, "E"), 4.9), 1); assert.equal(counterAt(counterSteps(stamps, "E"), 5), 3); assert.equal(counterAt([], 3), 0); assert.deepEqual(rulerTicks(8.7), { step: 1, ticks: [0, 1, 2, 3, 4, 5, 6, 7, 8] }); assert.equal(rulerTicks(1142).step, 120); assert.equal(rulerTicks(0).ticks.length, 1); assert.equal(runningTime(581.4), "09:41"); assert.equal(runningTime(3725), "1:02:05"); assert.equal(dateKeyLabel(20250227), "27 Feb 2025"); assert.equal(dateKeyLabel(null), null); }); test("the render's edges: each clip plays its snapped cut, and every later entry moves by what it gained", () => { const m = manifest(0.5); const segs = estimateSchedule(m).segments; const est = playPlan(segs, m.timeline); // a: snapped 0.5 s earlier at its start and 0.3 s later at its end (+0.8 s); b: its end 0.2 s earlier (-0.2 s). const cuts = { a: { cutStart: 9.5, cutEnd: 14.3, gainDb: -2 }, b: { cutStart: 103.6, cutEnd: 107.8, snapped: { start: false, end: true } } }; const plan = playPlan(segs, m.timeline, { cuts }); assert.equal(plan.edges, "render"); assert.equal(est.edges, "estimate"); const [a, b, k, c] = plan.items; assert.deepEqual([a.start, a.end], [0, 4.8]); assert.deepEqual(a.clip, { sourceFrom: 9.5, sourceTo: 14.3, playEnd: 4.8, hold: 0, gainDb: -2 }); // b starts 0.8 later, lasts 0.2 less; the overlap with a is the crossfade's, unchanged. assert.deepEqual([b.start, b.end, b.from], [4.3, 8.5, 4.55]); assert.equal(b.clip.playEnd, 8.5); // The card and c move by both: +0.6. assert.deepEqual([k.start, c.start], [8, 10.5]); assert.equal(plan.total, round3(est.total + 0.6)); assert.equal(c.clip.sourceFrom, 50); // The items still tile the cut. assert.equal(round3(plan.items.reduce((n, i) => n + shownSeconds(i), 0)), plan.total); // The source second at a Play second, and back. assert.equal(sourceAt(a, 0), 9.5); assert.equal(cutAtSource(b, 105), round3(4.3 + 105 - 103.6)); }); test("the chrome's second: by source second inside a clip, held at the item's own handover, a card's own", () => { const m = manifest(0.5); const segs = estimateSchedule(m).segments; const est = playPlan(segs, m.timeline); const plan = playPlan(segs, m.timeline, { cuts: { a: { cutStart: 9.5, cutEnd: 14.3 } } }); // Without render edges, the identity. assert.equal(chromeTime(est, 2.2), 2.2); // a's extra head (source 9.5..10, before the estimate's window) holds at a's first second. assert.equal(chromeTime(plan, 0.2), 0); // Source 12 plays at Play 2.5; the estimate placed it at 2. assert.equal(chromeTime(plan, 2.5), 2); // a's extra tail holds just before the handover (the estimate's 3.75). assert.equal(chromeTime(plan, 4.5), 3.75); // b is the estimate shifted: source 105 at Play 4.3 + 1.4 is the estimate's 3.5 + 1.4. assert.equal(chromeTime(plan, 5.7), 4.9); // A stamp the estimate lands at 4.9 (b's source 105) lands at Play 5.7; one in a's held tail at the estimate's last second of a. assert.equal(playTimeOfChrome(plan, 4.9), 5.7); assert.equal(playTimeOfChrome(plan, 2), 2.5); assert.equal(playTimeOfChrome(est, 4.9), 4.9); }); const round3 = (v) => Math.round(v * 1000) / 1000; test("skipEntries: a skipped clip's shown span leaves the clock, later items move up, the chrome still seeks the estimate", async () => { const { skipEntries, playPlan, chromeTime } = await import("./playCut.mjs"); const segs = [ { id: "a", start: 0, duration: 3 }, { id: "b", start: 2.5, duration: 3 }, { id: "c", start: 5, duration: 3 }, ]; const ents = [ { id: "a", type: "clip", start: 10, end: 12.6 }, { id: "b", type: "clip", start: 20, end: 22.6 }, { id: "c", type: "clip", start: 30, end: 32.6 }, ]; const plan = playPlan(segs, ents, { leadIn: 0.4 }); const none = skipEntries(plan, []); assert.deepEqual(none.items, plan.items); assert.deepEqual(none.skipped, []); const out = skipEntries(plan, new Set(["b"])); assert.deepEqual(out.items.map((i) => i.id), ["a", "c"]); assert.deepEqual(out.skipped, [{ id: "b", at: plan.items[1].from }]); const bShown = plan.items[1].to - plan.items[1].from; const c = out.items[1]; assert.equal(c.index, 1); assert.ok(Math.abs(c.from - (plan.items[2].from - bShown)) < 1e-6); assert.ok(Math.abs(out.total - (plan.total - bShown)) < 1e-6); // a's seam meets c's: c comes up where a's shown span ends. assert.ok(Math.abs(c.from - out.items[0].to) < 1e-6); // The chrome is seeked to c's own second on the estimate's clock. assert.ok(Math.abs(chromeTime(out, c.from + 0.5) - (plan.items[2].from + 0.5)) < 0.01, String(chromeTime(out, c.from + 0.5))); // The first item skipped: the next starts the clock at 0. const first = skipEntries(plan, ["a"]); assert.equal(first.items[0].id, "b"); assert.equal(first.items[0].from, 0); });