Skip to content

Commit 831d28d

Browse files
authored
test(utils): check rate limit TTL boundary robustness (JhaSourav07#1561) (JhaSourav07#1662)
## Description Fixes JhaSourav07#1561 Added a boundary robustness test for the rate limit utility to verify that keys expire exactly at the configured window limit when requests occur across sliding time ranges. ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (test-only changes) ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [ ] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [ ] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that i have only one commit to merge in this PR. * [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard (not applicable for test-only changes). * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support. ## Test Validation Command executed: ```bash npx vitest run lib/rate-limit.test.ts ``` Result: ```text ✓ lib/rate-limit.test.ts Test Files 1 passed (1) Tests 12 passed (12) ``` ### Implementation Notes * Updated `lib/rate-limit.test.ts` * Added a boundary-focused test: * `expires at the window boundary after sliding requests` * Verified expiration behavior at the configured window limit for sliding request timings. * No production code changes were made.
2 parents 585b3bd + 6e3dfc4 commit 831d28d

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

lib/rate-limit.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ describe('rateLimit', () => {
7070
expect(result.remaining).toBe(limit - 1);
7171
});
7272

73+
it('expires at the window boundary after sliding requests', async () => {
74+
const ip = '7.7.7.7';
75+
const windowMs = 60000;
76+
const limit = 3;
77+
78+
vi.setSystemTime(0);
79+
await rateLimit(ip, limit, windowMs);
80+
vi.advanceTimersByTime(20000);
81+
await rateLimit(ip, limit, windowMs);
82+
vi.advanceTimersByTime(20000);
83+
await rateLimit(ip, limit, windowMs);
84+
85+
// Still within the same fixed window, before the boundary
86+
vi.advanceTimersByTime(19999);
87+
expect((await rateLimit(ip, limit, windowMs)).success).toBe(false);
88+
89+
// Move just past the window limit. The old entry should have expired.
90+
vi.advanceTimersByTime(2);
91+
92+
const result = await rateLimit(ip, limit, windowMs);
93+
expect(result.success).toBe(true);
94+
expect(result.remaining).toBe(limit - 1);
95+
});
96+
7397
it('tracks different IPs separately', async () => {
7498
const ip1 = '11.11.11.11';
7599
const ip2 = '22.22.22.22';

0 commit comments

Comments
 (0)