import test from "node:test"; import assert from "node:assert/strict"; import { readFile } 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/site-config/route.test.ts" // // `site-config` (release 24 H6): a site's cited-only channels and its publish // policy, written through patchSite — so through writeSite, as the site form's // save is. Every refusal comes before anything is written. const corpus = await setupOpsCorpus(null); for (const slug of ["pooled", "critic", "other-critic"]) { await corpus.writeChannelConfig(slug, { url: `https://www.youtube.com/@${slug}` }); } const pool = { channels: [{ slug: "pooled", groupId: "default" }] }; await corpus.writeSite("citing", pool); await corpus.writeSite("mine", { ...pool, audience: "private" }); await corpus.writeSite("shipped", { ...pool, cloudflareProject: "shipped-pages" }); const { POST } = await import("./route"); test.after(() => corpus.cleanup()); const siteFile = async (id: string) => JSON.parse(await readFile(path.join(corpus.transcripts, "sites", id, "site.json"), "utf8")) as Record< string, unknown >; test("sets the cited scope, drops a pooled slug as the parser does, and [] clears it", async () => { const set = await callPost(POST, { siteId: "citing", citedChannels: ["critic", "pooled", "other-critic", "critic", " "], }); assert.equal(set.status, 200, JSON.stringify(set.body)); assert.deepEqual(set.body, { ok: true, siteId: "citing", citedChannels: ["critic", "other-critic"], publishAuto: "off", }); const file = await siteFile("citing"); assert.deepEqual(file.citedChannels, [{ slug: "critic" }, { slug: "other-critic" }]); assert.deepEqual(file.channels, [{ slug: "pooled", groupId: "default" }], "the pool is untouched"); const cleared = await callPost(POST, { siteId: "citing", citedChannels: [] }); assert.equal(cleared.status, 200, JSON.stringify(cleared.body)); assert.deepEqual(cleared.body.citedChannels, []); assert.equal("citedChannels" in (await siteFile("citing")), false, "an empty scope is not written"); }); test("publishAuto round-trips; a deploying policy needs a Pages project; a private site is clamped to build", async () => { for (const policy of ["build", "preview", "production", "off"]) { const r = await callPost(POST, { siteId: "shipped", publishAuto: policy }); assert.equal(r.status, 200, JSON.stringify(r.body)); assert.equal(r.body.publishAuto, policy); const file = await siteFile("shipped"); if (policy === "off") assert.equal("publish" in file, false); else assert.deepEqual(file.publish, { auto: policy }); } const before = await siteFile("citing"); const refused = await callPost(POST, { siteId: "citing", publishAuto: "production" }); assert.equal(refused.status, 400); assert.match(refused.body.error ?? "", /publish\.auto "production" deploys the site, and it has no cloudflareProject/); assert.deepEqual(await siteFile("citing"), before, "nothing written"); // A private site is never deployed: "production" is saved as "build". const clamped = await callPost(POST, { siteId: "mine", publishAuto: "production" }); assert.equal(clamped.status, 200, JSON.stringify(clamped.body)); assert.equal(clamped.body.publishAuto, "build"); assert.deepEqual((await siteFile("mine")).publish, { auto: "build" }); // Both keys in one call. const both = await callPost(POST, { siteId: "shipped", citedChannels: ["critic"], publishAuto: "build" }); assert.deepEqual(both.body, { ok: true, siteId: "shipped", citedChannels: ["critic"], publishAuto: "build" }); }); test("refusals, each before anything is written: keys, site, slugs, policy, nothing to change", async () => { const before = await siteFile("citing"); const cases: [Record, RegExp][] = [ [{ siteId: "citing", channels: ["critic"] }, /unknown key\(s\): channels — this route accepts siteId, citedChannels, publishAuto/], [{ siteId: "citing", citedChannels: ["critic"], listed: false }, /unknown key\(s\): listed/], [{ siteId: "no-such-site", citedChannels: ["critic"] }, /Site "no-such-site" not found/], [{ siteId: "Bad Id", publishAuto: "build" }, /not a valid site id/], [{ citedChannels: ["critic"] }, /"siteId" is required/], [{ siteId: "citing", citedChannels: ["critic", "no-such-channel"] }, /not a channel: "no-such-channel"/], [{ siteId: "citing", citedChannels: ["../etc"] }, /not a valid channel slug/], [{ siteId: "citing", citedChannels: "critic" }, /"citedChannels" must be an array of channel slugs/], [{ siteId: "citing", publishAuto: "sometimes" }, /"publishAuto" must be one of off, build, preview, production/], [{ siteId: "citing" }, /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 siteFile("citing"), before, "nothing written by any refusal"); assert.equal((await callPost(POST, { siteId: "citing", publishAuto: "build" }, {})).status, 401); });