import * as request from 'supertest'; import { INestApplication } from '@nestjs/common'; import { createApp } from './utils/testing-app'; import { PrismaService } from 'src/prisma/prisma.service'; import { makeShift, makeInvalidShift } from './factories/shift.factory'; const BASE = '/shifts'; describe('Shifts (e2e) — autonome', () => { let app: INestApplication; let prisma: PrismaService; let createdId: number | null = null; // FKs existants let tsId: number; // any timesheet let bcShiftId: number; // any bank code with categorie='SHIFT' beforeAll(async () => { app = await createApp(); prisma = app.get(PrismaService); // récupère un timesheet existant const ts = await prisma.timesheets.findFirst({ select: { id: true } }); if (!ts) throw new Error('No timesheet found — seed timesheets first.'); tsId = ts.id; // récupère un bank code SHIFT (ta seed utilise 'SHIFT' en MAJ) const bc = await prisma.bankCodes.findFirst({ where: { categorie: 'SHIFT' }, select: { id: true }, }); if (!bc) throw new Error('No SHIFT bank code found — seed bank codes first.'); bcShiftId = bc.id; }); afterAll(async () => { if (createdId) { try { await prisma.shifts.delete({ where: { id: createdId } }); } catch {} } await app.close(); await prisma.$disconnect(); }); it(`POST ${BASE} (valid) → 201 puis GET /:id → 200`, async () => { const payload = makeShift(tsId, bcShiftId); const createRes = await request(app.getHttpServer()) .post(BASE) .send(payload); if (createRes.status !== 201) { console.log('Create error:', createRes.body || createRes.text); } expect(createRes.status).toBe(201); expect(createRes.body).toEqual( expect.objectContaining({ id: expect.any(Number), timesheet_id: tsId, bank_code_id: bcShiftId, }) ); createdId = createRes.body.id; const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`); expect(getRes.status).toBe(200); expect(getRes.body).toEqual(expect.objectContaining({ id: createdId })); }); it(`PATCH ${BASE}/:id → 200 (description mise à jour)`, async () => { if (!createdId) { const created = await request(app.getHttpServer()) .post(BASE) .send(makeShift(tsId, bcShiftId)); expect(created.status).toBe(201); createdId = created.body.id; } const patchRes = await request(app.getHttpServer()) .patch(`${BASE}/${createdId}`) .send({ description: 'Updated shift description' }); expect([200, 204]).toContain(patchRes.status); const getRes = await request(app.getHttpServer()).get(`${BASE}/${createdId}`); expect(getRes.status).toBe(200); // on tolère l’absence, mais si elle est là, on vérifie la valeur. if (getRes.body?.description !== undefined) { expect(getRes.body.description).toBe('Updated shift description'); } }); it(`GET ${BASE}/999999 (not found) → 404/400`, async () => { const res = await request(app.getHttpServer()).get(`${BASE}/999999`); expect([404, 400]).toContain(res.status); }); it(`POST ${BASE} (invalid payload) → 400`, async () => { const res = await request(app.getHttpServer()) .post(BASE) .send(makeInvalidShift()); expect(res.status).toBeGreaterThanOrEqual(400); expect(res.status).toBeLessThan(500); }); it(`DELETE ${BASE}/:id → 200/204`, async () => { let id = createdId; if (!id) { const created = await request(app.getHttpServer()) .post(BASE) .send(makeShift(tsId, bcShiftId)); expect(created.status).toBe(201); id = created.body.id; } const del = await request(app.getHttpServer()).delete(`${BASE}/${id}`); expect([200, 204]).toContain(del.status); if (createdId === id) createdId = null; }); });