// THE REVIEW PASS's answers (lib/report/reviewPass.mjs): a clip's from its walk verdict, a post's or a card's from // review-pass.json; what the pass walks, where it resumes, and the store's refusals. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { clipKeepable } from "./reviewPassModel.mjs"; import { blockAnswer, entrySignature, marksAgainst, readReviewPass, reviewBlocks, reviewPassCommand, reviewProgress, setReviewMark, REVIEW_PASS_FILE } from "./reviewPass.mjs"; const timeline = [ { type: "clip", id: "c01", verdict: "confirmed" }, { type: "clip", id: "c02", verdict: "incorrect", correction: "the date is wrong" }, { type: "post", id: "p-5" }, { type: "card", id: "k01" }, { type: "clip", id: "c03", verdict: "rejected", reason: "weak" }, { type: "clip", id: "c04" }, ]; test("the pass walks every block but the intro and a clip rejected for a cut reason; a clip the bench ruled incorrect is answered by the bench, a mark wins over a verdict", () => { const view = [{ id: "intro", type: "intro" }, ...timeline.map((e) => ({ id: e.id, type: e.type, review: { verdict: e.verdict ?? null, correction: e.correction ?? null } }))]; assert.deepEqual(reviewBlocks(view).map((b) => b.id), ["c01", "c02", "p-5", "k01", "c04"]); assert.deepEqual(reviewBlocks(timeline).map((b) => b.id), ["c01", "c02", "p-5", "k01", "c04"]); assert.deepEqual(blockAnswer(timeline[0], {}), { mark: "keep", note: null }); assert.deepEqual(blockAnswer(timeline[1], {}), { mark: "ruled", note: "the date is wrong", verdict: "incorrect" }); // A Fix on a clip is a mark (never its verdict), and it is what the pass shows. assert.deepEqual(blockAnswer(timeline[0], { c01: { mark: "fix", note: "trim the tail", at: "" } }), { mark: "fix", note: "trim the tail" }); assert.equal(clipKeepable(timeline[0]), true); assert.equal(clipKeepable(timeline[1]), false); assert.equal(clipKeepable(timeline[5]), true); assert.equal(clipKeepable({ type: "clip", id: "w", verdict: "incorrect", reason: "wrong-speaker" }), false); assert.equal(blockAnswer(timeline[2], {}), null); assert.deepEqual(blockAnswer(timeline[2], { "p-5": { mark: "keep", at: "" } }), { mark: "keep", note: null }); }); test("progress counts the answered blocks and resumes on the first with none", () => { assert.deepEqual(reviewProgress(timeline, {}), { done: 2, total: 5, next: "p-5", ids: ["c01", "c02", "p-5", "k01", "c04"] }); const marks = { "p-5": { mark: "fix", note: "crop", at: "" }, k01: { mark: "keep", at: "" } }; assert.deepEqual(reviewProgress(timeline, marks).next, "c04"); assert.deepEqual(reviewProgress([...timeline.slice(0, 4)], marks), { done: 4, total: 4, next: null, ids: ["c01", "c02", "p-5", "k01"] }); }); test("the store: a post's mark written and cleared; a clip, an unknown block and a Fix with no note refused", async () => { const dir = mkdtempSync(path.join(tmpdir(), "review-pass-")); try { const m = { timeline }; assert.deepEqual((await readReviewPass(dir)).marks, {}); const r = await setReviewMark(dir, m, "p-5", "fix", { note: " the crop cuts his face ", now: new Date("2026-11-10T10:00:00Z") }); assert.deepEqual(r.mark, { mark: "fix", note: "the crop cuts his face", sig: entrySignature(timeline[2]), at: "2026-11-10T10:00:00.000Z" }); await setReviewMark(dir, m, "k01", "keep"); assert.deepEqual(Object.keys((await readReviewPass(dir)).marks).sort(), ["k01", "p-5"]); await setReviewMark(dir, m, "k01", null); assert.deepEqual(Object.keys(JSON.parse(readFileSync(path.join(dir, REVIEW_PASS_FILE), "utf8")).marks), ["p-5"]); await assert.rejects(setReviewMark(dir, m, "c04", "keep"), /walk verdict/); // A clip's Fix and its clearing are marks: its verdict is untouched. assert.deepEqual((await setReviewMark(dir, m, "c04", "fix", { note: "trim the tail", now: new Date("2026-11-10T11:00:00Z") })).mark, { mark: "fix", note: "trim the tail", sig: entrySignature(timeline[5]), at: "2026-11-10T11:00:00.000Z" }); assert.equal((await setReviewMark(dir, m, "c04", null)).mark, null); await assert.rejects(setReviewMark(dir, m, "nope", "keep"), /no block/); await assert.rejects(setReviewMark(dir, m, "k01", "fix"), /what needs work/); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("umtool review-pass: ls says where it resumes; mark writes a card's", async () => { const dir = mkdtempSync(path.join(tmpdir(), "review-pass-cli-")); try { const { writeFileSync } = await import("node:fs"); writeFileSync(path.join(dir, "video.manifest.json"), JSON.stringify({ timeline })); const out = []; const o = { resolve: async () => ({ id: "reports/x", dir }), log: (s) => out.push(String(s)), err: (s) => out.push(`ERR ${s}`) }; assert.equal(await reviewPassCommand(["reports/x"], o), 0); assert.match(out[0], /2 \/ 5 reviewed ยท resumes at p-5/); assert.equal(await reviewPassCommand(["reports/x", "mark", "k01", "keep"], o), 0); assert.equal(await reviewPassCommand(["reports/x", "mark", "c04", "keep"], o), 1); assert.equal(await reviewPassCommand(["reports/x", "mark", "c04", "fix", "--note", "the tail"], o), 0); assert.equal(await reviewPassCommand(["reports/x", "mark", "c04", "clear"], o), 0); out.length = 0; assert.equal(await reviewPassCommand(["reports/x", "--json"], o), 0); const j = JSON.parse(out.join("\n")); assert.equal(j.done, 3); assert.equal(j.next, "p-5"); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("a mark is of what was reviewed: an edge moved, a post edited, an id reused -- changed since reviewed, unanswered", async () => { const dir = mkdtempSync(path.join(tmpdir(), "review-pass-sig-")); try { const tl = [ { type: "clip", id: "c1", video: "v", start: 1, end: 3, verdict: "confirmed" }, { type: "post", id: "p1", post: "x/1", text: "the scale said 291" }, { type: "card", id: "k1", heading: "Between", seconds: 1.5 }, ]; for (const [id, mark] of [["c1", "fix"], ["p1", "keep"], ["k1", "keep"]]) await setReviewMark(dir, { timeline: tl }, id, mark, { note: mark === "fix" ? "trim" : undefined }); const doc = await readReviewPass(dir); const same = marksAgainst(doc, { timeline: tl }); assert.deepEqual(Object.values(same).map((m) => !!m.stale), [false, false, false]); // The walk's own fields are not what was reviewed: a verdict or a note added since changes nothing. assert.equal(marksAgainst(doc, { timeline: tl.map((e) => ({ ...e, walkNote: "ok", star: true })) }).c1.stale, undefined); const moved = [{ ...tl[0], cutEnd: 2.5 }, { ...tl[1], text: "the scale said 290" }, { type: "teaser", id: "k1", lines: ["reused"] }]; const now = marksAgainst(doc, { timeline: moved }); assert.deepEqual([now.c1.stale, now.p1.stale, now.k1.stale], [true, true, true]); // Stale is no answer: the clip falls back to its verdict, the post and the card are unanswered. assert.deepEqual(blockAnswer(moved[0], now), { mark: "keep", note: null }); assert.equal(blockAnswer(moved[1], now), null); assert.equal(reviewProgress(moved, now).next, "p1"); // A block gone: its mark is stale; a mark from before signatures too. assert.equal(marksAgainst(doc, { timeline: [] }).c1.stale, true); assert.equal(marksAgainst({ version: 1, marks: { c1: { mark: "keep", at: "" } } }, { timeline: tl }).c1.stale, true); } finally { rmSync(dir, { recursive: true, force: true }); } }); test("a twin id's mark is signed and judged on the same entry, its first: not stale for being a twin", async () => { const dir = mkdtempSync(path.join(tmpdir(), "review-pass-twin-")); try { const tl = [ { type: "card", id: "k1", heading: "First", seconds: 1.5 }, { type: "card", id: "k1", heading: "Second", seconds: 2 }, ]; await setReviewMark(dir, { timeline: tl }, "k1", "keep"); const doc = await readReviewPass(dir); assert.equal(doc.marks.k1.sig, entrySignature(tl[0]), "signed on the first"); assert.equal(marksAgainst(doc, { timeline: tl }).k1.stale, undefined, "and judged on the first"); assert.equal(marksAgainst(doc, { timeline: [{ ...tl[0], heading: "Changed" }, tl[1]] }).k1.stale, true); } finally { rmSync(dir, { recursive: true, force: true }); } });