Files
TeleUploader/test/rateLimit.test.ts
T

36 lines
982 B
TypeScript
Raw Normal View History

import { beforeEach, describe, expect, it } from 'bun:test';
import { config } from '../src/env';
import {
checkRateLimit,
cleanupRateLimitCache,
clearRateLimitCache,
} from '../src/interfaces/http/middleware/rate-limit';
2026-05-18 07:01:32 +07:00
describe('Rate Limiter', () => {
2026-05-18 07:01:32 +07:00
beforeEach(() => {
clearRateLimitCache();
2026-05-18 07:01:32 +07:00
});
it('should allow requests up to the configured limit then block', () => {
const key = 'user-1';
const limit = config.rateLimitMaxRequests;
for (let i = 0; i < limit; i++) {
expect(checkRateLimit(key)).toBe(true);
}
expect(checkRateLimit(key)).toBe(false);
});
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);
});
it('should no-op on cleanup of active entries', () => {
checkRateLimit('user-3');
expect(() => cleanupRateLimitCache()).not.toThrow();
});
2026-05-18 07:01:32 +07:00
});