Files
faunosteo/apps/app/e2e/support/browser.ts
T
bchanot 8dd2d78d38 fix(anatomy): V1-2.2 refuse zones without a usable interior
A polygon or path zone or trigger must fill a disc of 4 CSS px on the
320 x 568 preview under the layer's nonzero rule, stroke left out:
conservative widest-disc search on the flattened outline, caps and a
per-sheet budget, closed codes interior_too_small and
marker_interior_too_small. Browser probe on six projects.
2026-11-01 12:19:08 +01:00

283 lines
8.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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<GuardOptions & { requests: RequestGuard }>({
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, '<fixture>'),
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: /^<button [^>]*aria-label="(Passer en français|Switch to English)"/,
path: /^\//,
note: 'LanguageSwitcher (every page): shows « FR » / « EN », named « Passer en français » / « Switch to English »',
},
{
id: 'label-content-name-mismatch',
target: /^\.pr-1$/,
html: /^<a [^>]*href="\/app\/account"/,
path: /^\/app(\/|$)/,
note: 'AppLayout top bar account link: shows the display name, named « Mon compte » / « My account »',
},
{
id: 'definition-list',
target: /^dl$/,
html: /^<dl class="grid gap-2 text-sm">/,
path: /^\/app\/?$/,
note: 'DashboardRoute account card: the verified-status row is a div without dt/dd inside the dl',
},
]
const isKnown = (id: string, target: string, html: string, path: string) =>
KNOWN_VIOLATIONS.some(
(known) =>
known.id === id &&
known.target.test(target) &&
known.html.test(html) &&
known.path.test(path),
)
/**
* Zero serious or critical axe violation on the page as it stands (WCAG 2.0 to
* 2.2 A and AA rules plus axe best practices), known ones aside; the failure
* message lists them.
*/
export async function expectAccessible(
page: Page,
state: string,
): Promise<void> {
// Colours are measured at rest: an entrance animation still running would blend them.
await page.waitForFunction(() =>
document
.getAnimations()
.every((animation) => animation.playState !== 'running'),
)
const { violations } = await new AxeBuilder({ page })
.withTags([
'wcag2a',
'wcag2aa',
'wcag21a',
'wcag21aa',
'wcag22aa',
'best-practice',
])
.analyze()
const path = new URL(page.url()).pathname
const blocking = violations
.filter((v) => v.impact === 'serious' || v.impact === 'critical')
.flatMap((v) =>
v.nodes
.filter(
(node) => !isKnown(v.id, node.target.join(' '), node.html, path),
)
.map(
(node) =>
`${v.impact} ${v.id}: ${v.help} → ${node.target.join(' ')} on ${path}`,
),
)
expect(blocking, `axe on ${state}`).toEqual([])
}
/**
* Where the focus is: in the open modal dialog, in the browser's own interface
* (the document lost focus, as Tab past a modal dialog's last control does), or
* on something else of the page.
*/
export function focusOwner(page: Page): Promise<string> {
return page.evaluate(() => {
const active = document.activeElement
if (!document.hasFocus()) return 'browser'
if (!active || active === document.body) return 'body'
if (active.closest('dialog:modal')) return 'dialog'
return `outside:${active.tagName.toLowerCase()}:${active.textContent?.trim().slice(0, 40)}`
})
}
/** Accessible name of the focused element (aria-label, else its text). */
export function focusedName(page: Page): Promise<string> {
return page.evaluate(() => {
const active = document.activeElement as HTMLElement | null
return (
active?.getAttribute('aria-label') ?? active?.textContent?.trim() ?? ''
)
})
}
/** No horizontal page scroll: the document is no wider than the viewport. */
export async function expectNoHorizontalOverflow(
page: Page,
state: string,
): Promise<void> {
const { scrollWidth, clientWidth } = await page.evaluate(() => ({
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
}))
expect(scrollWidth, `horizontal overflow on ${state}`).toBeLessThanOrEqual(
clientWidth,
)
}