import assert from "node:assert/strict"; import { test } from "node:test"; import { type Clock, XLimiter, XRateLimitError } from "../src/x-limits.ts"; /** A clock that only moves when told to, recording the waits asked of it. */ const fakeClock = () => { let now = 1_000_000; const waits: number[] = []; let draw = 0.5; const clock: Clock = { random: () => draw, now: () => now, sleep: async (ms) => { waits.push(ms); now += ms; }, }; return { clock, waits, advance: (ms: number) => (now += ms), setDraw: (r: number) => (draw = r) }; }; const signal = new AbortController().signal; const ok = async () => "ok"; test("a search sooner than the gap waits out the rest of it", async () => { const { clock, waits, advance } = fakeClock(); const l = new XLimiter(clock, { gap: 10_000, jitter: 0, hour: 30, day: 150 }); await l.run(signal, ok); advance(3_000); await l.run(signal, ok); assert.deepEqual(waits, [7_000]); }); test("over the hourly cap a search fails at once, naming when it can run", async () => { const { clock, advance } = fakeClock(); const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 2, day: 150 }); await l.run(signal, ok); await l.run(signal, ok); await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /last hour/.test(e.message)); advance(3_600_001); assert.equal(await l.run(signal, ok), "ok"); }); test("the daily cap holds across hours", async () => { const { clock, advance } = fakeClock(); const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 100, day: 3 }); for (let i = 0; i < 3; i++) { await l.run(signal, ok); advance(3_600_001); } await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /24 hours/.test(e.message)); }); test("one at a time: the second starts only after the first finishes", async () => { const { clock } = fakeClock(); const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); const order: string[] = []; let release!: () => void; const first = l.run(signal, async () => { order.push("first start"); await new Promise((r) => (release = r)); order.push("first end"); }); const second = l.run(signal, async () => { order.push("second start"); }); await new Promise((r) => setTimeout(r, 10)); release(); await Promise.all([first, second]); assert.deepEqual(order, ["first start", "first end", "second start"]); }); test("a failed search does not block the queue", async () => { const { clock } = fakeClock(); const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); await assert.rejects(l.run(signal, async () => { throw new Error("boom"); })); assert.equal(await l.run(signal, ok), "ok"); }); test("the gap is drawn afresh each time, within gap ± jitter", async () => { const { clock, waits, setDraw } = fakeClock(); const l = new XLimiter(clock, { gap: 30_000, jitter: 25_000, hour: 30, day: 150 }); await l.run(signal, ok); setDraw(0); // the shortest: 30 - 25 s await l.run(signal, ok); setDraw(0.999999); // close to the longest: 30 + 25 s await l.run(signal, ok); assert.equal(waits[0], 5_000); assert.ok(waits[1]! > 54_999 && waits[1]! <= 55_000); }); test("the defaults are 30 s ± 25 s", async () => { const { GAP_MS, JITTER_MS } = await import("../src/x-limits.ts"); assert.equal(GAP_MS, 30_000); assert.equal(JITTER_MS, 25_000); });