|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { refreshRateLimiter } from './refresh-rate-limiter'; |
| 3 | + |
| 4 | +describe('RefreshRateLimiter', () => { |
| 5 | + beforeEach(() => { |
| 6 | + refreshRateLimiter.reset(); |
| 7 | + delete process.env.MAX_REFRESHES_PER_HOUR; |
| 8 | + vi.restoreAllMocks(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('allows the initial refresh request from an IP', () => { |
| 12 | + const result = refreshRateLimiter.checkLimit('127.0.0.1'); |
| 13 | + |
| 14 | + expect(result.success).toBe(true); |
| 15 | + expect(result.limit).toBe(3); |
| 16 | + expect(result.remaining).toBe(2); |
| 17 | + }); |
| 18 | + |
| 19 | + it('blocks requests after the limit is reached within the same window', () => { |
| 20 | + refreshRateLimiter.setLimit(3); |
| 21 | + |
| 22 | + refreshRateLimiter.checkLimit('127.0.0.1'); |
| 23 | + refreshRateLimiter.checkLimit('127.0.0.1'); |
| 24 | + refreshRateLimiter.checkLimit('127.0.0.1'); |
| 25 | + |
| 26 | + const blocked = refreshRateLimiter.checkLimit('127.0.0.1'); |
| 27 | + |
| 28 | + expect(blocked.success).toBe(false); |
| 29 | + expect(blocked.remaining).toBe(0); |
| 30 | + expect(blocked.limit).toBe(3); |
| 31 | + }); |
| 32 | + |
| 33 | + it('resets the limit after the cooldown window expires', () => { |
| 34 | + vi.useFakeTimers(); |
| 35 | + |
| 36 | + refreshRateLimiter.setLimit(2, 1000); |
| 37 | + |
| 38 | + refreshRateLimiter.checkLimit('127.0.0.1'); |
| 39 | + refreshRateLimiter.checkLimit('127.0.0.1'); |
| 40 | + |
| 41 | + expect(refreshRateLimiter.checkLimit('127.0.0.1').success).toBe(false); |
| 42 | + |
| 43 | + vi.advanceTimersByTime(1001); |
| 44 | + |
| 45 | + const result = refreshRateLimiter.checkLimit('127.0.0.1'); |
| 46 | + |
| 47 | + expect(result.success).toBe(true); |
| 48 | + expect(result.remaining).toBe(1); |
| 49 | + |
| 50 | + vi.useRealTimers(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns correct reset timestamp information', () => { |
| 54 | + vi.useFakeTimers(); |
| 55 | + |
| 56 | + const now = new Date('2025-01-01T00:00:00Z'); |
| 57 | + vi.setSystemTime(now); |
| 58 | + |
| 59 | + refreshRateLimiter.setLimit(3, 60_000); |
| 60 | + |
| 61 | + const result = refreshRateLimiter.checkLimit('127.0.0.1'); |
| 62 | + |
| 63 | + expect(result.reset).toBe(now.getTime() + 60_000); |
| 64 | + |
| 65 | + vi.useRealTimers(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('uses MAX_REFRESHES_PER_HOUR environment override', () => { |
| 69 | + process.env.MAX_REFRESHES_PER_HOUR = '5'; |
| 70 | + |
| 71 | + refreshRateLimiter.reset(); |
| 72 | + |
| 73 | + const result = refreshRateLimiter.checkLimit('127.0.0.1'); |
| 74 | + |
| 75 | + expect(result.limit).toBe(5); |
| 76 | + expect(result.remaining).toBe(4); |
| 77 | + }); |
| 78 | +}); |
0 commit comments