Skip to content

Commit 573195b

Browse files
test
1 parent 2e604a1 commit 573195b

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)