// Real Chrome evidence for the Flutter search screen. Serves the fixture build // from build/web on an OS-assigned loopback port and drives the page through // Chrome DevTools Protocol, like tool/shell_probe.mjs. Usage: // flutter build web -t tool/search_fixture/main.dart // OPENCODE_ARTIFACT_DIR= node tool/search_probe.mjs import { createServer } from 'node:http'; import { readFile, mkdtemp, rm, writeFile } from 'node:fs/promises'; import { spawn } from 'node:child_process'; import { once } from 'node:events'; import { setTimeout as delay } from 'node:timers/promises'; import { join, extname, resolve, sep } from 'node:path'; import { tmpdir } from 'node:os'; const root = join(process.cwd(), 'build/web'); const artifactDir = process.env.OPENCODE_ARTIFACT_DIR; if (!artifactDir) throw new Error('OPENCODE_ARTIFACT_DIR is required for captures'); const mime = { '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm', '.png': 'image/png', '.ico': 'image/x-icon', '.ttf': 'font/ttf', '.otf': 'font/otf', }; const server = createServer(async (req, res) => { try { const path = resolve( root, '.' + decodeURIComponent(new URL(req.url, 'http://localhost').pathname), ); if (path !== root && !path.startsWith(root + sep)) { return res.writeHead(403).end(); } const filePath = path === root || path.endsWith('/') ? join(path, 'index.html') : path; const file = await readFile(filePath); res .writeHead(200, { 'content-type': mime[extname(filePath)] ?? 'application/octet-stream', }) .end(file); } catch { res.writeHead(404).end('not found'); } }); server.listen(0, '127.0.0.1'); await once(server, 'listening'); const port = server.address().port; const profile = await mkdtemp(join(tmpdir(), 'x1-search-chrome-')); const chrome = spawn( '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', [ '--headless=new', '--no-first-run', '--no-default-browser-check', '--disable-gpu', '--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank', ], { stdio: ['ignore', 'ignore', 'pipe'] }, ); let stderr = ''; chrome.stderr.on('data', (data) => { stderr += data; }); const waitFor = async (fn) => { for (let i = 0; i < 100; i++) { const value = await fn(); if (value) return value; await delay(100); } throw new Error('Timed out waiting for Chrome DevTools'); }; const result = { captures: [], evidence: {} }; let ws; try { const endpoint = await waitFor(() => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], ); ws = new WebSocket(endpoint); await once(ws, 'open'); let id = 0; const pending = new Map(); ws.addEventListener('message', (event) => { const msg = JSON.parse(event.data); if (msg.id && pending.has(msg.id)) { pending.get(msg.id)(msg); pending.delete(msg.id); } }); const send = (method, params = {}, sessionId) => new Promise((resolve) => { const callId = ++id; pending.set(callId, resolve); ws.send( JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}), }), ); }); const created = await send('Target.createTarget', { url: 'about:blank' }); if (!created.result) { throw new Error(`Target creation failed: ${JSON.stringify(created)}`); } const target = created.result.targetId; const session = ( await send('Target.attachToTarget', { targetId: target, flatten: true }) ).result.sessionId; await send('Page.enable', {}, session); await send('Runtime.enable', {}, session); const evaluate = async (expression) => { const response = await send( 'Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, session, ); if (response.error) { throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`); } return response.result?.result?.value; }; const clickMatching = (needle) => evaluate(`(() => { const needle = ${JSON.stringify(needle)}; const nodes = [...document.querySelectorAll('flt-semantics')]; const e = nodes.find(x => x.hasAttribute('flt-tappable') && (x.getAttribute('aria-label') || x.innerText || '').includes(needle)); if (!e) return false; e.click(); return true; })()`); const capture = async (name, width, height) => { await send( 'Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: width < 600 }, session, ); await delay(350); const data = ( await send( 'Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session, ) ).result.data; const path = join(artifactDir, `${name}.png`); await writeFile(path, Buffer.from(data, 'base64')); result.captures.push(path); }; const load = async (url, width, height) => { await send( 'Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: width < 600 }, session, ); const navigation = await send('Page.navigate', { url }, session); if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation)); await delay(3500); await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); await delay(900); }; const typeText = async (query) => { const focused = await evaluate(`(() => { const input = document.querySelector('input[type="search"]') || document.querySelector('input.flt-text-editing'); if (!input) return false; input.focus(); return true; })()`); if (!focused) throw new Error('Search input not found'); await delay(400); const inserted = await send('Input.insertText', { text: query }, session); if (inserted.error) throw new Error(JSON.stringify(inserted)); await delay(1400); return (await evaluate('document.body.innerText')).trim(); }; const typeQuery = async (query) => { const trimmed = await typeText(query); if (!trimmed.includes('Plan and land')) { throw new Error(`Results missing after typing: ${JSON.stringify(trimmed)}`); } return { typed: query, resultTitles: ['Plan and land', 'run #457 · Plan and land', 'run #457 · step 3 · input', 'plan.md', 'run #457 · planner visit 1'].filter((title) => trimmed.includes(title)), showsServerSnippet: trimmed.includes('Plan the change before implementing it'), showsRecency: /\d{1,2} [A-Z][a-z]{2} \d{2}:\d{2}/.test(trimmed), showsBounds: trimmed.includes('Results truncated') && trimmed.includes('cap 50') && trimmed.includes('scanned 2000 steps'), }; }; const typeTruncatedEmpty = async (query) => { const text = await typeText(query); const emptyCopy = `No matches for "${query}" within the server's scan limit.`; const missing = [ emptyCopy, 'Results truncated', '0 results', 'scanned 2000 steps', 'scan limit 1.0 MiB', ].filter((needle) => !text.includes(needle)); if (missing.length) throw new Error(`Truncated empty missing ${JSON.stringify(missing)}`); return { typed: query, emptyCopy, showsTruncationWarning: true, showsBounds: true }; }; const base = `http://127.0.0.1:${port}/`; await load(base, 390, 844); result.evidence.materialPhone = await typeQuery('plan'); await capture('x1-search-material-phone', 390, 844); if (!(await clickMatching('Plan and land'))) { throw new Error('Could not open the pipeline row'); } await delay(700); const opened = await evaluate('document.body.innerText'); if (!opened.includes('pipeline:24')) { throw new Error(`Opened target missing: ${JSON.stringify(opened)}`); } result.evidence.opened = 'pipeline:24'; await capture('x1-search-open-target', 390, 844); await load(base, 1280, 800); result.evidence.materialDesktop = await typeQuery('plan'); await capture('x1-search-material-desktop', 1280, 800); await load(`${base}?cupertino=1`, 390, 844); result.evidence.cupertinoPhone = await typeQuery('plan'); await capture('x1-search-cupertino-phone', 390, 844); await load(base, 390, 844); result.evidence.materialTruncatedEmpty = await typeTruncatedEmpty('needle'); await capture('x1-search-truncated-empty', 390, 844); await load(`${base}?cupertino=1`, 390, 844); result.evidence.cupertinoTruncatedEmpty = await typeTruncatedEmpty('needle'); await capture('x1-search-truncated-empty-cupertino', 390, 844); console.log(JSON.stringify(result, null, 2)); } finally { ws?.close(); chrome.kill('SIGTERM'); await Promise.race([once(chrome, 'exit'), delay(3000)]); server.close(); await rm(profile, { recursive: true, force: true }); }