import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { buildFakeRepos } from '../helpers/fake-repos.js'; import { makeAttribution, makeIntervention, makePersonne } from '../helpers/fixtures.js'; import { resetTestContainer } from '../helpers/test-app.js'; import { READ_ALL_TOKEN, buildTestApp, installTestContainer } from '../helpers/test-app.js'; function bootApp() { const repos = buildFakeRepos({ personnes: [makePersonne({ id: 1 })], attributions: [ makeAttribution({ id: 500, moduleId: 200, personneId: 1 }), makeAttribution({ id: 501, moduleId: 200, personneId: 99 }), ], interventions: [makeIntervention({ id: 600, personneId: 1 })], }); const container = installTestContainer({ repos }); return { app: buildTestApp(container), repos }; } describe('GET /api/v1/personnes', () => { beforeEach(() => {}); afterEach(resetTestContainer); it('401 sans token', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes'); expect(res.status).toBe(401); }); it('200 list paginee', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(200); const body = (await res.json()) as { data: unknown[]; meta: { total: number } }; expect(body.data).toHaveLength(1); expect(body.meta.total).toBe(1); }); it('GET /:id 200 avec heures restantes', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes/1', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(200); const body = (await res.json()) as { data: { id: number; heures_restantes_formation: string; heures_restantes_total: string; }; }; expect(body.data.id).toBe(1); expect(body.data.heures_restantes_formation).toBe('600.00'); expect(body.data.heures_restantes_total).toBe('1000.00'); }); it('GET /:id 404 si inconnu', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes/9999', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(404); const body = (await res.json()) as { error: { code: string } }; expect(body.error.code).toBe('NOT_FOUND'); }); it('GET /:id 400 si id non numerique', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes/abc', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(400); }); it('GET /:id/dashboard agrege attributions + interventions', async () => { const { app } = bootApp(); const res = await app.request('/api/v1/personnes/1/dashboard', { headers: { Authorization: `Bearer ${READ_ALL_TOKEN}` }, }); expect(res.status).toBe(200); const body = (await res.json()) as { data: { attributions: { total: number; actives: number }; interventions: { total: number; heures_total: string }; }; }; expect(body.data.attributions.total).toBe(1); expect(body.data.attributions.actives).toBe(1); expect(body.data.interventions.total).toBe(1); expect(body.data.interventions.heures_total).toBe('2.00'); }); });