import { afterEach, describe, expect, it } from 'vitest'; import { buildFakeRepos } from '../helpers/fake-repos.js'; import { makePersonne, makeTache } from '../helpers/fixtures.js'; import { WRITE_ALL_TOKEN, buildTestApp, installTestContainer, resetTestContainer, } from '../helpers/test-app.js'; function postIntervention(app: ReturnType, body: Record) { return app.request('/api/v1/interventions', { method: 'POST', headers: { Authorization: `Bearer ${WRITE_ALL_TOKEN}`, 'Content-Type': 'application/json', }, body: JSON.stringify(body), }); } describe('POST /api/v1/interventions', () => { afterEach(resetTestContainer); it('happy path 201', async () => { const repos = buildFakeRepos({ personnes: [makePersonne({ id: 2, roles: ['developpeur'] })], taches: [makeTache(400, 300)], }); const app = buildTestApp(installTestContainer({ repos })); const res = await postIntervention(app, { tache_id: 400, personne_id: 2, heures: 3, date: '2026-05-07T10:00:00Z', notes: 'cours JS', }); expect(res.status).toBe(201); const body = (await res.json()) as { data: { intervention_id: number; heures: string } }; expect(body.data.heures).toBe('3.00'); expect(repos.interventions.lastCreated?.heures.toNumber()).toBe(3); }); it('422 (validation) si role pas developpeur', async () => { const repos = buildFakeRepos({ personnes: [makePersonne({ id: 1, roles: ['formateur'] })], taches: [makeTache(400, 300)], }); const app = buildTestApp(installTestContainer({ repos })); const res = await postIntervention(app, { tache_id: 400, personne_id: 1, heures: 3, date: '2026-05-07T10:00:00Z', }); expect(res.status).toBe(400); const body = (await res.json()) as { error: { code: string } }; expect(body.error.code).toBe('VALIDATION_ERROR'); }); it('400 si heures <= 0 (Zod)', async () => { const repos = buildFakeRepos({}); const app = buildTestApp(installTestContainer({ repos })); const res = await postIntervention(app, { tache_id: 400, personne_id: 2, heures: 0, date: '2026-05-07T10:00:00Z', }); expect(res.status).toBe(400); }); it('404 si tache inconnue', async () => { const repos = buildFakeRepos({ personnes: [makePersonne({ id: 2, roles: ['developpeur'] })], }); const app = buildTestApp(installTestContainer({ repos })); const res = await postIntervention(app, { tache_id: 999, personne_id: 2, heures: 3, date: '2026-05-07T10:00:00Z', }); expect(res.status).toBe(404); }); });