// E2E tests for /notes endpoints — handler + real DB import { assertEquals, assertExists } from "@std/assert"; import { makeEmployeeContext, makeGetRequest, makeJsonRequest, } from "../helpers/handler.ts"; import { seedModules, seedNotes, seedPromotions, seedStudents, truncateAll, } from "../helpers/db_integration.ts"; import { handler as notesHandler } from "$apps/notes/api/notes.ts"; import { handler as noteHandler } from "$apps/notes/api/notes/[numEtud]/[idModule].ts"; // --- GET /notes --- Deno.test({ name: "e2e notes: GET /notes returns all notes", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Dupont", prenom: "Jean", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }, { id: "M2", nom: "Mod B" }]); await seedNotes([ { numEtud: s.numEtud, idModule: "M1", note: 15.0 }, { numEtud: s.numEtud, idModule: "M2", note: 12.0 }, ]); const res = await notesHandler.GET!(makeGetRequest("/notes"), makeEmployeeContext()); assertEquals(res.status, 200); const body = await res.json(); assertEquals(body.length, 2); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: GET /notes?numEtud filters by student", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s1] = await seedStudents([{ nom: "Dupont", prenom: "Jean", idPromo: "P1" }]); const [s2] = await seedStudents([{ nom: "Martin", prenom: "Alice", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }]); await seedNotes([ { numEtud: s1.numEtud, idModule: "M1", note: 15.0 }, { numEtud: s2.numEtud, idModule: "M1", note: 12.0 }, ]); const res = await notesHandler.GET!( makeGetRequest("/notes", { numEtud: String(s1.numEtud) }), makeEmployeeContext(), ); assertEquals(res.status, 200); const body = await res.json(); assertEquals(body.length, 1); assertEquals(body[0].numEtud, s1.numEtud); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: GET /notes?numEtud=NaN returns 400", async fn() { await truncateAll(); const res = await notesHandler.GET!( makeGetRequest("/notes", { numEtud: "abc" }), makeEmployeeContext(), ); assertEquals(res.status, 400); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: GET /notes?idModule filters by module", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Dupont", prenom: "Jean", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }, { id: "M2", nom: "Mod B" }]); await seedNotes([ { numEtud: s.numEtud, idModule: "M1", note: 15.0 }, { numEtud: s.numEtud, idModule: "M2", note: 10.0 }, ]); const res = await notesHandler.GET!( makeGetRequest("/notes", { idModule: "M1" }), makeEmployeeContext(), ); assertEquals(res.status, 200); const body = await res.json(); assertEquals(body.length, 1); assertEquals(body[0].idModule, "M1"); }, sanitizeResources: false, sanitizeOps: false, }); // --- POST /notes --- Deno.test({ name: "e2e notes: POST /notes creates note (201)", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Leroy", prenom: "Paul", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }]); const res = await notesHandler.POST!( makeJsonRequest("/notes", "POST", { numEtud: s.numEtud, idModule: "M1", note: 14.0 }), makeEmployeeContext(), ); assertEquals(res.status, 201); const body = await res.json(); assertExists(body.numEtud); assertEquals(body.note, 14.0); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: POST /notes 400 on missing fields", async fn() { await truncateAll(); const res = await notesHandler.POST!( makeJsonRequest("/notes", "POST", { numEtud: 12345 }), makeEmployeeContext(), ); assertEquals(res.status, 400); }, sanitizeResources: false, sanitizeOps: false, }); // --- GET /notes/:numEtud/:idModule --- Deno.test({ name: "e2e notes: GET /notes/:numEtud/:idModule returns note", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Bernard", prenom: "Lucie", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "M1", note: 18.0 }]); const res = await noteHandler.GET!( makeGetRequest(`/notes/${s.numEtud}/M1`), makeEmployeeContext({ numEtud: String(s.numEtud), idModule: "M1" }), ); assertEquals(res.status, 200); const body = await res.json(); assertEquals(body.note, 18.0); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: GET /notes/:numEtud/:idModule 404 when not found", async fn() { await truncateAll(); const res = await noteHandler.GET!( makeGetRequest("/notes/99999/GHOST"), makeEmployeeContext({ numEtud: "99999", idModule: "GHOST" }), ); assertEquals(res.status, 404); }, sanitizeResources: false, sanitizeOps: false, }); // --- PUT /notes/:numEtud/:idModule --- Deno.test({ name: "e2e notes: PUT /notes/:numEtud/:idModule updates note", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Thomas", prenom: "Eva", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "M1", note: 10.0 }]); const res = await noteHandler.PUT!( makeJsonRequest(`/notes/${s.numEtud}/M1`, "PUT", { note: 16.0 }), makeEmployeeContext({ numEtud: String(s.numEtud), idModule: "M1" }), ); assertEquals(res.status, 200); const body = await res.json(); assertEquals(body.note, 16.0); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: PUT /notes/:numEtud/:idModule 404 when not found", async fn() { await truncateAll(); const res = await noteHandler.PUT!( makeJsonRequest("/notes/99999/GHOST", "PUT", { note: 10.0 }), makeEmployeeContext({ numEtud: "99999", idModule: "GHOST" }), ); assertEquals(res.status, 404); }, sanitizeResources: false, sanitizeOps: false, }); // --- DELETE /notes/:numEtud/:idModule --- Deno.test({ name: "e2e notes: DELETE /notes/:numEtud/:idModule returns 204", async fn() { await truncateAll(); await seedPromotions([{ id: "P1" }]); const [s] = await seedStudents([{ nom: "Petit", prenom: "Hugo", idPromo: "P1" }]); await seedModules([{ id: "M1", nom: "Mod A" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "M1", note: 9.0 }]); const res = await noteHandler.DELETE!( makeGetRequest(`/notes/${s.numEtud}/M1`), makeEmployeeContext({ numEtud: String(s.numEtud), idModule: "M1" }), ); assertEquals(res.status, 204); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "e2e notes: DELETE /notes/:numEtud/:idModule 404 when not found", async fn() { await truncateAll(); const res = await noteHandler.DELETE!( makeGetRequest("/notes/99999/GHOST"), makeEmployeeContext({ numEtud: "99999", idModule: "GHOST" }), ); assertEquals(res.status, 404); }, sanitizeResources: false, sanitizeOps: false, });