// An X search channel's leads, read and acted on, over a temp corpus: the // ranking names the accounts an X channel already archives, Never archive and // Restore go through the config lock and keep every other key, Archive this // account plans `-x` and refuses an archived account or a taken slug, // and no site may carry a search channel (writeSite refuses it). // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/xSearchLeads.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ROOT = await mkdtemp(path.join(os.tmpdir(), "xsearch-leads-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const { getPaths } = await import("../lib/paths"); const { writePosts } = await import("../lib/posts-server"); const { readXSearchLeads, setXSearchLeadHidden, planArchiveXLead, readXTrendingAccounts } = await import("./xSearchLeads"); const { judgeXTrending } = await import("../lib/xTrending/judgements"); const { recordXTrendingRead, writeXTrendingPosts } = await import("../lib/xTrending/store"); const { writeSite, X_SEARCH_NOT_ON_A_SITE } = await import("../lib/site"); const paths = getPaths(); async function channel(slug: string, config: Record): Promise { const dir = path.join(ROOT, "channels", slug); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "config.json"), JSON.stringify({ handling: "transcribe", name: slug, ...config })); return dir; } let n = 0; const post = (author: string, day: string) => { n++; const id = String(1899000000000000000n + BigInt(n)); return { id, slug: `demo-search/${id}`, channelSlug: "demo-search", author, createdAt: `${day}T12:00:00.000Z`, uploadDate: day.replace(/-/g, ""), text: `zephyr ${n}`, url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [], }; }; const searchRoot = await channel("demo-search", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", url: "https://x.com/search?q=zephyr&f=live", syncIntervalMinutes: 720, xSearch: { query: "zephyr", since: "2025-01-01" }, }); await writePosts(searchRoot, [ post("alice", "2025-02-01"), post("alice", "2025-03-01"), post("bob", "2025-04-01"), post("carol", "2025-05-01"), ]); // bob is archived already, by handle (case aside). await channel("bob-x", { sourceKind: "social", platform: "twitter", postFetcher: "x-gallery-dl", socialHandle: "Bob", url: "https://x.com/Bob" }); // A slug taken by something else entirely. await channel("carol-x", { url: "https://www.youtube.com/@carol" }); await channel("plain-video", { url: "https://www.youtube.com/@plain" }); test("the leads: ranked, archived accounts named, the walk described", async () => { const view = await readXSearchLeads(paths, "demo-search"); assert.ok(view.ok); assert.equal(view.query, "zephyr"); assert.equal(view.walk, "not searched yet"); assert.deepEqual(view.leads.map((l) => [l.key, l.count]), [["alice", 2], ["carol", 1], ["bob", 1]]); assert.deepEqual(view.leads.find((l) => l.key === "bob")?.archivedAs, ["bob-x"]); const limited = await readXSearchLeads(paths, "demo-search", { limit: 1 }); assert.ok(limited.ok); assert.equal(limited.more, 2); const notSearch = await readXSearchLeads(paths, "plain-video"); assert.deepEqual(notSearch, { ok: false, error: "plain-video is not an X search channel (its config has no xSearch query)." }); }); test("Never archive and Restore: one handle in or out of xSearch.exclude, every other key kept", async () => { const hide = await setXSearchLeadHidden(paths, "demo-search", "@Alice", true); assert.deepEqual(hide, { ok: true, exclude: ["alice"], changed: true }); const again = await setXSearchLeadHidden(paths, "demo-search", "alice", true); assert.deepEqual(again, { ok: true, exclude: ["alice"], changed: false }); const config = JSON.parse(await readFile(path.join(searchRoot, "config.json"), "utf8")); assert.deepEqual(config.xSearch, { query: "zephyr", since: "2025-01-01", exclude: ["alice"] }); assert.equal(config.syncIntervalMinutes, 720); const view = await readXSearchLeads(paths, "demo-search"); assert.ok(view.ok); assert.deepEqual(view.leads.map((l) => l.key), ["carol", "bob"]); assert.deepEqual(view.hidden, [{ handle: "alice", count: 2 }]); const restore = await setXSearchLeadHidden(paths, "demo-search", "alice", false); assert.deepEqual(restore, { ok: true, exclude: [], changed: true }); const after = JSON.parse(await readFile(path.join(searchRoot, "config.json"), "utf8")); assert.deepEqual(after.xSearch, { query: "zephyr", since: "2025-01-01" }); assert.deepEqual(await setXSearchLeadHidden(paths, "demo-search", "not a handle", true), { ok: false, error: '"not a handle" is not an X handle (letters, digits and "_")', }); assert.equal((await setXSearchLeadHidden(paths, "plain-video", "alice", true)).ok, false); }); test("Archive this account: -x on no site; an archived account or a taken slug refused", async () => { assert.deepEqual(await planArchiveXLead(paths, "demo-search", "Alice", "Alice A"), { ok: true, slug: "alice-x", fields: { name: "Alice A (X)", handling: "transcribe", url: "https://x.com/Alice", platform: "twitter", sourceKind: "social", postFetcher: "x-gallery-dl", socialHandle: "Alice", }, }); assert.deepEqual(await planArchiveXLead(paths, "demo-search", "bob"), { ok: false, error: "@bob is already archived: bob-x.", }); const taken = await planArchiveXLead(paths, "demo-search", "carol"); assert.equal(taken.ok, false); assert.match((taken as { error: string }).error, /A channel "carol-x" already exists/); assert.equal((await planArchiveXLead(paths, "plain-video", "alice")).ok, false); }); test("no site may carry an X search channel, as a member or a cited channel", async () => { const site = { siteId: "demo-site", siteTitle: "Demo", siteDescription: "", headerTitle: "Demo", homeTagline: "", groups: [{ id: "default", name: "All", selectedByDefault: true }], defaultGroupId: "default", channels: [{ slug: "plain-video", groupId: "default" }], }; await writeSite(site as never, paths); await assert.rejects( writeSite({ ...site, channels: [...site.channels, { slug: "demo-search", groupId: "default" }] } as never, paths), { message: X_SEARCH_NOT_ON_A_SITE("demo-search") }, ); await assert.rejects( writeSite({ ...site, citedChannels: [{ slug: "demo-search" }] } as never, paths), { message: "demo-search is an X search channel: it is never put on a site." }, ); }); test("an X trending library's accounts: handle, posts, kept, topics, who archives it; Archive this account plans -x for an account in the library only", async () => { const libRoot = await channel("demo-library", { sourceKind: "social", platform: "twitter", xTrending: {} }); const lp = (id: string, author: string, name: string, at: string) => ({ id, slug: `demo-library/${id}`, channelSlug: "demo-library", author, authorName: name, createdAt: at, uploadDate: at.slice(0, 10).replace(/-/g, ""), text: `kettle ${id}`, url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [], }); await writeXTrendingPosts(libRoot, [ lp("901", "Dora", "Dora D", "2026-11-16T10:00:00.000Z"), lp("902", "dora", "Dora Now", "2026-11-16T12:00:00.000Z"), lp("903", "bob", "Bob", "2026-11-16T11:00:00.000Z"), lp("904", "Dora", "Dora D", "2026-11-16T09:00:00.000Z"), lp("905", "erin", "Erin", "2026-11-16T09:00:00.000Z"), ]); const ref = (n: number) => ({ trendId: `190000000000000000${n}`, url: `https://x.com/i/trending/190000000000000000${n}` }); await recordXTrendingRead(libRoot, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: ref(1), topic: "Kettles", postIds: ["901", "902", "903"], added: 3 }); await recordXTrendingRead(libRoot, { readId: "tr-01JB0000000000000000000000", at: "2026-11-17T11:00:00.000Z", ref: ref(2), topic: "Cups", postIds: ["904", "905"], added: 2 }); await judgeXTrending(libRoot, { by: "operator", set: [{ id: "901", state: "keep" }, { id: "903", state: "drop" }] }); await judgeXTrending(libRoot, { by: "agent", set: [{ id: "904", state: "keep" }] }); const view = await readXTrendingAccounts(paths, "demo-library"); assert.ok(view.ok); assert.deepEqual([view.total, view.posts, view.more], [3, 5, 0]); assert.deepEqual(view.accounts, [ { handle: "dora", key: "dora", name: "Dora Now", posts: 3, kept: 2, keptByOperator: 1, topics: ["kettles", "cups"], last: "2026-11-16T12:00:00.000Z" }, { handle: "bob", key: "bob", name: "Bob", posts: 1, kept: 0, keptByOperator: 0, topics: ["kettles"], last: "2026-11-16T11:00:00.000Z", archivedAs: ["bob-x"] }, { handle: "erin", key: "erin", name: "Erin", posts: 1, kept: 0, keptByOperator: 0, topics: ["cups"], last: "2026-11-16T09:00:00.000Z" }, ]); const cups = await readXTrendingAccounts(paths, "demo-library", { topic: "cups", limit: 1 }); assert.ok(cups.ok); assert.deepEqual([cups.accounts.map((a) => [a.key, a.posts]), cups.total, cups.more], [[["dora", 1]], 2, 1]); assert.deepEqual(await readXTrendingAccounts(paths, "demo-search"), { ok: false, error: 'demo-search is not an X trending library (create one with platform "x-trending").' }); // The plan: the leads' own, and it names nothing of the library. const plan = await planArchiveXLead(paths, "demo-library", "@Dora", "Dora Now"); assert.deepEqual(plan, { ok: true, slug: "dora-x", fields: { name: "Dora Now (X)", handling: "transcribe", url: "https://x.com/Dora", platform: "twitter", sourceKind: "social", postFetcher: "x-gallery-dl", socialHandle: "Dora" }, }); assert.doesNotMatch(JSON.stringify(plan), /demo-library|trending/i); assert.deepEqual(await planArchiveXLead(paths, "demo-library", "bob"), { ok: false, error: "@bob is already archived: bob-x." }); assert.deepEqual(await planArchiveXLead(paths, "demo-library", "alice"), { ok: false, error: 'demo-library holds no post by @alice: "Archive this account" is for an account in the library.', }); // "Never archive" stays a search channel's. assert.equal((await setXSearchLeadHidden(paths, "demo-library", "dora", true)).ok, false); }); // Review 1, nit: the listing stops at 500 accounts; the plan must not. test("Archive this account finds an account of a library that holds more than 500", async () => { const libRoot = await channel("big-library", { sourceKind: "social", platform: "twitter", xTrending: {} }); const posts = Array.from({ length: 520 }, (_, i) => { const id = String(1899100000000000000n + BigInt(i)); // The LAST account has the fewest and oldest posts: it sorts past the 500th. const author = `acct_${String(i).padStart(3, "0")}`; return { id, slug: `big-library/${id}`, channelSlug: "big-library", author, createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text: "kettle", url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [] }; }); await writeXTrendingPosts(libRoot, posts); await recordXTrendingRead(libRoot, { readId: "tr-01JA0000000000000000000000", at: "2026-11-17T10:00:00.000Z", ref: { trendId: "1900000000000000009", url: "https://x.com/i/trending/1900000000000000009" }, topic: "Kettles", postIds: posts.map((p) => p.id), added: posts.length }); const view = await readXTrendingAccounts(paths, "big-library", { limit: 500 }); assert.ok(view.ok); assert.deepEqual([view.total, view.accounts.length, view.more], [520, 500, 20]); const beyond = "acct_519"; assert.ok(!view.accounts.some((a) => a.key === beyond), "the account is past the listing's cap"); const plan = await planArchiveXLead(paths, "big-library", beyond); assert.ok(plan.ok, JSON.stringify(plan)); assert.equal(plan.slug, "acct_519-x"); });