Skip to content

Commit 89073a1

Browse files
authored
test: add empty input and fallback coverage for track user protection (JhaSourav07#2995)
## Description Fixes JhaSourav07#2963 Adds edge-case and fallback coverage for `TrackUserProtection`. ### Added tests - Empty string returns `INVALID_FORMAT` - Whitespace-only input returns `INVALID_FORMAT` - Username longer than GitHub's 39-character limit returns `INVALID_FORMAT` - Missing GitHub user returns `USER_NOT_FOUND` - Cooldown protection returns `COOLDOWN_ACTIVE` Created: `services/security/track-user-protection.empty-fallback.test.ts` ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview <img width="842" height="281" alt="image" src="https://github.com/user-attachments/assets/4574ceda-d980-4150-9964-fe083f129959" /> ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally. - [ ] I have run `npm run format` and `npm run lint` locally and resolved all errors. - [x] My commits follow the Conventional Commits format. - [x] I have updated `README.md` if I added a new theme or URL parameter. (Not applicable) - [x] I have starred the repo. - [x] I have made sure that I have only one commit to merge in this PR. - [x] The change is limited to test coverage and does not affect SVG output. - [x] (Recommended) I joined the CommitPulse Discord community.
2 parents 3e6b88e + 2b43de5 commit 89073a1

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { describe, it, expect, beforeEach, vi } from 'vitest';
2+
import { trackUserProtection } from './track-user-protection';
3+
import { gitHubUserValidator } from '../github/validate-user';
4+
5+
vi.mock('../github/validate-user', () => ({
6+
gitHubUserValidator: {
7+
validateUser: vi.fn(),
8+
},
9+
}));
10+
11+
describe('trackUserProtection empty/fallback cases', () => {
12+
beforeEach(() => {
13+
trackUserProtection.reset();
14+
vi.clearAllMocks();
15+
});
16+
17+
it('returns INVALID_FORMAT for empty string', async () => {
18+
const result = await trackUserProtection.verifyAndDeduplicate('');
19+
20+
expect(result).toEqual({
21+
allowed: false,
22+
reason: 'INVALID_FORMAT',
23+
});
24+
});
25+
26+
it('returns INVALID_FORMAT for whitespace only input', async () => {
27+
const result = await trackUserProtection.verifyAndDeduplicate(' ');
28+
29+
expect(result).toEqual({
30+
allowed: false,
31+
reason: 'INVALID_FORMAT',
32+
});
33+
});
34+
35+
it('returns INVALID_FORMAT for username longer than 39 characters', async () => {
36+
const result = await trackUserProtection.verifyAndDeduplicate('a'.repeat(40));
37+
38+
expect(result).toEqual({
39+
allowed: false,
40+
reason: 'INVALID_FORMAT',
41+
});
42+
});
43+
44+
it('returns USER_NOT_FOUND when validator reports missing user', async () => {
45+
vi.mocked(gitHubUserValidator.validateUser).mockResolvedValue(false);
46+
47+
const result = await trackUserProtection.verifyAndDeduplicate('octocat');
48+
49+
expect(result).toEqual({
50+
allowed: false,
51+
reason: 'USER_NOT_FOUND',
52+
});
53+
});
54+
55+
it('handles cooldown after write record', async () => {
56+
vi.mocked(gitHubUserValidator.validateUser).mockResolvedValue(true);
57+
58+
trackUserProtection.recordWrite('octocat');
59+
60+
const result = await trackUserProtection.verifyAndDeduplicate('octocat');
61+
62+
expect(result.allowed).toBe(false);
63+
expect(result.reason).toBe('COOLDOWN_ACTIVE');
64+
expect(result.remainingMs).toBeGreaterThan(0);
65+
});
66+
});

0 commit comments

Comments
 (0)