|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { GET } from './route'; |
| 3 | + |
| 4 | +vi.mock('@/lib/github', () => ({ |
| 5 | + fetchUserProfile: vi.fn(), |
| 6 | + fetchGitHubContributions: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +import { fetchUserProfile, fetchGitHubContributions } from '@/lib/github'; |
| 10 | +import type { ContributionCalendar } from '@/types'; |
| 11 | + |
| 12 | +const mockCalendar: ContributionCalendar = { |
| 13 | + totalContributions: 15, |
| 14 | + weeks: [ |
| 15 | + { |
| 16 | + contributionDays: [ |
| 17 | + { contributionCount: 5, date: '2024-06-10' }, |
| 18 | + { contributionCount: 5, date: '2024-06-11' }, |
| 19 | + { contributionCount: 5, date: '2024-06-12' }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + ], |
| 23 | +}; |
| 24 | + |
| 25 | +const mockProfile = { |
| 26 | + login: 'testuser', |
| 27 | + name: 'Test User', |
| 28 | + avatar_url: 'https://github.com/testuser.png', |
| 29 | + public_repos: 12, |
| 30 | +}; |
| 31 | + |
| 32 | +function makeRequest(params: Record<string, string> = {}): Request { |
| 33 | + const url = new URL('http://localhost/api/user-details'); |
| 34 | + for (const [key, value] of Object.entries(params)) { |
| 35 | + url.searchParams.set(key, value); |
| 36 | + } |
| 37 | + return new Request(url.toString()); |
| 38 | +} |
| 39 | + |
| 40 | +describe('GET /api/user-details', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 44 | + vi.mocked(fetchUserProfile).mockResolvedValue(mockProfile as any); |
| 45 | + vi.mocked(fetchGitHubContributions).mockResolvedValue({ |
| 46 | + calendar: mockCalendar, |
| 47 | + repoContributions: [], |
| 48 | + totalPRs: 0, |
| 49 | + totalIssues: 0, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns 400 when username is missing', async () => { |
| 54 | + const response = await GET(makeRequest()); |
| 55 | + expect(response.status).toBe(400); |
| 56 | + const body = await response.json(); |
| 57 | + expect(body.error).toBe('Username is required'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('returns 400 when username format is invalid', async () => { |
| 61 | + const response = await GET(makeRequest({ username: 'invalid_user_name_@' })); |
| 62 | + expect(response.status).toBe(400); |
| 63 | + const body = await response.json(); |
| 64 | + expect(body.error).toBe('Invalid username format'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns 200 with user details and streak stats on success', async () => { |
| 68 | + const response = await GET(makeRequest({ username: 'testuser' })); |
| 69 | + expect(response.status).toBe(200); |
| 70 | + const body = await response.json(); |
| 71 | + expect(body).toEqual({ |
| 72 | + exists: true, |
| 73 | + login: 'testuser', |
| 74 | + name: 'Test User', |
| 75 | + avatar_url: 'https://github.com/testuser.png', |
| 76 | + public_repos: 12, |
| 77 | + stats: { |
| 78 | + currentStreak: 3, |
| 79 | + longestStreak: 3, |
| 80 | + totalContributions: 15, |
| 81 | + }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns 404 when user is not found', async () => { |
| 86 | + vi.mocked(fetchUserProfile).mockRejectedValue(new Error('User not found')); |
| 87 | + const response = await GET(makeRequest({ username: 'missinguser' })); |
| 88 | + expect(response.status).toBe(404); |
| 89 | + const body = await response.json(); |
| 90 | + expect(body.error).toBe('User not found'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('gracefully handles contributions fetch failure and returns profile details', async () => { |
| 94 | + vi.mocked(fetchGitHubContributions).mockRejectedValue(new Error('API limit reached')); |
| 95 | + const response = await GET(makeRequest({ username: 'testuser' })); |
| 96 | + expect(response.status).toBe(200); |
| 97 | + const body = await response.json(); |
| 98 | + expect(body.stats).toEqual({ |
| 99 | + currentStreak: 0, |
| 100 | + longestStreak: 0, |
| 101 | + totalContributions: 0, |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments