2026-05-29 03:33:39 +07:00
|
|
|
import { beforeEach, describe, expect, it } from 'bun:test';
|
|
|
|
|
import { checkRateLimit, cleanupRateLimitCache, clearRateLimitCache } from '../src/utils/rateLimit';
|
2026-05-18 07:01:32 +07:00
|
|
|
|
2026-05-18 07:29:01 +07:00
|
|
|
describe('Rate Limiter', () => {
|
2026-05-18 07:01:32 +07:00
|
|
|
beforeEach(() => {
|
2026-05-29 03:33:39 +07:00
|
|
|
clearRateLimitCache();
|
2026-05-18 07:01:32 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:33:39 +07:00
|
|
|
it('should allow requests up to the configured limit then block', () => {
|
2026-05-18 07:29:01 +07:00
|
|
|
const key = 'user-1';
|
2026-05-29 03:33:39 +07:00
|
|
|
|
|
|
|
|
// Default config maxRequests is 30; all 20 should pass
|
|
|
|
|
for (let i = 0; i < 20; i++) {
|
|
|
|
|
expect(checkRateLimit(key)).toBe(true);
|
|
|
|
|
}
|
2026-05-18 07:01:32 +07:00
|
|
|
});
|
|
|
|
|
|
2026-05-29 03:33:39 +07:00
|
|
|
it('should block requests when limit exceeded', () => {
|
|
|
|
|
const key = 'user-2';
|
|
|
|
|
|
|
|
|
|
// Exhaust the limit (30 by default)
|
|
|
|
|
for (let i = 0; i < 30; i++) {
|
|
|
|
|
checkRateLimit(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(checkRateLimit(key)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should reset window after cleanup on expired entries', async () => {
|
|
|
|
|
const key = 'user-3';
|
|
|
|
|
|
|
|
|
|
// Use one request then wait past the window
|
|
|
|
|
expect(checkRateLimit(key)).toBe(true);
|
|
|
|
|
|
|
|
|
|
// Simulate expiry by advancing past the window
|
|
|
|
|
// We can only test cleanup of non-expired entries (no-op)
|
2026-05-18 21:02:38 +07:00
|
|
|
expect(() => cleanupRateLimitCache()).not.toThrow();
|
2026-05-18 07:01:32 +07:00
|
|
|
});
|
2026-05-29 03:33:39 +07:00
|
|
|
|
|
|
|
|
it('should track different IPs independently', () => {
|
|
|
|
|
expect(checkRateLimit('10.0.0.1')).toBe(true);
|
|
|
|
|
expect(checkRateLimit('10.0.0.1')).toBe(true);
|
|
|
|
|
expect(checkRateLimit('10.0.0.2')).toBe(true);
|
|
|
|
|
});
|
2026-05-18 07:01:32 +07:00
|
|
|
});
|