SPB Git

Repository Public

ops-helpers.ts 4.0 KB 81 lines typescript

1// Outils des essais d'exploitation : trois thèmes (clair, sombre, minuit), captures, SQL sur la base jetable, second contexte connecté.2import type { Browser, BrowserContext, Page } from "@playwright/test"3import { execFileSync } from "node:child_process"4import fs from "node:fs"5import { addAuthenticator, type Authn } from "../helpers"6import { DB_NAME, ORIGIN } from "./env"7import { axe, expect, st } from "./fixtures"89export const SHOTS = "/tmp/spmail-next-shots/web-ops"10fs.mkdirSync(SHOTS, { recursive: true })11export const THEMES = ["light", "dark", "midnight"] as const12export type ThemeName = (typeof THEMES)[number]1314/** Thème résolu du document (les feuilles de style n'ont qu'un bloc par thème, porté par `data-theme`). */15export async function setTheme(page: Page, theme: ThemeName) {16  await page.evaluate((th) => {17    document.documentElement.classList.add("theme-switching")18    document.documentElement.setAttribute("data-theme", th)19    requestAnimationFrame(() => requestAnimationFrame(() => document.documentElement.classList.remove("theme-switching")))20  }, theme)21  await page.waitForTimeout(250)22}23/** axe (sérieux et critique = 0) dans les trois thèmes, puis retour au clair. */24export async function axeThemes(page: Page, name: string) {25  for (const th of THEMES) {26    await setTheme(page, th)27    await axe(page, `${name} (${th})`)28  }29  await setTheme(page, "light")30}31/**32 * Le contenu d'une page de la console défile dans son propre volet : pour une capture entière, la fenêtre est agrandie à la33 * hauteur du contenu le temps de la prise, puis rétablie.34 */35async function whole<T>(page: Page, fn: () => Promise<T>): Promise<T> {36  const size = page.viewportSize() ?? { width: 1440, height: 900 }37  const height = await page.evaluate(() => {38    const m = document.querySelector("main#main")39    return m ? Math.ceil(m.scrollHeight + (m.getBoundingClientRect().top || 0) + 24) : 040  })41  if (height > size.height) await page.setViewportSize({ width: size.width, height: Math.min(height, 6000) })42  await page.waitForTimeout(150)43  try {44    return await fn()45  } finally {46    await page.setViewportSize(size)47  }48}49/** Captures clair + minuit (page entière) de l'écran courant. */50export async function shots(page: Page, name: string) {51  await whole(page, async () => {52    await setTheme(page, "light")53    await page.screenshot({ path: `${SHOTS}/${name}-light.png`, fullPage: true })54    await setTheme(page, "midnight")55    await page.screenshot({ path: `${SHOTS}/${name}-midnight.png`, fullPage: true })56    await setTheme(page, "light")57  })58}59export const shot = (page: Page, name: string) => whole(page, () => page.screenshot({ path: `${SHOTS}/${name}.png`, fullPage: true }))6061/** SQL sur la base jetable de la suite (préparation de données et vérifications seulement). */62export function sql(query: string): string {63  return execFileSync("psql", ["-q", "-At", "-d", DB_NAME, "-c", query], { encoding: "utf8" }).trim()64}6566let bump = 067/** Compteur de signatures en avance sur tous les usages précédents de la clé du lanceur. */68export const nextSignCount = () => 1000 + Math.floor((Date.now() - st.epoch) / 10) + 50 + bump++ * 506970/** Un autre navigateur (contexte neuf, aucun service worker ni cache), connecté avec la même clé d'accès : une seconde session. */71export async function secondSession(browser: Browser): Promise<{ context: BrowserContext; page: Page; authn: Authn }> {72  const context = await browser.newContext({ baseURL: ORIGIN, locale: "fr-CA", timezoneId: "America/Toronto", storageState: { cookies: [], origins: [] } })73  const page = await context.newPage()74  const authn = await addAuthenticator(context, page)75  await authn.cdp.send("WebAuthn.addCredential", { authenticatorId: authn.id, credential: { ...st.credential, signCount: nextSignCount() } })76  await page.goto("/")77  await page.getByRole("button", { name: "Se connecter avec une clé d'accès" }).click()78  await expect(page.locator("#sidebar")).toBeAttached()79  return { context, page, authn }80}81
Files main
Menu

Theme