import { createServer } from 'node:http'; import { readFile, mkdir, 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'; // Real-browser acceptance for the reusable Templates gallery. // // Usage (from app/): // flutter build web --target=tool/templates_gallery_probe.dart \ // -o build/templates_gallery_probe_web // OPENCODE_ARTIFACT_DIR= node tool/templates_gallery_probe.mjs // // PROBE_HOST (material|cupertino), PROBE_SCALE, PROBE_WIDTH/PROBE_HEIGHT, // PROBE_SCENARIO (default|empty|refresh-refusal) and PROBE_CAPTURE_PREFIX // select the host and evidence. The fixture gateway is in-process: no socket // is opened and no real Pipelines server is contacted. const root = resolve(process.env.PROBE_WEB_ROOT ?? 'build/templates_gallery_probe_web'); const artifactDir = process.env.OPENCODE_ARTIFACT_DIR; if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures'); await mkdir(artifactDir, { recursive: true }); const width = Number(process.env.PROBE_WIDTH ?? 390); const height = Number(process.env.PROBE_HEIGHT ?? 844); const textScale = process.env.PROBE_SCALE ?? '1'; const host = process.env.PROBE_HOST ?? 'material'; const scenario = process.env.PROBE_SCENARIO ?? 'default'; const capturePrefix = process.env.PROBE_CAPTURE_PREFIX ?? 't1-gallery'; const deviceScaleFactor = Number(process.env.PROBE_DPR ?? 1); const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm', '.png': 'image/png', '.ico': 'image/x-icon', '.otf': 'font/otf', '.ttf': 'font/ttf', }; const server = createServer(async (req, res) => { try { const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname); const path = resolve(root, `.${pathname}`); if (path !== root && !path.startsWith(root + sep)) { res.writeHead(403).end(); return; } 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 origin = `http://127.0.0.1:${server.address().port}`; const profile = await mkdtemp(join(tmpdir(), 'templates-gallery-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; }); async function waitFor(check, label, attempts = 160) { for (let attempt = 0; attempt < attempts; attempt++) { const value = await check(); if (value) return value; await delay(100); } throw new Error(`Timed out waiting for ${label}\n${stderr.slice(-1200)}`); } let ws; try { const endpoint = await waitFor( () => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1], 'Chrome DevTools', ); ws = new WebSocket(endpoint); await once(ws, 'open'); let id = 0; const pending = new Map(); ws.addEventListener('message', (event) => { const message = JSON.parse(event.data); if (message.method === 'Runtime.exceptionThrown') { console.error('PAGE EXCEPTION', JSON.stringify(message.params?.exceptionDetails?.exception?.description)); } if (message.id && pending.has(message.id)) { pending.get(message.id)(message); pending.delete(message.id); } }); const send = (method, params = {}, sessionId) => new Promise((resolveCall) => { const callId = ++id; pending.set(callId, resolveCall); ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) })); }); const target = (await send('Target.createTarget', { url: 'about:blank' })).result.targetId; const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).result.sessionId; await send('Page.enable', {}, session); await send('Runtime.enable', {}, session); await send( 'Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor, mobile: true }, session, ); await send( 'Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: 'dark' }] }, 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)}`); if (response.result?.exceptionDetails) { throw new Error(`Page exception: ${JSON.stringify(response.result.exceptionDetails)}`); } return response.result?.result?.value; }; const semanticText = () => evaluate(`(() => { const chunks = []; for (const el of document.querySelectorAll('flt-semantics')) { const label = el.getAttribute('aria-label'); if (label) chunks.push(label); if (!el.children.length && el.innerText) chunks.push(el.innerText); } for (const el of document.querySelectorAll('input, textarea')) { if (el.getAttribute('aria-label')) chunks.push(el.getAttribute('aria-label')); if (el.value) chunks.push(String(el.value)); } if (document.body?.innerText) chunks.push(document.body.innerText); return [...new Set(chunks)].join('\\n'); })()`); const labels = () => evaluate( "[...document.querySelectorAll('flt-semantics')].map((el) => (el.getAttribute('aria-label') || el.innerText || '').trim()).filter(Boolean)", ); const assertBody = async (text) => { try { await waitFor( async () => (await semanticText()).includes(text), `body to contain ${JSON.stringify(text)}`, ); } catch (error) { throw new Error(`${error.message}\nBody now:\n${(await semanticText()).slice(0, 3000)}`); } }; const clickLabel = async (label, { contains = false } = {}) => { const point = await evaluate(`(() => { const needle = ${JSON.stringify(label)}; const candidates = [...document.querySelectorAll('flt-semantics')].filter((el) => { const text = (el.getAttribute('aria-label') || el.innerText || '').trim(); return ${contains ? 'text.includes(needle)' : 'text === needle'}; }); const target = candidates.find((el) => ['button', 'tab', 'link', 'checkbox', 'menuitem'].includes(el.getAttribute('role')), ) ?? candidates[0]; if (!target) return null; const r = target.getBoundingClientRect(); if (r.width === 0 || r.height === 0) return null; return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; })()`); if (!point) { throw new Error(`Could not click ${JSON.stringify(label)}. Labels: ${JSON.stringify(await labels())}`); } await send('Input.dispatchMouseEvent', { type: 'mousePressed', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session); await send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session); await delay(400); }; const typeName = async (value) => { const focused = await evaluate(`(() => { const input = document.querySelector('input'); if (!input) return false; input.focus(); input.select?.(); input.setSelectionRange?.(0, (input.value || '').length); return true; })()`); if (!focused) throw new Error('The name field was not exposed as a semantics input'); await delay(300); await send('Input.insertText', { text: value }, session); await delay(400); }; const state = () => evaluate('globalThis.templatesGalleryProbe ?? null'); const capture = async (name) => { const data = (await send('Page.captureScreenshot', { format: 'png' }, session)).result.data; const path = join(artifactDir, `${name}.png`); await writeFile(path, Buffer.from(data, 'base64')); return path; }; const checks = []; const captures = []; await send( 'Page.navigate', { url: `${origin}/?scale=${textScale}&host=${host}&scenario=${scenario}` }, session, ); await waitFor( () => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 'semantics placeholder', ); await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); await delay(900); if (scenario === 'empty') { await assertBody('No templates are available on this server.'); const emptyState = await state(); if (emptyState?.listCalls !== 1) { throw new Error(`The empty catalogue made unexpected requests: ${JSON.stringify(emptyState)}`); } captures.push(await capture(`${capturePrefix}-empty`)); checks.push({ check: 'empty catalogue stated honestly', state: emptyState }); } else if (scenario === 'refresh-refusal') { // The catalogue loads, a template is open, and Refresh genuinely refuses: // the refusal must stay visible above that detail, with Retry. await assertBody('Release workflow'); await clickLabel('Release workflow', { contains: true }); await assertBody('Definition source'); const before = await state(); if (before?.listCalls !== 1 || before?.validates !== 2 || before?.submits !== 0) { throw new Error(`Opening the template made unexpected requests: ${JSON.stringify(before)}`); } await clickLabel('Refresh'); await assertBody('fixture refresh unavailable'); await assertBody('Retry'); await assertBody('Definition source'); await assertBody('Create pipeline from this template'); const refused = await state(); if (refused?.listCalls !== 2 || refused?.validates !== 2 || refused?.submits !== 0) { throw new Error(`The failed refresh did not stay read-only: ${JSON.stringify(refused)}`); } captures.push(await capture(`${capturePrefix}-detail-refresh-error`)); checks.push({ check: 'failed refresh visible above the open template', state: refused }); await clickLabel('Retry'); await waitFor( async () => !(await semanticText()).includes('fixture refresh unavailable'), 'the refusal banner to clear after Retry', ); await assertBody('Definition source'); const retried = await state(); if (retried?.listCalls !== 3) { throw new Error(`Retry did not re-read exactly once: ${JSON.stringify(retried)}`); } captures.push(await capture(`${capturePrefix}-detail-refresh-retried`)); checks.push({ check: 'Retry re-read while keeping the template open', state: retried }); } else { // The first read genuinely refuses; the screen must say so and retry. await assertBody('fixture catalogue unavailable'); await assertBody('Retry'); const failed = await state(); if (failed?.listCalls !== 1) { throw new Error(`The refusal state made unexpected requests: ${JSON.stringify(failed)}`); } captures.push(await capture(`${capturePrefix}-error`)); checks.push({ check: 'catalogue refusal shown with Retry', state: failed }); await clickLabel('Retry'); await assertBody('Loading templates…'); captures.push(await capture(`${capturePrefix}-loading`)); await assertBody('Release workflow'); await assertBody('(unnamed template)'); await assertBody('Ship a version'); await assertBody('1 stage'); const gallery = await state(); if (gallery?.listCalls !== 2) { throw new Error(`Retry did not re-read exactly once: ${JSON.stringify(gallery)}`); } captures.push(await capture(`${capturePrefix}-gallery`)); checks.push({ check: 'retry loaded the actual catalogue rows', state: gallery }); await clickLabel('Release workflow', { contains: true }); await assertBody('Definition source'); await assertBody('Key'); await assertBody('Includes a schedule'); await assertBody('Create pipeline from this template'); const detail = await state(); if (detail?.validates !== 2 || detail?.submits !== 0) { throw new Error(`Opening a template sent a write: ${JSON.stringify(detail)}`); } captures.push(await capture(`${capturePrefix}-detail`)); checks.push({ check: 'selected template shows metadata and verbatim source', state: detail }); await clickLabel('Back'); await assertBody('Release workflow'); await clickLabel('Refresh'); await assertBody('Added after refresh'); const refreshed = await state(); if (refreshed?.listCalls !== 3 || refreshed?.validates !== 5) { throw new Error(`Refresh made unexpected requests: ${JSON.stringify(refreshed)}`); } captures.push(await capture(`${capturePrefix}-refreshed`)); checks.push({ check: 'native back returned and refresh re-read the catalogue', state: refreshed }); await clickLabel('Release workflow', { contains: true }); await assertBody('Create pipeline from this template'); await clickLabel('Create pipeline from this template'); await assertBody('Create from Release workflow'); await assertBody('New pipeline name (required)'); captures.push(await capture(`${capturePrefix}-create-blank`)); await typeName('Probe release'); await clickLabel('Cancel'); await waitFor(async () => !(await semanticText()).includes('Create from Release workflow'), 'the prompt to close'); const cancelled = await state(); if (cancelled?.validates !== 5 || cancelled?.submits !== 0 || cancelled?.created?.length !== 0) { throw new Error(`Cancel sent a request: ${JSON.stringify(cancelled)}`); } checks.push({ check: 'Cancel sent nothing', state: cancelled }); await waitFor( async () => (await semanticText()).includes('Create pipeline from this template'), 'the detail after the prompt closed', ); await clickLabel('Create pipeline from this template'); await assertBody('Create from Release workflow'); await typeName('Probe release'); await clickLabel('Create'); await assertBody('Created "Probe created". It is in the Pipelines list.'); const created = await state(); if (created?.validates !== 6 || created?.submits !== 1 || created?.created?.length !== 1) { throw new Error(`The accepted create was not reported exactly once: ${JSON.stringify(created)}`); } if (created.lastSubmit?.name !== 'Probe release' || created.lastSubmit?.create_only !== true) { throw new Error(`Submit payload drifted: ${JSON.stringify(created.lastSubmit)}`); } const verbatim = ' name: "release"\n\tstages:\n - key: "añadir" 😀 \n'; if (created.lastSubmit?.source !== created.lastSource || created.lastSource !== verbatim) { throw new Error(`Source was not sent verbatim: ${JSON.stringify(created)}`); } captures.push(await capture(`${capturePrefix}-created`)); checks.push({ check: 'accepted create forwarded the detail once with the verbatim source', state: created, }); await clickLabel('Close'); await waitFor(async () => !(await semanticText()).includes('Create from Release workflow'), 'the prompt to close'); const closed = await state(); if (closed?.created?.length !== 1 || closed?.submits !== 1) { throw new Error(`Close changed the request counts: ${JSON.stringify(closed)}`); } checks.push({ check: 'Close dismissed without another request', state: closed }); } console.log(JSON.stringify({ ok: true, origin, viewport: { width, height }, deviceScaleFactor, textScale, host, scenario, checks, captures, }, 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 }); }