import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/judge-trending/route.test.ts" // // judge-trending over ops (lane x-trending-2), against a temp corpus: the // refusals, a write by an agent, the operator's judgement an agent cannot // replace, and the listing carrying what was written. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-trending-judge-route-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const READ = "tr-01JA0000000000000000000000"; const A = "1900000000000001001"; const B = "1900000000000001002"; async function channel(slug: string, config: Record): Promise { const dir = path.join(ROOT, "channels", slug); await mkdir(path.join(dir, "trending", "posts"), { recursive: true }); await writeFile(path.join(dir, "config.json"), JSON.stringify({ handling: "transcribe", name: slug, ...config })); return dir; } const lib = await channel("trending", { sourceKind: "social", platform: "twitter", xTrending: {} }); await channel("account-x", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "example", url: "https://x.com/example" }); await writeFile( path.join(lib, "trending", "posts", "2026-11.jsonl"), [A, B] .map((id) => JSON.stringify({ id, slug: `trending/${id}`, channelSlug: "trending", author: "teapot_news", createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: `post ${id}`, url: `https://x.com/teapot_news/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], }), ) .join("\n") + "\n", ); await writeFile(path.join(lib, "trending", "posts-archive"), `twitter ${A}\ntwitter ${B}\n`); const seen = [{ read: READ, trendId: "1900000000000000001", at: "2026-11-17T10:00:00.000Z", rank: 1 }]; await writeFile( path.join(lib, "trending", "membership.json"), JSON.stringify({ version: 1, posts: { [A]: { topic: "kettles", seen }, [B]: { topic: "kettles", seen } } }), ); const judge = (await import("./route")).POST; const listing = (await import("../trending/route")).GET; test.after(() => rm(ROOT, { recursive: true, force: true })); async function call(body: Record, token = "test-token") { const res = await judge( new Request("http://localhost/api/ops/judge-trending", { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as Record }; } test("judge-trending refuses with a sentence", async () => { assert.equal((await call({ slug: "trending", set: [{ id: A, state: "keep" }] }, "wrong")).status, 401); for (const [body, re] of [ [{ slug: "nope", set: [{ id: A, state: "keep" }] }, /No such channel: nope/], [{ slug: "account-x", set: [{ id: A, state: "keep" }] }, /not an X trending library/], [{ slug: "trending" }, /"set" is a list of judgements/], [{ slug: "trending", set: [] }, /"set" is a list of judgements/], [{ slug: "trending", set: [{ id: A, state: "maybe" }] }, /keep, drop, later or undecided/], [{ slug: "trending", set: [{ id: A, state: "keep" }], by: "someone" }, /"by" is who decided/], [{ slug: "trending", set: [{ id: A, state: "keep" }], force: true }, /unknown key\(s\): force/], ] as const) { const r = await call(body); assert.equal(r.status, 400, JSON.stringify(body)); assert.match(String(r.json.error), re); } }); test("an agent's judgement is written; the operator's is not replaced by an agent; the listing carries both", async () => { const first = await call({ slug: "trending", archive: false, set: [{ id: A, state: "keep", note: "the opening" }, { id: "42", state: "keep" }] }); assert.equal(first.json.archive, undefined); assert.equal(first.status, 200); assert.deepEqual(first.json.written, [{ id: A, state: "keep" }]); assert.deepEqual(first.json.skipped, [{ id: "42", reason: "not in this library" }]); assert.deepEqual(first.json.kept, [A]); const op = await call({ slug: "trending", by: "operator", set: [{ id: A, state: "drop" }, { id: B, state: "later" }] }); assert.deepEqual(op.json.written, [{ id: A, state: "drop" }, { id: B, state: "later" }]); const again = await call({ slug: "trending", archive: false, set: [{ id: A, state: "keep" }] }); assert.equal(again.status, 200); assert.deepEqual(again.json.written, []); assert.match(JSON.stringify(again.json.skipped), /judged by the operator/); const file = JSON.parse(await readFile(path.join(lib, "trending", "judgements.json"), "utf8")); assert.equal(file.posts[A].state, "drop"); assert.equal(file.posts[A].by, "operator"); const res = await listing(new Request("http://localhost/api/ops/trending?slug=trending", { headers: { authorization: "Bearer test-token" } })); const rows = ((await res.json()) as { posts: Array<{ id: string; judgement?: { state: string } }> }).posts; assert.deepEqual(rows.map((p) => [p.id, p.judgement?.state]).sort(), [[A, "drop"], [B, "later"]]); }); test("a keep archives through capture-posts; a refused capture leaves the judgement written and says why", async () => { // This corpus has no connected X profile and no cookie jar: the capture is // refused before a job exists (nothing is asked of X), the keep stands. const r = await call({ slug: "trending", by: "operator", set: [{ id: B, state: "keep" }] }); assert.equal(r.status, 200); assert.deepEqual(r.json.kept, [B]); const archive = r.json.archive as { error?: string; jobId?: string; ids: string[] }; assert.deepEqual(archive.ids, [B]); assert.equal(archive.jobId, undefined); assert.ok(archive.error && archive.error.length > 20, String(archive.error)); const file = JSON.parse(await readFile(path.join(lib, "trending", "judgements.json"), "utf8")); assert.equal(file.posts[B].state, "keep"); }); test("capture-posts keptMedia is for a library's kept posts only", async () => { const capture = (await import("../capture-posts/route")).POST; const ask = async (body: Record) => { const res = await capture( new Request("http://localhost/api/ops/capture-posts", { method: "POST", headers: { authorization: "Bearer test-token", "content-type": "application/json" }, body: JSON.stringify(body), }), ); return { status: res.status, json: (await res.json()) as Record }; }; // A is dropped by the operator above: not kept. const notKept = await ask({ slug: "trending", ids: [A], keptMedia: true }); assert.equal(notKept.status, 400); assert.match(String(notKept.json.error), new RegExp(`not kept: ${A}`)); const notLibrary = await ask({ slug: "account-x", ids: ["1"], keptMedia: true }); assert.equal(notLibrary.status, 400); // Review 1 S5: a library's capture runs on X's queue only. for (const queueKey of ["", "side-queue"]) { const moved = await ask({ slug: "trending", ids: [B], queueKey }); assert.equal(moved.status, 400); assert.match(String(moved.json.error), /is an X trending library: its captures run on X's queue only \("queueKey" is refused\)/); } // An id that is not the library's is refused as stray before anything else. assert.match(String((await ask({ slug: "trending", ids: ["42"], keptMedia: true })).json.error), /not in trending's posts archive/); });