import { Decimal } from 'decimal.js'; import { afterEach, describe, expect, it } from 'vitest'; import { Attribution } from '../../src/domain/attribution.js'; import { buildFakeRepos } from '../helpers/fake-repos.js'; import { makeAttribution } from '../helpers/fixtures.js'; import { WRITE_ALL_TOKEN, buildTestApp, installTestContainer, resetTestContainer, } from '../helpers/test-app.js'; function patchHeures( app: ReturnType, id: number, body: Record, ) { return app.request(`/api/v1/attributions/${id}/heures-realisees`, { method: 'PATCH', headers: { Authorization: `Bearer ${WRITE_ALL_TOKEN}`, 'Content-Type': 'application/json', }, body: JSON.stringify(body), }); } describe('PATCH /api/v1/attributions/:id/heures-realisees', () => { afterEach(resetTestContainer); it('happy path 200', async () => { const attrib = makeAttribution({ id: 500 }); const repos = buildFakeRepos({ attributions: [attrib] }); const app = buildTestApp(installTestContainer({ repos })); const res = await patchHeures(app, 500, { heures_realisees: 4.5, comment: 'sprint 1' }); expect(res.status).toBe(200); const body = (await res.json()) as { data: { heures_realisees: string } }; expect(body.data.heures_realisees).toBe('4.50'); expect(repos.attributions.lastUpdate?.id).toBe(500); expect(repos.attributions.lastUpdate?.heures.toNumber()).toBe(4.5); }); it('409 si attribution annulee', async () => { const annulee = new Attribution({ id: 500, moduleId: 200, personneId: 1, heuresAttribuees: new Decimal(10), statut: 'annule', }); const repos = buildFakeRepos({ attributions: [annulee] }); const app = buildTestApp(installTestContainer({ repos })); const res = await patchHeures(app, 500, { heures_realisees: 2 }); expect(res.status).toBe(409); const body = (await res.json()) as { error: { code: string } }; expect(body.error.code).toBe('CONFLICT'); }); it('404 si attribution inconnue', async () => { const repos = buildFakeRepos({}); const app = buildTestApp(installTestContainer({ repos })); const res = await patchHeures(app, 9999, { heures_realisees: 1 }); expect(res.status).toBe(404); }); it('400 si heures negatives', async () => { const repos = buildFakeRepos({ attributions: [makeAttribution({ id: 500 })] }); const app = buildTestApp(installTestContainer({ repos })); const res = await patchHeures(app, 500, { heures_realisees: -1 }); expect(res.status).toBe(400); }); });