import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, rm, writeFile, utimes } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { awaitingReview, draftsDecisions, draftsWhy } from "./drafts.mjs"; import { annotate, summarizeGroups } from "./groups.mjs"; import { reportSignature } from "../projects/report.mjs"; const row = (address, label, state, model = "qwen3:8b") => ({ address, label, state, drafted: { model } }); test("one row per waiting field; approved, rejected and the operator's own are not waiting", () => { const view = { rows: [ row("narrate/k01/narration", "narration", "unreviewed"), row("narrate/k02/narration", "narration", "edited"), row("onscreen/c02/onscreen", "on-screen text", "approved"), row("teaser/t01/lines", "teaser lines", "rejected"), { address: "narrate/k03/narration", label: "narration", state: "unreviewed", drafted: null }, ], }; const out = draftsDecisions({ project: "reports/x", projectTitle: "X", href: "/projects/reports/x/review", at: 5, view }); assert.deepEqual(out.map((d) => d.target), ["narrate/k01/narration", "narrate/k02/narration"]); assert.ok(out.every((d) => d.kind === "drafts-unreviewed" && d.severity === "open" && d.href === "/projects/reports/x/review")); assert.equal(out[0].why, "Narration draft · 2 fields · by qwen3:8b · 1 changed since approved"); assert.equal(out[0].what, "Narration draft · 2 fields"); assert.deepEqual(draftsDecisions({ project: "p", projectTitle: "p", href: "/", at: 0, view: { rows: [row("a", "narration", "approved")] } }), []); // A narration with no mark that a task's draft file records (release 25's narrate) waits too (P4b). const withFile = draftsDecisions({ project: "reports/x", projectTitle: "X", href: "/", at: 5, view, provenance: { "narrate/k03/narration": { file: "narration-draft.json" } } }); assert.deepEqual(withFile.map((d) => d.target), ["narrate/k01/narration", "narrate/k02/narration", "narrate/k03/narration"]); assert.deepEqual(awaitingReview(view.rows).map((r) => r.address), ["narrate/k01/narration", "narrate/k02/narration"]); }); test("the sentence names the kinds of field and the models", () => { assert.equal(draftsWhy([row("a", "narration", "unreviewed")]), "Narration draft · 1 field · by qwen3:8b"); assert.equal( draftsWhy([row("a", "narration", "unreviewed"), row("b", "on-screen text", "unreviewed", "claude-sonnet-5-5")]), "Narration and on-screen text drafts · 2 fields · by qwen3:8b, claude-sonnet-5-5", ); assert.equal( draftsWhy([row("a", "narration", "unreviewed"), row("b", "teaser lines", "unreviewed"), row("c", "flips", "unreviewed")]), "Narration, teaser lines and flips drafts · 3 fields · by qwen3:8b", ); assert.equal( draftsWhy([row("a", "narration", "unreviewed"), row("b", "teaser lines", "unreviewed"), row("c", "flips", "unreviewed"), row("d", "threads", "unreviewed")]), "Narration, teaser lines and 2 more drafts · 4 fields · by qwen3:8b", ); }); test("drafts are Review calls: one card per piece, the action Review drafts", () => { const view = { rows: [row("narrate/k01/narration", "narration", "unreviewed"), row("narrate/k02/narration", "narration", "unreviewed")] }; const rows = annotate(draftsDecisions({ project: "reports/x", projectTitle: "X", href: "/projects/reports/x/review", at: 1, view }), { pieceOf: () => "p:reports/x", lifecycleOf: () => "active", }); assert.ok(rows.every((d) => d.group === "review" && d.category === "review")); assert.deepEqual(rows[0].action, { label: "Review drafts", href: "/projects/reports/x/review" }); const review = summarizeGroups(rows).find((g) => g.id === "review"); assert.equal(review.cards.length, 1); assert.equal(review.cards[0].count, 2); // The card names itself: its title is the drafts' what, not the kind. assert.equal(review.cards[0].what, "Narration draft · 2 fields"); }); test("reportSignature moves when a review or a draft file is written, not only the manifest", async () => { const dir = await mkdtemp(path.join(os.tmpdir(), "umtool-sig-")); try { await writeFile(path.join(dir, "video.manifest.json"), "{}\n"); const a = await reportSignature(dir); await writeFile(path.join(dir, "drafts.review.json"), '{"rows":{}}\n'); const b = await reportSignature(dir); assert.notEqual(b, a, "drafts.review.json"); await writeFile(path.join(dir, "narration.review.json"), "{}\n"); const c = await reportSignature(dir); assert.notEqual(c, b, "narration.review.json"); await writeFile(path.join(dir, "onscreen-draft.json"), "{}\n"); const d = await reportSignature(dir); assert.notEqual(d, c, "a -draft.json"); // A rewrite of the same size is still seen: the mtime is signed. const later = new Date(Date.now() + 5000); await utimes(path.join(dir, "drafts.review.json"), later, later); assert.notEqual(await reportSignature(dir), d, "the review's mtime"); // A file that only looks like one is not signed. const e = await reportSignature(dir); await writeFile(path.join(dir, "notes-draft.txt"), "x"); assert.equal(await reportSignature(dir), e); } finally { await rm(dir, { recursive: true, force: true }); } });