// The research case store (cases.mjs): ids, the snapshot discipline, the // arrangement, and the lifecycle store's rules -- serialised, atomic, an // unreadable file never overwritten. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { CaseError, addQuote, arrangeCase, caseFile, createCase, isCaseId, listCases, mutateCase, patchQuote, readCase, removeQuote, slugOf, } from "./cases.mjs"; const root = () => mkdtemp(path.join(os.tmpdir(), "umtool-cases-")); const cite = (video, t, extra = {}) => ({ kind: "video", source: "local:/x", channel: "harbor-radio", video, t, end: t + 3.8, url: `https://a.example/?v=harbor-radio%2F${video}&t=${t}`, title: "Ep", ...extra }); test("ids: a piece segment that is not reserved; a title's slug", () => { assert.equal(isCaseId("employee-count"), true); for (const bad of ["archive", "sessions", "census", "runs", "Bad", "-x", "", "a/b"]) assert.equal(isCaseId(bad), false, bad); assert.equal(slugOf(" Employee count: “how many?” "), "employee-count-how-many"); assert.equal(slugOf("Café déjà vu"), "cafe-deja-vu"); }); test("create: from a query (the title), from a title; a taken slug gets -2; a named id must be free", async () => { const r = await root(); const a = await createCase(r, { query: "how many employees", scope: { source: "remote:https://j.example", channels: ["x"], dateFrom: "2024-01-01", junk: 1 } }); assert.equal(a.id, "how-many-employees"); assert.equal(a.title, "how many employees"); assert.deepEqual(a.scope, { source: "remote:https://j.example", channels: ["x"], dateFrom: "2024-01-01", query: "how many employees" }); const b = await createCase(r, { title: "How many employees" }); assert.equal(b.id, "how-many-employees-2"); await assert.rejects(createCase(r, { title: "x", id: "how-many-employees" }), /already exists/); await assert.rejects(createCase(r, { title: "x", id: "archive" }), /reserved/); await assert.rejects(createCase(r, {}), /needs a title or a query/); const c = await createCase(r, { title: "Archive" }); assert.equal(c.id, "archive-case", "a reserved slug is not a case id"); const list = await listCases(r); assert.deepEqual(list.map((x) => x.id).sort(), ["archive-case", "how-many-employees", "how-many-employees-2"]); assert.equal(list.find((x) => x.id === "how-many-employees").piece, "c:how-many-employees"); }); test("a pin is a snapshot; the same cite twice is one quote; a search's log becomes the case's", async () => { const r = await root(); await createCase(r, { title: "Board" }); const search = { session: "FAKE-TEST-VALUE", query: "budget", total: 8, complete: true, notReached: [] }; const one = await mutateCase(r, "board", (d) => addQuote(d, { text: "the budget was never approved", cite: cite("v1", 600) }, { search, scope: { source: "local:/x", channels: ["harbor-radio"] } })); assert.equal(one.result.duplicate, false); assert.equal(one.result.quote.id, "q1"); assert.equal(one.case.search.session, search.session); assert.deepEqual(one.case.sessions, [search.session]); assert.deepEqual(one.case.scope, { source: "local:/x", channels: ["harbor-radio"], query: "budget" }, "the first pin's search scope becomes the case's"); const again = await mutateCase(r, "board", (d) => addQuote(d, { text: "other words", cite: cite("v1", 600.2) })); assert.equal(again.result.duplicate, true); assert.equal(again.case.quotes.length, 1); assert.equal(again.case.quotes[0].text, "the budget was never approved", "a duplicate pin never rewrites the snapshot"); const alt = await mutateCase(r, "board", (d) => addQuote(d, { text: "on en", cite: cite("v1", 600, { trackId: "en" }) })); assert.equal(alt.result.duplicate, false, "another track is another quote"); await assert.rejects(mutateCase(r, "board", (d) => addQuote(d, { text: "", cite: cite("v2", 1) })), /has words/); }); test("arrange: sections as a whole list, every quote in order; a removed section's quotes go to Unsorted", async () => { const r = await root(); await createCase(r, { title: "Board" }); for (const t of [1, 2, 3]) await mutateCase(r, "board", (d) => addQuote(d, { text: `line ${t}`, cite: cite(`v${t}`, t) })); const s = await mutateCase(r, "board", (d) => arrangeCase(d, { sections: [{ title: "First" }, { title: "Second" }] })); assert.deepEqual(s.case.sections, [{ id: "s1", title: "First" }, { id: "s2", title: "Second" }]); const o = await mutateCase(r, "board", (d) => arrangeCase(d, { order: [{ id: "q3", section: "s1" }, { id: "q1", section: "s2" }, { id: "q2", section: null }] }), ); assert.deepEqual(o.case.quotes.map((q) => [q.id, q.section]), [["q3", "s1"], ["q1", "s2"], ["q2", null]]); await assert.rejects(mutateCase(r, "board", (d) => arrangeCase(d, { order: ["q1", "q2"] })), /every quote/); await assert.rejects(mutateCase(r, "board", (d) => arrangeCase(d, { order: [{ id: "q1", section: "s9" }, { id: "q2", section: null }, { id: "q3", section: null }] })), /no section/); await assert.rejects(mutateCase(r, "board", (d) => arrangeCase(d, { sections: [{ id: "s9", title: "x" }] })), /no section/); const gone = await mutateCase(r, "board", (d) => arrangeCase(d, { sections: [{ id: "s2", title: "Second, renamed" }] })); assert.deepEqual(gone.case.sections, [{ id: "s2", title: "Second, renamed" }]); assert.equal(gone.case.quotes.find((q) => q.id === "q3").section, null); const added = await mutateCase(r, "board", (d) => arrangeCase(d, { sections: [{ id: "s2", title: "Second" }, { title: "Third" }] })); assert.equal(added.case.sections[1].id, "s3", "a new section never reuses a removed one's id"); }); test("a quote's claim, note, speaker check; the current words only on the operator's word, the old kept", async () => { const r = await root(); await createCase(r, { title: "Board" }); await mutateCase(r, "board", (d) => addQuote(d, { text: "nine to one", cite: cite("v1", 10) })); const p = await mutateCase(r, "board", (d) => patchQuote(d, "q1", { claim: " The vote ", note: "panel", speakerChecked: true })); assert.deepEqual([p.result.claim, p.result.note, p.result.speakerChecked], ["The vote", "panel", true]); await assert.rejects(mutateCase(r, "board", (d) => patchQuote(d, "q1", { useCurrentWords: true })), /only a quote re-verify found drifted/); await mutateCase(r, "board", (d) => { d.quotes[0].verify = { status: "drifted", at: "2026-10-14T00:00:00.000Z", now: "eight to one", nowAt: 11 }; }); const took = await mutateCase(r, "board", (d) => patchQuote(d, "q1", { useCurrentWords: true })); assert.equal(took.result.text, "eight to one"); assert.equal(took.result.cite.t, 11); assert.equal(took.result.history[0].text, "nine to one"); assert.equal(took.result.verify.status, "ok"); const rm = await mutateCase(r, "board", (d) => removeQuote(d, "q1")); assert.equal(rm.case.quotes.length, 0); await assert.rejects(mutateCase(r, "board", (d) => patchQuote(d, "q1", { claim: "x" })), /no quote/); }); test("a case.json that does not parse is never overwritten; a missing case is a 404", async () => { const r = await root(); await assert.rejects(readCase(r, "nope"), (e) => e instanceof CaseError && e.status === 404); await mkdir(path.dirname(caseFile(r, "broken")), { recursive: true }); await writeFile(caseFile(r, "broken"), "{ not json"); await assert.rejects(mutateCase(r, "broken", () => {}), (e) => e.status === 409); assert.equal(await readFile(caseFile(r, "broken"), "utf8"), "{ not json"); const list = await listCases(r); assert.equal(list.length, 1); assert.match(list[0].error, /not JSON/); // A directory with no case.json is not a case (a half-made one). await mkdir(path.join(r, ".umtool", "research", "half"), { recursive: true }); assert.equal((await listCases(r)).length, 1); }); test("writes are serialised: concurrent pins all land", async () => { const r = await root(); await createCase(r, { title: "Board" }); await Promise.all(Array.from({ length: 12 }, (_, i) => mutateCase(r, "board", (d) => addQuote(d, { text: `w${i}`, cite: cite(`v${i}`, i) })))); const d = await readCase(r, "board"); assert.equal(d.quotes.length, 12); assert.equal(new Set(d.quotes.map((q) => q.id)).size, 12); });