/** * 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 AxeBuilder from '@axe-core/playwright' import { devices, expect, test as base, 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, }, }, ] 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: /^