import { afterEach, describe, expect, it } from 'vitest'; import { buildFakeRepos } from '../helpers/fake-repos.js'; import { makeBloc, makeFormation, makeModule } from '../helpers/fixtures.js'; import { READ_ALL_TOKEN, WRITE_ALL_TOKEN, buildTestApp, installTestContainer, resetTestContainer, } from '../helpers/test-app.js'; function bootApp() { const repos = buildFakeRepos({ formations: [makeFormation(10)], blocs: [makeBloc(100, 10)], modules: [makeModule(200, 100)], }); const container = installTestContainer({ repos }); return { app: buildTestApp(container) }; } describe('GET /api/v1/formations', () => { afterEach(resetTestContainer); it('401 sans token', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/formations'); expect(res.status).toBe(401); }); it('403 si scope manquant', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/formations', { headers: { Authorization: `Bearer ${WRITE_ALL_TOKEN}` }, }); expect(res.status).toBe(403); }); it('200 list', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/formations', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(200); const body = (await res.json()) as { data: { id: number }[] }; expect(body.data).toHaveLength(1); }); it('GET /:id avec blocs/modules + rollups', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/formations/10', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(200); const body = (await res.json()) as { data: { blocs: { id: number; modules: { id: number }[] }[]; heures_attribuees: string }; }; expect(body.data.blocs).toHaveLength(1); expect(body.data.blocs[0]?.modules).toHaveLength(1); expect(body.data.heures_attribuees).toBe('100.00'); }); it('404 si formation inconnue', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/formations/9999', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(404); }); });