// The Posts library's calls to the editor (lane posts-library): the query a // view becomes, judging in chunks the editor takes, and the asker's refusals. // No socket: `ask` and `doFetch` are fakes. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { archiveSentence, editorAsker, judge, JUDGE_CHUNK, libraryQuery, listLibraries, listLibraryIds, listLibraryRows, purge, watch } from "./client.mjs"; function recorder(answers) { const asked = []; const ask = async (route, init = {}) => { asked.push({ route, ...init }); return typeof answers === "function" ? answers(route, init, asked.length) : answers; }; return { ask, asked }; } test("a view becomes the editor's query: only what is asked is sent", () => { assert.equal(libraryQuery({ slug: "lib" }), "/api/ops/trending?slug=lib"); assert.equal( libraryQuery({ slug: "lib", topic: "kettles", limit: 50, offset: 100, state: "unjudged", gone: true, media: true, author: "teapot_news", q: " kettle lid ", since: "2026-11-17T10:00:00.000Z", detail: true }), "/api/ops/trending?slug=lib&topic=kettles&limit=50&offset=100&state=unjudged&gone=1&media=1&author=teapot_news&q=kettle++lid&since=2026-11-17T10%3A00%3A00.000Z&detail=1", ); assert.equal(libraryQuery({ slug: "lib", topic: null, state: null, gone: false, q: " ", ids: true }), "/api/ops/trending?slug=lib&ids=1"); // The topics' seen marks ride the detail read (one read answers every topic's new posts); never without detail. assert.equal(libraryQuery({ slug: "lib", detail: true, seen: { kettles: "2026-11-17T10:00:00.000Z" }, by: "operator" }), `/api/ops/trending?slug=lib&by=operator&seen=${encodeURIComponent('{"kettles":"2026-11-17T10:00:00.000Z"}').replace(/%20/g, "+")}&detail=1`); assert.equal(libraryQuery({ slug: "lib", ids: true, seen: { kettles: "2026-11-17T10:00:00.000Z" } }), "/api/ops/trending?slug=lib&ids=1"); }); test("the libraries, the ids of a filter, and every row a page at a time", async () => { const libs = recorder({ ok: true, libraries: [{ slug: "lib", name: "Lib", topics: 1, posts: 2, unjudged: 1 }] }); assert.deepEqual(await listLibraries(libs.ask), [{ slug: "lib", name: "Lib", topics: 1, posts: 2, unjudged: 1 }]); assert.equal(libs.asked[0].route, "/api/ops/trending"); const ids = recorder({ ok: true, ids: ["1", "2"], posts: [] }); assert.deepEqual(await listLibraryIds(ids.ask, { slug: "lib", state: "drop", limit: 5, offset: 5, detail: true }), ["1", "2"]); assert.equal(ids.asked[0].route, "/api/ops/trending?slug=lib&state=drop&ids=1"); const total = 620; const rows = recorder((route) => { const offset = Number(new URL(route, "http://x").searchParams.get("offset") ?? 0); return { ok: true, total, posts: Array.from({ length: Math.min(500, total - offset) }, (_, i) => ({ id: String(offset + i) })) }; }); assert.equal((await listLibraryRows(rows.ask, { slug: "lib" })).length, total); assert.deepEqual(rows.asked.map((a) => a.route), ["/api/ops/trending?slug=lib&limit=500&detail=1", "/api/ops/trending?slug=lib&limit=500&offset=500&detail=1"]); }); test("judging goes in chunks of at most 500: one ask, so one capture job, per chunk — never one per post", async () => { const set = Array.from({ length: 2 * JUDGE_CHUNK + 7 }, (_, i) => ({ id: String(1000 + i), state: "keep" })); const r = recorder((route, init, n) => ({ ok: true, written: init.body.set.map((s) => ({ id: s.id, state: s.state })), skipped: [], kept: init.body.set.map((s) => s.id), archive: { jobId: `job-${n}`, ids: init.body.set.map((s) => s.id) } })); const out = await judge(r.ask, { slug: "lib", by: "operator", set }); assert.equal(r.asked.length, 3); assert.deepEqual(r.asked.map((a) => a.body.set.length), [500, 500, 7]); assert.ok(r.asked.every((a) => a.route === "/api/ops/judge-trending" && a.method === "POST" && a.body.by === "operator" && a.body.slug === "lib" && !("archive" in a.body))); assert.equal(out.asks, 3); assert.equal(out.kept.length, 1007); assert.deepEqual(out.archives.map((a) => a.jobId), ["job-1", "job-2", "job-3"]); assert.match(archiveSentence(out), /^1007 posts queued for archiving \(the editor's capture jobs job-1, job-2, job-3\)\.$/); // archive: false is passed on (an undo), and starts nothing. const undo = recorder({ ok: true, written: [], skipped: [], kept: [] }); await judge(undo.ask, { slug: "lib", by: "operator", set: [{ id: "1", state: "undecided" }], archive: false }); assert.equal(undo.asked[0].body.archive, false); assert.equal(archiveSentence({ kept: [], archives: [] }), ""); assert.match(archiveSentence({ kept: ["1"], archives: [{ error: "X's cookie jar is 70 minutes old", ids: ["1"] }] }), /^1 post kept but not archived now: X's cookie jar is 70 minutes old\.$/); }); test("the watch and the purge post their bodies as given", async () => { const r = recorder({ ok: true }); await watch(r.ask, { slug: "lib", topic: "kettles", on: true, by: "operator" }); await purge(r.ask, { slug: "lib", dryRun: true }); assert.deepEqual(r.asked, [ { route: "/api/ops/watch-trending", method: "POST", body: { slug: "lib", topic: "kettles", on: true, by: "operator" } }, { route: "/api/ops/purge-trending", method: "POST", body: { slug: "lib", dryRun: true } }, ]); }); test("the CLI's asker: the bearer, the editor's words on a refusal, a sentence when nothing answers or no token is set", async () => { const calls = []; const ok = editorAsker({ editorUrl: "http://editor.invalid:1/", token: "t", doFetch: async (url, init) => (calls.push({ url, init }), new Response(JSON.stringify({ ok: true, n: 1 }))) }); assert.deepEqual(await ok("/api/ops/trending"), { ok: true, n: 1 }); assert.equal(calls[0].url, "http://editor.invalid:1/api/ops/trending"); assert.equal(calls[0].init.headers.authorization, "Bearer t"); const refused = editorAsker({ editorUrl: "http://editor.invalid:1", token: "t", doFetch: async () => new Response(JSON.stringify({ ok: false, error: "lib is not an X trending library" }), { status: 400 }) }); await assert.rejects(refused("/x"), /lib is not an X trending library/); const down = editorAsker({ editorUrl: "http://editor.invalid:1", token: "t", doFetch: async () => { throw new Error("ECONNREFUSED"); } }); await assert.rejects(down("/x"), /could not reach the editor at http:\/\/editor\.invalid:1: ECONNREFUSED/); let asked = false; const none = editorAsker({ editorUrl: "http://editor.invalid:1", token: "", doFetch: async () => ((asked = true), new Response("{}")) }); await assert.rejects(none("/x"), /WORKER_TOKEN is not set/); assert.equal(asked, false); }); test("S6: a later chunk that fails is said: what was written, the editor's words, how many were never asked; the first chunk failing throws", async () => { const set = Array.from({ length: 2 * JUDGE_CHUNK + 7 }, (_, i) => ({ id: String(1000 + i), state: "drop" })); const r = recorder((route, init, n) => { if (n === 2) throw new Error("X is cooling down"); return { ok: true, written: init.body.set.map((s) => ({ id: s.id, state: s.state })), skipped: [], kept: [] }; }); const out = await judge(r.ask, { slug: "lib", by: "operator", set }); // Two asks: the third chunk was never sent after the second failed. assert.equal(r.asked.length, 2); assert.equal(out.written.length, 500); assert.deepEqual(out.failed, { error: "X is cooling down", asked: 500, unasked: 507 }); const first = recorder(() => { throw new Error("lib is not an X trending library"); }); await assert.rejects(judge(first.ask, { slug: "lib", by: "operator", set }), /not an X trending library/); });