Skip to content

Commit 61fe7d7

Browse files
authored
test(quota-monitor): add coverage for GitHub API quota tracking and rate limit handling (JhaSourav07#3151)
## Description This PR introduces a comprehensive test suite for services/github/quota-monitor.ts to validate quota tracking, rate limit header parsing, refresh accounting, low-quota detection, and reset window handling. ## Changes Made 1.Added services/github/quota-monitor.test.ts 2. Test Coverage The new test suite includes 5 test cases: - Validates parsing of standard GitHub X-RateLimit-* response headers. - Verifies correct tracking of remaining API credits. - Ensures low-quota detection triggers when remaining requests fall below the configured threshold. - Confirms refresh operations are counted accurately. - Validates reset timestamps are converted from seconds to milliseconds correctly. Fixes JhaSourav07#2295 ## 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 22228bb + 573195b commit 61fe7d7

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import { QuotaMonitor } from './quota-monitor';
3+
4+
describe('QuotaMonitor', () => {
5+
let monitor: QuotaMonitor;
6+
7+
beforeEach(() => {
8+
monitor = QuotaMonitor.getInstance();
9+
monitor.reset();
10+
});
11+
12+
it('parses X-RateLimit headers correctly', () => {
13+
monitor.updateQuotaFromHeaders({
14+
'x-ratelimit-limit': '5000',
15+
'x-ratelimit-remaining': '4200',
16+
'x-ratelimit-reset': '1710000000',
17+
});
18+
19+
const quota = monitor.getQuota();
20+
21+
expect(quota.limit).toBe(5000);
22+
expect(quota.remaining).toBe(4200);
23+
});
24+
25+
it('calculates remaining API credits correctly', () => {
26+
monitor.setQuota(5000, 1234, Date.now());
27+
28+
const quota = monitor.getQuota();
29+
30+
expect(quota.limit).toBe(5000);
31+
expect(quota.remaining).toBe(1234);
32+
});
33+
34+
it('flags quota as low when remaining credits are below 10%', () => {
35+
monitor.setQuota(5000, 499, Date.now());
36+
37+
expect(monitor.isQuotaLow()).toBe(true);
38+
39+
monitor.setQuota(5000, 500, Date.now());
40+
41+
expect(monitor.isQuotaLow()).toBe(false);
42+
});
43+
44+
it('tracks refresh operations correctly', () => {
45+
monitor.incrementRefreshCount();
46+
monitor.incrementRefreshCount();
47+
monitor.incrementRefreshCount();
48+
49+
expect(monitor.getQuota().totalRefreshes).toBe(3);
50+
});
51+
52+
it('parses reset window timestamps into milliseconds', () => {
53+
monitor.updateQuotaFromHeaders({
54+
'x-ratelimit-reset': '1710000000',
55+
});
56+
57+
expect(monitor.getQuota().resetTime).toBe(1710000000 * 1000);
58+
});
59+
});

0 commit comments

Comments
 (0)