import { describe, expect, it, vi, beforeEach } from 'vitest'; import { load } from './+page.server'; vi.mock('$lib/api.server', () => ({ createApiClient: vi.fn() })); import { createApiClient } from '$lib/api.server'; const adminUser = { groups: [{ permissions: ['ADMIN'] }] }; const readOnlyUser = { groups: [{ permissions: ['READ_ALL'] }] }; function mockApiReturning(users: unknown[], groups: unknown[], tags: unknown[]) { vi.mocked(createApiClient).mockReturnValue({ GET: vi .fn() .mockResolvedValueOnce({ response: { ok: true }, data: users }) .mockResolvedValueOnce({ response: { ok: true }, data: groups }) .mockResolvedValueOnce({ response: { ok: true }, data: tags }) } as ReturnType); } beforeEach(() => vi.clearAllMocks()); // ─── permission check ───────────────────────────────────────────────────────── describe('admin load — permission check', () => { it('throws 403 when user has no ADMIN permission', async () => { await expect( load({ fetch: vi.fn() as unknown as typeof fetch, locals: { user: readOnlyUser } }) ).rejects.toMatchObject({ status: 403 }); }); it('throws 403 when user is undefined', async () => { await expect( load({ fetch: vi.fn() as unknown as typeof fetch, locals: { user: undefined } }) ).rejects.toMatchObject({ status: 403 }); }); it('throws 403 when user has no groups', async () => { await expect( load({ fetch: vi.fn() as unknown as typeof fetch, locals: { user: { groups: [] } } }) ).rejects.toMatchObject({ status: 403 }); }); }); // ─── happy path ─────────────────────────────────────────────────────────────── describe('admin load — happy path', () => { it('returns users, groups, and tags for an admin user', async () => { mockApiReturning( [{ id: 'u1', username: 'alice' }], [{ id: 'g1', name: 'Editors' }], [{ id: 't1', name: 'Familie' }] ); const result = await load({ fetch: vi.fn() as unknown as typeof fetch, locals: { user: adminUser } }); expect(result.users).toHaveLength(1); expect(result.groups).toHaveLength(1); expect(result.tags).toHaveLength(1); }); it('returns empty arrays when API returns no data', async () => { mockApiReturning([], [], []); const result = await load({ fetch: vi.fn() as unknown as typeof fetch, locals: { user: adminUser } }); expect(result.users).toEqual([]); expect(result.groups).toEqual([]); expect(result.tags).toEqual([]); }); });