// A triage's marks (R6): set, unmark, per run, the file shape; a broken file // is never overwritten. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { MarksError, marksFile, marksOfRun, readMarks, setMark } from "./triage-marks.mjs"; test("marks: right and wrong per flag, kept per schedule; unmark; a broken file refuses", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "triage-marks-")); try { assert.deepEqual((await readMarks(root, "co-host-watch")).marks, {}); const now = new Date("2026-10-14T10:00:00Z"); await setMark(root, "co-host-watch", { k: 2, f: "f1", verdict: "right", score: 3, id: "v1", line: "a line", now }); await setMark(root, "co-host-watch", { k: 2, f: "f2", verdict: "wrong", score: 2, now }); const doc = await setMark(root, "co-host-watch", { k: 3, f: "f1", verdict: "wrong", score: 2, now }); assert.deepEqual(Object.keys(doc.marks).sort(), ["2/f1", "2/f2", "3/f1"]); assert.deepEqual(marksOfRun(doc, 2), { f1: { verdict: "right", score: 3, at: now.toISOString(), id: "v1", line: "a line" }, f2: { verdict: "wrong", score: 2, at: now.toISOString() } }); const after = await setMark(root, "co-host-watch", { k: 2, f: "f2", verdict: null }); assert.deepEqual(Object.keys(after.marks).sort(), ["2/f1", "3/f1"]); const onDisk = JSON.parse(await readFile(marksFile(root, "co-host-watch"), "utf8")); assert.equal(onDisk.format, "umtool-triage-marks"); await assert.rejects(setMark(root, "co-host-watch", { k: 2, f: "nope", verdict: "right" }), MarksError); await assert.rejects(setMark(root, "Bad Id", { k: 1, f: "f1", verdict: "right" }), /not a schedule id/); await mkdir(path.dirname(marksFile(root, "broken")), { recursive: true }); await writeFile(marksFile(root, "broken"), "{ nope"); await assert.rejects(setMark(root, "broken", { k: 1, f: "f1", verdict: "right" }), (e) => e.status === 409); assert.equal(await readFile(marksFile(root, "broken"), "utf8"), "{ nope"); } finally { await rm(root, { recursive: true, force: true }); } });