import type { Page, TestInfo } from '@playwright/test' import { scan, scanResults } from '../../shared/axe.ts' import { ENFORCED_RULES, EXCLUDED_RULES, type A11yScanResult } from './axe-rules.ts' export * from './axe-rules.ts' export const WCAG_TAGS = ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'] export async function scanArticle( page: Page, surface: string, include: string ): Promise { const reported = await scan(page, { tags: WCAG_TAGS, excludeRules: EXCLUDED_RULES, include }) const enforcedResults = await scanResults(page, { rules: ENFORCED_RULES }) const enforced = [ ...enforcedResults.violations, ...enforcedResults.incomplete.filter((result) => result.id === 'duplicate-id-aria'), ] const byRule = new Map([...reported, ...enforced].map((violation) => [violation.id, violation])) const elementCount = await page.evaluate( (selector) => document.querySelector(selector)?.querySelectorAll('*').length ?? 0, include ) return { surface, url: page.url(), include, excludedRules: EXCLUDED_RULES, loaded: true, status: null, elementCount, violations: [...byRule.values()], } } export function unloadedResult( surface: string, url: string, status: number | null, include: string ): A11yScanResult { return { surface, url, include, excludedRules: EXCLUDED_RULES, loaded: false, status, elementCount: 0, violations: [], } } export async function attachScanReport(testInfo: TestInfo, result: A11yScanResult): Promise { await testInfo.attach('axe-results.json', { body: JSON.stringify(result, null, 2), contentType: 'application/json', }) } export { formatViolations, settleForAxe, violationIds } from '../../shared/axe.ts'