import assert from 'node:assert/strict' import { existsSync, mkdtempSync, readFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import test from 'node:test' import type { Suite, TestCase } from '@playwright/test/reporter' import A11yFailuresReporter from './a11y-failures-reporter.ts' import { compareFailures, type OpenFailure } from './open-failures.ts' const scanTest = (page: string, scan: object, outcome: string) => ({ title: `${page} a11y`, tags: ['@a11y'], outcome: () => outcome, results: [ { attachments: [{ name: 'axe-results.json', body: Buffer.from(JSON.stringify(scan)) }], }, ], }) as unknown as TestCase test('fails a page that returned an error status, and leaves unchecked what no run observed', () => { const outputFile = join(mkdtempSync(join(tmpdir(), 'a11y-reporter-')), 'failures.json') const reporter = new A11yFailuresReporter({ outputFile }) const tests = [ scanTest( '/docs/guides/a', { loaded: false, status: 503, elementCount: 0, violations: [] }, 'unexpected' ), scanTest( '/docs/guides/b', { loaded: true, status: 200, elementCount: 50, violations: [{ id: 'heading-order' }] }, 'unexpected' ), scanTest( '/docs/guides/c', { loaded: false, status: null, elementCount: 0, violations: [] }, 'unexpected' ), ] reporter.onBegin({} as never, { allTests: () => tests } as unknown as Suite) reporter.onEnd() assert.deepEqual(JSON.parse(readFileSync(outputFile, 'utf8')), { failures: [ { key: '/docs/guides/a: http-503', page: '/docs/guides/a' }, { key: '/docs/guides/b: heading-order', page: '/docs/guides/b' }, ], unchecked: ['/docs/guides/a', '/docs/guides/c: http', '/docs/guides/c'], }) }) test('writes no file when no @a11y test ran, such as when a spec fails to load', () => { const outputFile = join(mkdtempSync(join(tmpdir(), 'a11y-reporter-')), 'failures.json') const reporter = new A11yFailuresReporter({ outputFile }) reporter.onBegin({} as never, { allTests: () => [] } as unknown as Suite) reporter.onEnd() assert.equal(existsSync(outputFile), false) }) test('leaves unchecked a guide the run skipped because production does not serve it', () => { const outputFile = join(mkdtempSync(join(tmpdir(), 'a11y-reporter-')), 'failures.json') const reporter = new A11yFailuresReporter({ outputFile, skippedPages: ['/docs/guides/hidden'] }) const tests = [ scanTest( '/docs/guides/a', { loaded: true, status: 200, elementCount: 50, violations: [] }, 'expected' ), ] reporter.onBegin({} as never, { allTests: () => tests } as unknown as Suite) reporter.onEnd() const current = JSON.parse(readFileSync(outputFile, 'utf8')) assert.deepEqual(current.unchecked, ['/docs/guides/hidden', '/docs/guides/hidden: http']) const open: OpenFailure[] = ['heading-order', 'http-404'].map((rule) => ({ key: `/docs/guides/hidden: ${rule}`, page: '/docs/guides/hidden', firstSeen: '2026-12-01T05:17:00Z', attribution: { kind: 'preexisting' }, })) const { stillOpen, fixed } = compareFailures(open, current) assert.deepEqual(fixed, []) assert.equal(stillOpen.length, 2) })