Repository Public
1// Console d'administration — infrastructure : métriques de remise (graphiques SVG accessibles + tableaux équivalents),2// stockage par boîte, santé (API, base, serveur de courrier, travailleurs) et résumé de l'exposition réseau.3import crypto from "node:crypto"4import { api, axeBoth, expect, noOverflow, openAdmin, sendMail, st, test } from "./fixtures"56interface RawMetrics {7 days: number8 by_day: Record<string, string | number>[]9 by_address: Record<string, string | number>[]10 pending_approvals: number11}12const rowTotal = (r: Record<string, string | number>): number => Object.values(r).reduce<number>((n, v) => n + (typeof v === "number" ? v : 0), 0)1314test("livraison : graphiques accessibles, tableaux aux mêmes nombres, période, état vide", async ({ page }) => {15 // un envoi interne (boîte A → boîte B) pour que la période ne soit pas vide16 await sendMail(st.addrA, st.addrB, `web-infra ${st.run} ${crypto.randomBytes(2).toString("hex")}`)17 let raw: RawMetrics | null = null18 await expect19 .poll(20 async () => {21 const r = await api<RawMetrics>("GET", "/api/admin/metrics?days=14")22 raw = r.ok ? r.body : null23 return raw?.by_address.some((a) => a.address === st.addrA && rowTotal(a) > 0) ?? false24 },25 { timeout: 30_000 },26 )27 .toBe(true)2829 await openAdmin(page, "metrics")30 await expect(page.getByRole("heading", { level: 2, name: "Livraison" })).toBeVisible()31 // les compteurs ne changent pas pendant l'essai (les essais ne tournent pas en parallèle) : seul l'état d'un envoi évolue32 const fresh = (await api<RawMetrics>("GET", "/api/admin/metrics?days=14")).body33 const total = fresh.by_day.reduce((n, d) => n + rowTotal(d), 0)34 const fromA = rowTotal(fresh.by_address.find((a) => a.address === st.addrA) ?? {})35 expect(total).toBeGreaterThan(0)36 await expect(page.getByTestId("metrics-total")).toHaveText(String(total))37 await expect(page.getByTestId("metrics-approvals")).toHaveText(String(fresh.pending_approvals))3839 // ---- par jour : SVG nommé et décrit, somme des barres = total, légende à motifs, tableau équivalent40 const dayChart = page.getByRole("img", { name: /Envois par jour \(14 derniers jours\)/ })41 await expect(dayChart).toBeVisible()42 expect(await dayChart.evaluate((el) => el.tagName.toLowerCase())).toBe("svg")43 await expect(dayChart.locator("desc")).toContainText(/envois? sur la période/)44 const days = page.getByTestId("metrics-days")45 expect(await days.locator('[data-testid="chart-days"] g[data-day]').count()).toBe(14)46 const barSum = await days.locator('[data-testid="chart-days"] g[data-day]').evaluateAll((gs) => gs.reduce((n, g) => n + Number(g.getAttribute("data-total")), 0))47 expect(barSum).toBe(total)48 await expect(days.getByTestId("chart-legend").locator("li[data-series]")).toHaveCount(6)49 await expect(days.getByTestId("metrics-days-summary")).toContainText(String(total))50 await axeBoth(page, "infra-livraison-graphiques")51 await noOverflow(page)5253 await page.getByRole("group", { name: /Affichage de « Envois par jour/ }).getByRole("button", { name: "Tableau" }).click()54 const dayTable = page.getByTestId("table-days")55 await expect(dayTable).toBeVisible()56 await expect(dayChart).toHaveCount(0)57 await expect(dayTable.locator('[data-testid="table-days-total"] [data-total]')).toHaveText(String(total))58 const tableSum = await dayTable.locator("tbody tr [data-total]").evaluateAll((tds) => tds.reduce((n, td) => n + Number((td.textContent ?? "").replace(/\D/g, "")), 0))59 expect(tableSum).toBe(total)6061 // ---- par adresse : la boîte A y figure avec le même nombre dans le graphique et dans le tableau62 const addrChart = page.getByRole("img", { name: /Envois par adresse d'expédition/ })63 await expect(addrChart).toBeVisible()64 await expect(addrChart.locator(`g[data-address="${st.addrA}"]`)).toHaveAttribute("data-total", String(fromA))65 await page.getByRole("group", { name: /Affichage de « Envois par adresse/ }).getByRole("button", { name: "Tableau" }).click()66 const addrTable = page.getByTestId("table-addresses")67 await expect(addrTable.locator(`tr[data-address="${st.addrA}"] [data-total]`)).toHaveText(String(fromA))68 await expect(addrTable.locator('[data-testid="table-addresses-total"] [data-total]')).toHaveText(String(total))69 await axeBoth(page, "infra-livraison-tableaux")70 await noOverflow(page)7172 // ---- téléphone : tableaux en fiches, graphiques retracés à la largeur, pas de défilement horizontal73 await page.setViewportSize({ width: 390, height: 844 })74 await noOverflow(page)75 await page.getByRole("group", { name: /Affichage de « Envois par jour/ }).getByRole("button", { name: "Graphique" }).click()76 await expect(dayChart).toBeVisible()77 await noOverflow(page)78 await page.setViewportSize({ width: 1440, height: 900 })7980 // ---- période : 30 jours81 await page.getByRole("group", { name: "Période" }).getByRole("button", { name: "30 jours" }).click()82 await expect(page.getByRole("heading", { level: 3, name: /Envois par jour \(30 derniers jours\)/ })).toBeVisible()83 await expect(page.getByRole("group", { name: "Période" }).getByRole("button", { name: "30 jours" })).toHaveAttribute("aria-pressed", "true")8485 // ---- état vide : la base d'essai contient désormais un envoi, donc la réponse « aucun envoi » est simulée ici86 // (page.route sur la seule route des métriques ; aucune donnée du serveur n'est modifiée)87 await page.route("**/api/admin/metrics*", (route) => route.fulfill({ json: { days: 14, by_day: [], by_address: [], queue_depth: {}, webhook_backlog: {}, pending_approvals: 0 } }))88 await page.getByRole("button", { name: "Actualiser" }).click()89 await expect(page.getByTestId("metrics-empty")).toContainText("Aucun envoi par l'API depuis 14 jours")90 await expect(page.getByTestId("metrics-total")).toHaveText("0")91 await expect(page.getByRole("img", { name: /Envois par jour/ })).toHaveCount(0)92 await expect(page.getByTestId("metrics-queue-list")).toContainText("Aucun envoi en cours.")93 await expect(page.getByTestId("metrics-hooks-list")).toContainText("Aucune livraison de webhook")94 await axeBoth(page, "infra-livraison-vide")95 await page.unroute("**/api/admin/metrics*")96})9798test("stockage : les boîtes d'essai, utilisé et quota, totaux", async ({ page }) => {99 await openAdmin(page, "storage")100 await expect(page.getByRole("heading", { level: 2, name: "Stockage" })).toBeVisible()101 const table = page.getByTestId("storage-table")102 const rowA = table.locator(`tr[data-address="${st.addrA}"]`)103 await expect(rowA).toBeVisible()104 await expect(rowA).toContainText(st.addrA)105 await expect(table.locator(`tr[data-address="${st.addrB}"]`)).toBeVisible()106 expect(Number((await page.getByTestId("storage-count").innerText()).replace(/\D/g, ""))).toBeGreaterThanOrEqual(2)107 // occupation : part du quota, ou part du total quand la boîte n'a pas de quota — toujours dite en toutes lettres108 await expect(rowA.locator("td").last()).toContainText(/% du quota|% de l'espace utilisé au total|Inconnu/)109 await axeBoth(page, "infra-stockage")110 await noOverflow(page)111 await page.setViewportSize({ width: 390, height: 844 })112 await expect(rowA).toBeVisible()113 await noOverflow(page)114})115116test("santé : base, serveur de courrier, travailleurs, exposition documentée, 503 lu", async ({ page, watch }) => {117 await openAdmin(page, "health")118 await expect(page.getByRole("heading", { level: 2, name: "Santé" })).toBeVisible()119 await expect(page.getByTestId("health-banner")).toHaveAttribute("data-ok", "true")120 await expect(page.getByTestId("health-database")).toHaveAttribute("data-state", "ok")121 await expect(page.getByTestId("health-database")).toContainText("En service")122 await expect(page.getByTestId("health-mail")).toHaveAttribute("data-state", "reachable")123 await expect(page.getByTestId("health-mail")).toContainText("Joignable")124 await expect(page.getByTestId("health-latency")).toContainText(/\d+ ms/)125 await expect(page.getByTestId("health-workers")).toHaveAttribute("data-state", "on")126 await expect(page.getByTestId("health-workers")).toContainText("Actifs")127128 // exposition : faits documentés, source dite, pas de balayage de ports prétendu129 await expect(page.getByText("Selon la configuration de déploiement documentée")).toBeVisible()130 const exposure = page.getByTestId("health-exposure")131 await expect(exposure.locator('tr[data-service="smtp"]')).toContainText("Public")132 await expect(exposure.locator('tr[data-service="smtp"]')).toContainText("port 25")133 await expect(exposure.locator('tr[data-service="jmap"]')).toContainText("Privé")134 await expect(exposure.locator('tr[data-service="admin"]')).toContainText("Privé")135 await expect(exposure.locator('tr[data-service="legacy"]')).toContainText("Non exposé")136 await expect(exposure.locator('tr[data-service="legacy"]')).toContainText("IMAP, POP, soumission SMTP")137 await expect(page.getByText(/ne résulte pas d'un balayage de ports en direct/)).toBeVisible()138 await expect(page.getByTestId("health-live")).toContainText("stg.spboucher.ai")139140 // actualisation automatique : un bouton à bascule, jamais plus vite que 30 s141 const auto = page.getByTestId("health-auto")142 await expect(auto).toHaveAttribute("aria-pressed", "false")143 const before = watch.requests.filter((u) => u.endsWith("/api/v1/health")).length144 await auto.click()145 await expect(auto).toHaveAttribute("aria-pressed", "true")146 await page.waitForTimeout(3000)147 expect(watch.requests.filter((u) => u.endsWith("/api/v1/health")).length).toBe(before)148 await auto.click()149 await axeBoth(page, "infra-sante")150 await noOverflow(page)151 await page.setViewportSize({ width: 390, height: 844 })152 await noOverflow(page)153 await page.setViewportSize({ width: 1440, height: 900 })154155 // panne : la route répond 503 avec le même corps JSON — il est lu et affiché. Réponse simulée (page.route) : on ne156 // coupe pas le vrai serveur de courrier. Le navigateur signale le 503 dans la console : attendu ici.157 watch.allow.push(/status of 503/)158 await page.route("**/api/v1/health", (route) => route.fulfill({ status: 503, json: { ok: false, version: "essai", database: "ok", mail_server: "unreachable", workers: "on" } }))159 await page.getByRole("button", { name: "Actualiser" }).click()160 await expect(page.getByTestId("health-banner")).toHaveAttribute("data-ok", "false")161 await expect(page.getByTestId("health-banner")).toContainText("Service dégradé")162 await expect(page.getByTestId("health-mail")).toContainText("Injoignable")163 await expect(page.getByTestId("health-database")).toContainText("En service")164 await axeBoth(page, "infra-sante-panne")165 await page.unroute("**/api/v1/health")166})167