Skip to content

Commit 9c4cb4d

Browse files
authored
test(refresh-rate-limiter): add mock integration coverage (JhaSourav07#3032)
## Description Fixes JhaSourav07#2951 Added isolated test coverage for Asynchronous Service Layer Mocking & Local Cache Stubs in `services/github/refresh-rate-limiter.mock-integrations.test.ts`. The test suite covers: * Mocked asynchronous service loading paths * Local cache checks before refresh validation * Fallback behavior during fake endpoint timeout blocks * Cache sync updates on successful callbacks * Isolated database/network stubs without slow external fetches ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [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. * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors. * [x] My commits follow the Conventional Commits format. * [ ] 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. * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 6eed39f + 41fcb30 commit 9c4cb4d

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)