// The gap between two jobs on one platform queue (plans/pacing-guard.md). // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test jobs/queueGap.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { clearQueueGaps, gapReason, lastQueueEnd, noteQueueJobEnd, queueGapFor, queueGapLine, queueGapPlatform, queueGapWait, waitForQueueGap, waitText, setXGapRandom, } from "./queueGap"; import { jobGapSecondsFor, sanitizePacing } from "../lib/settingsSchema"; async function jobsDir() { const root = await mkdtemp(path.join(os.tmpdir(), "queue-gap-")); return { jobsDir: path.join(root, ".jobs") }; } test("only platform: queues have a gap", () => { assert.equal(queueGapPlatform("platform:twitter"), "twitter"); assert.equal(queueGapPlatform("platform:megaphone.fm"), "megaphone.fm"); assert.equal(queueGapPlatform("clips:youtube"), null); assert.equal(queueGapPlatform("transcription"), null); assert.equal(queueGapPlatform(""), null); }); test("the gap: the operator's per platform, else the built-in per platform, else default", () => { delete process.env.E2E_JOB_GAP_SECONDS; assert.deepEqual(jobGapSecondsFor(undefined, "twitter"), { seconds: 300, key: "twitter" }); assert.deepEqual(jobGapSecondsFor(undefined, "xenforo"), { seconds: 60, key: "xenforo" }); assert.deepEqual(jobGapSecondsFor(undefined, "bluesky"), { seconds: 60, key: "bluesky" }); assert.deepEqual(jobGapSecondsFor(undefined, "youtube"), { seconds: 20, key: "default" }); const pacing = sanitizePacing({ jobGapSeconds: { twitter: 600, default: 45, "Bad Key": 3, youtube: -1 } }); assert.deepEqual(pacing.jobGapSeconds, { twitter: 600, default: 45 }); assert.deepEqual(jobGapSecondsFor(pacing, "twitter"), { seconds: 600, key: "twitter" }); // A platform with a built-in keeps it over the operator's default. assert.deepEqual(jobGapSecondsFor(pacing, "xenforo"), { seconds: 60, key: "xenforo" }); assert.deepEqual(jobGapSecondsFor(pacing, "megaphone.fm"), { seconds: 45, key: "default" }); assert.deepEqual(sanitizePacing({ jobGapSeconds: { twitter: 10 ** 9 } }).jobGapSeconds, { twitter: 86400 }); }); test("E2E_JOB_GAP_SECONDS replaces the built-ins only, and only on the test server (E2E_TEST_ROUTES=1); a set value still applies", () => { process.env.E2E_JOB_GAP_SECONDS = "0"; try { // An editor started normally ignores it (review 1 F6). delete process.env.E2E_TEST_ROUTES; assert.equal(queueGapFor(undefined, "twitter").seconds, 300); process.env.E2E_TEST_ROUTES = "1"; assert.equal(queueGapFor(undefined, "twitter").seconds, 0); assert.equal(queueGapFor({ jobGapSeconds: { twitter: 3 } }, "twitter").seconds, 3); } finally { delete process.env.E2E_JOB_GAP_SECONDS; delete process.env.E2E_TEST_ROUTES; } }); test("the sentence", () => { assert.equal( gapReason("twitter", { seconds: 300, key: "twitter" }), "X jobs are spaced 5 min apart (pacing.jobGapSeconds.twitter)", ); assert.equal(waitText(252_000), "4m12s"); assert.equal(waitText(45_000), "45s"); assert.equal( queueGapLine({ until: 0, remainingMs: 252_000, reason: "X jobs are spaced 5 min apart (pacing.jobGapSeconds.twitter)" }), "waiting 4m12s: X jobs are spaced 5 min apart (pacing.jobGapSeconds.twitter)", ); }); test("no end yet: no wait; an end: wait out the rest; the end is persisted and read back after a restart", async () => { delete process.env.E2E_JOB_GAP_SECONDS; const paths = await jobsDir(); const q = "platform:twitter"; // X's gap is drawn from a range, never one value (jobs/xSittings.test.ts): // a 5-5 setting is widened to 5-8 min, and a seeded draw makes it exact here. const exact = { jobGapSeconds: {}, jitter: 0 }; const xGap = { gapMinMinutes: 5, gapMaxMinutes: 5 }; setXGapRandom(0); try { assert.equal(await queueGapWait({ paths, queueKey: q, pacing: exact, now: 1_000_000, xGap }), null); await noteQueueJobEnd(paths, q, 1_000_000); const w = await queueGapWait({ paths, queueKey: q, pacing: exact, now: 1_048_000, xGap }); assert.ok(w && w.drawn); assert.deepEqual(w!.drawn!.range, { minSeconds: 300, maxSeconds: 480 }); assert.equal(w!.remainingMs, w!.drawn!.seconds * 1000 - 48_000); assert.match(queueGapLine(w!), /^waiting \d+m\d\ds \(a \d+m\d\ds gap drawn from 5-8 min\): X jobs are spaced 5-8 min apart, drawn per job/); assert.equal(await queueGapWait({ paths, queueKey: q, pacing: exact, now: 1_000_000 + 480_001, xGap }), null); } finally { setXGapRandom(null); } const file = JSON.parse(await readFile(path.join(paths.jobsDir, "queue-gaps.json"), "utf8")); assert.equal(file.ends[q], 1_000_000); clearQueueGaps(); // the restart assert.equal(await lastQueueEnd(paths, q), 1_000_000); // Not a platform queue: nothing noted. await noteQueueJobEnd(paths, "transcription", 5); assert.equal(await lastQueueEnd(paths, "transcription"), undefined); }); test("waitForQueueGap waits, logs once per wait, tells the row, clears it", async () => { const paths = await jobsDir(); const q = "platform:xenforo"; let now = 10_000_000; await noteQueueJobEnd(paths, q, now); now += 15_000; const lines: string[] = []; const seen: (string | null)[] = []; const waited = await waitForQueueGap({ paths, queueKey: q, pacing: () => ({ jobGapSeconds: {}, jitter: 0 }), onLog: (l) => lines.push(l), onWait: (w) => seen.push(w ? w.reason : null), clock: { now: () => now, sleep: async (ms) => void (now += ms) }, }); assert.equal(waited, 45_000); assert.deepEqual(lines, ["waiting 45s: Forum jobs are spaced 1 min apart (pacing.jobGapSeconds.xenforo)"]); assert.deepEqual(seen, ["Forum jobs are spaced 1 min apart (pacing.jobGapSeconds.xenforo)", null]); }); test("the gap is stretched by up to pacing.jitter, the same on every re-read of one wait", async () => { const paths = await jobsDir(); const q = "platform:youtube"; const end = 50_000_000; await noteQueueJobEnd(paths, q, end); const pacing = { jobGapSeconds: {}, jitter: 0.25 }; const a = await queueGapWait({ paths, queueKey: q, pacing, now: end + 1 }); const b = await queueGapWait({ paths, queueKey: q, pacing, now: end + 5_000 }); assert.ok(a && b); // Never shorter than the 20 s default, never more than a quarter longer. assert.ok(a.until >= end + 20_000 && a.until <= end + 25_000, String(a.until - end)); // Stable: a later read of the same wait ends at the same instant. assert.equal(b.until, a.until); // A different previous end draws a different share (seeded by it). const draws = new Set(); for (let i = 0; i < 20; i++) { await noteQueueJobEnd(paths, q, end + i * 100_000); const w = await queueGapWait({ paths, queueKey: q, pacing, now: end + i * 100_000 + 1 }); draws.add(w!.until - (end + i * 100_000)); } assert.ok(draws.size > 10, `only ${draws.size} distinct gaps`); for (const g of draws) assert.ok(g >= 20_000 && g <= 25_000, String(g)); }); test("waitForQueueGap rejects on abort", async () => { const paths = await jobsDir(); const q = "platform:twitter"; await noteQueueJobEnd(paths, q, Date.now()); const ac = new AbortController(); const p = waitForQueueGap({ paths, queueKey: q, pacing: () => undefined, signal: ac.signal }); setTimeout(() => ac.abort(), 10); await assert.rejects(p, { name: "AbortError" }); });