// Wrong speaker: the walk's structured reason (`reason: "wrong-speaker"` // beside `verdict: "incorrect"`), the one write that marks a clip so and // takes it out of the cut (removeWrongSpeaker) with what named it // re-anchored, and the record of it (removed.json). // // 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, REMOVED_FILE, patchClipEntry, readRemoved, reanchorAfterRemove, removeEntry, removeWrongSpeaker, undoStructural, } from "./manifest.mjs"; import { correctionsOf, incorrectWhy } from "../projects/report.mjs"; const clip = (id, section, extra = {}) => ({ type: "clip", id, video: `v-${id}`, start: 10, end: 20, section, ...extra }); const base = () => ({ slug: "t", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, transition: 0.5, chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [{ after: "e01", label: "Weeks pass" }] }, chapter: { list: [{ at: "e01", title: "Part two" }] }, pair: { pairs: [{ topic: "Then", then: { clip: "c01", date: "2020" }, now: { clip: "e01", date: "2024" } }] }, status: { items: [{ item: "The suit", announced: { clip: "e01" }, evidence: ["c01", "e01"] }] }, }, }, timeline: [clip("c01", 0), clip("e01", 1, { channel: "other", cutStart: 11, cutEnd: 19 }), clip("c02", 1), clip("c03", 2)], }); async function project(m = base()) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-wrong-speaker-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(m, null, 2) + "\n"); return dir; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); test("the reason: wrong-speaker stands for a note; only with incorrect; anything else refused; a new verdict drops it", () => { const m = base(); // The shorthand: verdict=wrong-speaker is incorrect + the reason, no note needed. assert.equal(patchClipEntry(m, "c01", { verdict: "wrong-speaker" }).entry.reason, "wrong-speaker"); assert.equal(m.timeline[0].verdict, "incorrect"); assert.equal(incorrectWhy(m.timeline[0]), "c01 is marked incorrect: wrong speaker — remove it, fix it and re-confirm, or clear the verdict"); assert.equal(correctionsOf(m)[0].text, "Wrong speaker."); // Confirmed again: the reason goes. patchClipEntry(m, "c01", { verdict: "confirmed" }); assert.equal(m.timeline[0].reason, undefined); assert.throws(() => patchClipEntry(m, "c01", { reason: "wrong-speaker" }), /goes with an incorrect verdict/); assert.throws(() => patchClipEntry(m, "c02", { verdict: "incorrect", reason: "bad-date" }), /reason must be "wrong-speaker"/); // Without a reason an incorrect verdict still needs its note. assert.throws(() => patchClipEntry(m, "c03", { verdict: "incorrect" }), /needs its note/); patchClipEntry(m, "c03", { verdict: "incorrect", reason: "wrong-speaker", correction: "It is the guest." }); assert.equal(m.timeline[3].correction, "It is the guest."); // Clearing the reason off a note-less incorrect clip is refused (it would be a complaint with no words). const n = base(); patchClipEntry(n, "c01", { verdict: "wrong-speaker" }); assert.throws(() => patchClipEntry(n, "c01", { reason: "" }), /needs its note/); }); test("reanchorAfterRemove: a marker to the clip before, a chapter to the next in its section, pair/status references dropped", () => { const m = base(); const i = m.timeline.findIndex((e) => e.id === "e01"); const [removed] = m.timeline.splice(i, 1); const notes = reanchorAfterRemove(m, removed, i); assert.deepEqual(m.render.chrome.deck.markers, [{ after: "c01", label: "Weeks pass" }]); assert.deepEqual(m.render.chrome.chapter.list, [{ at: "c02", title: "Part two" }]); assert.equal(m.render.chrome.pair, undefined); assert.deepEqual(m.render.chrome.status.items, [{ item: "The suit", announced: {}, evidence: ["c01"] }]); assert.deepEqual(notes, [ 'deck marker 0 ("Weeks pass") moved from after e01 to after c01 (the same boundary)', 'chapter 0 ("Part two") moved from e01 to c02, the next clip in its section', 'then-and-now pair 0 ("Then") dropped: its now clip was e01', 'status item 0 ("The suit"): e01 dropped from it', ]); // No clip after it in its section: the chapter is dropped, and the module with it. const k = base(); k.render.chrome.chapter.list = [{ at: "c03", title: "Last" }]; const j = k.timeline.findIndex((e) => e.id === "c03"); const [gone] = k.timeline.splice(j, 1); assert.deepEqual(reanchorAfterRemove(k, gone, j), ['chapter 0 ("Last") dropped: no other clip in c03\'s section opens it']); assert.equal(k.render.chrome.chapter, undefined); }); test("removeEntry re-anchors instead of refusing a clip a marker names, and says so", async () => { const dir = await project(); try { const r = await removeEntry(dir, "e01"); assert.equal(r.reanchored.length, 4); const m = await read(dir); assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "c02", "c03"]); assert.equal(m.render.chrome.deck.markers[0].after, "c01"); // A plain removal is not on the wrong-speaker record. assert.deepEqual(await readRemoved(dir), []); } finally { await rm(dir, { recursive: true }); } }); test("removeWrongSpeaker: marked, removed, re-anchored and on record; undo puts the clip back marked", async () => { const dir = await project(); try { const r = await removeWrongSpeaker(dir, "e01", { correction: "Other speaker, not Ember.", now: () => new Date("2026-10-29T12:00:00Z") }); assert.equal(r.removed.verdict, "incorrect"); assert.equal(r.removed.reason, "wrong-speaker"); assert.ok(r.snapshot?.rel, "an undo point"); assert.deepEqual((await read(dir)).timeline.map((e) => e.id), ["c01", "c02", "c03"]); const rec = JSON.parse(await readFile(path.join(dir, REMOVED_FILE), "utf8")).removed; assert.equal(rec.length, 1); assert.deepEqual( { ...rec[0], reanchored: rec[0].reanchored.length }, { id: "e01", reason: "wrong-speaker", correction: "Other speaker, not Ember.", channel: "other", video: "v-e01", start: 10, end: 20, cutStart: 11, cutEnd: 19, at: 1, reanchored: 4, when: "2026-10-29T12:00:00.000Z" }, ); // Undo: the clip is back where it was, still marked; the record stays (append-only). await undoStructural(dir); const back = await read(dir); assert.deepEqual(back.timeline.map((e) => e.id), ["c01", "e01", "c02", "c03"]); assert.equal(back.timeline[1].verdict, undefined, "the undo point is the file before the mark"); assert.equal((await readRemoved(dir)).length, 1); // A card is not a clip. await assert.rejects(removeWrongSpeaker(dir, "nope"), /no timeline entry|not in the timeline|nope/); } finally { await rm(dir, { recursive: true }); } });