SPB Git

Repository Public

harness.ts 17.3 KB 301 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : SP Mail8 * File             : tests/e2e/next-prod/harness.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/sp-mail11 * Project Website  : https://mail.spboucher.ai12 * Created          : 2026-11-2013 * 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// Outillage commun des essais « SP Mail Next — productivité » : session (enrôlement ou clé importée), boîtes d'essai, dépôt de44// messages sans envoi (JMAP Email/set), appels d'API depuis la page, axe, captures, faux fournisseur d'IA. Chaque zone45// (libellés, recherche, contacts…) vit dans son fichier `*.part.ts` et reçoit ce harnais.46import AxeBuilder from "@axe-core/playwright"47import { expect, test, type Browser, type BrowserContext, type Page } from "@playwright/test"48import fs from "node:fs"49import { OUT, PORT, readState, writeState } from "../env"50import { addAuthenticator, exportCredential, importCredential, purgeLeftoverMailboxes, signIn, track } from "../helpers"5152export const SHOTS = "/tmp/spmail-next-shots/web-prod"53export const THEMES = [["light", "Clair"], ["dark", "Sombre"], ["midnight", "Minuit"]] as const54export const AI_STUB = `http://127.0.0.1:${PORT + 1000}`5556export interface Seed {57  subject: string58  from: [string, string]59  /** destinataire « À » ; par défaut l'adresse de la boîte */60  to?: [string, string][]61  preview: string62  keywords?: string[]63  html?: string64  folder?: "inbox" | "trash" | "sent" | "archive"65  messageId?: string66  references?: string67  /** en-têtes bruts supplémentaires (« List-Id », « List-Unsubscribe »…) */68  headers?: Record<string, string>69  /** pièces jointes déposées avec le message (contenu texte ou base64) */70  attachments?: { name: string; type: string; text?: string; base64?: string }[]71  minutesAgo: number72}7374export interface Harness {75  /** suffixe unique du parcours (dans chaque adresse et chaque objet) */76  run: string77  /** deux boîtes du propriétaire, créées avant les essais : web-p1-<run>@…, web-p2-<run>@… */78  P1: string79  P2: string80  page: Page81  context: BrowserContext82  browser: Browser83  storage: () => Awaited<ReturnType<BrowserContext["storageState"]>>84  /** inscrit des boîtes au registre du parcours AVANT de les créer (purge exacte au démontage, ou au lancement suivant) */85  track: (...addresses: string[]) => void86  /** nettoyage d'une zone, exécuté avant la fermeture de la session du parcours */87  onTeardown: (fn: () => Promise<void>) => void88  consoleErrors: string[]89  api: <T = any>(p: Page, method: string, url: string, body?: unknown) => Promise<T>90  seed: (p: Page, address: string, seeds: Seed[]) => Promise<string[]>91  mailboxId: (p: Page, address: string) => Promise<string>92  /** appel JMAP brut dans une boîte (comme un autre appareil) */93  jmap: (p: Page, address: string, methodCalls: unknown[]) => Promise<any[]>94  openBox: (p: Page, address: string) => Promise<void>95  palette: (p: Page, query: string, expected: string | RegExp) => Promise<void>96  setTheme: (p: Page, label: string, theme: string) => Promise<void>97  /** nouvelle page sur la même session (téléphone : `touch`) ; à fermer par `ctx.close()` */98  fromStorage: (viewport: { width: number; height: number }, touch?: boolean) => Promise<{ ctx: BrowserContext; p: Page }>99  axe: (p: Page, name: string) => Promise<void>100  /** axe dans les trois thèmes, sur l'écran affiché */101  axeThemes: (p: Page, name: string) => Promise<void>102  shot: (p: Page, name: string) => Promise<void>103  /** captures clair + minuit (ordinateur) de l'écran affiché : `<name>-light.png`, `<name>-midnight.png` */104  shotThemes: (p: Page, name: string) => Promise<void>105  /** faux fournisseur d'IA : mode des prochaines réponses, requêtes reçues */106  ai: { mode: (mode: "normal" | "hostile" | "down") => Promise<void>; seen: () => Promise<{ path: string; body: any }[]>; reset: () => Promise<void> }107}108export type Part = (h: () => Harness) => void109110async function api<T>(p: Page, method: string, url: string, body?: unknown): Promise<T> {111  return p.evaluate(112    async ({ method, url, body }) => {113      const r = await fetch(url, { method, headers: { "x-spmail": "1", ...(body === undefined ? {} : { "content-type": "application/json" }) }, body: body === undefined ? undefined : JSON.stringify(body) })114      if (!r.ok) throw new Error(`${method} ${url} → ${r.status} ${await r.text()}`)115      return r.status === 204 ? null : r.json()116    },117    { method, url, body },118  ) as Promise<T>119}120async function mailboxId(p: Page, address: string): Promise<string> {121  const id = await p.evaluate(async (address) => ((await (await fetch("/api/me")).json()).mailboxes as { id: string; address: string }[]).find((m) => m.address === address)?.id ?? null, address)122  if (!id) throw new Error(`no mailbox ${address}`)123  return id124}125async function jmap(p: Page, address: string, methodCalls: unknown[]): Promise<any[]> {126  return p.evaluate(127    async ({ address, methodCalls }) => {128      const me = await (await fetch("/api/me")).json()129      const mb = me.mailboxes.find((m: { address: string }) => m.address === address).id130      const session = await (await fetch(`/api/jmap/${mb}/session`)).json()131      const accountId = session.primaryAccounts["urn:ietf:params:jmap:mail"]132      const calls = (methodCalls as [string, Record<string, unknown>, string][]).map(([name, args, tag]) => [name, { accountId, ...args }, tag])133      const r = await fetch(`/api/jmap/${mb}/api`, { method: "POST", headers: { "content-type": "application/json", "x-spmail": "1" }, body: JSON.stringify({ using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], methodCalls: calls }) })134      if (!r.ok) throw new Error(`jmap → ${r.status}`)135      return (await r.json()).methodResponses136    },137    { address, methodCalls },138  )139}140/** Dépose des messages directement dans une boîte (aucun envoi SMTP) ; rend leurs identifiants JMAP, dans l'ordre. */141async function seed(p: Page, address: string, seeds: Seed[]): Promise<string[]> {142  return p.evaluate(143    async ({ address, seeds }) => {144      const me = await (await fetch("/api/me")).json()145      const mb = me.mailboxes.find((m: { address: string }) => m.address === address).id146      const session = await (await fetch(`/api/jmap/${mb}/session`)).json()147      const accountId = session.primaryAccounts["urn:ietf:params:jmap:mail"]148      const call = async (methodCalls: unknown[]) => (await (await fetch(`/api/jmap/${mb}/api`, { method: "POST", headers: { "content-type": "application/json", "x-spmail": "1" }, body: JSON.stringify({ using: ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], methodCalls }) })).json()).methodResponses149      const boxes = (await call([["Mailbox/get", { accountId, properties: ["role"] }, "m"]]))[0][1].list as { id: string; role: string }[]150      const upload = async (a: { type: string; text?: string; base64?: string }) => {151        const bytes = a.base64 !== undefined ? Uint8Array.from(atob(a.base64), (c) => c.charCodeAt(0)) : new TextEncoder().encode(a.text ?? "")152        const r = await fetch(`/api/jmap/${mb}/upload/${accountId}/`, { method: "POST", headers: { "content-type": a.type, "x-spmail": "1" }, body: bytes })153        if (!r.ok) throw new Error(`upload → ${r.status}`)154        return (await r.json()) as { blobId: string; size: number }155      }156      const create: Record<string, unknown> = {}157      for (const [i, s] of seeds.entries()) {158        const folder = boxes.find((b) => b.role === (s.folder ?? "inbox"))!.id159        const atts = []160        for (const a of s.attachments ?? []) {161          const up = await upload(a)162          atts.push({ blobId: up.blobId, type: a.type, name: a.name, disposition: "attachment", size: up.size })163        }164        create[`e${i}`] = {165          mailboxIds: { [folder]: true }, keywords: Object.fromEntries((s.keywords ?? []).map((k) => [k, true])), from: [{ name: s.from[0], email: s.from[1] }],166          to: s.to ? s.to.map(([name, email]) => ({ name, email })) : [{ email: address }], subject: s.subject,167          receivedAt: new Date(Date.now() - s.minutesAgo * 60_000).toISOString().replace(/\.\d+Z$/, "Z"),168          ...(s.messageId ? { messageId: [s.messageId] } : {}), ...(s.references ? { references: [s.references], inReplyTo: [s.references] } : {}),169          ...Object.fromEntries(Object.entries(s.headers ?? {}).map(([k, v]) => [`header:${k}`, ` ${v}`])),170          ...(s.html ? { bodyValues: { h: { value: s.html } }, htmlBody: [{ partId: "h", type: "text/html" }] } : { bodyValues: { t: { value: s.preview } }, textBody: [{ partId: "t", type: "text/plain" }] }),171          ...(atts.length ? { attachments: atts } : {}),172        }173      }174      const r = (await call([["Email/set", { accountId, create }, "c"]]))[0][1]175      if (r.notCreated && Object.keys(r.notCreated).length) throw new Error(JSON.stringify(r.notCreated))176      return seeds.map((_, i) => r.created[`e${i}`].id as string)177    },178    { address, seeds },179  )180}181async function openBox(p: Page, address: string) {182  await p.getByRole("link", { name: address }).or(p.getByRole("heading", { level: 2, name: address })).first().waitFor()183  if (await p.getByRole("link", { name: address }).count()) await p.getByRole("link", { name: address }).click()184  await expect(p.getByRole("heading", { level: 2, name: address })).toBeVisible()185  await expect(p.getByRole("heading", { level: 1, name: "Boîte de réception" })).toBeVisible()186}187async function palette(p: Page, query: string, expected: string | RegExp) {188  await p.keyboard.press("Control+k")189  const dlg = p.getByRole("dialog", { name: "Palette de commandes" })190  await expect(dlg).toBeVisible()191  await dlg.getByRole("combobox").fill(query)192  await expect(dlg.getByRole("option").first()).toHaveText(expected)193  await p.keyboard.press("Enter")194  await expect(dlg).toHaveCount(0)195}196async function setTheme(p: Page, label: string, theme: string) {197  await palette(p, `>theme ${label}`, `Thème : ${label}`)198  await expect(p.locator("html")).toHaveAttribute("data-theme", theme)199}200async function axe(p: Page, name: string) {201  const r = await new AxeBuilder({ page: p }).withTags(["wcag2a", "wcag2aa", "wcag21a", "wcag21aa"]).exclude(".html-frame iframe").exclude(".html-measure").exclude("iframe.nx-preview-frame").analyze()202  const bad = r.violations.filter((v) => v.impact === "serious" || v.impact === "critical")203  fs.appendFileSync(`${OUT}/axe.log`, `prod-${name}: ${r.violations.length} violation(s), ${bad.length} serious/critical, ${r.passes.length} passes\n${bad.map((v) => `  ${v.id}: ${v.nodes.map((n) => n.target.join(" ")).join(" | ")}`).join("\n")}\n`)204  expect(bad.map((v) => `${v.id}: ${v.nodes.map((n) => n.target.join(" ")).slice(0, 4).join(" | ")}`), `axe ${name}`).toEqual([])205}206const shot = async (p: Page, name: string) => void (await p.screenshot({ path: `${SHOTS}/${name}.png` }))207208/**209 * Déclare le parcours : une session et deux boîtes pour toutes les zones données, exécutées en série dans l'ordre.210 * Lancé seul (base vide), il fait l'enrôlement ; après le parcours « desktop », il reprend la clé d'accès exportée.211 */212export function runParts(parts: Part[]) {213  test.describe.configure({ mode: "serial" })214  const st = readState()215  let h: Harness216  const cleanups: (() => Promise<void>)[] = []217  test.beforeAll(async ({ browser }) => {218    test.setTimeout(180_000)219    fs.mkdirSync(SHOTS, { recursive: true })220    const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, locale: "fr-CA", timezoneId: "America/Toronto" })221    const page = await context.newPage()222    const consoleErrors: string[] = []223    page.on("console", (m) => {224      if (m.type() === "error") consoleErrors.push(m.text())225    })226    page.on("pageerror", (e) => consoleErrors.push(`pageerror: ${e.message}`))227    const authn = await addAuthenticator(context, page)228    if (readState().credential) {229      await importCredential(authn, 10_000 + Math.floor(Math.random() * 100_000))230      await signIn(page)231    } else {232      await page.goto(st.enrollUrl)233      await page.getByLabel("Nom affiché").fill("Propriétaire Essai")234      await page.getByLabel("Identifiant").fill("owner")235      await page.getByRole("button", { name: "Créer la clé d'accès" }).click()236      await expect(page.locator("#sidebar")).toBeAttached()237      await exportCredential(authn)238    }239    const P1 = `web-p1-${st.run}@stg.spboucher.ai`240    const P2 = `web-p2-${st.run}@stg.spboucher.ai`241    await purgeLeftoverMailboxes(page)242    track(P1, P2)243    const have = ((await api<{ mailboxes: { address: string }[] }>(page, "GET", "/api/me")).mailboxes ?? []).map((m) => m.address)244    for (const [address, name] of [[P1, "Prod Un"], [P2, "Prod Deux"]]) if (!have.includes(address)) await api(page, "POST", "/api/admin/mailboxes", { address, display_name: name })245    // après les autres parcours, d'autres boîtes existent : P1 et P2 passent en tête, pour que « / » ouvre P1 comme lorsque le246    // fichier est lancé seul (PUT /api/next/mailboxes/order : toutes les boîtes du propriétaire, dans l'ordre voulu)247    const mine = (await api<{ mailboxes: { id: string; address: string; access?: string }[] }>(page, "GET", "/api/me")).mailboxes.filter((m) => m.access !== "shared")248    const rank = (m: { address: string }) => (m.address === P1 ? 0 : m.address === P2 ? 1 : 2)249    await api(page, "PUT", "/api/next/mailboxes/order", { ids: [...mine].sort((x, y) => rank(x) - rank(y)).map((m) => m.id) })250    await page.goto("/")251    await openBox(page, P1)252    let storage = await context.storageState()253    // le démontage global purge par cette session (fichier lancé seul : aucun autre parcours ne l'a enregistrée)254    writeState({ storage: JSON.stringify(storage) })255    const stub = async (path: string, body?: unknown) => (await fetch(`${AI_STUB}${path}`, { method: body === undefined ? "GET" : "POST", body: body === undefined ? undefined : JSON.stringify(body) })).json()256    h = {257      run: st.run, P1, P2, page, context, browser, consoleErrors, api, seed, mailboxId, jmap, openBox, palette, setTheme, axe, shot,258      storage: () => storage,259      track,260      onTeardown: (fn) => void cleanups.push(fn),261      fromStorage: async (viewport, touch = false) => {262        storage = await context.storageState()263        const ctx = await browser.newContext({ viewport, locale: "fr-CA", timezoneId: "America/Toronto", storageState: storage, ...(touch ? { isMobile: true, hasTouch: true, deviceScaleFactor: 2 } : {}) })264        return { ctx, p: await ctx.newPage() }265      },266      axeThemes: async (p, name) => {267        for (const [theme, label] of THEMES) {268          await setTheme(p, label, theme)269          await axe(p, `${name}-${theme}`)270        }271        await setTheme(p, "Clair", "light")272      },273      shotThemes: async (p, name) => {274        await setTheme(p, "Minuit", "midnight")275        await shot(p, `${name}-midnight`)276        await setTheme(p, "Clair", "light")277        await shot(p, `${name}-light`)278      },279      ai: { mode: async (mode) => void (await stub("/__script", { mode })), seen: () => stub("/__seen"), reset: async () => void (await stub("/__reset", {})) },280    }281  })282  test.afterAll(async () => {283    if (!h) return284    fs.writeFileSync(`${OUT}/prod-console-errors.log`, h.consoleErrors.join("\n"))285    // nettoyages des zones d'abord : ils ont besoin de la session, fermée plus bas (les `afterAll` des zones passeraient après)286    for (const fn of cleanups.splice(0)) await fn().catch((e) => console.warn(`[e2e] nettoyage d'une zone : ${e}`))287    for (const address of [h.P1, h.P2]) {288      const id = await mailboxId(h.page, address).catch(() => null)289      if (id) await api(h.page, "DELETE", `/api/admin/mailboxes/${id}?purge=delete-all-mail`).catch(() => {})290    }291    await h.context?.close()292  })293  test("socle : le serveur annonce ses capacités, la coquille est en place", async () => {294    const caps = await api<{ features: string[] }>(h.page, "GET", "/api/next/capabilities")295    expect(caps.features).toEqual(expect.arrayContaining(["labels", "snooze", "search_all", "contacts", "attachments", "dashboard", "ai", "rules"]))296    await expect(h.page.locator("#sidebar")).toBeVisible()297    await expect(h.page.getByRole("heading", { level: 1, name: "Boîte de réception" })).toBeVisible()298  })299  for (const part of parts) part(() => h)300}301
Files main
Menu

Theme