import { test, beforeEach } from "node:test"; import assert from "node:assert/strict"; import { clearPlatformGaps, notePlatformGap, platformGapRemainingMs, waitForPlatformGap, } from "./platformGap"; beforeEach(() => clearPlatformGaps()); test("a platform with no import yet is asked at once", () => { assert.equal(platformGapRemainingMs("odysee", 1_000), 0); }); test("the next import waits the gap from when the last one settled, per platform", () => { notePlatformGap("odysee", 60_000, 1_000); assert.equal(platformGapRemainingMs("odysee", 1_000), 60_000); assert.equal(platformGapRemainingMs("odysee", 31_000), 30_000); assert.equal(platformGapRemainingMs("odysee", 61_000), 0); assert.equal(platformGapRemainingMs("bitchute", 1_000), 0); }); test("a later start is never shortened by a shorter gap", () => { notePlatformGap("odysee", 90_000, 0); notePlatformGap("odysee", 10_000, 5_000); assert.equal(platformGapRemainingMs("odysee", 5_000), 85_000); }); test("the gap lives on globalThis, so every bundle shares it", () => { notePlatformGap("odysee", 1_000, 0); assert.equal(globalThis.__yttPlatformGap__?.get("odysee"), 1_000); }); test("waiting re-reads the remaining time after each sleep and logs each wait", async () => { const left = [45_000, 2_000, 0]; const slept: number[] = []; const lines: string[] = []; const waited = await waitForPlatformGap({ label: "Odysee", remainingMs: () => left.shift() ?? 0, onLog: (l) => lines.push(l), sleep: async (ms) => { slept.push(ms); }, }); assert.deepEqual(slept, [45_000, 2_000]); assert.equal(waited, 47_000); assert.deepEqual(lines, [ "Odysee asks for a gap between videos: waiting 45s.", "Odysee asks for a gap between videos: waiting 2s.", ]); }); test("nothing to wait: no sleep, no line", async () => { const lines: string[] = []; const waited = await waitForPlatformGap({ label: "Odysee", remainingMs: async () => 0, onLog: (l) => lines.push(l), sleep: async () => assert.fail("must not sleep"), }); assert.equal(waited, 0); assert.deepEqual(lines, []); }); test("an aborted job stops waiting", async () => { const ac = new AbortController(); const p = waitForPlatformGap({ label: "Odysee", remainingMs: () => 60_000, signal: ac.signal }); ac.abort(); await assert.rejects(p, { name: "AbortError" }); });