// The channel context notes' review (lib/digestContextReview.mjs): the plan's // evidence parsed (from the real plan file, read only), and the one route's // Yes and No against a TEMP channels dir -- never the corpus. import { test } from "node:test"; import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { ACTIVE, REFUSED, SUGGESTED, handleDigestContext, listDigestContextNotes, parseReviewPlan } from "./digestContextReview.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const REAL_PLAN = path.join(HERE, "..", "..", "plans", "digest-context-review.md"); const sha = (t) => createHash("sha256").update(t).digest("hex"); const PLAN = `# Notes ## Why these channels, in this order | # | Channel | Chunks remaining | Note | | ---: | --- | ---: | --- | | 1 | \`alpha\` | 1,200 | drafted | | 2 | \`beta\` | 900 | drafted | ## Evidence behind each note ### \`alpha\` — **check this one first** - **Claims it is the archive of A.** Rests on titles "A plays", "A talks". - The segment labels are the channel's own. ### \`beta\`, \`gamma\` - Same show, two archives. ## What to check before promoting 1. **Identity claims** — alpha = A's archive. 2. **Cast lists** — a one-off guest listed as a regular. ## What promoting one costs, and how to tell it worked Promoting a note changes that channel's \`contextHash\`, so **every digest already generated for that channel becomes stale and regenerates.** Nothing else is touched. `; function tempChannels() { const root = mkdtempSync(path.join(os.tmpdir(), "umtool-digest-ctx-")); const channels = path.join(root, "channels"); for (const slug of ["alpha", "beta", "gamma", "plain"]) mkdirSync(path.join(channels, slug), { recursive: true }); writeFileSync(path.join(channels, "alpha", SUGGESTED), "This channel is A's archive.\n"); writeFileSync(path.join(channels, "beta", SUGGESTED), "The host is B.\n"); writeFileSync(path.join(channels, "gamma", ACTIVE), "Already active.\n"); writeFileSync(path.join(channels, "gamma", SUGGESTED), "A second draft.\n"); return { root, channels }; } const call = (channels, init) => handleDigestContext(new Request("http://x/api/digest-context", init), { channelsDir: channels, readPlan: async () => PLAN }); const post = (channels, body, headers = {}) => call(channels, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) }); test("the plan's evidence, queue, checks and cost, by channel", () => { const p = parseReviewPlan(PLAN); assert.deepEqual(p.queue, { alpha: { rank: 1, chunks: 1200 }, beta: { rank: 2, chunks: 900 } }); assert.equal(p.evidence.alpha.flag, "check this one first"); assert.equal(p.evidence.alpha.heading, "`alpha`"); assert.deepEqual(p.evidence.alpha.bullets, ['**Claims it is the archive of A.** Rests on titles "A plays", "A talks".', "The segment labels are the channel's own."]); // One heading, two channels: the evidence is each one's. assert.equal(p.evidence.gamma.bullets[0], "Same show, two archives."); assert.deepEqual(p.evidence.beta, p.evidence.gamma); assert.equal(p.checks.length, 2); assert.match(p.cost, /every digest already generated for that channel becomes stale/); }); test("the real plan file gives evidence for every one of its eleven channels", { skip: !existsSync(REAL_PLAN) && "no plans/ here" }, () => { const p = parseReviewPlan(readFileSync(REAL_PLAN, "utf8")); const eleven = ["omnibased", "HasanAbiVODs3", "destiny", "rekietalaw", "the-quartering", "chibi-reviews", "chrissie-mayr", "HasanAbiVODs", "kirsche", "nuxanor", "HasanAbiVODsbackup"]; assert.deepEqual(Object.keys(p.queue).sort(), [...eleven].sort()); for (const slug of eleven) assert.ok(p.evidence[slug]?.bullets.length, `evidence for ${slug}`); assert.equal(p.evidence.omnibased.flag, "check this one first"); assert.equal(p.queue.omnibased.chunks, 16747); assert.equal(p.checks.length, 3); assert.match(p.cost, /contextHash/); }); test("GET lists the pending notes in queue order, each with its evidence; a channel with no note is not listed", async () => { const { root, channels } = tempChannels(); try { const r = await call(channels, { method: "GET" }); assert.equal(r.status, 200); const j = await r.json(); assert.deepEqual(j.notes.map((n) => [n.slug, n.state, n.active]), [["alpha", "pending", false], ["beta", "pending", false], ["gamma", "pending", true]]); assert.equal(j.notes[0].text, "This channel is A's archive.\n"); assert.equal(j.notes[0].sha256, sha("This channel is A's archive.\n")); assert.equal(j.notes[0].chunks, 1200); assert.equal(j.notes[0].evidence.bullets.length, 2); assert.equal(j.checks.length, 2); assert.equal(j.planFound, true); } finally { rmSync(root, { recursive: true, force: true }); } }); test("Yes renames the suggestion to digest-context.md -- the note shown, never over an active one", async () => { const { root, channels } = tempChannels(); try { const text = "This channel is A's archive.\n"; // The note it was shown, or nothing. let r = await post(channels, { slug: "alpha", decision: "yes", sha256: sha("something else") }); assert.equal(r.status, 409); assert.match((await r.json()).error, /changed since it was shown/); assert.ok(existsSync(path.join(channels, "alpha", SUGGESTED))); r = await post(channels, { slug: "alpha", decision: "yes", sha256: sha(text) }); assert.equal(r.status, 200); // (alpha is in the plan's queue: listed on as promoted.) assert.equal((await r.json()).note.state, "active"); assert.equal(readFileSync(path.join(channels, "alpha", ACTIVE), "utf8"), text); assert.equal(existsSync(path.join(channels, "alpha", SUGGESTED)), false); const j = await (await call(channels, { method: "GET" })).json(); assert.deepEqual(j.notes.map((n) => [n.slug, n.state]), [["beta", "pending"], ["gamma", "pending"], ["alpha", "active"]]); // gamma has an active note already: Yes is refused, both files as they were. const g = await post(channels, { slug: "gamma", decision: "yes", sha256: sha("A second draft.\n") }); assert.equal(g.status, 409); assert.match((await g.json()).error, /already has an active digest-context\.md/); assert.equal(readFileSync(path.join(channels, "gamma", ACTIVE), "utf8"), "Already active.\n"); assert.ok(existsSync(path.join(channels, "gamma", SUGGESTED))); } finally { rmSync(root, { recursive: true, force: true }); } }); test("No records the refusal beside the note, which stays; a redrafted note is pending again", async () => { const { root, channels } = tempChannels(); try { const r = await post(channels, { slug: "beta", decision: "no", sha256: sha("The host is B.\n") }); assert.equal(r.status, 200); const { note } = await r.json(); assert.equal(note.state, "refused"); assert.equal(note.refusal.by, "operator"); assert.equal(readFileSync(path.join(channels, "beta", SUGGESTED), "utf8"), "The host is B.\n"); const rec = JSON.parse(readFileSync(path.join(channels, "beta", REFUSED), "utf8")); assert.equal(rec.sha256, sha("The host is B.\n")); let j = await (await call(channels, { method: "GET" })).json(); assert.deepEqual(j.notes.filter((n) => n.state === "pending").map((n) => n.slug), ["alpha", "gamma"]); writeFileSync(path.join(channels, "beta", SUGGESTED), "The host is B, with C.\n"); j = await (await call(channels, { method: "GET" })).json(); assert.equal(j.notes.find((n) => n.slug === "beta").state, "pending"); } finally { rmSync(root, { recursive: true, force: true }); } }); test("refused: an Authorization header (an agent), a bad name, a link out, a bad decision, a missing note", async () => { const { root, channels } = tempChannels(); try { const good = { slug: "alpha", decision: "yes", sha256: sha("This channel is A's archive.\n") }; let r = await post(channels, good, { authorization: "Bearer umtool-e2e-token" }); assert.equal(r.status, 403); assert.ok(existsSync(path.join(channels, "alpha", SUGGESTED)), "nothing renamed"); for (const slug of ["../alpha", "alpha/..", ".", "..", "a/b", ""]) { r = await post(channels, { ...good, slug }); assert.equal(r.status, 400, slug); } // A channel dir that is a link out of the channels dir is not written. const outside = path.join(root, "outside"); mkdirSync(outside); writeFileSync(path.join(outside, SUGGESTED), "x\n"); symlinkSync(outside, path.join(channels, "linked")); r = await post(channels, { slug: "linked", decision: "no", sha256: sha("x\n") }); assert.equal(r.status, 409); assert.equal(existsSync(path.join(outside, REFUSED)), false); r = await post(channels, { ...good, decision: "maybe" }); assert.equal(r.status, 400); r = await post(channels, { slug: "plain", decision: "no", sha256: sha("x") }); assert.equal(r.status, 404); r = await post(channels, { slug: "nope", decision: "no", sha256: sha("x") }); assert.equal(r.status, 404); r = await call(channels, { method: "DELETE" }); assert.equal(r.status, 405); } finally { rmSync(root, { recursive: true, force: true }); } }); test("no channels dir at all: an empty list, not an error", async () => { const j = await listDigestContextNotes({ channelsDir: path.join(os.tmpdir(), "umtool-no-such-channels-dir"), planText: null }); assert.deepEqual(j, { notes: [], checks: [], cost: "", planFound: false }); });