aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 05:00:44 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 05:00:44 +0000
commit39f887e07721c5b3ddd31657e5e8e25e4486d2e5 (patch)
tree95e0e6f98db7cfe3923f9bd8d7718f29e38f65b0 /castle-search/test
parentb03415aafeea985561f3980f8b484173530a05f8 (diff)
castle-search: limit X searches so the account reads X like a person
One at a time, 10 s apart (a sooner one waits), at most 30 an hour and 150 a day; over a cap a search fails at once, saying when the next may run.
Diffstat (limited to 'castle-search/test')
-rw-r--r--castle-search/test/x-limits.test.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/castle-search/test/x-limits.test.ts b/castle-search/test/x-limits.test.ts
new file mode 100644
index 0000000..b239f5b
--- /dev/null
+++ b/castle-search/test/x-limits.test.ts
@@ -0,0 +1,78 @@
+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[] = [];
+ const clock: Clock = {
+ now: () => now,
+ sleep: async (ms) => {
+ waits.push(ms);
+ now += ms;
+ },
+ };
+ return { clock, waits, advance: (ms: number) => (now += ms) };
+};
+
+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, 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, 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, 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, hour: 30, day: 150 });
+ const order: string[] = [];
+ let release!: () => void;
+ const first = l.run(signal, async () => {
+ order.push("first start");
+ await new Promise<void>((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, hour: 30, day: 150 });
+ await assert.rejects(l.run(signal, async () => {
+ throw new Error("boom");
+ }));
+ assert.equal(await l.run(signal, ok), "ok");
+});