// X's two rules (lib/xJobRule.ts) and what counts. The count on disk is jobs/xJobCap.test.ts. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/xJobRule.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import path from "node:path"; import { runInJobContext } from "./jobContext"; import { X_JOB_CAP, X_QUEUE_KEY, isXUrl, xJobCapSentence, xJobCapVerdict, xJobAsksX, xJobContextRefusal, xJobCounts, xQueueRefusal } from "./xJobRule"; const T0 = Date.parse("2026-11-18T12:00:00.000Z"); const MIN = 60_000; test("the numbers: 6 X jobs in any 60 minutes, 150 in any 24 hours, and they are frozen", () => { assert.deepEqual({ ...X_JOB_CAP }, { perHour: 6, hourMs: 3_600_000, perDay: 150, dayMs: 86_400_000 }); assert.ok(Object.isFrozen(X_JOB_CAP)); assert.equal(X_QUEUE_KEY, "platform:twitter"); // Nothing in the rule's two modules reads a setting, an environment variable or an option. for (const file of ["xJobRule.ts", "../jobs/xJobCap.ts"]) { const code = readFileSync(path.join(import.meta.dirname, file), "utf8").replace(/^\s*\/\/.*$/gm, ""); assert.doesNotMatch(code, /process\.env|getSettings|settings\b|\bforce\b/i, file); } }); test("the hour window: the 7th start in 60 minutes is refused, and says when the next may start", () => { const starts = Array.from({ length: 6 }, (_, i) => T0 + i * 10 * MIN); // 12:00 … 12:50 assert.deepEqual(xJobCapVerdict(starts.slice(0, 5), T0 + 56 * MIN), { ok: true, lastHour: 5, lastDay: 5 }); const full = xJobCapVerdict(starts, T0 + 56 * MIN); assert.deepEqual(full, { ok: false, lastHour: 6, lastDay: 6, nextAt: T0 + 60 * MIN, window: "hour" }); assert.equal( xJobCapSentence(full, T0 + 56 * MIN), "X's job cap is reached on this machine: 6 X jobs started in the last 60 minutes (the cap is 6). The next X job may start at 13:00 UTC (in 4 min); " + "ask again then — nothing queues or retries by itself. The cap is fixed in the code (common/lib/xJobRule.ts): no setting, option or queue raises it.", ); // Rolling: at 13:00 the 12:00 start has left the window (a start exactly 60 minutes old is out). assert.equal(xJobCapVerdict(starts, T0 + 60 * MIN).ok, true); assert.equal(xJobCapVerdict(starts, T0 + 60 * MIN - 1).ok, false); assert.equal(xJobCapVerdict(starts, T0 + 60 * MIN).lastHour, 5); // At X's floor gap (5 minutes) six jobs take 25 minutes: the hourly cap bites a run at the floor. const atFloor = Array.from({ length: 6 }, (_, i) => T0 + i * 5 * MIN); assert.equal(xJobCapVerdict(atFloor, T0 + 30 * MIN).ok, false); }); test("the day window: the 151st start in 24 hours is refused until the oldest of the last 150 is a day old", () => { // 150 starts in 20 hours (one every 8 minutes — more than the hourly cap lets through, but the verdict // judges each window as it is at `now`, and the last hour here is empty). const starts = Array.from({ length: 150 }, (_, i) => T0 + i * 8 * MIN); const now = T0 + 1300 * MIN; const full = xJobCapVerdict(starts, now); assert.equal(full.ok, false); assert.equal(full.window, "day"); assert.equal(full.lastHour, 0); assert.equal(full.lastDay, 150); assert.equal(full.nextAt, T0 + 24 * 60 * MIN); assert.match(xJobCapSentence(full, now), /150 X jobs started in the last 24 hours \(the cap is 150\)\. The next X job may start at 12:00 UTC \(in about 2 h\)/); // The day is named when it is not today's. assert.match(xJobCapSentence(full, T0 + 5 * MIN), /may start at 2026-11-19 12:00 UTC \(in about 24 h\)/); assert.equal(xJobCapVerdict(starts, T0 + 24 * 60 * MIN).ok, true); assert.equal(xJobCapVerdict(starts.slice(1), now).ok, true); }); test("a clock set back never frees a start early", () => { const starts = Array.from({ length: 6 }, (_, i) => T0 + i * MIN); // 12:00 … 12:05 // The clock now reads two hours EARLIER than the latest start: every start still counts. const back = xJobCapVerdict(starts, T0 - 120 * MIN); assert.equal(back.ok, false); assert.equal(back.lastHour, 6); assert.equal(back.nextAt, T0 + 60 * MIN); // Only once the clock has passed them again by the window do they leave it. assert.equal(xJobCapVerdict(starts, T0 + 59 * MIN).ok, false); assert.equal(xJobCapVerdict(starts, T0 + 60 * MIN).ok, true); }); test("X jobs run on X's queue only: any other queue is refused, when queued and where the job runs", () => { assert.equal(xQueueRefusal("fetch-posts for example-x", "platform:twitter"), null); assert.equal( xQueueRefusal("fetch-posts for example-x", "manual"), 'fetch-posts for example-x asks X, and X jobs run on X\'s queue only (platform:twitter): the queue "manual" is refused. ' + "On another queue it would run beside the job on X's, outside the gap between X jobs. Leave the queue at its default.", ); assert.match(xQueueRefusal("x", "platform:youtube") ?? "", /"platform:youtube" is refused/); assert.match(xQueueRefusal("x", undefined) ?? "", /the queue it was given is refused/); const ctx = (queueKey?: string) => ({ jobId: "j", kind: "fetch-posts", ...(queueKey ? { queueKey } : {}), onLog: () => {}, staleServices: new Set() }); // No job at all. assert.match(xJobContextRefusal("example-x", "pnpm ops fetch-posts") ?? "", /^example-x asks X, and X is asked only by the editor's own jobs .* nothing was asked\. Queue it instead: pnpm ops fetch-posts$/); // A job on another queue. assert.match(runInJobContext(ctx("manual"), () => xJobContextRefusal("example-x", "d")) ?? "", /the queue "manual" is refused\..* Nothing was asked\.$/); assert.match(runInJobContext(ctx(), () => xJobContextRefusal("example-x", "d")) ?? "", /the queue it was given is refused/); // X's queue. assert.equal(runInJobContext(ctx("platform:twitter"), () => xJobContextRefusal("example-x", "d")), null); }); test("review 1 F8: every X job counts — a library's media-only capture too; only the transcription worker does not", () => { const mediaOnly = { ids: ["1"], shots: false, media: true, force: false, articles: false, avatars: false, context: false, watch: true }; assert.equal(xJobCounts({ kind: "capture-posts", params: mediaOnly, channel: { library: true } }), true); for (const kind of ["fetch-posts", "x-run", "read-x-trending", "import-post", "check-post-availability", "sync", "download-missing", "capture-posts"]) { assert.equal(xJobCounts({ kind }), true, kind); } assert.equal(xJobCounts({ kind: "transcribe-one" }), false); }); test("S5: which network jobs ask x.com — by X's queue, X's clip queue, an import's URL, or an X channel's job that is not local", () => { const x = (kind: string, queueKey: string, params?: Record) => xJobAsksX({ kind, queueKey, channelPlatform: "twitter", ...(params ? { params } : {}) }); // X's queue, whatever the kind and channel. assert.equal(xJobAsksX({ kind: "transcribe-one", queueKey: "platform:twitter" }), true); assert.equal(xJobAsksX({ kind: "fetch-windows", queueKey: "clips:twitter" }), true, "X's clip windows, a batch with no channel"); // An X channel's jobs that ask x.com, on any other queue: X's (and refused there). for (const [kind, queueKey] of [ ["fetch-posts", "manual"], ["fetch-window", "clips:twitter"], ["fetch-windows", "clips:twitter"], ["record-live", "record-live:x-chan:1"], ["check-kept-deleted", "channel:x-chan"], ["redownload-incomplete-bucket", "transcription"], ["import-media", "platform:youtube"], ["sync", "platform:youtube"], ] as const) { assert.equal(x(kind, queueKey), true, `${kind} on ${queueKey}`); } // An X channel's jobs that ask x.com nothing: never X's. assert.equal(x("transcribe-one", "transcribe-one:x-chan:1"), false, "the transcription worker: local"); assert.equal(x("fetch-windows", "clips:saved-video"), false, "windows cut from the saved copy on disk"); assert.equal(x("fetch-window", "clips:saved-video"), false); assert.equal(x("import-media", "import-media:x-chan"), false, "a local file (a URL on X is queued on X's queue)"); assert.equal(x("import-archive-org", "platform:archiveorg"), false, "archive.org"); assert.equal(x("import-one", "platform:archiveorg", { url: "https://archive.org/details/example" }), false, "the URL's host"); // An import asks its URL's host, whatever the channel and queue. assert.equal(xJobAsksX({ kind: "import-one", queueKey: "platform:youtube", channelPlatform: "youtube", params: { url: "https://x.com/example/status/1" } }), true); assert.equal(xJobAsksX({ kind: "import-one", queueKey: "platform:youtube", channelPlatform: "youtube", params: { url: "https://twitter.com/example/status/1" } }), true); assert.equal(x("import-one", "platform:twitter", { url: "https://x.com/example/status/1" }), true); // Not an X channel, not X's queue: not X's. assert.equal(xJobAsksX({ kind: "fetch-posts", queueKey: "platform:bluesky", channelPlatform: "bluesky" }), false); assert.equal(xJobAsksX({ kind: "fetch-window", queueKey: "clips:youtube", channelPlatform: "youtube" }), false); // The transcription worker asks x.com nothing even on X's queue: it does not count. assert.equal(xJobCounts({ kind: "transcribe-one" }), false); }); // Review 3 M2, S5, S6: a window job is X's by the URL it will fetch, on ANY // channel; an item with no URL is its channel's; t.co is X's. test("review 3 M2: a window job is judged by its URLs — an x.com or twitter.com page on any channel is X's", () => { const yt = (kind: string, queueKey: string, params: Record, channelPlatform: string | null = "youtube") => xJobAsksX({ kind, queueKey, channelPlatform, params }); // The single window: its webpageUrl, on a channel that is not X's. assert.equal(yt("fetch-window", "clips:youtube", { webpageUrl: "https://x.com/example/status/1" }), true); assert.equal(yt("fetch-window", "clips:youtube", { webpageUrl: "https://mobile.twitter.com/example/status/1" }), true); assert.equal(yt("fetch-window", "clips:youtube", { webpageUrl: "https://www.youtube.com/watch?v=abc" }), false); // A batch: any item's URL that is X's makes the whole job X's. const items = (...urls: Array) => urls.map((u, i) => ({ slug: "c", id: `v${i}`, from: 1, to: 2, ...(u ? { webpageUrl: u } : {}) })); assert.equal(yt("fetch-windows", "clips:youtube", { items: items("https://www.youtube.com/watch?v=a", "https://x.com/example/status/2") }), true); assert.equal(yt("fetch-windows", "manual", { items: items("https://x.com/example/status/2") }, null), true, "a multi-channel batch under a queue override"); assert.equal(yt("fetch-windows", "clips:youtube", { items: items("https://www.youtube.com/watch?v=a") }), false); // X's own queue is X's whatever the URLs. assert.equal(yt("fetch-windows", "platform:twitter", { items: items("https://www.youtube.com/watch?v=a") }), true); // Windows cut from the saved copy: the controller never takes the network // for an X source there (M1), so the queue passes whatever the URL. assert.equal(yt("fetch-windows", "clips:saved-video", { items: items("https://x.com/example/status/2") }, "twitter"), false); }); test("review 3 S5: an X channel's window of a copy or a mirror on another platform passes; an item with no URL is its channel's", () => { const x = (kind: string, queueKey: string, params?: Record, extra: Partial[0]> = {}) => xJobAsksX({ kind, queueKey, channelPlatform: "twitter", ...(params ? { params } : {}), ...extra }); const item = (webpageUrl?: string, slug = "x-chan") => ({ slug, id: "1", from: 1, to: 2, ...(webpageUrl ? { webpageUrl } : {}) }); // find-copy's windows of a copy, on archive.org's clip queue. assert.equal(x("fetch-windows", "clips:archiveorg", { items: [item("https://web.archive.org/web/2026/https://example.org/v.mp4"), item("https://archive.org/details/example")] }), false); // A mirror of the X video on another platform. assert.equal(x("fetch-window", "clips:twitter-mirror", { webpageUrl: "https://rumble.com/v-example.html" }), false); assert.equal(x("fetch-windows", "clips:rumble", { items: [item("https://rumble.com/v-example.html")] }), false); // NO URL NEVER PASSES BY ITSELF: the item's channel decides. assert.equal(x("fetch-window", "clips:youtube", {}), true, "a single window with no URL on an X channel"); assert.equal(x("fetch-window", "clips:youtube"), true, "no plan at all on an X channel"); assert.equal(x("fetch-windows", "clips:archiveorg", { items: [item("https://archive.org/details/example"), item(undefined)] }), true); // A multi-channel batch: each URL-less item by its own channel's platform. const multi = { items: [item("https://archive.org/details/example", "yt-chan"), item(undefined, "x-chan")] }; assert.equal(xJobAsksX({ kind: "fetch-windows", queueKey: "manual", params: multi, itemChannelPlatforms: { "yt-chan": "youtube", "x-chan": "twitter" } }), true); assert.equal(xJobAsksX({ kind: "fetch-windows", queueKey: "manual", params: multi, itemChannelPlatforms: { "yt-chan": "youtube", "x-chan": "youtube" } }), false); // fetch-copies: archive.org's (its controller refuses an X address). assert.equal(x("fetch-copies", "platform:archiveorg"), false); }); test("review 3 S6: a t.co short link is X's", () => { for (const url of ["https://t.co/AbCdEf123", "http://t.co/x", "https://www.t.co/x"]) { assert.equal(isXUrl(url), true, url); assert.equal(xJobAsksX({ kind: "import-one", queueKey: "platform:youtube", channelPlatform: "youtube", params: { url } }), true, url); } for (const url of ["https://not-t.co/x", "https://example.com/t.co", "https://nottwitter.com/x", "not a url", ""]) { assert.equal(isXUrl(url), false, url); } assert.equal(isXUrl("https://x.com/a/status/1"), true); assert.equal(isXUrl("https://twitter.com/a/status/1"), true); }); test("review 4 C: X's addresses — a trailing dot, X's media hosts, a schemeless address; never a lookalike or a userinfo", () => { for (const url of [ "https://x.com./u/status/1", "https://twitter.com./u/status/1", "https://t.co./AbC", "https://video.twimg.com/ext_tw_video/1/pu/vid/720x1280/a.mp4", "https://pbs.twimg.com/media/abc.jpg", "https://twimg.com/x", "x.com/u/status/1", "twitter.com/u/status/1", "mobile.twitter.com/u/status/1", "t.co/AbC", "video.twimg.com/a.mp4", "X.COM/u/status/1", "//x.com/u/status/1", " https://x.com/u/status/1 ", "HTTPS://X.COM/u/status/1", "https://mobile.TWITTER.com/u/status/1", "https://evil.example@x.com/u/status/1", "https://x.com:443/u/status/1", "x.com:443/u/status/1", ]) { assert.equal(isXUrl(url), true, url); } for (const url of [ "https://x.com@evil.example/u/status/1", "https://x.com.evil.example/u/status/1", "https://evilx.com/u", "https://nottwitter.com/u", "https://twitter.com.evil.example/u", "https://nottwimg.com/a.mp4", "https://twimg.com.evil.example/a.mp4", "https://t.co.evil.example/a", "evil.example/x.com/u/status/1", "x.com.evil.example/u", "nottwitter.com/u", "mailto:someone@example.org", "javascript:alert(1)", "/u/status/1", "x", ]) { assert.equal(isXUrl(url), false, url); } }); test("review 4 B: an import of a file by its page is X's by that page's address, on any channel", () => { const im = (url: string | undefined, channelPlatform: string, queueKey = "import-media:c") => xJobAsksX({ kind: "import-media", queueKey, channelPlatform, ...(url !== undefined ? { params: { url } } : {}) }); for (const url of ["https://x.com/u/status/1", "https://t.co/AbC", "https://twitter.com/u/status/1"]) { assert.equal(im(url, "youtube"), true, `${url} on a YouTube channel`); assert.equal(im(url, "twitter"), true, `${url} on an X channel`); } assert.equal(im("https://www.youtube.com/watch?v=a", "twitter"), false, "another platform's page, on an X channel"); assert.equal(im("https://rumble.com/v-a.html", "rumble"), false); assert.equal(im(undefined, "twitter"), false, "a local file with no page"); assert.equal(im("https://x.com/u/status/1", "youtube", "platform:twitter"), true, "on X's queue"); }); test("review 3 S4: a start in the future counts in both windows, and never moves the windows off the real starts", () => { const real = Array.from({ length: 5 }, (_, i) => T0 + i * MIN); const future = T0 + 365 * 24 * 60 * MIN; const v = xJobCapVerdict([...real, future], T0 + 5 * MIN); assert.deepEqual([v.ok, v.lastHour, v.lastDay], [false, 6, 6]); // The next may start when the oldest of the newest six has left: the real 12:00 start, at 13:00. assert.equal(v.nextAt, T0 + 60 * MIN); // The future start alone keeps counting until the clock has passed it by the window. assert.deepEqual([xJobCapVerdict([future], T0).lastHour, xJobCapVerdict([future], T0).lastDay], [1, 1]); assert.equal(xJobCapVerdict([future], future + 60 * MIN).lastHour, 0); });