Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : SP Mail8 * File : tests/e2e/global-teardown.ts9 * Module : tests10 * Repository : https://git.spboucher.ai/sp-mail11 * Project Website : https://mail.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-2014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Nettoyage : les boîtes d'essai de ce parcours (celles de sa base jetable et celles de son registre) sont supprimées du serveur de courrier, puis le backend est arrêté.44import fs from "node:fs"45import { ORIGIN, OUT, STATE_FILE, readState } from "./env"46import { killPort } from "./global-setup"47import { cookieCall, purgeTracked, trackedMailboxes } from "./mailboxes"4849export default async function globalTeardown() {50 try {51 if (!fs.existsSync(STATE_FILE)) return52 const st = readState()53 const cookies = st.storage ? (JSON.parse(st.storage) as { cookies: { name: string; value: string }[] }).cookies : []54 const cookie = cookies.map((c) => `${c.name}=${c.value}`).join("; ")55 if (!cookie) return console.warn("[e2e] no session saved: test mailboxes (if any) were NOT purged")56 const h = { cookie, origin: ORIGIN, "x-spmail": "1" }57 const users = (await (await fetch(`${ORIGIN}/api/admin/users`, { headers: h })).json()) as { mailboxes: { id: string; address: string }[] }[]58 for (const u of users)59 for (const m of u.mailboxes) {60 if (!/^web-[a-z0-9-]+@stg\.spboucher\.ai$/.test(m.address)) continue61 const r = await fetch(`${ORIGIN}/api/admin/mailboxes/${m.id}?purge=delete-all-mail`, { method: "DELETE", headers: h })62 console.log(`[e2e] purge ${m.address} → ${r.status}`)63 }64 // boîtes inscrites par ce parcours que sa base ne connaît plus (détachées par un essai) : reprises puis purgées — celles-là seulement65 const tracked = trackedMailboxes(OUT)66 const failed = await purgeTracked(OUT, cookieCall(ORIGIN, cookie))67 // contrôle : plus aucun compte de CE parcours sur le serveur de courrier (les comptes d'autres parcours ne sont pas les nôtres)68 const ov = (await (await fetch(`${ORIGIN}/api/admin/overview`, { headers: h })).json()) as { accounts?: { address: string }[] }69 const left = (ov.accounts ?? []).map((a) => a.address ?? "").filter((a) => tracked.includes(a) || a.includes(`-${st.run}`))70 console.log(`[e2e] boîtes de ce parcours restées sur le serveur de courrier : ${left.length ? left.join(", ") : "aucune"}${failed.length ? ` (purge en échec, gardées au registre : ${failed.join(", ")})` : ""}`)71 } finally {72 if (!process.env.E2E_KEEP_SERVER) killPort()73 }74}75