// Helper pour les tests E2E — appel direct des handlers Fresh // sans lancer de serveur HTTP import { FreshContext } from "$fresh/server.ts"; import { AuthenticatedState } from "$root/defaults/interfaces.ts"; import { CasContent } from "$root/defaults/interfaces.ts"; const BASE_EMPLOYEE_SESSION: CasContent = { amuCampus: "", amuComposante: "", amuDateValidation: "", coGroup: "", eduPersonPrimaryAffiliation: "employee", eduPersonPrincipalName: "test.user@polytech.fr", mail: "test.user@polytech.fr", displayName: "Test User", givenName: "Test", memberOf: [], sn: "User", supannCivilite: "M.", supannEntiteAffectation: "", supannEtuAnneeInscription: "", supannEtuEtape: "", uid: "test.user", }; /** * Crée un FreshContext mock authentifié en tant qu'employee. */ export function makeEmployeeContext( params: Record = {}, ): FreshContext { return { params, state: { isAuthenticated: true, session: { ...BASE_EMPLOYEE_SESSION }, availablePages: {}, }, render: () => Promise.resolve(new Response()), renderNotFound: () => Promise.resolve(new Response(null, { status: 404 })), next: () => Promise.resolve(new Response()), } as unknown as FreshContext; } /** * Crée un FreshContext mock avec un affiliation personnalisée. */ export function makeContextWithAffiliation( affiliation: string, params: Record = {}, ): FreshContext { const ctx = makeEmployeeContext(params); (ctx.state as AuthenticatedState).session.eduPersonPrimaryAffiliation = affiliation; return ctx; } /** * Crée une Request GET simple. */ export function makeGetRequest( path: string, searchParams?: Record, ): Request { const url = new URL(`http://localhost${path}`); if (searchParams) { for (const [k, v] of Object.entries(searchParams)) { url.searchParams.set(k, v); } } return new Request(url.toString()); } /** * Crée une Request POST/PUT avec un corps JSON. */ export function makeJsonRequest( path: string, method: string, body: unknown, ): Request { return new Request(`http://localhost${path}`, { method, headers: { "content-type": "application/json" }, body: JSON.stringify(body), }); }