import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, 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/trending/route.test.ts" // // The library's listing over ops, as the judge view asks it (lane // posts-library), against a temp corpus: the libraries by their marker, the // filters and pages, ids, detail — and the answer unchanged when none of // them is asked. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-trending-list-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: {}, name: "Trending" }); await channel("account-x", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "example", url: "https://x.com/example" }); const row = (id: string, author: string, text: string) => JSON.stringify({ id, slug: `trending/${id}`, channelSlug: "trending", author, createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text, url: `https://x.com/${author}/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [] }); await writeFile(path.join(lib, "trending", "posts", "2026-11.jsonl"), `${row(A, "teapot_news", "The kettle festival opens.")}\n${row(B, "saucer_fan", "Saucers.")}\n`); await writeFile(path.join(lib, "trending", "posts-archive"), `twitter ${A}\ntwitter ${B}\n`); const seen = (rank: number) => [{ read: READ, trendId: "1900000000000000001", at: "2026-11-17T10:00:00.000Z", rank }]; await writeFile(path.join(lib, "trending", "topics.json"), JSON.stringify({ version: 1, topics: [{ id: "kettles", title: "Kettles", createdAt: "2026-11-17T10:00:00.000Z", sources: [] }] })); await writeFile(path.join(lib, "trending", "membership.json"), JSON.stringify({ version: 1, posts: { [A]: { topic: "kettles", seen: seen(1) }, [B]: { topic: "kettles", seen: seen(2) } } })); await writeFile(path.join(lib, "trending", "judgements.json"), JSON.stringify({ version: 1, posts: { [B]: { state: "drop", by: "operator", at: "2026-11-17T12:00:00.000Z" } }, threads: {} })); const GET = (await import("./route")).GET; test.after(() => rm(ROOT, { recursive: true, force: true })); async function get(query: string, token = "test-token") { const res = await GET(new Request(`http://localhost/api/ops/trending${query}`, { headers: { authorization: `Bearer ${token}` } })); return { status: res.status, body: (await res.json()) as Record }; } test("without a slug: the libraries, by their marker, each in one line; and no other parameter", async () => { const all = await get(""); assert.equal(all.status, 200); assert.deepEqual(all.body, { ok: true, libraries: [{ slug: "trending", name: "Trending", topics: 1, posts: 2, unjudged: 1 }] }); assert.match((await get("?detail=1")).body.error, /lists the libraries/); assert.equal((await get("", "wrong")).status, 401); }); test("asked as before, the answer is as before", async () => { const plain = await get("?slug=trending"); assert.deepEqual(Object.keys(plain.body).sort(), ["name", "ok", "posts", "reads", "slug", "topics", "total"]); assert.ok(plain.body.posts.every((p: object) => !("detail" in p))); assert.ok(plain.body.topics.every((t: object) => !("counts" in t))); }); test("filters, pages, ids and detail", async () => { assert.deepEqual((await get("?slug=trending&state=drop")).body.posts.map((p: { id: string }) => p.id), [B]); assert.deepEqual((await get("?slug=trending&state=unjudged")).body.posts.map((p: { id: string }) => p.id), [A]); assert.deepEqual((await get("?slug=trending&q=KETTLE+festival")).body.posts.map((p: { id: string }) => p.id), [A]); assert.deepEqual((await get("?slug=trending&author=%40Saucer_Fan")).body.posts.map((p: { id: string }) => p.id), [B]); assert.deepEqual((await get("?slug=trending&limit=1&offset=1")).body.posts.map((p: { id: string }) => p.id), [B]); const ids = await get("?slug=trending&ids=1"); assert.deepEqual(ids.body.ids, [A, B]); assert.deepEqual(ids.body.posts, []); const detail = await get("?slug=trending&detail=1"); assert.deepEqual(detail.body.topics[0].counts, { unjudged: 1, keep: 0, drop: 1, later: 0, gone: 0 }); assert.equal(detail.body.posts[0].detail.firstSeenAt, "2026-11-17T10:00:00.000Z"); assert.deepEqual(detail.body.authors, [{ author: "saucer_fan", posts: 1 }, { author: "teapot_news", posts: 1 }]); assert.equal("fresh" in detail.body.topics[0], false); // Every topic's new posts in the one read, from the marks handed in. const seen = (marks: object) => get(`?slug=trending&detail=1&limit=1&seen=${encodeURIComponent(JSON.stringify(marks))}`); assert.equal((await seen({})).body.topics[0].fresh, 2); assert.equal((await seen({ kettles: "2026-11-17T10:00:00.000Z" })).body.topics[0].fresh, 0); assert.equal((await seen({ kettles: "2026-11-17T09:00:00.000Z" })).body.topics[0].fresh, 2); // The hand whose judgement is in force. assert.deepEqual((await get("?slug=trending&state=drop&by=operator&ids=1")).body.ids, [B]); assert.deepEqual((await get("?slug=trending&state=drop&by=agent&ids=1")).body.ids, []); }); test("refused with a sentence: an unknown parameter, a state, a flag, an author, a time, an offset, a channel that is no library", async () => { assert.match((await get("?slug=trending&nope=1")).body.error, /unknown parameter "nope"/); assert.match((await get("?slug=trending&state=maybe")).body.error, /"state" is one of unjudged, keep, drop, later/); assert.match((await get("?slug=trending&gone=yes")).body.error, /"gone" is 1/); assert.match((await get("?slug=trending&author=a/b")).body.error, /handle/); assert.match((await get("?slug=trending&since=soon")).body.error, /"since" is a time/); assert.match((await get("?slug=trending&offset=-1")).body.error, /"offset"/); assert.match((await get("?slug=trending&by=me")).body.error, /"by" is "agent" or "operator"/); assert.match((await get("?slug=trending&seen=%7B%7D")).body.error, /"seen" goes with detail=1/); assert.match((await get("?slug=trending&detail=1&seen=nope")).body.error, /"seen" is a JSON object/); assert.match((await get(`?slug=trending&detail=1&seen=${encodeURIComponent('{"kettles":"soon"}')}`)).body.error, /"seen" is a JSON object/); assert.match((await get("?slug=account-x")).body.error, /library/i); assert.equal((await get("?slug=nope")).status, 404); });