|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { test, expect } from '@playwright/test' |
| 5 | + |
| 6 | +/** |
| 7 | + * Recording-readiness guard for the `a11y_fix` demo on the React + Vite |
| 8 | + * playground (ANN-10). |
| 9 | + * |
| 10 | + * The 90-second demo opens the playground, scans the Hero CTA for a11y |
| 11 | + * violations, and lets the agent apply a token-level fix. For that to be |
| 12 | + * recording-clean we need two things to hold on `main`: |
| 13 | + * 1. The default playground state has exactly one Hero color-contrast hit |
| 14 | + * that maps to `var(--accent)` + `var(--text-on-accent)` — so the demo |
| 15 | + * has a real failure to scan. |
| 16 | + * 2. Darkening `--accent` at the token definition clears the violation |
| 17 | + * without touching component CSS — so the agent's first-try token fix |
| 18 | + * is mechanically guaranteed to work. |
| 19 | + * |
| 20 | + * Both checks run against the live playground at port 5174 (see |
| 21 | + * `playwright.config.ts`). axe-core is injected directly from |
| 22 | + * `node_modules` so we don't need `@axe-core/playwright` as a dep. |
| 23 | + */ |
| 24 | + |
| 25 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 26 | +const repoRoot = path.resolve(__dirname, '..') |
| 27 | +const axePath = path.join(repoRoot, 'node_modules', 'axe-core', 'axe.min.js') |
| 28 | +const axeSource = fs.readFileSync(axePath, 'utf-8') |
| 29 | +const tokensPath = path.join( |
| 30 | + repoRoot, |
| 31 | + 'playgrounds', |
| 32 | + 'simple', |
| 33 | + 'react-vite', |
| 34 | + 'src', |
| 35 | + 'styles', |
| 36 | + 'tokens.css', |
| 37 | +) |
| 38 | + |
| 39 | +interface AxeNode { |
| 40 | + target: string[] |
| 41 | + html: string |
| 42 | + failureSummary: string |
| 43 | + any: Array<{ id: string; data?: { fgColor?: string; bgColor?: string; contrastRatio?: number } }> |
| 44 | +} |
| 45 | +interface AxeRunResult { |
| 46 | + violations: Array<{ id: string; nodes: AxeNode[] }> |
| 47 | +} |
| 48 | + |
| 49 | +async function runContrast(page: import('@playwright/test').Page): Promise<AxeRunResult> { |
| 50 | + await page.evaluate(axeSource) |
| 51 | + return await page.evaluate(async () => { |
| 52 | + // @ts-ignore - axe is global once injected |
| 53 | + return await axe.run(document, { |
| 54 | + runOnly: { type: 'rule', values: ['color-contrast'] }, |
| 55 | + resultTypes: ['violations'], |
| 56 | + }) |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +function heroCtaNodes(result: AxeRunResult): AxeNode[] { |
| 61 | + const contrast = result.violations.find(v => v.id === 'color-contrast') |
| 62 | + if (!contrast) return [] |
| 63 | + return contrast.nodes.filter(n => n.html.includes('data-annotask-file="src/components/Hero.tsx"')) |
| 64 | +} |
| 65 | + |
| 66 | +test.describe('React + Vite a11y_fix recording readiness', () => { |
| 67 | + test('default playground has the Hero CTA color-contrast violation the demo expects', async ({ |
| 68 | + page, |
| 69 | + }) => { |
| 70 | + await page.goto('/', { waitUntil: 'networkidle' }) |
| 71 | + const result = await runContrast(page) |
| 72 | + const heroHits = heroCtaNodes(result) |
| 73 | + expect( |
| 74 | + heroHits.length, |
| 75 | + 'Hero primary CTA must trigger a color-contrast violation so the demo has something to scan', |
| 76 | + ).toBeGreaterThan(0) |
| 77 | + |
| 78 | + const cta = heroHits[0] |
| 79 | + const contrastData = cta.any.find(a => a.id === 'color-contrast')?.data |
| 80 | + expect(contrastData, 'axe must surface fg/bg/ratio for the demo violation').toBeTruthy() |
| 81 | + expect(contrastData?.fgColor?.toLowerCase()).toBe('#ffffff') |
| 82 | + expect(contrastData?.bgColor?.toLowerCase()).toBe('#007bff') |
| 83 | + expect(contrastData?.contrastRatio ?? 99).toBeLessThan(4.5) |
| 84 | + }) |
| 85 | + |
| 86 | + test('tokens.css resolves the violation when --accent is darkened at the definition', async ({ |
| 87 | + page, |
| 88 | + }) => { |
| 89 | + await page.goto('/', { waitUntil: 'networkidle' }) |
| 90 | + const before = heroCtaNodes(await runContrast(page)) |
| 91 | + expect(before.length).toBeGreaterThan(0) |
| 92 | + |
| 93 | + // Simulate the agent's first-try token-level fix in-page only — do not |
| 94 | + // touch tokens.css on disk, so the recording state on main stays |
| 95 | + // "failing by default". |
| 96 | + await page.addStyleTag({ |
| 97 | + content: `:root, :root[data-theme="dark"] { --accent: #0061f2; }`, |
| 98 | + }) |
| 99 | + |
| 100 | + const after = heroCtaNodes(await runContrast(page)) |
| 101 | + expect( |
| 102 | + after.length, |
| 103 | + 'Darkening --accent at the token definition must clear the Hero CTA violation', |
| 104 | + ).toBe(0) |
| 105 | + }) |
| 106 | + |
| 107 | + test('Hero CTA CSS still references the token (no inline hex override creeps back in)', async () => { |
| 108 | + const hero = fs.readFileSync( |
| 109 | + path.join( |
| 110 | + repoRoot, |
| 111 | + 'playgrounds', |
| 112 | + 'simple', |
| 113 | + 'react-vite', |
| 114 | + 'src', |
| 115 | + 'components', |
| 116 | + 'Hero.module.css', |
| 117 | + ), |
| 118 | + 'utf-8', |
| 119 | + ) |
| 120 | + expect(hero).toMatch(/\.primary\s*\{[\s\S]*?background:\s*var\(--accent\)/) |
| 121 | + expect(hero).toMatch(/\.primary\s*\{[\s\S]*?color:\s*var\(--text-on-accent\)/) |
| 122 | + |
| 123 | + const tokens = fs.readFileSync(tokensPath, 'utf-8') |
| 124 | + // Dark theme default keeps the well-known failing value so the demo has |
| 125 | + // a deterministic scan target. If this ever shifts, update the e2e and |
| 126 | + // the readiness note together. |
| 127 | + expect(tokens).toMatch(/--accent:\s*#007bff/) |
| 128 | + }) |
| 129 | +}) |
0 commit comments