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/x-search-lead/route.test.ts" // // An X search channel over ops (release 38 lane S), against a temp corpus: the // leads read (`GET /api/ops/channel/?leads=1`), x-search-lead's Never // archive / Restore and the Archive dry run, and every refusal — create-channel // of a search channel on a site, a lead on a channel that is not a search, an // account already archived. A successful create ends in revalidatePath, which // needs a running Next server: Archive this account is driven in // e2e/x-search.spec.ts. const ROOT = await mkdtemp(path.join(os.tmpdir(), "x-search-route-")); process.env.WORKER_TOKEN = "test-token"; process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); async function channel(slug: string, config: Record, posts?: string[]): Promise { const dir = path.join(ROOT, "channels", slug); await mkdir(path.join(dir, "posts"), { recursive: true }); await writeFile(path.join(dir, "config.json"), JSON.stringify({ handling: "transcribe", name: slug, ...config })); if (posts) { const lines = posts.map((author, i) => { const id = String(1899000000000000000n + BigInt(i)); return JSON.stringify({ id, slug: `${slug}/${id}`, channelSlug: slug, author, createdAt: `2025-0${i + 1}-01T00:00:00.000Z`, uploadDate: `20250${i + 1}01`, text: `zephyr ${i}`, url: `https://x.com/${author}/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], }); }); await writeFile(path.join(dir, "posts", "2025-01.jsonl"), lines.join("\n") + "\n"); } } await channel( "demo-search", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", url: "https://x.com/search?q=zephyr&f=live", xSearch: { query: "zephyr" }, }, ["alice", "alice", "bob"], ); await channel("bob-x", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "bob", url: "https://x.com/bob" }); await mkdir(path.join(ROOT, "sites", "demo-site"), { recursive: true }); await writeFile( path.join(ROOT, "sites", "demo-site", "site.json"), JSON.stringify({ siteId: "demo-site", siteTitle: "Demo", channels: [] }), ); const lead = (await import("./route")).POST; const create = (await import("../create-channel/route")).POST; const read = (await import("../channel/[slug]/route")).GET; test.after(() => rm(ROOT, { recursive: true, force: true })); async function call(handler: (r: Request) => Promise, body: Record) { const res = await handler( new Request("http://localhost/api/ops/x", { 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 }; } async function get(slug: string, query: string) { const res = await (read as unknown as (r: Request, ctx: { params: Promise<{ slug: string }> }) => Promise)( new Request(`http://localhost/api/ops/channel/${slug}?${query}`, { headers: { authorization: "Bearer test-token" } }), { params: Promise.resolve({ slug }) }, ); return { status: res.status, json: (await res.json()) as Record }; } const exists = (p: string) => access(p).then(() => true, () => false); test("the leads read: ranked, archived accounts named; refused for any other channel and a bad limit", async () => { const res = await get("demo-search", "leads=1"); assert.equal(res.status, 200); const leads = res.json.leads as { query: string; leads: { key: string; count: number; archivedAs?: string[] }[]; posts: number }; assert.equal(leads.query, "zephyr"); assert.equal(leads.posts, 3); assert.deepEqual(leads.leads.map((l) => [l.key, l.count, l.archivedAs ?? []]), [["alice", 2, []], ["bob", 1, ["bob-x"]]]); const plain = await get("demo-search", ""); assert.equal(plain.json.leads, undefined); const other = await get("bob-x", "leads=1"); assert.equal(other.status, 400); assert.match(String(other.json.error), /bob-x is not an X search channel/); assert.equal((await get("demo-search", "leads=1&limit=0")).status, 400); }); test("never, restore, and the archive dry run; refusals in the controller's sentences", async () => { const never = await call(lead, { slug: "demo-search", handle: "@alice", action: "never" }); assert.deepEqual(never.json, { ok: true, exclude: ["alice"], changed: true }); const hidden = await get("demo-search", "leads=1"); assert.deepEqual((hidden.json.leads as { hidden: unknown }).hidden, [{ handle: "alice", count: 2 }]); const restore = await call(lead, { slug: "demo-search", handle: "alice", action: "restore" }); assert.deepEqual(restore.json, { ok: true, exclude: [], changed: true }); const dry = await call(lead, { slug: "demo-search", handle: "alice", action: "archive", dryRun: true }); assert.equal(dry.status, 200); assert.equal(dry.json.slug, "alice-x"); assert.equal((dry.json.fields as Record).url, "https://x.com/alice"); assert.equal(await exists(path.join(ROOT, "channels", "alice-x")), false); const archived = await call(lead, { slug: "demo-search", handle: "bob", action: "archive" }); assert.equal(archived.status, 400); assert.equal(archived.json.error, "@bob is already archived: bob-x."); const notSearch = await call(lead, { slug: "bob-x", handle: "alice", action: "never" }); assert.equal(notSearch.status, 400); assert.match(String(notSearch.json.error), /not an X search channel/); const badAction = await call(lead, { slug: "demo-search", handle: "alice", action: "follow" }); assert.equal(badAction.status, 400); assert.match(String(badAction.json.error), /"action" must be one of archive, never, restore/); const stray = await call(lead, { slug: "demo-search", handle: "alice", action: "never", dryRun: true }); assert.equal(stray.status, 400); }); test("create-channel makes no X search channel on a site", async () => { const onSite = await call(create, { slug: "made-search", fields: { name: "Made search", handling: "transcribe", url: "https://x.com/search?q=zephyr" }, sites: [{ siteId: "demo-site" }], }); assert.equal(onSite.status, 400); assert.equal(onSite.json.error, "made-search is an X search channel: it is never put on a site."); assert.equal(await exists(path.join(ROOT, "channels", "made-search")), false); const badQuery = await call(create, { slug: "made-search", fields: { name: "Made search", handling: "transcribe", platform: "x-search", xSearchQuery: "zephyr until:2025-01-01" }, }); assert.equal(badQuery.status, 400); assert.match(String(badQuery.json.error), /Take until: out of the query/); const site = JSON.parse(await readFile(path.join(ROOT, "sites", "demo-site", "site.json"), "utf8")); assert.deepEqual(site.channels, []); });