// Reels track their source (reelAnchor.mjs, reelHistory.mjs; lane/reel-bench): // a beat's anchor, its classification against the take the reel plays now, the // reel's line, the clamp; the writer stamping anchors and keeping the history. import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { anchorContext, clampPatch, classifyBeat, clipSnapshot, fxFingerprint, reelStatus } from "./reelAnchor.mjs"; import { diffReel, readReelHistory } from "./reelHistory.mjs"; import { anchorTrailerBeats, readTrailerBeats, updateTrailerBeat } from "./trailerBeats.mjs"; const schedule = (aEnd = 10, extra = 0) => ({ version: 1, kind: "deck", fps: 30, transition: 0, total: 30 + extra, segments: [ { id: "a", type: "clip", start: 0, duration: aEnd, end: aEnd, title: "A", subtitle: "the stream · 1 Oct" }, { id: "b", type: "clip", start: aEnd, duration: 10, end: aEnd + 10, title: "B" }, { id: "c", type: "clip", start: aEnd + 10, duration: 10, end: aEnd + 20, title: "C" }, ], fx: { keys: ["receipts"], receipts: { clips: [{ segment: "c", in: aEnd + 12, out: aEnd + 15, cards: [{ shot: "x.png", at: aEnd + 12 }] }] } }, }); const records = (aEnd = 110) => new Map([ ["a", { id: "a", start: 100, end: aEnd, segKey: `a-${aEnd}` }], ["b", { id: "b", start: 200, end: 210, segKey: "b" }], ["c", { id: "c", start: 300, end: 310, segKey: "c" }], ]); const build = (o = {}) => ({ schedule: o.schedule ?? schedule(), records: o.records ?? records() }); const nowOf = (b, take = "t2", extra = {}) => ({ take, build: "2026-11-02T12:00:00Z", masterMtimeMs: 2, snapshot: (id) => clipSnapshot(b, id), ...extra }); const anchorOf = (b, beat, take = "t1") => { const s = clipSnapshot(b, beat.id); return { take, build: "2026-11-01T12:00:00Z", masterMtimeMs: 1, master: { start: 0, end: 1 }, clip: s.clip, seg: s.seg, fx: s.fx, at: "x" }; }; test("fx: the chrome and fx over a segment, timed from its start -- a shift is not a change, an edit is", () => { const s1 = schedule(10); const s2 = schedule(12.5); // everything after a moves 2.5 s later assert.equal(fxFingerprint(s1, "c"), fxFingerprint(s2, "c")); assert.equal(fxFingerprint(s1, "b"), fxFingerprint(s2, "b")); const s3 = schedule(10); s3.fx.receipts.clips[0].cards[0].shot = "y.png"; assert.notEqual(fxFingerprint(s1, "c"), fxFingerprint(s3, "c")); const s4 = schedule(10); s4.segments[1].title = "B, retitled"; assert.notEqual(fxFingerprint(s1, "b"), fxFingerprint(s4, "b")); assert.equal(fxFingerprint(s1, "zz"), null); }); test("each beat against the take now: unchanged, rerendered, edges, clipped, look, orphan, unanchored -- and the reel's line", () => { const was = build(); const beats = [ { id: "a", start: 101, end: 105 }, { id: "b", start: 201, end: 205 }, { id: "c", start: 301, end: 305 }, { id: "a", start: 106, end: 109.9 }, { id: "b", start: 202, end: 203 }, ].map((b) => ({ ...b, anchor: anchorOf(was, b) })); beats.push({ id: "c", start: 302, end: 304 }); // never anchored // The same master: unchanged. assert.equal(classifyBeat(beats[0], nowOf(was, "t1", { build: "2026-11-01T12:00:00Z", masterMtimeMs: 1 })).state, "unchanged"); // Rendered again, a's end moved 110 → 112.5 (the kf3c case): a's beats flagged, nothing else. const later = build({ schedule: schedule(12.5, 2.5), records: records(112.5) }); const st = reelStatus(beats, nowOf(later)); assert.deepEqual(st.rows.map((r) => r.state), ["edges", "rerendered", "rerendered", "edges", "rerendered", "unanchored"]); assert.deepEqual(st.changed, [1, 4]); assert.match(st.rows[0].why, /a's end 110 → 112.5 since take t1; the beat still fits/); assert.deepEqual(st.rows[0].was, { start: 100, end: 110, gaps: [] }); assert.equal(st.line, "cut against t1; take t2 changes beats 1, 4"); // a's end pulled IN past a beat: clipped, with the clamp that fits it. const shorter = build({ schedule: schedule(8), records: records(108) }); const c = classifyBeat(beats[3], nowOf(shorter)); assert.equal(c.state, "clipped"); assert.match(c.why, /a now plays source 100–108 \(was 100–110\)/); assert.deepEqual(clampPatch(beats[3], clipSnapshot(shorter, "a")), { start: 106, end: 108 }); // The receipts over c changed: look. const fx = build(); fx.schedule.fx.receipts.clips[0].cards[0].shot = "z.png"; assert.equal(classifyBeat(beats[2], nowOf(fx)).state, "look"); // b gone from the cut: orphan (anchored or not). const noB = build(); noB.schedule = { ...noB.schedule, segments: noB.schedule.segments.filter((s) => s.id !== "b") }; assert.equal(classifyBeat(beats[1], nowOf(noB)).state, "orphan"); assert.equal(classifyBeat({ id: "b", start: 201, end: 202 }, nowOf(noB)).state, "orphan"); // A master beat anchored: rerendered when the master is another. assert.equal(classifyBeat({ at: 1, dur: 2, anchor: { take: "t1", build: "x", masterMtimeMs: 1 } }, nowOf(was)).state, "rerendered"); }); test("diffReel: per beat old and new, adds, removes, the order, the settings", () => { const a = { beats: [{ id: "a", start: 1, end: 2 }, { id: "b", start: 3, end: 4 }], settings: { limit: 140 } }; const b = { beats: [{ id: "a", start: 1.5, end: 2, gaps: [{ from: 1.6, to: 1.8 }] }, { id: "b", start: 3, end: 4, verdict: "confirmed", anchor: { take: "t2" } }], settings: { limit: 90 } }; const d = diffReel(a, b); assert.deepEqual(d.changes, [ { n: 1, id: "a", field: "start", old: 1, new: 1.5 }, { n: 1, id: "a", field: "gaps", old: null, new: [{ from: 1.6, to: 1.8 }] }, { n: 2, id: "b", field: "verdict", old: null, new: "confirmed" }, { n: 2, id: "b", field: "anchor", old: null, new: "t2" }, ]); assert.deepEqual(d.settings, { limit: [140, 90] }); const r = diffReel(a, { beats: [{ id: "b", start: 3, end: 4 }, { id: "c", start: 5, end: 6 }] }); assert.deepEqual(r.changes.map((x) => x.op), ["remove", "add"]); assert.deepEqual(diffReel(a, { beats: [a.beats[1], a.beats[0]] }).changes, [{ op: "order", old: ["a", "b"], new: ["b", "a"] }]); }); test("the writer stamps a beat with the take it was edited against, anchors a reel to a take, and keeps the history", async () => { const dir = await mkdtemp(path.join(tmpdir(), "umtool-anchor-")); try { const take = path.join(dir, "takes", "t1"); await mkdir(path.join(take, "out", "sourced", "segments"), { recursive: true }); await writeFile(path.join(take, "video.manifest.json"), JSON.stringify({ slug: "s", render: { fps: 30 }, timeline: [] })); await writeFile(path.join(take, "take.json"), JSON.stringify({ id: "t1", builtAt: "2026-11-01T12:00:00Z" })); await writeFile(path.join(take, "out", "sourced", "schedule.json"), JSON.stringify(schedule())); for (const [id, rec] of records()) await writeFile(path.join(take, "out", "sourced", "segments", `${id}.cut.json`), JSON.stringify(rec)); await mkdir(path.join(dir, "promo"), { recursive: true }); await writeFile(path.join(dir, "promo", "trailer-beats.json"), JSON.stringify([{ id: "a", start: 101, end: 105 }, { id: "c", start: 301, end: 305 }])); const ctx = await anchorContext(take, { take: "t1", masterMtimeMs: 7, now: () => "2026-11-02T00:00:00Z" }); await updateTrailerBeat(dir, 0, { end: 106 }, { anchorFor: ctx.anchorFor }); let doc = await readTrailerBeats(dir); assert.deepEqual(doc.beats[0].anchor, { take: "t1", build: "2026-11-01T12:00:00Z", masterMtimeMs: 7, master: { start: 1, end: 6 }, at: "2026-11-02T00:00:00Z", clip: { start: 100, end: 110, gaps: [] }, seg: "a-110", fx: fxFingerprint(schedule(), "a"), }); assert.equal(doc.beats[1].anchor, undefined); // An edit with no take keeps the anchor it had. await updateTrailerBeat(dir, 0, { verdict: "confirmed" }); doc = await readTrailerBeats(dir); assert.equal(doc.beats[0].anchor.take, "t1"); await anchorTrailerBeats(dir, ctx.anchorFor, { take: "t1" }); doc = await readTrailerBeats(dir); assert.equal(doc.beats[1].anchor.take, "t1"); const st = reelStatus(doc.beats, { take: "t1", build: ctx.build, masterMtimeMs: 7, snapshot: ctx.snapshot }); assert.deepEqual(st.rows.map((r) => r.state), ["unchanged", "unchanged"]); const h = await readReelHistory(dir, path.join("promo", "trailer-beats.json")); assert.deepEqual(h.map((e) => e.what), ["beat 1: end", "beat 1: verdict", "anchor every beat to take t1"]); assert.equal(h[0].take, "t1"); assert.deepEqual(h[0].changes.map((c) => c.field), ["end", "anchor"]); assert.deepEqual(h[2].changes, [{ n: 2, id: "c", field: "anchor", old: null, new: "t1" }]); assert.ok((await readFile(path.join(dir, "promo", "trailer-beats.history.json"), "utf8")).includes('"entries"')); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a reel's version for its cut: the seconds, the gaps, the crossfade and the intro -- never a verdict, a label or an anchor", async () => { const { reelVersion } = await import("./trailerBeats.mjs"); const v = reelVersion({ beats: [{ id: "a", start: 1, end: 2 }], settings: { xfade: 0.3 } }); assert.equal(reelVersion({ beats: [{ id: "a", start: 1, end: 2, verdict: "confirmed", label: "x", anchor: { take: "t" } }], settings: { xfade: 0.3, title: "T", limit: 90 } }), v); assert.notEqual(reelVersion({ beats: [{ id: "a", start: 1, end: 2.5 }], settings: { xfade: 0.3 } }), v); assert.notEqual(reelVersion({ beats: [{ id: "a", start: 1, end: 2, gaps: [{ from: 1.2, to: 1.5 }] }], settings: { xfade: 0.3 } }), v); assert.notEqual(reelVersion({ beats: [{ id: "a", start: 1, end: 2 }], settings: { xfade: 0 } }), v); });