// Why a clip is rejected on the walk (lib/report/rejectReasons.mjs): FACT // reasons (not-matching, wrong-speaker) stay the `incorrect` verdict -- a // correction for the report, blocking in `umtool check` -- while CUT reasons // (irrelevant, repeats, weak, privacy, other) are `verdict: "rejected"`, never // a correction, and an OPEN check row while the clip is still in the cut. // Manifests written before (incorrect, with or without wrong-speaker; a legacy // correction with no verdict) read exactly as they did. // // Run with: pnpm test:scripts 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 { MANIFEST_NAME, REMOVED_FILE, patchClipEntry, readRemoved, rejectClip, undoStructural } from "./manifest.mjs"; import { CUT_REASONS, FACT_REASONS, REJECT_REASONS, rejectPatch, rejectionOf } from "./rejectReasons.mjs"; import { clipVerdict, correctionsOf, incorrectClips, incorrectWhy, rejectedClips, rejectedWhy, reviewOf } from "../projects/report.mjs"; import { decisionsFor } from "../projects/core.mjs"; import { walkProjects } from "../projects/walk.mjs"; const clip = (id, extra = {}) => ({ type: "clip", id, video: `v-${id}`, start: 10, end: 20, ...extra }); const base = () => ({ slug: "t", provenance: { channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30 }, timeline: [clip("c01"), clip("c02"), clip("c03")] }); test("the vocabulary: seven reasons in menu order, two of them fact errors", () => { assert.deepEqual(REJECT_REASONS.map((r) => r.id), ["not-matching", "wrong-speaker", "irrelevant", "repeats", "weak", "privacy", "other"]); assert.deepEqual(FACT_REASONS, ["wrong-speaker"]); assert.deepEqual(CUT_REASONS, ["irrelevant", "repeats", "weak", "privacy", "other"]); // Only "doesn't match" says doesn't match. assert.deepEqual(REJECT_REASONS.filter((r) => /match/.test(r.label)).map((r) => r.id), ["not-matching"]); }); test("rejectPatch: a fact reason is incorrect (the note its correction); a cut reason is rejected (the note its rejectNote)", () => { assert.deepEqual(rejectPatch("not-matching", "Says 2019, not 2020."), { verdict: "incorrect", reason: "", correction: "Says 2019, not 2020." }); assert.throws(() => rejectPatch("not-matching", ""), /needs its note/); assert.deepEqual(rejectPatch("wrong-speaker"), { verdict: "incorrect", reason: "wrong-speaker" }); assert.deepEqual(rejectPatch("irrelevant"), { verdict: "rejected", reason: "irrelevant", rejectNote: "" }); assert.deepEqual(rejectPatch("repeats", "c01"), { verdict: "rejected", reason: "repeats", rejectNote: "c01" }); assert.throws(() => rejectPatch("other", " "), /other needs a note/); assert.throws(() => rejectPatch("boring"), /a reject reason is one of/); }); test("the writer: rejected + a cut reason; shorthands; a reason only with the verdict it explains; a new verdict drops them", () => { const m = base(); // Shorthand: verdict= is rejected + that reason. patchClipEntry(m, "c01", { verdict: "irrelevant", rejectNote: "about the 2019 tour" }); assert.deepEqual([m.timeline[0].verdict, m.timeline[0].reason, m.timeline[0].rejectNote], ["rejected", "irrelevant", "about the 2019 tour"]); assert.equal(m.timeline[0].correction, undefined, "a cut reason is never a correction"); // Spelled out. patchClipEntry(m, "c02", { verdict: "rejected", reason: "weak" }); assert.equal(m.timeline[1].reason, "weak"); // Refusals, in sentences. assert.throws(() => patchClipEntry(m, "c03", { verdict: "rejected" }), /needs its reason/); assert.throws(() => patchClipEntry(m, "c03", { verdict: "rejected", reason: "wrong-speaker" }), /fact error: its verdict is incorrect/); assert.throws(() => patchClipEntry(m, "c03", { verdict: "incorrect", reason: "irrelevant" }), /not a fact error/); assert.throws(() => patchClipEntry(m, "c03", { verdict: "other" }), /other needs a note/); assert.throws(() => patchClipEntry(m, "c03", { verdict: "confirmed", rejectNote: "x" }), /goes with a rejected verdict/); assert.throws(() => patchClipEntry(m, "c03", { verdict: "maybe" }), /verdict must be "confirmed", "incorrect", "rejected"/); // not-matching is the bare incorrect verdict: no reason stored, a note required. assert.throws(() => patchClipEntry(m, "c03", { verdict: "not-matching" }), /needs its note/); patchClipEntry(m, "c03", { verdict: "not-matching", correction: "Wrong date." }); assert.deepEqual([m.timeline[2].verdict, m.timeline[2].reason], ["incorrect", undefined]); // Rejected -> incorrect: the cut reason and its note go; the correction is required again. assert.throws(() => patchClipEntry(m, "c01", { verdict: "incorrect" }), /needs its note/); patchClipEntry(m, "c01", { verdict: "wrong-speaker" }); assert.deepEqual([m.timeline[0].verdict, m.timeline[0].reason, m.timeline[0].rejectNote], ["incorrect", "wrong-speaker", undefined]); // Incorrect (wrong speaker) -> rejected: the fact reason goes with it. patchClipEntry(m, "c01", { verdict: "privacy" }); assert.deepEqual([m.timeline[0].verdict, m.timeline[0].reason], ["rejected", "privacy"]); // Confirmed: reason and note gone. patchClipEntry(m, "c02", { verdict: "confirmed" }); assert.deepEqual([m.timeline[1].verdict, m.timeline[1].reason, m.timeline[1].rejectNote], ["confirmed", undefined, undefined]); // Cleared. patchClipEntry(m, "c01", { verdict: "" }); assert.deepEqual([m.timeline[0].verdict, m.timeline[0].reason], [undefined, undefined]); }); test("back-compat: a manifest written before reasons reads exactly as before", () => { const old = { timeline: [ clip("a", { verdict: "incorrect", correction: "Other speaker, not Ember." }), clip("b", { verdict: "incorrect", reason: "wrong-speaker" }), clip("c", { correction: "wrong date" }), // the bench's legacy `x` clip("d", { verdict: "confirmed", correction: "window moved" }), clip("e"), ], }; assert.deepEqual(old.timeline.map(clipVerdict), ["incorrect", "incorrect", "incorrect", "confirmed", "unreviewed"]); assert.deepEqual(incorrectClips(old.timeline).map((e) => e.id), ["a", "b", "c"]); assert.deepEqual(rejectedClips(old.timeline), []); assert.equal(incorrectWhy(old.timeline[0]), "a is marked incorrect: Other speaker, not Ember — remove it, fix it and re-confirm, or clear the verdict"); assert.equal(incorrectWhy(old.timeline[1]), "b is marked incorrect: wrong speaker — remove it, fix it and re-confirm, or clear the verdict"); assert.deepEqual(correctionsOf(old).map((c) => [c.id, c.text, c.verdict]), [ ["a", "Other speaker, not Ember.", "incorrect"], ["b", "Wrong speaker.", "incorrect"], ["c", "wrong date", "incorrect"], ["d", "window moved", "confirmed"], ]); // And says why, in the new words. assert.deepEqual(old.timeline.map((e) => rejectionOf(e)?.why ?? null), ["doesn't match: Other speaker, not Ember", "wrong speaker", "doesn't match: wrong date", null, null]); const r = reviewOf(old); assert.deepEqual([r.incorrect, r.rejected, r.confirmed, r.unreviewed, r.reviewed], [3, 0, 1, 1, 4]); }); test("a rejected clip is not a correction: correctionsOf leaves it out unless it carries a note, and then as a note", () => { const m = { timeline: [clip("a", { verdict: "rejected", reason: "irrelevant" }), clip("b", { verdict: "rejected", reason: "weak", correction: "the window moved" })] }; assert.deepEqual(correctionsOf(m).map((c) => [c.id, c.verdict]), [["b", "rejected"]]); assert.deepEqual(incorrectClips(m.timeline), []); assert.deepEqual(rejectedClips(m.timeline).map((e) => e.id), ["a", "b"]); assert.equal(rejectedWhy(m.timeline[0], "p"), "a is rejected (irrelevant) but still in the cut — remove it (umtool timeline p reject a irrelevant), or clear the verdict"); assert.equal(reviewOf(m).rejected, 2); }); test("rejectClip: rejected, taken out, re-anchored and on record with the reason and note; undo puts it back", async () => { const dir = await mkdtemp(path.join(tmpdir(), "umtool-reject-")); try { await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(base(), null, 2) + "\n"); const r = await rejectClip(dir, "c02", "repeats", { note: "covered by c01", now: () => new Date("2026-10-30T12:00:00Z") }); assert.deepEqual([r.removed.verdict, r.removed.reason, r.removed.rejectNote], ["rejected", "repeats", "covered by c01"]); assert.ok(r.snapshot?.rel, "an undo point"); const m = JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); assert.deepEqual(m.timeline.map((e) => e.id), ["c01", "c03"]); const rec = (await readRemoved(dir)).at(-1); assert.deepEqual( { ...rec, reanchored: rec.reanchored.length }, { id: "c02", reason: "repeats", correction: null, note: "covered by c01", channel: "chan", video: "v-c02", start: 10, end: 20, at: 1, reanchored: 0, when: "2026-10-30T12:00:00.000Z" }, ); await undoStructural(dir); const back = JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); assert.deepEqual(back.timeline.map((e) => e.id), ["c01", "c02", "c03"]); assert.equal(back.timeline[1].verdict, undefined, "the undo point is the file before the rejection"); // A fact reason: incorrect, the note its correction, on record as such. const f = await rejectClip(dir, "c03", "not-matching", { note: "He says March." }); assert.deepEqual([f.removed.verdict, f.removed.reason, f.removed.correction], ["incorrect", undefined, "He says March."]); assert.deepEqual([f.record.reason, f.record.correction, "note" in f.record], ["not-matching", "He says March.", false]); // Refused before anything is written. await assert.rejects(rejectClip(dir, "c01", "not-matching"), /needs its note/); await assert.rejects(rejectClip(dir, "c01", "nope"), /a reject reason is one of/); assert.equal(JSON.parse(await readFile(path.join(dir, REMOVED_FILE), "utf8")).removed.length, 2); } finally { await rm(dir, { recursive: true }); } }); async function decisionsOf(timeline) { const root = await mkdtemp(path.join(tmpdir(), "clip-rejected-")); try { const channelsDir = path.join(root, "channels"); const reportsRoot = path.join(root, "reports"); const vdir = path.join(channelsDir, "chan", "data", "vid"); await mkdir(vdir, { recursive: true }); const cues = Array.from({ length: 200 }, (_, i) => ({ start: i, end: i + 1, text: `Line ${i}.` })); await writeFile(path.join(vdir, "transcript.cues.json"), JSON.stringify({ cues })); const dir = path.join(reportsRoot, "cut"); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "cut", title: "Cut", provenance: { channelSlug: "chan", channelsDir: path.relative(dir, channelsDir), siteOrigin: "https://example.test" }, render: { width: 1920, height: 1080, fps: 30 }, timeline: timeline.map((e) => ({ type: "clip", video: "vid", cite: Math.floor(e.start), lock: true, ...e })), }, null, 2)); const [ref] = (await walkProjects(reportsRoot)).filter((p) => p.id === "cut"); return (await decisionsFor(ref, reportsRoot)).filter((d) => d.kind === "clip-incorrect" || d.kind === "clip-rejected"); } finally { await rm(root, { recursive: true, force: true }); } } test("umtool check: a fact error still in the cut blocks; a cut reason kept in place is open, never blocking", async () => { const ds = await decisionsOf([ { id: "c01", start: 10, end: 20, verdict: "rejected", reason: "irrelevant" }, { id: "c02", start: 30, end: 40, verdict: "incorrect", reason: "wrong-speaker" }, { id: "c03", start: 50, end: 60, verdict: "rejected", reason: "other", rejectNote: "the audio clips" }, { id: "c04", start: 70, end: 80, verdict: "confirmed" }, ]); assert.deepEqual(ds.map((d) => [d.kind, d.target, d.severity, d.why]), [ ["clip-incorrect", "c02", "blocking", "c02 is marked incorrect: wrong speaker — remove it, fix it and re-confirm, or clear the verdict"], ["clip-rejected", "c01", "open", "c01 is rejected (irrelevant) but still in the cut — remove it (umtool timeline cut reject c01 irrelevant), or clear the verdict"], ["clip-rejected", "c03", "open", "c03 is rejected (other: the audio clips) but still in the cut — remove it (umtool timeline cut reject c03 other), or clear the verdict"], ]); });