import { describe, expect, it, vi, beforeEach } from 'vitest'; vi.mock('$lib/api.server', () => ({ createApiClient: vi.fn() })); import { load } from './+page.server'; import { createApiClient } from '$lib/api.server'; beforeEach(() => vi.clearAllMocks()); function makeUrl(params: Record = {}) { const url = new URL('http://localhost/'); for (const [key, value] of Object.entries(params)) { if (Array.isArray(value)) { value.forEach((v) => url.searchParams.append(key, v)); } else { url.searchParams.set(key, value); } } return url; } // ─── happy path ─────────────────────────────────────────────────────────────── describe('home page load — happy path', () => { it('returns documents and persons on success', async () => { vi.mocked(createApiClient).mockReturnValue({ GET: vi .fn() .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [{ id: 'd1', title: 'Brief' }] }) .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [{ id: 'p1', firstName: 'Hans', lastName: 'Müller' }] }) .mockResolvedValueOnce({ response: { ok: true }, data: { count: 3 } }) } as ReturnType); const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch }); expect(result.documents).toHaveLength(1); expect(result.incompleteCount).toBe(3); expect(result.error).toBeNull(); }); it('passes search params from the URL to the API', async () => { const mockGet = vi .fn() .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } }); vi.mocked(createApiClient).mockReturnValue({ GET: mockGet } as ReturnType); await load({ url: makeUrl({ q: 'Urlaub', from: '2020-01-01' }), fetch: vi.fn() as unknown as typeof fetch }); const firstCall = mockGet.mock.calls[0]; expect(firstCall[1].params.query.q).toBe('Urlaub'); expect(firstCall[1].params.query.from).toBe('2020-01-01'); }); it('returns incompleteCount 0 when the incomplete-count API fails', async () => { vi.mocked(createApiClient).mockReturnValue({ GET: vi .fn() .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: false }, data: null }) } as ReturnType); const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch }); expect(result.incompleteCount).toBe(0); }); }); // ─── 401 redirect ───────────────────────────────────────────────────────────── describe('home page load — auth redirect', () => { it('redirects to /login when documents API returns 401', async () => { vi.mocked(createApiClient).mockReturnValue({ GET: vi .fn() .mockResolvedValueOnce({ response: { ok: false, status: 401 }, data: null }) .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } }) } as ReturnType); await expect( load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch }) ).rejects.toMatchObject({ location: '/login' }); }); it('redirects to /login when persons API returns 401', async () => { vi.mocked(createApiClient).mockReturnValue({ GET: vi .fn() .mockResolvedValueOnce({ response: { ok: true, status: 200 }, data: [] }) .mockResolvedValueOnce({ response: { ok: false, status: 401 }, data: null }) .mockResolvedValueOnce({ response: { ok: true }, data: { count: 0 } }) } as ReturnType); await expect( load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch }) ).rejects.toMatchObject({ location: '/login' }); }); }); // ─── network error fallback ─────────────────────────────────────────────────── describe('home page load — network error fallback', () => { it('returns error string instead of throwing when API call throws', async () => { vi.mocked(createApiClient).mockReturnValue({ GET: vi.fn().mockRejectedValue(new Error('Network failure')) } as ReturnType); const result = await load({ url: makeUrl(), fetch: vi.fn() as unknown as typeof fetch }); expect(result.error).toBe('Daten konnten nicht geladen werden.'); expect(result.documents).toEqual([]); expect(result.incompleteCount).toBe(0); }); });