// The network guard's pure rules (plans/pacing-guard.md); the job funnel // end to end is pacingGuard.test.ts. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test jobs/networkGuard.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { ageText, emptyQueueRefusal, xJarVerdict } from "./networkGuard"; const NOW = Date.parse("2026-10-23T03:00:00Z"); const at = (msAgo: number) => new Date(NOW - msAgo).toISOString(); test("the empty queue is refused for a network kind only", () => { assert.match(emptyQueueRefusal("fetch-posts", "") ?? "", /^Fetch posts sends requests to a platform/); assert.match(emptyQueueRefusal("sync", "") ?? "", /never runs unqueued/); assert.equal(emptyQueueRefusal("fetch-posts", "platform:twitter"), null); assert.equal(emptyQueueRefusal("evict-clips", ""), null); assert.equal(emptyQueueRefusal("auto-download", ""), null); }); test("the fresh-jar rule (profile source)", () => { const v = (ageMs: number, authed = true, max = 60) => xJarVerdict({ source: "profile", status: { hasCookies: true, cookiesUpdatedAt: at(ageMs), looksAuthenticated: authed }, maxAgeMinutes: max, now: NOW, }); assert.deepEqual(v(59 * 60_000), { ok: true }); assert.deepEqual(v(16 * 24 * 3600_000), { ok: false, message: "X cookies were last refreshed 16 days ago (limit 60 min): press Refresh cookies (or Connect X account) on /cookies, then fetch again.", }); assert.match((v(61 * 60_000) as { message: string }).message, /61 min ago/); assert.match((v(60_000, false) as { message: string }).message, /holds no login/); // 0 turns the rule off. assert.deepEqual(v(16 * 24 * 3600_000, true, 0), { ok: true }); const none = xJarVerdict({ source: "profile", status: { hasCookies: false, looksAuthenticated: false }, maxAgeMinutes: 60, now: NOW, }); assert.match((none as { message: string }).message, /No X cookie jar/); }); test("the browser source cannot be checked: proceed, and say so", () => { const v = xJarVerdict({ source: "browser", status: { hasCookies: false, looksAuthenticated: false }, maxAgeMinutes: 60, now: NOW, }); assert.equal(v.ok, true); assert.match((v as { note?: string }).note ?? "", /cannot be read here/); }); test("ageText", () => { assert.equal(ageText(45 * 60_000), "45 min"); assert.equal(ageText(3 * 3600_000), "3 h"); assert.equal(ageText(16 * 24 * 3600_000), "16 days"); });