|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { GET } from './route'; |
| 3 | + |
| 4 | +vi.mock('@/lib/github', () => ({ |
| 5 | + getFullDashboardData: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +import { getFullDashboardData } from '@/lib/github'; |
| 9 | + |
| 10 | +function makeRequest(params: Record<string, string> = {}): Request { |
| 11 | + const url = new URL('http://localhost/api/github'); |
| 12 | + |
| 13 | + for (const [key, value] of Object.entries(params)) { |
| 14 | + url.searchParams.set(key, value); |
| 15 | + } |
| 16 | + |
| 17 | + return new Request(url.toString()); |
| 18 | +} |
| 19 | + |
| 20 | +describe('GET /api/github', () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + vi.mocked(getFullDashboardData).mockResolvedValue({ |
| 24 | + profile: { username: 'octocat' }, |
| 25 | + repositories: [], |
| 26 | + languages: [], |
| 27 | + insights: [], |
| 28 | + commitClock: [], |
| 29 | + } as never); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns a standards-compliant Cache-Control header', async () => { |
| 33 | + const response = await GET(makeRequest({ username: 'octocat' })); |
| 34 | + |
| 35 | + expect(response.status).toBe(200); |
| 36 | + expect(response.headers.get('Cache-Control')).toBe( |
| 37 | + 's-maxage=3600, stale-while-revalidate=86400' |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('bypasses cache with refresh=true', async () => { |
| 42 | + const response = await GET(makeRequest({ username: 'octocat', refresh: 'true' })); |
| 43 | + |
| 44 | + expect(response.headers.get('Cache-Control')).toBe('no-cache, no-store, must-revalidate'); |
| 45 | + expect(getFullDashboardData).toHaveBeenCalledWith('octocat', { bypassCache: true }); |
| 46 | + }); |
| 47 | +}); |
0 commit comments