Skip to content

Commit 96b6351

Browse files
authored
test(validate-user): add empty input fallback coverage (JhaSourav07#3048)
## Description Fixes JhaSourav07#2953 Added isolated unit tests for `services/github/validate-user.ts` focusing on edge cases, empty inputs, cache behavior, and fallback handling. ### Changes - Added `services/github/validate-user.empty-fallback.test.ts` - Verified behavior when empty usernames are provided - Tested whitespace-only username handling - Confirmed missing users are cached correctly to avoid repeated API calls - Verified username normalization (case-insensitive and trimmed input) uses cached results - Ensured unexpected API/service failures are rethrown instead of being silently swallowed - Improved coverage around empty inputs, missing users, and fallback execution paths ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (test-only changes) ## 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): ...`). - [ ] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have starred 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 SVG changes made). - [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents 8bc8e23 + cc3b71e commit 96b6351

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// services/github/validate-user.empty-fallback.test.ts
2+
3+
import { beforeEach, describe, expect, it, vi } from 'vitest';
4+
5+
vi.mock('../../lib/github', () => ({
6+
fetchUserProfile: vi.fn(),
7+
}));
8+
9+
import { fetchUserProfile } from '../../lib/github';
10+
import { gitHubUserValidator } from './validate-user';
11+
12+
describe('GitHubUserValidator Empty & Missing Input Fallbacks', () => {
13+
beforeEach(() => {
14+
vi.clearAllMocks();
15+
gitHubUserValidator.reset();
16+
});
17+
18+
it('throws for empty usernames', async () => {
19+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found'));
20+
21+
await expect(gitHubUserValidator.validateUser('')).rejects.toThrow('Cache key cannot be empty');
22+
});
23+
24+
it('throws for whitespace-only usernames', async () => {
25+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found'));
26+
27+
await expect(gitHubUserValidator.validateUser(' ')).rejects.toThrow(
28+
'Cache key cannot be empty'
29+
);
30+
});
31+
32+
it('caches negative results for missing users', async () => {
33+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found'));
34+
35+
const first = await gitHubUserValidator.validateUser('missing-user');
36+
const second = await gitHubUserValidator.validateUser('missing-user');
37+
38+
expect(first).toBe(false);
39+
expect(second).toBe(false);
40+
41+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
42+
});
43+
44+
it('returns cached result for usernames with different casing and spacing', async () => {
45+
vi.mocked(fetchUserProfile).mockResolvedValue({} as never);
46+
47+
const first = await gitHubUserValidator.validateUser('Ganesh');
48+
const second = await gitHubUserValidator.validateUser(' ganesh ');
49+
50+
expect(first).toBe(true);
51+
expect(second).toBe(true);
52+
53+
expect(fetchUserProfile).toHaveBeenCalledTimes(1);
54+
});
55+
56+
it('rethrows unexpected service errors instead of masking them', async () => {
57+
vi.mocked(fetchUserProfile).mockRejectedValue(new Error('GitHub API unavailable'));
58+
59+
await expect(gitHubUserValidator.validateUser('ganesh')).rejects.toThrow(
60+
'GitHub API unavailable'
61+
);
62+
});
63+
});

0 commit comments

Comments
 (0)