// What an X trending library is kept out of (lane x-trending): the post // fetch, the X run, and the job kinds' declarations. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/xTrendingGuards.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { getJobKind, isDrainableKind, isIngestKind, jobKindLabel, kindIsNetwork } from "../jobs/jobKinds"; import { fetchPosts } from "./fetchPosts"; import { isXPostsChannel } from "./xRun"; import { READ_X_TRENDING_KIND, REPARSE_X_TRENDING_KIND, X_PROFILE_BROWSER_JOB_KINDS } from "./readXTrending"; const TMP = mkdtempSync(path.join(tmpdir(), "x-trending-guards-")); after(() => rmSync(TMP, { recursive: true, force: true })); const LIBRARY = { handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Trending", xTrending: {} } as const; test("fetch-posts refuses a library with a sentence, before any fetcher is asked", async () => { const channelsDir = path.join(TMP, "channels"); mkdirSync(path.join(channelsDir, "trending"), { recursive: true }); writeFileSync(path.join(channelsDir, "trending", "config.json"), JSON.stringify(LIBRARY)); const res = await fetchPosts({ paths: { transcriptsDir: TMP, channelsDir } as unknown as Paths, slug: "trending", settings: {} as never, signal: new AbortController().signal, }); assert.equal(res.ok, false); assert.equal( res.error, "trending is an X trending library: it has no timeline to fetch. Read a trending page into it (pnpm ops read-x-trending).", ); }); test("the X run never lists a library among the X posts channels", () => { assert.equal(isXPostsChannel(LIBRARY as never), false); assert.equal(isXPostsChannel({ handling: "transcribe", sourceKind: "social", platform: "twitter", url: "https://x.com/teapot_news", socialHandle: "teapot_news" } as never), true); // With no account at all it is posts published from a library (lane // x-trending-publish): nothing an X run can fetch either. assert.equal(isXPostsChannel({ handling: "transcribe", sourceKind: "social", platform: "twitter" } as never), false); }); test("read-x-trending: a network kind on X's queue that nothing replays or resumes; reparse asks nothing", () => { const read = getJobKind(READ_X_TRENDING_KIND); assert.ok(read, "registered"); assert.equal(jobKindLabel(READ_X_TRENDING_KIND), "Read X trending page"); assert.equal(read.queueKeyStrategy, "platform"); assert.equal(kindIsNetwork(READ_X_TRENDING_KIND), true); assert.equal(isDrainableKind(READ_X_TRENDING_KIND), false); assert.equal(read.replayable, false); assert.equal(read.resumeOnBoot, undefined); assert.equal(read.startedByPerson, true); assert.equal(isIngestKind(READ_X_TRENDING_KIND), true); const reparse = getJobKind(REPARSE_X_TRENDING_KIND); assert.ok(reparse, "registered"); assert.equal(reparse.queueKeyStrategy, "platform"); assert.equal(kindIsNetwork(REPARSE_X_TRENDING_KIND), false); assert.equal(reparse.replayable, false); assert.equal(isIngestKind(REPARSE_X_TRENDING_KIND), true); // The kinds that hold the connected profile's browser while they run. assert.deepEqual([...X_PROFILE_BROWSER_JOB_KINDS], ["read-x-trending", "capture-posts"]); });