import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import path from "node:path"; import { setupOpsCorpus, callPost } from "../_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/articles-config/route.test.ts" // // `articles-config` (release 26 A2): an article's audience and its place in // the published list (transcripts/reports/index.json), written through the // article registry. Every refusal comes before anything is written. const corpus = await setupOpsCorpus(null); const reports = path.join(corpus.transcripts, "reports"); const article = (id: string, over: Record = {}) => ({ format: "archilyzer-report", version: 1, id, kind: "sweep", title: `Article ${id}`, sections: [{ id: "s1", title: "One", body: "Plain words." }], ...over, }); for (const id of ["first", "second"]) { await mkdir(path.join(reports, id), { recursive: true }); await writeFile(path.join(reports, id, "report.json"), JSON.stringify(article(id), null, 2)); } await mkdir(path.join(reports, "broken"), { recursive: true }); await writeFile( path.join(reports, "broken", "report.json"), JSON.stringify(article("broken", { summary: "see [x](cite:nobody)" }), null, 2), ); const { POST } = await import("./route"); test.after(() => corpus.cleanup()); const index = async () => JSON.parse(await readFile(path.join(reports, "index.json"), "utf8").catch(() => '{"published":[]}')) as { published: string[]; }; const reportJson = async (id: string) => JSON.parse(await readFile(path.join(reports, id, "report.json"), "utf8")) as Record; test("publishes, places, makes public, moves and unpublishes — the answer is what was saved", async () => { const a = await callPost(POST, { id: "first", published: true }); assert.equal(a.status, 200, JSON.stringify(a.body)); assert.deepEqual(a.body, { ok: true, id: "first", audience: "private", published: true, position: 0 }); const b = await callPost(POST, { id: "second", published: true, position: 0, audience: "public" }); assert.deepEqual(b.body, { ok: true, id: "second", audience: "public", published: true, position: 0 }); assert.deepEqual(await index(), { published: ["second", "first"] }); assert.equal((await reportJson("second")).audience, "public"); const moved = await callPost(POST, { id: "second", position: 1 }); assert.deepEqual(moved.body, { ok: true, id: "second", audience: "public", published: true, position: 1 }); assert.deepEqual(await index(), { published: ["first", "second"] }); const off = await callPost(POST, { id: "first", published: false }); assert.deepEqual(off.body, { ok: true, id: "first", audience: "private", published: false, position: null }); assert.deepEqual(await index(), { published: ["second"] }); }); test("refusals, each before anything is written", async () => { const before = await index(); const brokenBefore = await reportJson("broken"); const cases: [Record, RegExp][] = [ [{ id: "first", audience: "public", listed: true }, /unknown key\(s\): listed — this route accepts id, audience, published, position/], [{ audience: "public" }, /"id" is required/], [{ id: "Bad Id", published: true }, /not an article id/], [{ id: "missing", published: true }, /no article "missing"/], [{ id: "first", audience: "everyone" }, /"audience" must be one of public, private/], [{ id: "first", published: "yes" }, /"published" must be a boolean/], [{ id: "first", position: -1 }, /"position" must be a whole number, zero or above/], [{ id: "first", position: 0 }, /"first" is a draft/], [{ id: "second", position: 7 }, /position 7 is out of range/], [{ id: "broken", published: true }, /cannot be published while report.json has problems:[\s\S]*summary/], [{ id: "first" }, /nothing to change/], ]; for (const [body, re] of cases) { const r = await callPost(POST, body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(r.body.error ?? "", re, JSON.stringify(body)); } assert.deepEqual(await index(), before, "index.json untouched by any refusal"); assert.deepEqual(await reportJson("broken"), brokenBefore); assert.equal((await callPost(POST, { id: "first", published: true }, {})).status, 401); });