Skip to content

Commit 333674e

Browse files
test(quota-monitor): add massive scaling coverage
1 parent e3369ea commit 333674e

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { describe, expect, expectTypeOf, it } from 'vitest';
2+
import backgroundRefresh, { BackgroundRefresh } from './background-refresh';
3+
4+
describe('BackgroundRefresh type compiler validation', () => {
5+
it('exposes singleton instance with correct type', () => {
6+
expectTypeOf(BackgroundRefresh.getInstance()).toEqualTypeOf<BackgroundRefresh>();
7+
expectTypeOf(backgroundRefresh).toEqualTypeOf<BackgroundRefresh>();
8+
});
9+
10+
it('enforces public method input and return types', () => {
11+
expectTypeOf(backgroundRefresh.isStale).parameters.toEqualTypeOf<
12+
[lastSyncedAt: string | undefined]
13+
>();
14+
expectTypeOf(backgroundRefresh.isStale).returns.toEqualTypeOf<boolean>();
15+
16+
expectTypeOf(backgroundRefresh.triggerRefresh).parameters.toEqualTypeOf<[username: string]>();
17+
expectTypeOf(backgroundRefresh.triggerRefresh).returns.toEqualTypeOf<void>();
18+
19+
expectTypeOf(backgroundRefresh.isJobActive).parameters.toEqualTypeOf<[username: string]>();
20+
expectTypeOf(backgroundRefresh.isJobActive).returns.toEqualTypeOf<boolean>();
21+
});
22+
23+
it('blocks invalid parameters during static type checking', () => {
24+
type IsStaleParams = Parameters<typeof backgroundRefresh.isStale>;
25+
type TriggerRefreshParams = Parameters<typeof backgroundRefresh.triggerRefresh>;
26+
type IsJobActiveParams = Parameters<typeof backgroundRefresh.isJobActive>;
27+
28+
expectTypeOf<[number]>().not.toMatchTypeOf<IsStaleParams>();
29+
expectTypeOf<[]>().not.toMatchTypeOf<TriggerRefreshParams>();
30+
expectTypeOf<[null]>().not.toMatchTypeOf<IsJobActiveParams>();
31+
});
32+
33+
it('accepts optional timestamp values without compile errors', () => {
34+
expect(backgroundRefresh.isStale(undefined)).toBe(true);
35+
expect(backgroundRefresh.isStale(new Date().toISOString())).toBe(false);
36+
});
37+
38+
it('validates stale date constraints strictly', () => {
39+
const staleDate = new Date(Date.now() - 11 * 60 * 1000).toISOString();
40+
const freshDate = new Date().toISOString();
41+
42+
expect(backgroundRefresh.isStale(staleDate)).toBe(true);
43+
expect(backgroundRefresh.isStale(freshDate)).toBe(false);
44+
expect(backgroundRefresh.isStale('invalid-date')).toBe(false);
45+
});
46+
});
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import quotaMonitor, { QuotaMonitor } from './quota-monitor';
3+
4+
describe('QuotaMonitor massive scaling stability', () => {
5+
beforeEach(() => {
6+
quotaMonitor.reset();
7+
});
8+
9+
it('handles massive quota header values without breaking state', () => {
10+
const headers = {
11+
'x-ratelimit-limit': '1000000000',
12+
'x-ratelimit-remaining': '999999999',
13+
'x-ratelimit-reset': '9999999999',
14+
};
15+
16+
quotaMonitor.updateQuotaFromHeaders(headers);
17+
18+
expect(quotaMonitor.getQuota()).toEqual({
19+
limit: 1000000000,
20+
remaining: 999999999,
21+
resetTime: 9999999999 * 1000,
22+
totalRefreshes: 0,
23+
});
24+
});
25+
26+
it('handles thousands of refresh count increments efficiently', () => {
27+
const start = performance.now();
28+
29+
for (let i = 0; i < 10_000; i++) {
30+
quotaMonitor.incrementRefreshCount();
31+
}
32+
33+
const duration = performance.now() - start;
34+
const quota = quotaMonitor.getQuota();
35+
36+
expect(quota.totalRefreshes).toBe(10_000);
37+
expect(duration).toBeLessThan(100);
38+
});
39+
40+
it('keeps quota low calculation stable with extreme high bounds', () => {
41+
quotaMonitor.setQuota(Number.MAX_SAFE_INTEGER, 1, Date.now());
42+
43+
const quota = quotaMonitor.getQuota();
44+
45+
expect(Number.isFinite(quota.limit)).toBe(true);
46+
expect(Number.isFinite(quota.remaining)).toBe(true);
47+
expect(Number.isFinite(quota.resetTime)).toBe(true);
48+
expect(quotaMonitor.isQuotaLow()).toBe(true);
49+
});
50+
51+
it('does not overflow or corrupt state with massive mocked contributor actions', () => {
52+
const contributorActions = Array.from({ length: 25_000 }, (_, index) => ({
53+
username: `contributor-${index}`,
54+
commits: index + 1,
55+
pullRequests: index % 50,
56+
reviews: index % 20,
57+
}));
58+
59+
const totalActivity = contributorActions.reduce(
60+
(total, action) => total + action.commits + action.pullRequests + action.reviews,
61+
0
62+
);
63+
64+
quotaMonitor.setQuota(totalActivity, Math.floor(totalActivity * 0.75), Date.now() + 60_000);
65+
66+
const quota = quotaMonitor.getQuota();
67+
68+
expect(contributorActions).toHaveLength(25_000);
69+
expect(Number.isFinite(totalActivity)).toBe(true);
70+
expect(quota.limit).toBe(totalActivity);
71+
expect(quota.remaining).toBe(Math.floor(totalActivity * 0.75));
72+
expect(quotaMonitor.isQuotaLow()).toBe(false);
73+
});
74+
75+
it('returns the same singleton instance for high-volume access patterns', () => {
76+
const instances = Array.from({ length: 5_000 }, () => QuotaMonitor.getInstance());
77+
78+
expect(instances.every((instance) => instance === quotaMonitor)).toBe(true);
79+
});
80+
});

0 commit comments

Comments
 (0)