| 1 | // Content height of every widget of packages/conformance through the sandbox: the web dev app (apps/web) |
| 2 | // loads it through the real pipeline (QuickJS worker → validator → renderer) at a narrow bubble width, and |
| 3 | // the renderer's own size report (before the host clamps it to the cap) is the height. Fails when a widget |
| 4 | // is taller than MAX_HEIGHT (lib.ts) allows, except one marked "tall" (a test of the cap). build.ts checks |
| // the visual set drawn natively, and the automatic set with measureHeights() here (it starts a dev server |
| 6 | // for it): drawn natively an automatic widget also lists the out-of-profile checks Chromium fails. The |
| 7 | // height is taken before any input, so an interactive widget's pending checks are in it. |
| 8 | // |
| 9 | // Needs the dev server of this checkout: corepack pnpm dev --port 5175 |
| 10 | // corepack pnpm tsx scripts/conformance/heights.ts --origin http://127.0.0.1:5175 \ |
| 11 | // [--renderer shadow|iframe] [--widths 320,390] [--only auto/] |
| 12 |
| 13 | import { pathToFileURL } from 'node:url'; |
| 14 | import { chromium, type Page } from '@playwright/test'; |
| 15 | import { MAX_HEIGHT, listWidgets, type Widget } from './lib'; |
| 16 |
| 17 | /** Loads the widget at `width` and returns its unclamped content height once it has run its checks (or waits for input). */ |
| 18 | async function measure(page: Page, w: Widget, width: number): Promise<{ height: number; status: string }> { |
| 19 | await page.evaluate(({ html, name, width, safeArea }) => { |
| 20 | const wrap = document.querySelector('#sandbox-wrap') as HTMLElement; |
| 21 | (wrap.closest('.chat') as HTMLElement).style.width = width + 'px'; |
| 22 | (wrap.closest('.bubble') as HTMLElement).style.padding = '0'; |
| 23 | const tw = (window as any).__tw; |
| 24 | const m: any = ((window as any).__heights = { height: 0, attr: null, html: null }); |
| 25 | // A widget drawn with the client's time over its corner is that much taller (the UA layer's body padding, its own env()). |
| 26 | tw.setSafeArea(safeArea); |
| 27 | tw.load(html, name); |
| 28 | const host = tw.host; |
| 29 | const onRenderer = host.onRenderer; |
| 30 | host.onRenderer = function (msg: any) { |
| 31 | if (msg?.type === 'size' && Number.isFinite(msg.height)) m.height = msg.height; |
| 32 | return onRenderer.call(this, msg); |
| 33 | }; |
| 34 | const r = host.renderer; |
| 35 | const send = r.send.bind(r); |
| 36 | r.send = (msg: any) => { |
| 37 | if (msg?.type === 'ops') { |
| 38 | for (const op of msg.ops) { |
| 39 | if (op.op === 'el' && op.adopt === 'html') { |
| 40 | m.html = op.id; |
| 41 | for (const [n, v] of op.attrs) if (n === 'data-conformance') m.attr = v; |
| 42 | } else if (op.op === 'attr' && op.id === m.html && op.name === 'data-conformance') m.attr = op.value; |
| 43 | } |
| 44 | } |
| 45 | return send(msg); |
| 46 | }; |
| 47 | }, { html: w.html, name: `conformance/${w.id}.html`, width, safeArea: w.meta.safeArea ?? null }); |
| 48 | let status = 'none'; |
| 49 | const t0 = Date.now(); |
| 50 | while (Date.now() - t0 < 10_000) { |
| 51 | status = await page.evaluate(() => { |
| 52 | if ((window as any).__tw.state.dead) return 'dead'; |
| 53 | try { return JSON.parse((window as any).__heights.attr).status; } catch { return 'none'; } |
| 54 | }); |
| 55 | if (status === 'done' || status === 'awaiting' || status === 'dead') break; |
| 56 | await page.waitForTimeout(100); |
| 57 | } |
| 58 | await page.waitForTimeout(400); |
| 59 | return { height: Math.ceil(await page.evaluate(() => (window as any).__heights.height)), status }; |
| 60 | } |
| 61 |
| 62 | /** |
| 63 | * Measures `widgets` through the dev app at `origin` and returns the ones taller than MAX_HEIGHT allows at a |
| 64 | * width up to MAX_HEIGHT.width (a "tall" widget excepted), as "<id>: <h> px at <w> px wide". |
| 65 | */ |
| 66 | export async function measureHeights(widgets: Widget[], origin: string, renderer = 'shadow', widths = [MAX_HEIGHT.width], log = console.log): Promise<string[]> { |
| 67 | const browser = await chromium.launch(); |
| 68 | const page = await (await browser.newContext({ viewport: { width: 1400, height: 1100 }, deviceScaleFactor: 2 })).newPage(); |
| 69 | const q = new URLSearchParams({ theme: 'light', features: 'webAnimations', policy: 'strict' }); |
| 70 | if (renderer !== 'iframe') q.set(renderer, '1'); |
| 71 | await page.goto(`${origin}/?${q}`); |
| 72 | await page.waitForFunction(() => !!(window as any).__tw, null, { timeout: 60_000 }); |
| 73 |
| 74 | const over: string[] = []; |
| 75 | log(`${renderer}, widths ${widths.join(', ')}; at most ${MAX_HEIGHT.height} px at ${MAX_HEIGHT.width} px wide`); |
| 76 | for (const w of widgets) { |
| 77 | const cells: string[] = []; |
| 78 | let status = ''; |
| 79 | for (const width of widths) { |
| 80 | const r = await measure(page, w, width); |
| 81 | status = r.status; |
| 82 | const tooTall = width <= MAX_HEIGHT.width && r.height > MAX_HEIGHT.height; |
| 83 | if (tooTall && !w.meta.tall) over.push(`${w.id}: ${r.height} px at ${width} px wide`); |
| 84 | cells.push(`${String(r.height).padStart(5)}${tooTall ? (w.meta.tall ? '~' : '!') : ' '}`); |
| 85 | } |
| 86 | log(`${w.id.padEnd(40)} ${cells.join(' ')} ${status}`); |
| 87 | } |
| 88 | await browser.close(); |
| 89 | return over; |
| 90 | } |
| 91 |
| 92 | async function main() { |
| 93 | const args = process.argv.slice(2); |
| 94 | const opt = (name: string) => (args.includes('--' + name) ? args[args.indexOf('--' + name) + 1] : undefined); |
| 95 | const over = await measureHeights(listWidgets(opt('only')), opt('origin') ?? 'http://127.0.0.1:5175', opt('renderer') ?? 'shadow', (opt('widths') ?? String(MAX_HEIGHT.width)).split(',').map(Number)); |
| 96 | if (over.length) { |
| 97 | console.log(`\ntaller than ${MAX_HEIGHT.height} px (split the widget, or "tall": true for a test of the cap):`); |
| 98 | for (const o of over) console.log(' ' + o); |
| 99 | process.exitCode = 1; |
| 100 | } |
| 101 | } |
| 102 |
| 103 | if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main().catch((e) => { console.error(e); process.exit(1); }); |
| 104 |