// The Posts library page's presses (lane posts-library): each one the // editor's action with `by: "operator"` stamped on the server, whatever the // body says. A fake `ask`: no socket. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { judgePress, PressError, purgePress, seenPress, watchPress } from "./presses.mjs"; function editor(rows = [], { failAt = 0 } = {}) { const asked = []; let judged = 0; const ask = async (route, init = {}) => { asked.push({ route, ...init }); if (route === "/api/ops/trending") return { ok: true, libraries: [{ slug: "lib", name: "Lib", topics: 2, posts: rows.length, unjudged: 0 }] }; if (route === "/api/ops/judge-trending" && ++judged === failAt) throw new Error("X is cooling down for 14 minutes"); if (route.startsWith("/api/ops/trending?") && new URL(route, "http://x").searchParams.get("detail") !== "1") return { ok: true, topics: [{ id: "kettles" }, { id: "cups" }], posts: [], total: 0 }; if (route.startsWith("/api/ops/trending")) { const offset = Number(new URL(route, "http://x").searchParams.get("offset") ?? 0); return { ok: true, total: rows.length, posts: rows.slice(offset, offset + 500) }; } if (route === "/api/ops/judge-trending") { const kept = init.body.set.filter((s) => s.state === "keep" && s.id).map((s) => s.id); return { ok: true, written: init.body.set.map(({ note: _n, ...s }) => s), skipped: [], kept, ...(kept.length && init.body.archive !== false ? { archive: { jobId: `cap-${asked.length}`, ids: kept } } : {}) }; } return { ok: true, echoed: init.body }; }; return { ask, asked, posts: () => asked.filter((a) => a.method === "POST") }; } test("a press on the posts named is the operator's, whatever the body says about who", async () => { const e = editor(); const out = await judgePress(e.ask, { lib: "lib", by: "agent", set: [{ id: "1900000000000001001", state: "keep", note: "the opening", by: "agent" }, { thread: "1900000000000001002", state: "drop" }] }); assert.equal(e.posts().length, 1); assert.deepEqual(e.posts()[0].body, { slug: "lib", by: "operator", set: [{ id: "1900000000000001001", state: "keep", note: "the opening" }, { thread: "1900000000000001002", state: "drop" }] }); assert.equal(out.ok, true); assert.match(out.sentence, /^2 judgements written\. 1 post queued for archiving \(the editor's capture job cap-1\)\.$/); }); test("an undo archives nothing", async () => { const e = editor(); const out = await judgePress(e.ask, { lib: "lib", undo: true, set: [{ id: "1900000000000001001", state: "keep" }, { id: "1900000000000001002", state: "undecided", note: "dropped" }] }); assert.equal(e.posts()[0].body.archive, false); assert.equal(e.posts()[0].body.by, "operator"); assert.deepEqual(e.posts()[0].body.set[1], { id: "1900000000000001002", state: "undecided" }); assert.equal(out.sentence, "Undone: 2 judgements put back."); }); test("bulk: every post the view matches, in chunks of at most 500 (one capture job per chunk), with the set that undoes it", async () => { const rows = Array.from({ length: 620 }, (_, i) => ({ id: String(1900000000000002000n + BigInt(i)), thread: String(1900000000000002000n + BigInt(i)), detail: i === 0 ? { ownJudgement: { state: "later", note: "check", by: "agent", at: "t" } } : {} })); const e = editor(rows); const seen = []; const out = await judgePress(e.ask, { lib: "lib", by: "agent", bulk: { view: { lib: "other", topic: "kettles", state: "unjudged", media: "1", new: "1" }, state: "keep", note: "all of it" }, expect: 620 }, { seenAt: async (lib, topic) => (seen.push([lib, topic]), "2026-11-17T10:00:00.000Z") }); // The view's library is the body's `lib`, never the view's own. const lists = e.asked.filter((a) => a.route.startsWith("/api/ops/trending")); assert.deepEqual(seen, [["lib", "kettles"]]); assert.equal(lists[0].route, "/api/ops/trending?slug=lib&topic=kettles&limit=500&state=unjudged&media=1&since=2026-11-17T10%3A00%3A00.000Z&detail=1"); const judged = e.posts(); assert.deepEqual(judged.map((p) => p.body.set.length), [500, 120]); assert.ok(judged.every((p) => p.body.by === "operator" && p.body.set.every((s) => s.state === "keep" && s.note === "all of it" && s.id))); assert.equal(out.archives.length, 2); assert.equal(out.undo.length, 620); assert.deepEqual(out.undo[0], { id: rows[0].id, state: "later", note: "check" }); assert.deepEqual(out.undo[1], { id: rows[1].id, state: "undecided" }); assert.match(out.sentence, /^620 judgements kept\. 620 posts queued for archiving \(the editor's capture jobs cap-\d+, cap-\d+\)\.$/); }); test("bulk judges nothing when the filter no longer matches the count the confirm showed", async () => { const e = editor([{ id: "1900000000000001001", thread: "1900000000000001001" }]); await assert.rejects(judgePress(e.ask, { lib: "lib", bulk: { view: {}, state: "drop" }, expect: 3 }), (err) => err instanceof PressError && err.status === 409 && /now matches 1 post, not the 3 the confirm showed: nothing was judged/.test(err.message)); assert.equal(e.posts().length, 0); await assert.rejects(judgePress(e.ask, { lib: "lib", bulk: { view: {}, state: "drop" } }), /"expect"/); }); test("refused before anything is asked: no library, both or neither of set and bulk, an ill-shaped set", async () => { const e = editor(); for (const [body, re] of [ [null, /JSON object/], [{ set: [] }, /"lib"/], [{ lib: "../x", set: [{ id: "1", state: "keep" }] }, /"lib"/], [{ lib: "lib" }, /one of them/], [{ lib: "lib", set: [{ id: "1", state: "keep" }], bulk: {} }, /one of them/], [{ lib: "lib", set: [] }, /"set" is a list/], [{ lib: "lib", set: [{ id: "1", thread: "1", state: "keep" }] }, /one of them/], [{ lib: "lib", set: [{ id: "../x", state: "keep" }] }, /not a post id/], [{ lib: "lib", set: [{ id: "1", state: "deleted" }] }, /"state" is one of keep, drop, later, undecided/], [{ lib: "lib", bulk: { view: {}, state: "purge" }, expect: 1 }, /"bulk.state"/], ]) { await assert.rejects(judgePress(e.ask, body), re); } assert.equal(e.asked.length, 0); }); test("the watch's switch is the operator's; off sends no terms", async () => { const e = editor(); await watchPress(e.ask, { lib: "lib", topic: "kettles", on: true, intervalMinutes: 30, hours: 48, shots: true, by: "agent", settings: { maxTopics: 12 }, tick: true }); assert.deepEqual(e.posts()[0], { route: "/api/ops/watch-trending", method: "POST", body: { slug: "lib", topic: "kettles", on: true, intervalMinutes: 30, hours: 48, shots: true, by: "operator" } }); await watchPress(e.ask, { lib: "lib", topic: "kettles", on: false, intervalMinutes: 30 }); assert.deepEqual(e.posts()[1].body, { slug: "lib", topic: "kettles", on: false, by: "operator" }); await assert.rejects(watchPress(e.ask, { lib: "lib", topic: "kettles" }), /"on"/); await assert.rejects(watchPress(e.ask, { lib: "lib", on: true }), /"topic"/); await assert.rejects(watchPress(e.ask, { lib: "lib", topic: "kettles", on: true, hours: "48" }), /"hours" is a whole number/); }); test("the purge is two presses: the dry run, then the removal with the dry run's digest, both the operator's", async () => { const e = editor(); await purgePress(e.ask, { lib: "lib", topic: "kettles", dryRun: true, by: "agent", ids: ["1"] }); assert.deepEqual(e.posts()[0], { route: "/api/ops/purge-trending", method: "POST", body: { slug: "lib", topic: "kettles", dryRun: true, by: "operator" } }); await purgePress(e.ask, { lib: "lib", confirm: "FAKE-TEST-VALUE" }); assert.deepEqual(e.posts()[1].body, { slug: "lib", confirm: "FAKE-TEST-VALUE", by: "operator" }); await assert.rejects(purgePress(e.ask, { lib: "lib" }), /carries the "confirm" its dry run answered/); await assert.rejects(purgePress(e.ask, { lib: "lib", confirm: "yes" }), /confirm/); assert.equal(e.posts().length, 2); }); // ── review 1 ─────────────────────────────────────────────────────────────── test("S6: a bulk press whose later chunk fails says exactly how many were written, that the rest were not, and returns the undo of what WAS written", async () => { const rows = Array.from({ length: 620 }, (_, i) => ({ id: String(1900000000000003000n + BigInt(i)), thread: String(1900000000000003000n + BigInt(i)), detail: i === 0 ? { ownJudgement: { state: "keep", by: "agent", at: "t" } } : {} })); const e = editor(rows, { failAt: 2 }); const out = await judgePress(e.ask, { lib: "lib", bulk: { view: {}, state: "drop" }, expect: 620 }); assert.equal(out.ok, true); assert.equal(out.partial, true); assert.equal(out.written.length, 500); assert.equal(out.sentence, "500 judgements dropped. THE REST WERE NOT JUDGED: after 500 posts the editor refused (X is cooling down for 14 minutes), and 120 posts were never asked. Undo puts back the 500 that were written."); // The undo names the 500 written, in their order, and none of the 120. assert.equal(out.undo.length, 500); assert.deepEqual(out.undo[0], { id: rows[0].id, state: "keep" }); assert.equal(out.undo.at(-1).id, rows[499].id); assert.ok(!out.undo.some((u) => u.id === rows[500].id)); // The first chunk failing wrote nothing: a refusal, not a partial. const first = editor(rows, { failAt: 1 }); await assert.rejects(judgePress(first.ask, { lib: "lib", bulk: { view: {}, state: "drop" }, expect: 620 }), /cooling down/); }); test("nit: a bulk Clear leaves posts judged only through their thread, and says how many: not changed", async () => { const rows = [ { id: "1900000000000004001", thread: "1900000000000004001", judgement: { state: "keep", by: "operator", at: "t" }, detail: { ownJudgement: { state: "keep", by: "operator", at: "t" } } }, { id: "1900000000000004002", thread: "1900000000000004001", judgement: { state: "keep", by: "operator", at: "t", from: "thread" }, detail: { threadJudgement: { state: "keep", by: "operator", at: "t" } } }, { id: "1900000000000004003", thread: "1900000000000004001", judgement: { state: "keep", by: "operator", at: "t", from: "thread" }, detail: {} }, ]; const e = editor(rows); const out = await judgePress(e.ask, { lib: "lib", bulk: { view: { state: "keep" }, state: "undecided" }, expect: 3 }); assert.deepEqual(e.posts()[0].body.set, [{ id: "1900000000000004001", state: "undecided" }]); assert.equal(out.throughThread, 2); assert.equal(out.sentence, "1 judgement cleared. 2 posts are judged through their thread: not changed (clear the thread, with Judging: the whole thread)."); assert.deepEqual(out.undo, [{ id: "1900000000000004001", state: "keep" }]); // Only such posts: nothing is asked of the editor, and it is said. const only = editor(rows.slice(1)); const none = await judgePress(only.ask, { lib: "lib", bulk: { view: {}, state: "undecided" }, expect: 2 }); assert.equal(only.posts().length, 0); assert.equal(none.sentence, "Nothing cleared. 2 posts are judged through their thread: not changed (clear the thread, with Judging: the whole thread)."); // Any other state judges them all, post by post. const drop = editor(rows); await judgePress(drop.ask, { lib: "lib", bulk: { view: {}, state: "drop" }, expect: 3 }); assert.equal(drop.posts()[0].body.set.length, 3); }); test("nit: Mark seen checks the library and every topic through the editor's listing before anything is written", async () => { const e = editor(); const marks = []; const mark = async (lib, topics) => (marks.push([lib, topics]), { slug: lib, topics: Object.fromEntries(topics.map((t) => [t, { seenAt: "2026-11-18T10:00:00.000Z" }])) }); assert.deepEqual(await seenPress(e.ask, { lib: "lib", topics: ["kettles", "kettles", "cups"] }, { mark }), { ok: true, slug: "lib", topics: { kettles: { seenAt: "2026-11-18T10:00:00.000Z" }, cups: { seenAt: "2026-11-18T10:00:00.000Z" } } }); assert.deepEqual(marks, [["lib", ["kettles", "cups"]]]); await assert.rejects(seenPress(e.ask, { lib: "other", topics: ["kettles"] }, { mark }), (err) => err.status === 404 && /The editor has no library "other"/.test(err.message)); await assert.rejects(seenPress(e.ask, { lib: "lib", topics: ["kettles", "spoons"] }, { mark }), (err) => err.status === 404 && /lib has no topic "spoons": nothing was marked/.test(err.message)); await assert.rejects(seenPress(e.ask, { lib: "lib", topics: [] }, { mark }), /"topics"/); assert.equal(marks.length, 1); assert.equal(e.posts().length, 0); });