Skip to content

Commit 97f348f

Browse files
authored
test(validate-user): add comprehensive unit tests for GitHub user validation and caching (JhaSourav07#3162)
## Description Adds a dedicated test suite for services/github/validate-user.ts to verify GitHub user validation, cache behavior, and error handling. The validator is responsible for preventing unnecessary GitHub API requests by caching validation results and distinguishing between permanent failures (user not found) and temporary API errors. These tests ensure that behavior remains reliable and regression-free. ## Changes Added services/github/validate-user.test.ts ## Test Coverage Implemented 5 unit tests covering: - Successful validation of an existing GitHub user. - Returning false when GitHub reports that a user does not exist. - Caching successful validation results to avoid repeated API calls. - Caching negative validation results for nonexistent users. - Re-throwing temporary API errors without caching them. Fixes JhaSourav07#2298 ## 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 af3f095 + 26cced6 commit 97f348f

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { gitHubUserValidator } from './validate-user';
3+
import { fetchUserProfile } from '../../lib/github';
4+
5+
vi.mock('../../lib/github', () => ({
6+
fetchUserProfile: vi.fn(),
7+
}));
8+
9+
describe('GitHubUserValidator', () => {
10+
beforeEach(() => {
11+
gitHubUserValidator.reset();
12+
vi.clearAllMocks();
13+
});
14+
15+
it('returns true for an existing GitHub user', async () => {
16+
vi.mocked(fetchUserProfile).mockResolvedValue({} as never);
17+
18+
const result = await gitHubUserValidator.validateUser('octocat');
19+
20+
expect(result).toBe(true);
21+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
22+
});
23+
24+
it('returns false when GitHub reports user not found', async () => {
25+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found'));
26+
27+
const result = await gitHubUserValidator.validateUser('missing-user');
28+
29+
expect(result).toBe(false);
30+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
31+
});
32+
33+
it('caches successful validation results', async () => {
34+
vi.mocked(fetchUserProfile).mockResolvedValue({} as never);
35+
36+
await gitHubUserValidator.validateUser('octocat');
37+
await gitHubUserValidator.validateUser('octocat');
38+
39+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
40+
});
41+
42+
it('caches negative validation results', async () => {
43+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found'));
44+
45+
await gitHubUserValidator.validateUser('ghost-user');
46+
await gitHubUserValidator.validateUser('ghost-user');
47+
48+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
49+
});
50+
51+
it('rethrows temporary GitHub API errors without caching', async () => {
52+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('GitHub API rate limit exceeded'));
53+
54+
await expect(gitHubUserValidator.validateUser('octocat')).rejects.toThrow(
55+
'GitHub API rate limit exceeded'
56+
);
57+
58+
await expect(gitHubUserValidator.validateUser('octocat')).rejects.toThrow(
59+
'GitHub API rate limit exceeded'
60+
);
61+
62+
expect(fetchUserProfile).toHaveBeenCalledTimes(2);
63+
});
64+
});

0 commit comments

Comments
 (0)