Files
Jyotisha/frontend/tests/request-rate-limit.test.ts
T
Jesse_Chen c38f11dbd3
Independent Staging Quality Gate / validate (push) Has been cancelled
Independent Staging Quality Gate / publish (push) Has been cancelled
fix(api): bind daily and synastry charts to stored profiles
Stop accepting client-supplied birth data on those paths, and cap session writes plus location lookups so a logged-in caller cannot farm compute.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-19 19:39:01 +08:00

49 lines
1.2 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { consumeRequestRateLimit } from "../src/lib/request-rate-limit.ts";
test("a user is allowed up to the window limit and then must wait", () => {
const store = new Map<string, number[]>();
const first = consumeRequestRateLimit({
key: "search:user-1",
limit: 2,
windowMs: 60_000,
now: 1_000,
store,
});
const second = consumeRequestRateLimit({
key: "search:user-1",
limit: 2,
windowMs: 60_000,
now: 2_000,
store,
});
const blocked = consumeRequestRateLimit({
key: "search:user-1",
limit: 2,
windowMs: 60_000,
now: 3_000,
store,
});
const otherUser = consumeRequestRateLimit({
key: "search:user-2",
limit: 2,
windowMs: 60_000,
now: 3_000,
store,
});
const afterWindow = consumeRequestRateLimit({
key: "search:user-1",
limit: 2,
windowMs: 60_000,
now: 61_500,
store,
});
assert.deepEqual(first, { ok: true });
assert.deepEqual(second, { ok: true });
assert.deepEqual(blocked, { ok: false, retryAfterSeconds: 58 });
assert.deepEqual(otherUser, { ok: true });
assert.deepEqual(afterWindow, { ok: true });
});