import test from "node:test"; import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { setupOpsCorpus, callPost } from "../_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/channel-rank/route.test.ts" // // `channel-rank`: one channel placed in the order through the deck's one // writer. Every REFUSAL here is answered before anything is written; the // writes end in revalidatePath, which a bare tsx process has no request store // for, so they are driven over HTTP in e2e/ops-api.spec.ts. const corpus = await setupOpsCorpus(null); for (const slug of ["alpha", "beta", "gamma", "off"]) { await corpus.writeChannelConfig(slug, { url: `https://www.youtube.com/@${slug}` }); } await corpus.writeSettings({ channelPriority: { focus: { kind: "none" }, channels: { alpha: { tier: "normal", rank: 0 }, beta: { tier: "normal", rank: 1 }, off: { tier: "paused" } }, }, }); const { POST } = await import("./route"); test.after(() => corpus.cleanup()); const settingsText = () => readFile(corpus.settingsFile, "utf8"); test("exactly one placement, of the right type; unknown keys refused", async () => { const before = await settingsText(); for (const [body, re] of [ [{ slug: "alpha" }, /give exactly one placement/], [{ slug: "alpha", top: true, bottom: true }, /give exactly one placement .*; got "top", "bottom"/], [{ slug: "alpha", rank: -1 }, /"rank" must be a whole number, 0 or more/], [{ slug: "alpha", rank: 1.5 }, /"rank" must be a whole number/], [{ slug: "alpha", rank: "2" }, /"rank" must be a whole number/], [{ slug: "alpha", top: false }, /"top" must be true/], [{ slug: "alpha", before: "../x" }, /not a valid channel slug/], [{ slug: "alpha", first: true }, /first/], ] as const) { const r = await callPost(POST, body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(String(r.body.error), re, JSON.stringify(r.body)); } assert.equal(await settingsText(), before, "nothing written"); }); test("an unknown channel is a 404; an unknown anchor, an unranked anchor, a paused-everywhere channel are refused", async () => { const before = await settingsText(); const missing = await callPost(POST, { slug: "nope", top: true }); assert.equal(missing.status, 404); assert.match(String(missing.body.error), /Channel "nope" not found/); const anchor = await callPost(POST, { slug: "alpha", after: "nope" }); assert.equal(anchor.status, 400); assert.match(String(anchor.body.error), /Channel "nope" not found \(the "after" anchor\)/); const unranked = await callPost(POST, { slug: "alpha", before: "gamma" }); assert.equal(unranked.status, 400); assert.match(String(unranked.body.error), /"gamma" has no rank, so there is no place before it/); const self = await callPost(POST, { slug: "beta", after: "beta" }); assert.match(String(self.body.error), /cannot be placed after itself/); const paused = await callPost(POST, { slug: "off", top: true }); assert.equal(paused.status, 400); assert.match(String(paused.body.error), /"off" is paused for every operation/); assert.equal(await settingsText(), before, "nothing written"); });