// The clip editor (release 35 C2): the manifest operations (updateClip's // assist edits, restore, split, merge, add), the operations as the route runs // them (rulings follow their clips; a speaker's role), and the pure rules // (snap points, quotes, unused proposals, provenance in words). Synthetic. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MANIFEST_NAME, StaleToken, StructureRefused, addClip, manifestToken, mergeClips, restoreClip, splitClip, updateClip } from "./manifest.mjs"; import { carryRulings, clipEditorView, clipNeighbourhood, runClipEditorOp } from "./clipEditor.mjs"; import { readReactions, setSpeakerRole } from "./clipEditorBridge.mjs"; import { nearestPoint, provenanceWords, quoteBetween, snapCut, snapPoints, unusedProposals, wordsOf } from "./clipEditorModel.mjs"; import { momentKeysOf, setMontageRuling } from "./montageRulings.mjs"; const NOW = "2026-10-18T22:00:00.000Z"; const assist = (proposal, original) => ({ by: "assistant", proposal, score: 0.8, why: "host laughs", original, edits: [] }); const manifest = () => ({ schemaVersion: 1, slug: "rx", provenance: { channelSlug: "chan", siteOrigin: "https://example.test" }, render: { width: 640, height: 360, fps: 15 }, timeline: [ { type: "clip", id: "r001", video: "v1", start: 10, end: 20, cutStart: 11, cutEnd: 19, quote: "q one", assist: assist("p001", { start: 10, end: 20, cutStart: 11, cutEnd: 19 }) }, { type: "clip", id: "r002", video: "v1", start: 30, end: 40, cutStart: 31, cutEnd: 39, quote: "q two", assist: assist("p002", { start: 30, end: 40, cutStart: 31, cutEnd: 39 }) }, { type: "card", id: "k1", heading: "between" }, { type: "clip", id: "r003", video: "v1", start: 50, end: 60, cutStart: 51, cutEnd: 59, quote: "q three" }, { type: "clip", id: "plain", video: "v2", start: 5, end: 9 }, ], }); // Words every second from 0 to 70: "w0" … "w69", each 0.8 s long (a 0.2 s gap). const WORDS = { words: Array.from({ length: 70 }, (_, i) => ({ w: `w${i}`, start: i, end: i + 0.8 })) }; const REACTIONS = { version: 1, source: { channel: "chan", video: "v1", duration: 80, words: "words.json", diarization: { generatedAt: NOW, engine: "sherpa-onnx" } }, params: { target: 600, laughMin: 0.25, mergeGap: 6, setupMax: 20, clipMax: 75 }, speakers: [ { index: 0, role: "host", label: "Host", seconds: 30, turns: 12, how: "auto" }, { index: 1, role: "subject", label: "Subject", seconds: 50, turns: 3, how: "auto" }, ], turns: [ { start: 0, end: 9, speaker: 1 }, { start: 9.5, end: 12, speaker: 0 }, { start: 12.5, end: 30, speaker: 1 }, { start: 31, end: 33, speaker: 0 }, ], laughs: [{ start: 15, end: 16.5, score: 0.7, speaker: 0 }], moments: [], proposals: [ { id: "p001", start: 11, end: 19, score: 0.8, moments: [], used: true, clipId: "r001", why: "host laughs" }, { id: "p002", start: 31, end: 39, score: 0.7, moments: [], used: true, clipId: "r002", why: "a comment" }, { id: "p003", start: 62, end: 68, score: 0.5, moments: [], used: false, why: "a short laugh" }, { id: "p004", start: 70, end: 75, score: 0.6, moments: [], used: false, why: "a long comment" }, ], }; async function project({ withReactions = true } = {}) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-clip-editor-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest(), null, 2) + "\n"); if (withReactions) { await writeFile(path.join(dir, "reactions.json"), JSON.stringify(REACTIONS, null, 2) + "\n"); await writeFile(path.join(dir, "words.json"), JSON.stringify(WORDS)); } return { id: "reports/rx", dir }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); const clip = (m, id) => m.timeline.find((e) => e.id === id); const rulingsOf = async (dir) => JSON.parse(await readFile(path.join(dir, "montage-rulings.json"), "utf8")).rulings; test("updateClip: the first human edit of an assistant's cut flips assist.by and appends the edit; a no-op trim records nothing", async () => { const { dir } = await project({ withReactions: false }); try { await updateClip(dir, "r001", { cutStart: 11, cutEnd: 19 }, { now: NOW }); let e = clip(await read(dir), "r001"); assert.equal(e.assist.by, "assistant"); assert.deepEqual(e.assist.edits, []); await updateClip(dir, "r001", { cutStart: 11.5 + 0.004, cutEnd: 18 }, { now: NOW }); e = clip(await read(dir), "r001"); assert.equal(e.assist.by, "operator"); assert.deepEqual(e.assist.edits, [{ at: NOW, by: "operator", what: "trim", from: { cutStart: 11, cutEnd: 19 }, to: { cutStart: 11.5, cutEnd: 18 } }]); // A clip with no assist is edited as it always was: no assist appears. await updateClip(dir, "plain", { start: 5.5 }, { now: NOW }); assert.equal(clip(await read(dir), "plain").assist, undefined); } finally { await rm(dir, { recursive: true, force: true }); } }); test("restoreClip: the assistant's original comes back, by is the assistant's again, the restore is in the edits", async () => { const { dir } = await project({ withReactions: false }); try { await updateClip(dir, "r001", { cutStart: 12, cutEnd: 15, start: 10, end: 20 }, { now: NOW }); await restoreClip(dir, "r001", { now: NOW }); const e = clip(await read(dir), "r001"); assert.deepEqual([e.start, e.end, e.cutStart, e.cutEnd], [10, 20, 11, 19]); assert.equal(e.assist.by, "assistant"); assert.deepEqual(e.assist.edits.map((x) => x.what), ["trim", "restore"]); await assert.rejects(restoreClip(dir, "r003"), /no assistant's version/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("splitClip: two clips at the second, the first keeps id + original, the second is the operator's; quotes per half; refused near an edge", async () => { const { dir } = await project({ withReactions: false }); try { const r = await splitClip(dir, "r001", 15, { now: NOW, quotes: { first: "first half", second: "second half" } }); assert.equal(r.second, "r001-2"); const m = await read(dir); assert.deepEqual(m.timeline.map((e) => e.id), ["r001", "r001-2", "r002", "k1", "r003", "plain"]); const a = clip(m, "r001"); const b = clip(m, "r001-2"); assert.deepEqual([a.start, a.end, a.cutStart, a.cutEnd, a.quote], [10, 15, 11, 15, "first half"]); assert.deepEqual([b.start, b.end, b.cutStart, b.cutEnd, b.quote], [15, 20, 15, 19, "second half"]); assert.equal(a.assist.by, "operator"); assert.ok(a.assist.original); assert.equal(b.assist.original, undefined); assert.equal(b.assist.proposal, "p001"); assert.deepEqual(b.assist.edits.at(-1), { at: NOW, by: "operator", what: "split", from: { cutStart: 11, cutEnd: 19 }, to: { cutStart: 15, cutEnd: 19 } }); await assert.rejects(splitClip(dir, "r001", 14.8), /half a second inside the cut/); // An undo point was taken first. assert.ok(r.snapshot); } finally { await rm(dir, { recursive: true, force: true }); } }); test("mergeClips: with the next clip -- the union, quotes joined, the next removed; a card between, the last clip, another video are refused", async () => { const { dir } = await project({ withReactions: false }); try { const r = await mergeClips(dir, "r001", { now: NOW }); assert.equal(r.absorbed, "r002"); const m = await read(dir); const e = clip(m, "r001"); assert.deepEqual([e.start, e.end, e.cutStart, e.cutEnd, e.quote], [10, 40, 11, 39, "q one q two"]); assert.equal(clip(m, "r002"), undefined); assert.deepEqual(e.assist.edits.at(-1).what, "merge"); await assert.rejects(mergeClips(dir, "r001"), /a card stands between/); await assert.rejects(mergeClips(dir, "plain"), /the last clip/); await assert.rejects(mergeClips(dir, "r003"), /different recordings/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("addClip: in time order among the recording's clips, the operator's; a proposal brought in keeps its original", async () => { const { dir } = await project({ withReactions: false }); try { const a = await addClip(dir, { video: "v1", cutStart: 22, cutEnd: 25, quote: "a new one" }, { now: NOW }); let m = await read(dir); assert.deepEqual(m.timeline.map((e) => e.id), ["r001", "v1-22", "r002", "k1", "r003", "plain"]); assert.equal(a.id, "v1-22"); assert.deepEqual(clip(m, "v1-22").assist, { by: "operator", edits: [{ at: NOW, by: "operator", what: "add", from: null, to: { cutStart: 22, cutEnd: 25 } }] }); await addClip(dir, { video: "v1", cutStart: 62, cutEnd: 68, start: 61, end: 69, proposal: { id: "p003", score: 0.5, why: "a short laugh", start: 61, end: 69 } }, { now: NOW }); m = await read(dir); assert.equal(m.timeline.at(-2).id, "v1-62"); assert.deepEqual(m.timeline.at(-2).assist.original, { start: 61, end: 69, cutStart: 62, cutEnd: 68 }); assert.equal(m.timeline.at(-2).assist.proposal, "p003"); await assert.rejects(addClip(dir, { video: "v1", cutStart: 5, cutEnd: 5.2 }), /half a second/); } finally { await rm(dir, { recursive: true, force: true }); } }); test("a stale token is refused by every operation and writes nothing", async () => { const { dir } = await project({ withReactions: false }); try { const before = await readFile(path.join(dir, MANIFEST_NAME), "utf8"); for (const p of [ updateClip(dir, "r001", { cutStart: 12, cutEnd: 18 }, { token: "1" }), splitClip(dir, "r001", 15, { token: "1" }), mergeClips(dir, "r001", { token: "1" }), addClip(dir, { video: "v1", cutStart: 1, cutEnd: 2 }, { token: "1" }), restoreClip(dir, "r001", { token: "1" }), ]) await assert.rejects(p, StaleToken); assert.equal(await readFile(path.join(dir, MANIFEST_NAME), "utf8"), before); assert.ok(StructureRefused); } finally { await rm(dir, { recursive: true, force: true }); } }); test("runClipEditorOp: trim keeps the ruling with its clip (the key moves), widens the extent, the quote follows the words", async () => { const p = await project(); try { let view = await clipEditorView(p); await runClipEditorOp(p, { op: "ruling", clip: "r001", verdict: "keep" }); assert.equal((await rulingsOf(p.dir))["chan/v1@11.00"].verdict, "keep"); const out = await runClipEditorOp(p, { op: "trim", clip: "r001", cutStart: 9.1, cutEnd: 12.9, token: view.token }, { now: NOW }); const e = clip(await read(p.dir), "r001"); assert.deepEqual([e.start, e.end, e.cutStart, e.cutEnd], [9.1, 20, 9.1, 12.9]); assert.equal(e.quote, "w9 w10 w11 w12"); const rulings = await rulingsOf(p.dir); assert.equal(rulings["chan/v1@11.00"], undefined); assert.equal(rulings["chan/v1@9.10"].verdict, "keep"); assert.deepEqual(out.carried, { moved: 1, removed: 0, error: null }); assert.equal(out.view.clips.find((c) => c.id === "r001").ruling, "keep"); // A plain clip (no cut) is trimmed by its extent, and its quote is left alone. view = out.view; await runClipEditorOp(p, { op: "trim", clip: "plain", cutStart: 6, cutEnd: 8, token: view.token }); const plain = clip(await read(p.dir), "plain"); assert.deepEqual([plain.start, plain.end, plain.cutStart, plain.quote], [6, 8, undefined, undefined]); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("runClipEditorOp: split quotes each half from the words; merge takes the absorbed clip's ruling away; restore; cut", async () => { const p = await project(); try { await runClipEditorOp(p, { op: "split", clip: "r001", at: 15 }, { now: NOW }); let m = await read(p.dir); assert.equal(clip(m, "r001").quote, "w11 w12 w13 w14"); assert.equal(clip(m, "r001-2").quote, "w15 w16 w17 w18"); await runClipEditorOp(p, { op: "ruling", clip: "r001-2", verdict: "cut" }); assert.equal((await rulingsOf(p.dir))["chan/v1@15.00"].verdict, "cut"); const merged = await runClipEditorOp(p, { op: "merge", clip: "r001" }, { now: NOW }); assert.deepEqual(merged.carried, { moved: 0, removed: 1, error: null }); assert.equal((await rulingsOf(p.dir))["chan/v1@15.00"], undefined); await runClipEditorOp(p, { op: "restore", clip: "r001" }, { now: NOW }); m = await read(p.dir); assert.deepEqual([clip(m, "r001").cutStart, clip(m, "r001").cutEnd], [11, 19]); await assert.rejects(runClipEditorOp(p, { op: "ruling", clip: "r001", verdict: "not-him" }), /keep or cut/); await assert.rejects(runClipEditorOp(p, { op: "nope" }), /op must be/); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("runClipEditorOp: bring in an unused proposal (it leaves the list); add from a selection; set a speaker's role", async () => { const p = await project(); try { let view = await clipEditorView(p); assert.deepEqual(view.reactions.proposals.map((x) => x.id), ["p004", "p003"]); const out = await runClipEditorOp(p, { op: "bring-in", proposal: "p003", token: view.token }, { now: NOW }); view = out.view; assert.deepEqual(view.reactions.proposals.map((x) => x.id), ["p004"]); const brought = view.clips.find((c) => c.assist?.proposal === "p003"); assert.equal(brought.quote, "w62 w63 w64 w65 w66 w67"); assert.equal(brought.assist.by, "operator"); // The proposal is the cut; the window leaves 1.5 s either side. assert.deepEqual([brought.cutStart, brought.cutEnd, brought.start, brought.end], [62, 68, 60.5, 69.5]); assert.deepEqual(brought.assist.original, { start: 60.5, end: 69.5, cutStart: 62, cutEnd: 68 }); await runClipEditorOp(p, { op: "add", clip: "r002", cutStart: 41, cutEnd: 44 }, { now: NOW }); const m = await read(p.dir); assert.deepEqual(m.timeline.map((e) => e.id).slice(0, 4), ["r001", "r002", "v1-41", "k1"]); await runClipEditorOp(p, { op: "role", speaker: 1, role: "host" }); const { doc } = await readReactions(p.dir); assert.deepEqual(doc.speakers[1], { index: 1, role: "host", label: "Host", seconds: 50, turns: 3, how: "operator" }); // The clip finder's writer: one host -- the other speaker who held it is handed back as "other". assert.equal(doc.speakers[0].role, "other"); assert.ok(doc.rolesChangedAt); const after = await clipEditorView(p); assert.equal(after.reactions.rolesChangedAt, doc.rolesChangedAt); await assert.rejects(setSpeakerRole(p.dir, 9, "host"), /no speaker 9/); await assert.rejects(runClipEditorOp(p, { op: "bring-in", proposal: "p999" }), /no proposal p999/); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("clipNeighbourhood: turns, laughs and words in the span; cues and no colours with no reactions.json", async () => { const p = await project(); try { const n = await clipNeighbourhood(p, { clip: "r001", from: 8, to: 14 }); assert.deepEqual(n.turns.map((t) => t.speaker), [1, 0, 1]); assert.deepEqual(n.laughs, []); assert.equal(n.words[0].w, "w8"); assert.equal(n.duration, 80); assert.equal(n.cues, null); const none = await project({ withReactions: false }); try { const v = await clipEditorView(none); assert.equal(v.reactions, null); const m = await clipNeighbourhood(none, { clip: "r001", from: 8, to: 14 }); assert.deepEqual([m.turns, m.laughs, m.words], [[], [], null]); } finally { await rm(none.dir, { recursive: true, force: true }); } } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("carryRulings: a moved key moves its ruling, a gone clip takes its ruling, a ruled new key is not overwritten", async () => { const p = await project({ withReactions: false }); try { await setMontageRuling(p.dir, "chan/v1@11.00", { verdict: "keep" }); await setMontageRuling(p.dir, "chan/v1@31.00", { verdict: "cut" }); await setMontageRuling(p.dir, "chan/v1@51.00", { verdict: "cut" }); await setMontageRuling(p.dir, "chan/v1@52.00", { verdict: "keep" }); const before = momentKeysOf(await read(p.dir)); const after = new Map(before); after.set("r001", "chan/v1@12.00"); after.delete("r002"); after.set("r003", "chan/v1@52.00"); const r = await carryRulings(p.dir, before, after); const rulings = await rulingsOf(p.dir); assert.deepEqual(r, { moved: 1, removed: 1, error: null }); assert.equal(rulings["chan/v1@12.00"].verdict, "keep"); assert.equal(rulings["chan/v1@11.00"], undefined); assert.equal(rulings["chan/v1@31.00"], undefined); assert.equal(rulings["chan/v1@52.00"].verdict, "keep"); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("the pure rules: snap points, snapping a cut, a span's words, unused proposals, provenance in words", () => { const words = wordsOf(WORDS); const pts = snapPoints({ words: words.slice(0, 4), turns: [{ start: 0, end: 1.5, speaker: 0 }, { start: 2.5, end: 4, speaker: 1 }] }); assert.deepEqual(pts, [0, 0.9, 1.5, 1.9, 2.5, 2.9, 3.8]); assert.equal(nearestPoint(1.6, pts), 1.5); assert.equal(nearestPoint(10, pts), null); assert.deepEqual(snapCut({ cutStart: 0.95, cutEnd: 3.7 }, pts), { cutStart: 0.9, cutEnd: 3.8 }); assert.deepEqual(snapCut({ cutStart: 1.5, cutEnd: 1.8 }, [1.6, 1.7]), { cutStart: 1.5, cutEnd: 1.8 }); assert.deepEqual(snapPoints({ cues: [{ start: 0, end: 3, text: "a" }, { start: 3, end: 6, text: "b" }] }), [0, 3, 6]); assert.equal(quoteBetween({ words }, 2, 4), "w2 w3"); assert.equal(quoteBetween({ cues: [{ start: 0, end: 3, text: "one" }, { start: 3, end: 6, text: "two" }] }, 1, 3), "one"); assert.deepEqual(unusedProposals(REACTIONS, [{ assist: { proposal: "p001" } }, { assist: { proposal: "p002" } }]).map((x) => x.id), ["p004", "p003"]); assert.equal(unusedProposals(null, []).length, 0); assert.equal(provenanceWords({ by: "assistant", score: 0.834, why: "host laughs" }), "by the assistant · 0.83 · host laughs"); assert.equal(provenanceWords({ by: "operator", proposal: "p1", score: 0.5, edits: [{ what: "trim" }] }), "edited by you · the assistant scored it 0.50 · 1 edit"); assert.equal(provenanceWords({ by: "operator", edits: [{ what: "add" }] }), "added by you · 1 edit"); assert.equal(provenanceWords(null), null); }); test("manifestToken changes with every operation (the page's next write carries the new one)", async () => { const p = await project({ withReactions: false }); try { const t0 = await manifestToken(p.dir); await new Promise((r) => setTimeout(r, 15)); const out = await runClipEditorOp(p, { op: "merge", clip: "r001", token: t0 }); assert.notEqual(out.view.token, t0); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("runClipEditorOp: the video knobs -- claim (Count & stamp) and audio; the view carries them and their defaults", async () => { const p = await project({ withReactions: false }); try { const m = JSON.parse(await readFile(path.join(p.dir, MANIFEST_NAME), "utf8")); m.timeline[3].claim = { id: "k7", verdict: "CONTRADICTED" }; m.render.chrome = { engine: "hyperframes", layout: "deck", factcheck: { stamp: { seconds: 4 }, verdicts: { CONTRADICTED: { label: "Seizures", stampLabel: "SEIZURE" } } } }; await writeFile(path.join(p.dir, MANIFEST_NAME), JSON.stringify(m, null, 2) + "\n"); const v0 = await clipEditorView(p); const { verdicts, ...knobs0 } = v0.knobs; assert.deepEqual(knobs0, { hold: 4, stampShown: true, count: 1, labels: { ...v0.knobs.labels, CONTRADICTED: "SEIZURE" } }); // The marks' verdicts: the cut's own (relabelled, or in use) first, with its words. assert.deepEqual(verdicts[0], { verdict: "CONTRADICTED", label: "Seizures", stampLabel: "SEIZURE", color: "#e5534b", stock: true }); assert.equal(verdicts.length, 5); assert.equal(v0.knobs.labels.PARTLY, "Partly true"); const r = await runClipEditorOp(p, { op: "claim", clip: "r003", token: v0.token, knobs: { at: 55.5, count: 2 } }); assert.deepEqual(r.result.claim, { id: "k7", verdict: "CONTRADICTED", at: 55.5, count: 2 }); assert.equal(r.result.stamps, true); const c = r.view.clips.find((x) => x.id === "r003"); assert.equal(c.stampsOn, "r003"); assert.equal(c.claim.at, 55.5); const a = await runClipEditorOp(p, { op: "audio", clip: "r003", token: r.view.token, audio: { offset: 120, gain: -3 } }); assert.deepEqual(a.view.clips.find((x) => x.id === "r003").audio, { offset: 120, gain: -3 }); await assert.rejects(runClipEditorOp(p, { op: "claim", clip: "r001", knobs: { count: 2 } }), /carries no claim/); await assert.rejects(runClipEditorOp(p, { op: "audio", clip: "r003", audio: 5 }), /audio is/); await assert.rejects(runClipEditorOp(p, { op: "claim", clip: "r003", token: "stale", knobs: { count: 2 } }), StaleToken); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("runClipEditorOp marks: add at a second (a new id), a mark's knobs and verdict, remove, clear; the view lists every mark", async () => { const p = await project({ withReactions: false }); try { const v0 = await clipEditorView(p); assert.deepEqual(v0.clips.find((x) => x.id === "r003").marks, [], "a clip with no marks: an empty list (the box shows on every clip)"); const a = await runClipEditorOp(p, { op: "marks", clip: "r003", token: v0.token, add: { verdict: "PARTLY", at: 52 } }); assert.equal(a.result.added, "r003"); const b = await runClipEditorOp(p, { op: "marks", clip: "r003", token: a.view.token, add: { verdict: "PARTLY", at: 56 } }); assert.equal(b.result.added, "r003.2"); const c = b.view.clips.find((x) => x.id === "r003"); assert.deepEqual(c.marks.map((m) => [m.id, m.at, m.stampsOn]), [["r003", 52, "r003"], ["r003.2", 56, "r003"]]); assert.equal(c.claim.id, "r003", "claim: the first mark"); const k = await runClipEditorOp(p, { op: "claim", clip: "r003", token: b.view.token, mark: "r003.2", knobs: { count: 2 } }); assert.equal(k.view.clips.find((x) => x.id === "r003").marks[1].count, 2); const v = await runClipEditorOp(p, { op: "marks", clip: "r003", token: k.view.token, mark: "r003.2", verdict: "CONTRADICTED" }); assert.equal(v.view.clips.find((x) => x.id === "r003").marks[1].verdict, "CONTRADICTED"); await assert.rejects(runClipEditorOp(p, { op: "marks", clip: "r003", token: v.view.token, add: { verdict: "PARTLY", at: 53 } }), /overlap/); const r = await runClipEditorOp(p, { op: "marks", clip: "r003", token: v.view.token, remove: "r003" }); assert.deepEqual(r.view.clips.find((x) => x.id === "r003").marks.map((m) => m.id), ["r003.2"]); const z = await runClipEditorOp(p, { op: "marks", clip: "r003", token: r.view.token, set: null }); assert.deepEqual(z.view.clips.find((x) => x.id === "r003").marks, []); await assert.rejects(runClipEditorOp(p, { op: "marks", clip: "r003" }), /marks needs add, remove, set, or mark/); } finally { await rm(p.dir, { recursive: true, force: true }); } });