| 1 | // Compat harness: every widget in corpus/compat/ is run with a scripted |
| 2 | // interaction in SANDBOX mode and in REFERENCE mode (native, dev tooling), and |
| 3 | // the two screenshots are diffed. Results go to docs/compat-report.md. The |
| 4 | // widgets that need what profile 1 leaves out (libraries, form controls) are |
| 5 |
| 6 | // here (charts drawn by hand, buttons instead of inputs, taps only). |
| 7 |
| 8 | import { expect, test, type FrameLocator, type Page } from '@playwright/test'; |
| 9 | import fs from 'node:fs'; |
| 10 | import path from 'node:path'; |
| 11 | import { fileURLToPath } from 'node:url'; |
| 12 | import pixelmatch from 'pixelmatch'; |
| 13 | import { PNG } from 'pngjs'; |
| 14 |
| 15 | const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 16 | // Outside Playwright's test-results/ (which it wipes at every run start); cleared by tests/global-setup.ts. |
| 17 | const outDir = path.join(root, 'reports', 'compat'); |
| 18 | fs.mkdirSync(outDir, { recursive: true }); |
| 19 |
| 20 | interface Scenario { |
| 21 | file: string; |
| 22 | /** Interaction script, run identically in both modes. */ |
| 23 | steps?: (f: FrameLocator, page: Page) => Promise<void>; |
| 24 | /** Behavioural assertions after the steps (both modes). */ |
| 25 | check?: (f: FrameLocator) => Promise<void>; |
| 26 | /** Max fraction of differing pixels (default 0.02). Use 1 for animated / time-dependent widgets. */ |
| 27 | tolerance?: number; |
| 28 | /** Widgets that are expected not to work; they must degrade gracefully instead. */ |
| 29 | unsupported?: boolean; |
| 30 | /** Keep the mouse where the steps left it (tooltips / hover state are part of the screenshot). */ |
| 31 | hover?: boolean; |
| 32 | /** Run with the dark theme: host theme for the sandbox, emulated prefers-color-scheme for the reference. */ |
| 33 | theme?: 'dark'; |
| 34 | /** Report name when a file appears in more than one scenario. */ |
| 35 | id?: string; |
| 36 | note?: string; |
| 37 | } |
| 38 |
| 39 | const scenarios: Scenario[] = [ |
| 40 | { file: 'tabs.html', steps: async (f) => { await f.locator('#tab-2').click(); await f.locator('#panel-2 button').click(); await f.locator('#panel-2 button').click(); await f.locator('#tab-3').click(); await f.locator('#acc-1 .acc-h').click(); }, |
| 41 | check: async (f) => { await expect(f.locator('#panel-3')).toBeVisible(); await expect(f.locator('#acc-1')).toHaveClass(/open/); await expect(f.locator('#count')).toHaveText('2'); } }, |
| 42 | { file: 'fragment.html', steps: async (f) => { for (let i = 0; i < 3; i++) await f.locator('#btn').click(); }, check: async (f) => { await expect(f.locator('#n')).toHaveText('3'); await expect(f.locator('#status')).toHaveText('load fired'); } }, |
| 43 | { file: 'calculator.html', steps: async (f) => { await f.locator('#k7').click(); await f.locator('#kplus').click(); await f.locator('#k3').click(); await f.locator('#keq').click(); }, check: async (f) => expect(f.locator('#display')).toHaveText('10') }, |
| 44 | { file: 'table-sort.html', steps: async (f) => { await f.locator('#h-score').click(); await f.locator('#h-score').click(); await f.locator('#f-berlin').click(); }, |
| 45 | check: async (f) => { await expect(f.locator('#rows tr')).toHaveCount(2); await expect(f.locator('#rows tr').first()).toContainText('Alice'); await expect(f.locator('#count')).toHaveText('2 of 6 rows'); await expect(f.locator('#h-score')).toHaveClass('desc'); } }, |
| 46 | { file: 'quiz-buttons.html', steps: async (f) => { await f.locator('#q1-right').click(); await f.locator('#q2-wrong').click(); await f.locator('#q3-a').click(); await f.locator('#q3-b').click(); await f.locator('#submit').click(); }, |
| 47 | check: async (f) => { await expect(f.locator('#score')).toHaveText('Score: 2 / 3'); await expect(f.locator('#q2')).toHaveClass(/\bwrong\b/); await expect(f.locator('#q3')).toHaveClass(/\bcorrect\b/); await expect(f.locator('#q2-wrong')).toHaveAttribute('aria-pressed', 'true'); } }, |
| 48 | { file: 'svg-chart.html', steps: async (f) => { await f.locator('#bar-3').click(); }, check: async (f) => { await expect(f.locator('#tip')).toContainText('Apr: $71k'); await expect(f.locator('#bar-3')).toHaveClass(/active/); } }, |
| 49 | { file: 'svg-bar-chart.html', steps: async (f) => { await f.locator('#bar-F').click(); }, check: async (f) => { await expect(f.locator('#info')).toHaveText('F = 97'); await expect(f.locator('#bar-F')).toHaveAttribute('fill', '#f59e0b'); } }, |
| 50 | // A tap pins the crosshair (the hover pointer only moves it), so it survives the mouse parking before the screenshot. |
| 51 | { file: 'svg-line-chart.html', steps: async (f) => { const svg = f.locator('#svg'); const b = await svg.boundingBox(); if (b) await svg.click({ position: { x: b.width * 0.5, y: b.height * 0.5 } }); }, |
| 52 | check: async (f) => { await expect(f.locator('#readout')).toHaveText(/^\d+:00 — [\d.]+°C$/); await expect(f.locator('#svg circle')).toBeVisible(); } }, |
| 53 | { file: 'canvas-line-chart.html', steps: async (f, page) => { await f.locator('#add').click(); const c = f.locator('#c'); const b = await c.boundingBox(); if (b) { await c.click({ position: { x: b.width * 0.45, y: b.height * 0.5 } }); await page.waitForTimeout(300); } }, |
| 54 | check: async (f) => { await expect(f.locator('#n')).toHaveText('8 points'); await expect(f.locator('#readout')).toHaveText(/^\w+ — Visits \d+ · Signups \d+$/); } }, |
| 55 | // The middle of the third category, then the left legend entry (2024) on the canvas: Transport with 2024 hidden. |
| 56 | { file: 'canvas-bar-chart.html', steps: async (f, page) => { const c = f.locator('#c'); const b = await c.boundingBox(); if (b) { await c.click({ position: { x: b.width * 0.55, y: b.height * 0.6 } }); await c.click({ position: { x: b.width * 0.4, y: 14 } }); await page.waitForTimeout(300); } }, |
| 57 | check: async (f) => expect(f.locator('#readout')).toHaveText('Transport — 2025: $150') }, |
| 58 | // A frame between the toggles: two class changes the sandbox delivers in one frame start no transition (no user taps that fast). |
| 59 | { file: 'css-animation.html', steps: async (f, page) => { await f.locator('#toggle').click(); await page.waitForTimeout(100); await f.locator('#toggle').click(); }, check: async (f) => expect(f.locator('#events')).toHaveText(/[1-9]\d* transitionend/), tolerance: 1, note: 'spinner frame differs by timing' }, |
| 60 | { file: 'clock.html', steps: async (f, page) => { await page.waitForTimeout(1100); }, check: async (f) => { await expect(f.locator('#time')).toHaveText(/\d\d:\d\d:\d\d/); await expect(f.locator('#ticks')).toHaveText(/[2-9]\d* ticks/); }, tolerance: 1, note: 'time of day differs' }, |
| 61 | { file: 'raf-animation.html', steps: async (f, page) => { await page.waitForTimeout(600); await f.locator('#stop').click(); }, check: async (f) => expect(f.locator('#frames')).toHaveText(/^[1-9]\d+$/), tolerance: 1, note: 'ball position depends on frame timing' }, |
| 62 | { file: 'drag.html', steps: async (f, page) => { const b = await f.locator('#box').boundingBox(); if (!b) return; await page.mouse.move(b.x + 30, b.y + 30); await page.mouse.down(); for (let i = 1; i <= 8; i++) await page.mouse.move(b.x + 30 + i * 15, b.y + 30 + i * 8); await page.mouse.up(); }, |
| 63 | check: async (f) => expect(f.locator('#pos')).toHaveText('x: 140, y: 84') }, |
| 64 | { file: 'notes-storage.html', steps: async (f) => { await f.locator('#add-tests').click(); await f.locator('li .del').first().click(); }, |
| 65 | check: async (f) => { await expect(f.locator('#list li')).toHaveCount(2); await expect(f.locator('#list li').last()).toContainText('Write tests'); await expect(f.locator('#meta')).toContainText('2 notes'); } }, |
| 66 | { file: 'dark-mode.html', steps: async (f) => { await f.locator('#check').click(); }, check: async (f) => { await expect(f.locator('#mode')).toHaveText('light ☀️'); await expect(f.locator('#js')).toContainText('matches = false'); }, tolerance: 0.06, note: 'bridge line differs by design (reference has no TelegramWidget)' }, |
| 67 | { file: 'dark-mode.html', id: 'dark-mode.html (dark theme)', theme: 'dark', steps: async (f) => { await f.locator('#check').click(); }, check: async (f) => { await expect(f.locator('#mode')).toHaveText('dark 🌙'); await expect(f.locator('#js')).toContainText('matches = true'); await expect(f.locator('body')).toHaveCSS('background-color', 'rgb(15, 23, 42)'); }, tolerance: 0.06, note: 'host-owned colour scheme; bridge line differs by design' }, |
| 68 | { file: 'stepper.html', steps: async (f) => { await f.locator('#next').click(); await f.locator('#next').click(); await f.locator('#back').click(); }, check: async (f) => { await expect(f.locator('#panel h3')).toHaveText('Profile'); await expect(f.locator('#fill')).toHaveCSS('width', /px/); } }, |
| 69 | { file: 'three-js.html', unsupported: true, check: async (f) => expect(f.locator('#fallback')).toBeVisible(), tolerance: 1, note: 'expected unsupported: Three.js/WebGL → fallback text' }, |
| 70 | { file: 'webgl.html', unsupported: true, check: async (f) => expect(f.locator('#fallback')).toBeVisible(), tolerance: 1, note: 'expected unsupported: WebGL → 2D fallback' }, |
| 71 | ]; |
| 72 |
| 73 | interface Result { file: string; order: number; sandboxLoaded: boolean; behaviorSandbox: string; behaviorReference: string; diff: number | null; tolerance: number; pass: boolean; unsupported: boolean; diags: string[]; errors: string[]; stats: any; note?: string } |
| 74 | // Results are persisted per test: Playwright restarts the worker after a failure, which would lose in-memory state. |
| 75 | const resultFile = (id: string) => path.join(outDir, id.replace(/[^\w.-]+/g, '_') + '.result.json'); |
| 76 |
| 77 | async function waitReady(page: Page) { |
| 78 | await page.waitForFunction(() => { const s = (window as any).__tw?.state; return s && (s.loaded || s.dead); }, null, { timeout: 60_000 }); |
| 79 | await page.waitForTimeout(400); |
| 80 | } |
| 81 |
| 82 | async function snapshot(page: Page, sel: string, file: string): Promise<PNG> { |
| 83 | const buf = await page.locator(sel).screenshot({ animations: 'disabled', caret: 'hide' }); |
| 84 | fs.writeFileSync(path.join(outDir, file), buf); |
| 85 | return PNG.sync.read(buf); |
| 86 | } |
| 87 |
| 88 | function diffImages(a: PNG, b: PNG, file: string): number { |
| 89 | const w = Math.max(a.width, b.width), h = Math.max(a.height, b.height); |
| 90 | const pad = (img: PNG) => { |
| 91 | if (img.width === w && img.height === h) return img; |
| 92 | const out = new PNG({ width: w, height: h }); |
| 93 | out.data.fill(255); |
| 94 | PNG.bitblt(img, out, 0, 0, img.width, img.height, 0, 0); |
| 95 | return out; |
| 96 | }; |
| 97 | const pa = pad(a), pb = pad(b); |
| 98 | const out = new PNG({ width: w, height: h }); |
| 99 | const n = pixelmatch(pa.data, pb.data, out.data, w, h, { threshold: 0.15, includeAA: true }); |
| 100 | fs.writeFileSync(path.join(outDir, file), PNG.sync.write(out)); |
| 101 | return n / (w * h); |
| 102 | } |
| 103 |
| 104 | async function runMode(page: Page, s: Scenario, mode: 'sandbox' | 'reference') { |
| 105 | // Park the mouse so hover state from the previous layout cannot leak into this run. |
| 106 | await page.mouse.move(2, 2); |
| 107 | await page.emulateMedia({ colorScheme: s.theme === 'dark' ? 'dark' : 'light' }); |
| 108 | // TW_POOL=N: the sandbox worker comes from a pool of N warmed ones (worker-pool.ts), the `warm` + `start` path instead of `init`. |
| 109 | const pool = process.env.TW_POOL && mode === 'sandbox' ? `&pool=${process.env.TW_POOL}` : ''; |
| 110 | await page.goto(`/?file=compat/${s.file}${mode === 'reference' ? '&reference=1' : ''}${s.theme === 'dark' ? '&theme=dark' : ''}${pool}`); |
| 111 | await waitReady(page); |
| 112 | const sel = mode === 'sandbox' ? '#sandbox-wrap iframe' : '#reference-wrap iframe'; |
| 113 | const frame = page.frameLocator(sel); |
| 114 | if (mode === 'reference') { |
| 115 | // The native frame reports its height after load; wait until it is sized and its scripts ran. |
| 116 | await page.waitForFunction((s) => (document.querySelector(s) as HTMLElement | null)?.getBoundingClientRect().height! > 24, sel, { timeout: 15_000 }).catch(() => {}); |
| 117 | await page.waitForTimeout(600); |
| 118 | } |
| 119 | let behavior = 'ok'; |
| 120 | try { |
| 121 | await s.steps?.(frame, page); |
| 122 | await page.waitForTimeout(500); |
| 123 | // Known-unsupported widgets: the native reference may well support the feature (headless Chromium has WebGL). |
| 124 | if (mode === 'sandbox' || !s.unsupported) await s.check?.(frame); |
| 125 | } catch (e) { |
| 126 | behavior = String((e as Error).message).split('\n')[0].slice(0, 200); |
| 127 | } |
| 128 | // Element screenshots can shift the layout under the cursor and fire hover |
| 129 | // events; park the mouse first unless the scenario is about hover state. |
| 130 | if (!s.hover) { |
| 131 | await page.mouse.move(2, 2); |
| 132 | await page.waitForTimeout(150); |
| 133 | } |
| 134 | const png = await snapshot(page, sel, `${(s.id ?? s.file).replace(/[^\w.-]+/g, '_')}.${mode}.png`); |
| 135 | const state = await page.evaluate(() => { const st = (window as any).__tw.state; return { loaded: st.loaded, dead: st.dead, diags: st.diags, logs: st.logs, stats: st.stats }; }); |
| 136 | return { behavior, png, state }; |
| 137 | } |
| 138 |
| 139 | for (const s of scenarios) { |
| 140 | test(`compat ${s.id ?? s.file}`, async ({ page }) => { |
| 141 | test.setTimeout(120_000); |
| 142 | const sb = await runMode(page, s, 'sandbox'); |
| 143 | const ref = await runMode(page, s, 'reference'); |
| 144 | const tolerance = s.tolerance ?? 0.02; |
| 145 | const diff = diffImages(sb.png, ref.png, `${(s.id ?? s.file).replace(/[^\w.-]+/g, '_')}.diff.png`); |
| 146 | const errors = sb.state.logs.filter((l: any) => l.level === 'error').map((l: any) => l.text.split('\n')[0]); |
| 147 | const diags = Object.entries(sb.state.diags as Record<string, { count: number; details: string[] }>) |
| 148 | .filter(([k]) => k !== 'boot' && k !== 'library') |
| 149 | .map(([k, v]) => `${k}×${v.count}: ${v.details[0]}`); |
| 150 | const pass = sb.state.loaded && !sb.state.dead && sb.behavior === 'ok' && diff <= tolerance && (s.unsupported || errors.length === 0); |
| 151 | const result: Result = { file: s.id ?? s.file, order: scenarios.indexOf(s), sandboxLoaded: !!sb.state.loaded && !sb.state.dead, behaviorSandbox: sb.behavior, behaviorReference: ref.behavior, diff, tolerance, pass, unsupported: !!s.unsupported, diags, errors, stats: sb.state.stats, note: s.note }; |
| 152 | fs.writeFileSync(resultFile(result.file), JSON.stringify(result)); |
| 153 | expect.soft(sb.state.dead, 'sandbox died').toBeNull(); |
| 154 | expect.soft(sb.behavior, 'sandbox behaviour').toBe('ok'); |
| 155 | expect.soft(ref.behavior, 'reference behaviour (harness sanity)').toBe('ok'); |
| 156 | expect.soft(diff, 'pixel diff').toBeLessThanOrEqual(tolerance); |
| 157 | }); |
| 158 | } |
| 159 |
| 160 | test.afterAll(() => { |
| 161 | const results: Result[] = fs.readdirSync(outDir).filter((f) => f.endsWith('.result.json')).map((f) => JSON.parse(fs.readFileSync(path.join(outDir, f), 'utf8')) as Result).sort((a, b) => a.order - b.order); |
| 162 | if (!results.length) return; |
| 163 | const supported = results.filter((r) => !r.unsupported); |
| 164 | const passed = supported.filter((r) => r.pass).length; |
| 165 | const pct = Math.round((passed / supported.length) * 100); |
| 166 | const lines = [ |
| 167 | '# Compat report', |
| 168 | '', |
| 169 | `Generated ${new Date().toISOString()} by \`pnpm test:compat\`.`, |
| 170 | '', |
| 171 | `**${passed} / ${supported.length} supported widgets pass (${pct}%).** ${results.filter((r) => r.unsupported).length} known-unsupported widgets checked for graceful fallback: ${results.filter((r) => r.unsupported && r.pass).length} ok.`, |
| 172 | '', |
| 173 | 'Pass = sandbox loaded without JS errors, the scripted interaction produced the expected result, and the screenshot differs from the native reference by at most the tolerance.', |
| 174 | '', |
| 175 | '| Widget | Pass | Behaviour (sandbox) | Pixel diff | Tolerance | JS ms | Ops | Sync queries (cache hits) | Diagnostics | Note |', |
| 176 | '|---|---|---|---|---|---|---|---|---|---|', |
| 177 | ...results.map((r) => `| ${r.file} | ${r.pass ? '✅' : '❌'} | ${r.behaviorSandbox.replace(/\|/g, '\\|')} | ${(r.diff! * 100).toFixed(2)}% | ${r.tolerance * 100}% | ${r.stats?.jsMs?.toFixed(0) ?? ''} | ${r.stats?.ops ?? ''} | ${r.stats?.queries ?? ''} (${r.stats?.queryCacheHits ?? ''}) | ${[...r.errors.map((e) => 'error: ' + e), ...r.diags].join('<br>').replace(/\|/g, '\\|')} | ${r.note ?? ''} |`), |
| 178 | '', |
| 179 | '## Failures and top causes', |
| 180 | '', |
| 181 | ...(results.filter((r) => !r.pass).length ? results.filter((r) => !r.pass).map((r) => `- **${r.file}**: ${r.behaviorSandbox !== 'ok' ? 'behaviour — ' + r.behaviorSandbox : ''} ${r.diff! > r.tolerance ? `pixel diff ${(r.diff! * 100).toFixed(2)}% > ${r.tolerance * 100}%` : ''} ${r.errors.length ? 'errors: ' + r.errors.join('; ') : ''}`) : ['- none']), |
| 182 | '', |
| 183 | `Screenshots and diffs: \`reports/compat/<widget>.{sandbox,reference,diff}.png\`.`, |
| 184 | '', |
| 185 | ]; |
| 186 | // Hand-written analysis lives in docs/compat-notes.md so regeneration keeps it. |
| 187 | const notes = path.join(root, 'docs', 'compat-notes.md'); |
| 188 | if (fs.existsSync(notes)) lines.push(fs.readFileSync(notes, 'utf8')); |
| 189 | fs.writeFileSync(path.join(root, 'docs', 'compat-report.md'), lines.join('\n')); |
| 190 | }); |
| 191 |