|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { GET } from './route'; |
| 3 | +import { NextRequest } from 'next/server'; |
| 4 | +vi.mock('../../../lib/github', () => ({ |
| 5 | + fetchGitHubContributions: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock('../../../lib/calculate', () => ({ |
| 9 | + calculateStreak: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +import { fetchGitHubContributions } from '../../../lib/github'; |
| 13 | +import { calculateStreak } from '../../../lib/calculate'; |
| 14 | + |
| 15 | +describe('OG Route', () => { |
| 16 | + beforeEach(() => { |
| 17 | + vi.clearAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns 200 successfully', async () => { |
| 21 | + vi.mocked(fetchGitHubContributions).mockResolvedValue({} as never); |
| 22 | + |
| 23 | + vi.mocked(calculateStreak).mockReturnValue({ |
| 24 | + totalContributions: 120, |
| 25 | + longestStreak: 20, |
| 26 | + currentStreak: 5, |
| 27 | + todayDate: '2026-05-27', |
| 28 | + }); |
| 29 | + |
| 30 | + const req = new NextRequest('http://localhost:3000/api/og?user=testuser'); |
| 31 | + |
| 32 | + const res = await GET(req); |
| 33 | + |
| 34 | + expect(res).toBeDefined(); |
| 35 | + expect(res.status).toBe(200); |
| 36 | + }); |
| 37 | + |
| 38 | + it('falls back to zeros when github fetch fails', async () => { |
| 39 | + vi.mocked(fetchGitHubContributions).mockRejectedValue(new Error('GitHub API failed')); |
| 40 | + |
| 41 | + const req = new NextRequest('http://localhost:3000/api/og?user=testuser'); |
| 42 | + |
| 43 | + const res = await GET(req as never); |
| 44 | + |
| 45 | + expect(res.status).toBe(200); |
| 46 | + }); |
| 47 | + |
| 48 | + it('handles missing user query param', async () => { |
| 49 | + const req = new NextRequest('http://localhost:3000/api/og'); |
| 50 | + |
| 51 | + const res = await GET(req as never); |
| 52 | + |
| 53 | + expect(res.status).toBe(200); |
| 54 | + }); |
| 55 | +}); |
0 commit comments