import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, 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/fetch-chat-backlog/route.test.ts" // // A channel's stream chats after its downloads (2026-10-26): the dry run counts // them from the disk with no job; the body is checked before anything. const corpus = await setupOpsCorpus(null); await corpus.writeSettings(); await corpus.writeChannelConfig("demo-streams", { handling: "youtube", url: "https://www.youtube.com/@demo", chat: "later" }); const DATA = path.join(corpus.transcripts, "channels", "demo-streams", "data"); for (const [id, live] of [["stream00001", "was_live"], ["stream00002", "was_live"], ["upload00001", "not_live"]]) { await mkdir(path.join(DATA, id), { recursive: true }); await writeFile( path.join(DATA, id, "metadata.info.json"), JSON.stringify({ id, title: id, live_status: live, upload_date: "20251001", webpage_url: `https://www.youtube.com/watch?v=${id}` }), ); } const { POST } = await import("./route"); test.after(() => corpus.cleanup()); test("a dry run counts the streams whose chat is not here, and starts no job", async () => { const before = await corpus.listJobIds(); const res = await callPost(POST, { slug: "demo-streams", dryRun: true }); assert.equal(res.status, 200, JSON.stringify(res.body)); assert.equal(res.body.chats, 2); assert.equal(res.body.notStreams, 1); assert.deepEqual([...(res.body.ids as string[])].sort(), ["stream00001", "stream00002"]); assert.deepEqual(await corpus.listJobIds(), before); }); test("the body is checked first: a bad limit, a limit on a dry run, an unknown key", async () => { assert.match(String((await callPost(POST, { slug: "demo-streams", limit: 0 })).body.error), /"limit" must be a whole number above 0/); assert.match(String((await callPost(POST, { slug: "demo-streams", dryRun: true, limit: 5 })).body.error), /a dry run starts no job/); assert.equal((await callPost(POST, { slug: "demo-streams", ids: ["x"] })).status, 400); });