// X's cooldown after a trending read met a rate limit (lane x-trending, review // 1 SHOULD 5), on the real state file of a temp corpus: 15 minutes on the // `twitter` platform entry — X's one stop (lane x-sittings) — read back by the // read's own held check. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test controller/xTrendingCooldown.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "x-trending-cooldown-")); Object.assign(process.env, { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SETTINGS_FILE: path.join(ROOT, "settings.json") }); writeFileSync(path.join(ROOT, "settings.json"), "{}"); after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("../lib/paths"); const { clearXStop, clearPlatformHold, platformCooldownRemainingMs, recordPlatformClean, recordPlatformCooldown, xStopRemainingMs } = await import( "../jobs/downloadBackoff" ); const { X_RATE_LIMIT_HOLD_MS, xTrendingHeld } = await import("./readXTrending"); // THE ONE X STOP (lane x-sittings): a trending read's 429 is X's 15-minute // stop, the same entry every X job's guard reads — not a cooldown of the // read's own — and NOTHING ends it early but the operator's "Clear the X // stop" (clearXStop, confirmed): not a clean answer, not Clear hold. test("no cooldown: a read may start; after a 429: refused until the minute it names; only the operator's clear ends it early", async () => { const paths = getPaths(); assert.equal(await xTrendingHeld(paths), null); const now = Date.now(); const until = await recordPlatformCooldown("twitter", X_RATE_LIMIT_HOLD_MS, paths, now); assert.equal(until, now + 15 * 60_000); const left = await platformCooldownRemainingMs("twitter", paths); assert.ok(left > 14 * 60_000 && left <= 15 * 60_000, String(left)); const held = await xTrendingHeld(paths, now); assert.match( held ?? "", /^X answered a rate limit \(429\) a short while ago: no trending read starts before \d\d:\d\d UTC \(15 min from now\)\. Ask again then; nothing retries by itself\.$/, ); assert.ok(held!.includes(new Date(until).toISOString().slice(11, 16))); // Another platform is not held by it. assert.equal(await platformCooldownRemainingMs("bluesky", paths), 0); // A second, shorter cooldown never brings the end forward. assert.equal(await recordPlatformCooldown("twitter", 60_000, paths, now), until); // It is the X stop itself, as the guard of every X job reads it. const stop = await xStopRemainingMs(paths, now); assert.equal(stop, until - now); // A clean answer does not clear it, nor does Clear hold: the read stays refused. await recordPlatformClean("twitter", paths); assert.equal(await xStopRemainingMs(paths, now), stop); assert.ok(await xTrendingHeld(paths, now)); await clearPlatformHold("twitter", paths); assert.equal(await xStopRemainingMs(paths, now), stop); assert.ok(await xTrendingHeld(paths, now)); // The operator's clear, unconfirmed, removes nothing. const asked = await clearXStop(paths, { confirm: false, by: "test", now }); assert.equal(asked.ok, false); assert.ok(await xTrendingHeld(paths, now)); // Confirmed, it ends the stop, and a read may start again. const cleared = await clearXStop(paths, { confirm: true, by: "test", now }); assert.ok(cleared.ok && cleared.cleared?.until === until); assert.equal(await xStopRemainingMs(paths, now), 0); assert.equal(await xTrendingHeld(paths, now), null); }); // Review 2 N4: a clean trending read settles nothing on the platform. The // download lane's own backoff and pace for X are its own; the read's cooldown // simply runs out. test("N4: a clean read leaves the download lane's X backoff exactly as it was", async () => { const { mkdirSync, writeFileSync: write } = await import("node:fs"); const { recordDownloadBackoff } = await import("../jobs/downloadBackoff"); const { planReadXTrending, runXTrendingRead: runXTrendingReadDirect } = await import("./readXTrending"); // X is asked only inside a job of the editor's, on X's queue (lib/xJobRule.ts): these calls stand where one does. const { insideEditorJob } = await import("../testing/jobs/asEditorJob"); const runXTrendingRead = insideEditorJob(runXTrendingReadDirect); const { fakeTrendingPage } = await import("../social/fakeXProfileBrowser"); const { FIXTURE_TREND_URL, fixtureTrendingResponses } = await import("../social/__fixtures__/xTrendingCapture"); const paths = getPaths(); mkdirSync(path.join(paths.channelsDir, "trending"), { recursive: true }); write(path.join(paths.channelsDir, "trending", "config.json"), JSON.stringify({ handling: "transcribe", sourceKind: "social", platform: "twitter", xTrending: {} })); // The lane met a rate limit on X for its own reason. await recordDownloadBackoff("twitter", paths, "rate_limit"); const before = await platformCooldownRemainingMs("twitter", paths); assert.ok(before > 0); const planned = await planReadXTrending(paths, { slug: "trending", url: FIXTURE_TREND_URL }, { hasProfile: async () => true, held: async () => null }); assert.ok(planned.ok && planned.plan.mode === "read"); const page = fakeTrendingPage(() => ({ trending: fixtureTrendingResponses() })); const result = await runXTrendingRead({ paths, plan: planned.plan as never, deps: { openPage: async () => ({ page, close: async () => {} }), pauseMs: () => 0, pause: async () => {}, held: async () => null, noteOutcome: async () => null }, }); assert.equal(result.outcome, "read"); const after = await platformCooldownRemainingMs("twitter", paths); assert.ok(after > 0 && after <= before, `the lane's cooldown stands (${before} → ${after})`); });