// An X search channel's leads: authors ranked by matching posts, a sample the // account wrote itself, hidden handles kept apart, accounts already archived // named, and the channel an "Archive this account" makes. // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/xSearch/leads.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import type { Post } from "../posts"; import { rankXSearchLeads, xLeadChannelFields, xLeadChannelSlug } from "./leads"; let n = 0; function post(author: string, createdAt: string, extra: Partial = {}): Post { n++; const id = String(1899000000000000000n + BigInt(n)); return { id, slug: `demo-search/${id}`, channelSlug: "demo-search", author, createdAt, uploadDate: createdAt.slice(0, 10).replace(/-/g, ""), text: `post ${n} by ${author} about zephyr`, url: `https://x.com/${author}/status/${id}`, platform: "twitter", isReply: false, isRepost: false, links: [], ...extra, }; } test("most matches first, then the most recent, then the handle", () => { const posts = [ post("alice", "2026-01-01T00:00:00.000Z", { authorName: "Alice A" }), post("Alice", "2026-03-01T00:00:00.000Z", { authorName: "Alice B" }), post("alice", "2026-02-01T00:00:00.000Z", { isRepost: true }), post("bob", "2026-04-01T00:00:00.000Z"), post("carol", "2026-05-01T00:00:00.000Z"), post("dave", "2026-05-01T00:00:00.000Z"), ]; // The same post read twice (two shards) counts once. const out = rankXSearchLeads([...posts, posts[0]]); assert.deepEqual( out.leads.map((l) => [l.key, l.count]), [["alice", 3], ["carol", 1], ["dave", 1], ["bob", 1]], ); const alice = out.leads[0]; assert.equal(alice.handle, "Alice"); assert.equal(alice.name, "Alice B"); assert.equal(alice.reposts, 1); assert.equal(alice.first, "2026-01-01T00:00:00.000Z"); assert.equal(alice.last, "2026-03-01T00:00:00.000Z"); assert.equal(alice.sample.id, posts[1].id); assert.equal(out.posts, 6); assert.equal(out.authors, 4); assert.equal(out.more, 0); }); test("the sample is the account's own newest post, else its newest repost; long text is cut", () => { const own = post("erin", "2026-01-01T00:00:00.000Z", { text: "x".repeat(400) }); const repost = post("erin", "2026-06-01T00:00:00.000Z", { isRepost: true }); const [erin] = rankXSearchLeads([own, repost]).leads; assert.equal(erin.sample.id, own.id); assert.equal(erin.sample.text.length, 280); assert.ok(erin.sample.text.endsWith("…")); const [only] = rankXSearchLeads([post("fay", "2026-01-01T00:00:00.000Z", { isRepost: true })]).leads; assert.equal(only.sample.createdAt, "2026-01-01T00:00:00.000Z"); }); test("hidden handles are kept apart, archived accounts named, the list capped", () => { const posts = [ post("alice", "2026-01-01T00:00:00.000Z"), post("alice", "2026-01-02T00:00:00.000Z"), post("bob", "2026-01-03T00:00:00.000Z"), post("carol", "2026-01-04T00:00:00.000Z"), ]; const out = rankXSearchLeads(posts, { exclude: ["ALICE", "ghost"], archived: new Map([["bob", ["bob-x", "bob-x-replies"]]]), limit: 1, }); assert.deepEqual(out.leads.map((l) => l.key), ["carol"]); assert.equal(out.more, 1); assert.deepEqual(out.hidden, [ { handle: "alice", count: 2 }, { handle: "ghost", count: 0 }, ]); const all = rankXSearchLeads(posts, { archived: new Map([["bob", ["bob-x-replies", "bob-x"]]]) }); assert.deepEqual(all.leads.find((l) => l.key === "bob")?.archivedAs, ["bob-x", "bob-x-replies"]); }); test("Archive this account makes -x with the form's fields", () => { assert.equal(xLeadChannelSlug("@Some_Account"), "some_account-x"); assert.equal(xLeadChannelSlug("_under"), "x_under-x"); assert.throws(() => xLeadChannelSlug("not one"), /not an X handle/); assert.deepEqual(xLeadChannelFields({ handle: "Some_Account", name: "Some Account" }), { name: "Some Account (X)", handling: "transcribe", url: "https://x.com/Some_Account", platform: "twitter", sourceKind: "social", postFetcher: "x-gallery-dl", socialHandle: "Some_Account", }); assert.equal(xLeadChannelFields({ handle: "@bob" }).name, "bob (X)"); });