|
| 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