import test from "node:test"; import assert from "node:assert/strict"; import { access, 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/purge-trending/route.test.ts" // // purge-trending over ops (lane posts-library), against a temp corpus: the // dry run that removes nothing, the removal that is the operator's and // carries the dry run's digest, and what stays. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-trending-purge-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" }); const row = (id: string) => 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: [] }); await writeFile(path.join(lib, "trending", "posts", "2026-11.jsonl"), `${row(A)}\n${row(B)}\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", "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 }, [B]: { topic: "kettles", seen } } })); await writeFile(path.join(lib, "trending", "judgements.json"), JSON.stringify({ version: 1, posts: { [A]: { state: "drop", by: "operator", at: "2026-11-17T12:00:00.000Z" }, [B]: { state: "keep", by: "operator", at: "2026-11-17T12:00:00.000Z" } }, threads: {} })); for (const id of [A, B]) { await mkdir(path.join(lib, "trending", "posts-media", id), { recursive: true }); await writeFile(path.join(lib, "trending", "posts-media", id, "shot.png"), "12345"); } const POST = (await import("./route")).POST; test.after(() => rm(ROOT, { recursive: true, force: true })); const exists = (p: string) => access(p).then(() => true, () => false); async function call(body: Record, token = "test-token") { const res = await POST(new Request("http://localhost/api/ops/purge-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("the dry run counts and removes nothing; the removal is the operator's, with the dry run's digest", async () => { assert.equal((await call({ slug: "trending", dryRun: true }, "wrong")).status, 401); const dry = await call({ slug: "trending", dryRun: true, topic: "kettles" }); assert.equal(dry.status, 200); assert.deepEqual({ posts: dry.json.posts, ids: dry.json.ids, files: dry.json.files, bytes: dry.json.bytes, dryRun: dry.json.dryRun }, { posts: 1, ids: [A], files: 1, bytes: 5, dryRun: true }); assert.equal(dry.json.sentence, "Would remove the capture of 1 dropped post: 1 file, 5 B. The judgements stay. Keeping it again captures it again."); assert.deepEqual({ gone: dry.json.gone, agentDropped: dry.json.agentDropped }, { gone: [], agentDropped: 0 }); assert.equal(await exists(path.join(lib, "trending", "posts-media", A)), true); // An agent, a missing digest, a wrong one: refused, nothing removed. assert.match((await call({ slug: "trending", confirm: dry.json.confirm })).json.error, /the operator's press/); assert.match((await call({ slug: "trending", confirm: dry.json.confirm, by: "agent" })).json.error, /the operator's press/); assert.match((await call({ slug: "trending", by: "operator" })).json.error, /"dryRun": true first/); assert.match((await call({ slug: "trending", by: "operator", confirm: "0000000000000000" })).json.error, /have changed since the dry run/); assert.equal(await exists(path.join(lib, "trending", "posts-media", A)), true); const judgements = await readFile(path.join(lib, "trending", "judgements.json"), "utf8"); const done = await call({ slug: "trending", by: "operator", confirm: dry.json.confirm, topic: "kettles" }); assert.equal(done.status, 200); assert.equal(done.json.dryRun, false); assert.match(done.json.sentence, /^Removed the capture of 1 dropped post/); assert.equal(await exists(path.join(lib, "trending", "posts-media", A)), false); // The kept post's capture and every judgement stay. assert.equal(await exists(path.join(lib, "trending", "posts-media", B, "shot.png")), true); assert.equal(await readFile(path.join(lib, "trending", "judgements.json"), "utf8"), judgements); }); test("refused with a sentence: not a library, no such topic, ill-shaped ids, an unknown key", async () => { assert.match((await call({ slug: "account-x", dryRun: true })).json.error, /library/i); assert.match((await call({ slug: "trending", dryRun: true, topic: "nope" })).json.error, /has no topic "nope"/); assert.match((await call({ slug: "trending", dryRun: true, ids: "x" })).json.error, /"ids" is a list/); assert.match((await call({ slug: "trending", dryRun: true, ids: ["../x"] })).json.error, /"ids" is a list of post ids/); assert.equal((await call({ slug: "trending", dryRun: true, force: true })).status, 400); });