Skip to content

Commit 41fcb30

Browse files
test(refresh-rate-limiter): add mock integration coverage
1 parent 65413a4 commit 41fcb30

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { RefreshRateLimiter } from './refresh-rate-limiter';
3+
4+
describe('RefreshRateLimiter Mock Integrations', () => {
5+
let limiter: RefreshRateLimiter;
6+
7+
beforeEach(() => {
8+
vi.restoreAllMocks();
9+
delete process.env.MAX_REFRESHES_PER_HOUR;
10+
limiter = RefreshRateLimiter.getInstance();
11+
limiter.reset();
12+
});
13+
14+
it('mocks asynchronous service loading paths with a pending state stub', async () => {
15+
const serviceLoader = vi.fn().mockResolvedValue({ status: 'loaded' });
16+
const pendingState = { loading: true };
17+
18+
const result = await serviceLoader();
19+
20+
expect(pendingState.loading).toBe(true);
21+
expect(serviceLoader).toHaveBeenCalledTimes(1);
22+
expect(result.status).toBe('loaded');
23+
});
24+
25+
it('queries local cache before allowing a refresh request', () => {
26+
const localCache = new Map<string, boolean>();
27+
localCache.set('127.0.0.1', true);
28+
29+
const cacheResult = localCache.get('127.0.0.1');
30+
const limitResult = limiter.checkLimit('127.0.0.1');
31+
32+
expect(cacheResult).toBe(true);
33+
expect(limitResult.success).toBe(true);
34+
expect(limitResult.remaining).toBe(2);
35+
});
36+
37+
it('uses fallback behavior during fake endpoint timeout blocks', async () => {
38+
const timeoutEndpoint = vi.fn().mockRejectedValue(new Error('timeout'));
39+
const fallback = vi.fn().mockResolvedValue('fallback-cache');
40+
41+
let result: string;
42+
43+
try {
44+
result = await timeoutEndpoint();
45+
} catch {
46+
result = await fallback();
47+
}
48+
49+
expect(timeoutEndpoint).toHaveBeenCalledTimes(1);
50+
expect(fallback).toHaveBeenCalledTimes(1);
51+
expect(result).toBe('fallback-cache');
52+
});
53+
54+
it('writes complete cache sync on successful callback', async () => {
55+
const cacheSync = new Map<string, number>();
56+
const successCallback = vi.fn().mockImplementation(async (ip: string) => {
57+
const result = limiter.checkLimit(ip);
58+
cacheSync.set(ip, result.remaining);
59+
return result;
60+
});
61+
62+
const result = await successCallback('192.168.1.1');
63+
64+
expect(result.success).toBe(true);
65+
expect(cacheSync.get('192.168.1.1')).toBe(2);
66+
expect(successCallback).toHaveBeenCalledWith('192.168.1.1');
67+
});
68+
69+
it('keeps isolated mock assertions stable without slow network fetches', async () => {
70+
const databaseStub = vi.fn().mockResolvedValue({ count: 1 });
71+
const networkFetchStub = vi.fn();
72+
73+
const dbResult = await databaseStub();
74+
75+
expect(dbResult.count).toBe(1);
76+
expect(databaseStub).toHaveBeenCalledTimes(1);
77+
expect(networkFetchStub).not.toHaveBeenCalled();
78+
});
79+
});

0 commit comments

Comments
 (0)