import { readFileSync, writeFileSync } from 'node:fs' import type { FullConfig, Reporter, Suite, TestCase } from '@playwright/test/reporter' import { blockingViolations, scanLooksEmpty, type A11yScanResult } from './axe-rules.ts' import { httpFailureKey, statusCheckKey, type Failure, type RunFailures } from './open-failures.ts' function scanResult(test: TestCase): A11yScanResult | null { const attachment = test.results .at(-1) ?.attachments.find((item) => item.name === 'axe-results.json') if (!attachment) return null const body = attachment.body ?? (attachment.path ? readFileSync(attachment.path) : null) return body ? (JSON.parse(body.toString('utf8')) as A11yScanResult) : null } // Writes the pages and axe rules that failed, for docs-master-alert.yml. A page that returned an // error status fails as http-, and its axe rules stay unchecked. A page whose status or // scan is missing, empty, or skipped is listed as unchecked, so a flaky run doesn't count as a new // failure or a fix. So is a guide the run skipped because production doesn't serve it. A run with // no @a11y tests, such as when a spec fails to load, writes no file. export default class A11yFailuresReporter implements Reporter { private suite: Suite | undefined private outputFile: string private skippedPages: string[] constructor(options: { outputFile: string; skippedPages?: string[] }) { this.outputFile = options.outputFile this.skippedPages = options.skippedPages ?? [] } onBegin(_config: FullConfig, suite: Suite) { this.suite = suite } onEnd() { const failures: Failure[] = [] const unchecked: string[] = [] const tests = (this.suite?.allTests() ?? []).filter((test) => test.tags.includes('@a11y')) if (tests.length === 0) return for (const test of tests) { const page = test.title.split(' ')[0] const result = scanResult(test) if (result?.status == null) unchecked.push(statusCheckKey(page)) if (result?.status != null && result.status >= 400) { failures.push({ key: httpFailureKey(page, result.status), page }) unchecked.push(page) continue } if (test.outcome() === 'skipped' || !result?.loaded || scanLooksEmpty(result)) { unchecked.push(page) continue } if (test.outcome() !== 'unexpected') continue const blocking = blockingViolations(result) if (blocking.length === 0) unchecked.push(page) for (const violation of blocking) failures.push({ key: `${page}: ${violation.id}`, page }) } for (const page of this.skippedPages) unchecked.push(page, statusCheckKey(page)) const output: RunFailures = { failures, unchecked } writeFileSync(this.outputFile, JSON.stringify(output, null, 2)) } }