Skip to content

Commit e0891d3

Browse files
authored
test(refresh-policy): add coverage for cooldown timing and quota restrictions (JhaSourav07#3156)
## Description Adds a comprehensive test suite for services/github/refresh-policy.ts to validate refresh cooldown behavior, quota enforcement, and timing calculations. ## Changes Created: services/github/refresh-policy.test.ts Implemented 5 test cases covering: Allows refresh when a user has never been refreshed before. Blocks refresh requests during the active cooldown window. Allows refresh again once the cooldown period has elapsed. Correctly calculates remaining cooldown time. Prevents refreshes when GitHub API quota is critically low. Fixes JhaSourav07#2296 ## Pillar - [x] 📐 Pillar 2 — Geometric SVG Improvement - [x] 🛠️ Other (Bug fix, refactoring, docs) ## 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`). - [x] 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): ...`). - [x] 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. - [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). - [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents b04ef58 + aa9202c commit e0891d3

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { refreshPolicy } from './refresh-policy';
3+
import { quotaMonitor } from './quota-monitor';
4+
5+
describe('RefreshPolicy', () => {
6+
beforeEach(() => {
7+
refreshPolicy.reset();
8+
quotaMonitor.reset();
9+
10+
vi.restoreAllMocks();
11+
});
12+
13+
it('allows refresh for a user with no previous refresh history', () => {
14+
expect(refreshPolicy.isRefreshAllowed('kanishka')).toBe(true);
15+
});
16+
17+
it('blocks refresh during cooldown period', () => {
18+
refreshPolicy.setCooldown(60_000);
19+
20+
refreshPolicy.recordRefresh('kanishka');
21+
22+
expect(refreshPolicy.isRefreshAllowed('kanishka')).toBe(false);
23+
});
24+
25+
it('allows refresh again after cooldown expires', () => {
26+
vi.useFakeTimers();
27+
28+
refreshPolicy.setCooldown(60_000);
29+
30+
refreshPolicy.recordRefresh('kanishka');
31+
32+
vi.advanceTimersByTime(60_001);
33+
34+
expect(refreshPolicy.isRefreshAllowed('kanishka')).toBe(true);
35+
36+
vi.useRealTimers();
37+
});
38+
39+
it('returns remaining cooldown time correctly', () => {
40+
vi.useFakeTimers();
41+
42+
refreshPolicy.setCooldown(60_000);
43+
44+
refreshPolicy.recordRefresh('kanishka');
45+
46+
vi.advanceTimersByTime(20_000);
47+
48+
expect(refreshPolicy.getRemainingCooldown('kanishka')).toBe(40_000);
49+
50+
vi.useRealTimers();
51+
});
52+
53+
it('blocks refresh when GitHub quota is low', () => {
54+
quotaMonitor.setQuota(
55+
5000,
56+
100, // < 10%
57+
Date.now() + 60_000
58+
);
59+
60+
expect(refreshPolicy.isRefreshAllowed('kanishka')).toBe(false);
61+
});
62+
});

0 commit comments

Comments
 (0)