/** * Shared pieces of the Playwright suites (docs/development/BROWSER_TESTS.md): * the two Chromium projects, the request guard every test runs under, the axe * gate, and the focus and layout probes. */ import { existsSync } from 'node:fs' import AxeBuilder from '@axe-core/playwright' import { devices, expect, firefox, test as base, webkit, type Page, type Project, } from '@playwright/test' /** * Desktop and the 375 × 667 phone of the V1-1 contract (§12.5); Chromium only * until V1-5. A test tagged `@phone` covers what exists on small screens only. */ export const PROJECTS: Project[] = [ { name: 'chromium-desktop', grepInvert: /@phone/, use: { ...devices['Desktop Chrome'], viewport: { width: 1280, height: 800 }, }, }, { name: 'chromium-375', use: { ...devices['Desktop Chrome'], viewport: { width: 375, height: 667 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true, }, }, ] /** * The canvas click proofs (V1-2.2, e2e/ui/canvas-clicks.spec.ts and interior-clicks.spec.ts) * also run on Firefox and * WebKit, desktop, and on the WebKit phone, when the pinned engine is installed, and on a * 320 px Chromium phone * (`playwright install firefox webkit`, no sudo, never `--with-deps`). A missing engine is * named in the run's output, never passed silently. */ const CANVAS_CLICKS = /(canvas|interior)-clicks\.spec\.ts$/ function installed(name: string, executable: string): boolean { const present = existsSync(executable) if (!present) console.warn(`canvas clicks: ${name} is not installed, its projects do not run`) return present } export const CLICK_PROJECTS: Project[] = [ // The smallest preview the markers' visible size is measured on (owner decision 2026-11-01). { name: 'chromium-320', testMatch: CANVAS_CLICKS, use: { ...devices['Desktop Chrome'], viewport: { width: 320, height: 568 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true, }, }, ...(installed('firefox', firefox.executablePath()) ? [ { name: 'firefox-desktop', testMatch: CANVAS_CLICKS, grepInvert: /@phone/, use: { ...devices['Desktop Firefox'], viewport: { width: 1280, height: 800 } }, }, ] : []), ...(installed('webkit', webkit.executablePath()) ? [ { name: 'webkit-desktop', testMatch: CANVAS_CLICKS, grepInvert: /@phone/, use: { ...devices['Desktop Safari'], viewport: { width: 1280, height: 800 } }, }, { name: 'webkit-375', testMatch: CANVAS_CLICKS, use: { ...devices['iPhone SE'], viewport: { width: 375, height: 667 }, }, }, ] : []), ] export interface GuardOptions { /** Origins the page may reach; every other request is aborted and fails the test. */ allowedOrigins: string[] /** Strings no request URL may carry (fixture names, emails, search terms). */ forbiddenInUrls: string[] } export interface RequestGuard { /** Adds values made during the test (a fictitious account) to `forbiddenInUrls`. */ forbid: (...values: string[]) => void } /** * Every test runs under a request guard: a request to an origin outside the * allowlist (the DEV stack, a CDN, anything) is aborted, and the test fails * listing it, as it fails on a URL holding a fixture value (masked in the * report, which never shows the value itself). */ export const test = base.extend({ allowedOrigins: [[], { option: true }], forbiddenInUrls: [[], { option: true }], requests: [ async ({ context, allowedOrigins, forbiddenInUrls }, use) => { const forbidden = forbiddenInUrls.filter((value) => value !== '') const refused: string[] = [] const leaking: string[] = [] const mask = (url: string) => forbidden.reduce( (masked, value) => masked.replaceAll(value, ''), url, ) await context.route( (url) => !allowedOrigins.includes(url.origin), (route) => { refused.push(mask(decodeURIComponent(route.request().url()))) return route.abort('blockedbyclient') }, ) context.on('request', (request) => { const url = decodeURIComponent(request.url()) if (forbidden.some((value) => url.includes(value))) leaking.push(mask(url)) }) await use({ forbid: (...values) => forbidden.push(...values.filter((v) => v !== '')), }) expect(refused, 'requests outside the allowed origins').toEqual([]) expect(leaking, 'request URLs carrying a fixture value').toEqual([]) }, { auto: true }, ], }) export { expect } /** * Serious violations found before V1-1.3, outside its code, listed in TODO RR-7 * until their own fix: the scan lets through only the node that matches the * rule, the selector, the opening markup and the route of each entry, and fails * on any other, so a new screen or a new element never inherits an exception * (WCAG 2.5.3 label in name, 1.3.1 info and relationships). */ const KNOWN_VIOLATIONS: { id: string target: RegExp html: RegExp path: RegExp note: string }[] = [ { id: 'label-content-name-mismatch', target: /^button\[aria-label="(Passer en français|Switch to English)"\]$/, html: /^