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 create-from-template prompt. // // Usage (from app/): // flutter build web --target=tool/template_create_probe.dart \ // -o build/template_create_probe_web // OPENCODE_ARTIFACT_DIR= node tool/template_create_probe.mjs // // Optional PROBE_WIDTH/PROBE_HEIGHT (viewport), PROBE_SCALE (text scale), // PROBE_HOST (material|cupertino) and PROBE_CAPTURE_PREFIX. The bundle's // fixture gateway is in-process, so this never contacts a real Pipelines // server and never submits to one. Requires Google Chrome. const root = resolve(process.env.PROBE_WEB_ROOT ?? 'build/template_create_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 capturePrefix = process.env.PROBE_CAPTURE_PREFIX ?? 't1-create'; 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(), 'template-create-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) { for (let attempt = 0; attempt < 160; 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: 1, mobile: true }, 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 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) => { const point = await evaluate(`(() => { const needle = ${JSON.stringify(label)}; const candidates = [...document.querySelectorAll('flt-semantics')].filter( (el) => (el.getAttribute('aria-label') || el.innerText || '').trim() === 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(); return { x: r.x + r.width / 2, y: r.y + r.height / 2 }; })()`); if (!point) { const labels = await evaluate( "[...document.querySelectorAll('flt-semantics')].map((el) => (el.getAttribute('aria-label') || el.innerText || '').trim()).filter(Boolean)", ); throw new Error(`Could not click ${JSON.stringify(label)}. Labels: ${JSON.stringify(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); }; // The prompt is open as soon as its native alert-dialog surface exists. The // Create action's button role/aria-disabled is host- and scale-dependent on // the web semantics layer, so the disabled state is asserted module-side for // Material and by widget tests for Cupertino. const promptOpen = () => evaluate("Boolean(document.querySelector('[role=alertdialog]'))"); // Null unless a Material Create button exposes its disabled state. const createDisabled = () => evaluate(`(() => { const el = [...document.querySelectorAll('flt-semantics')].find( (node) => (node.getAttribute('aria-label') || node.innerText || '').trim() === 'Create' && node.getAttribute('role') === 'button', ); return el ? el.getAttribute('aria-disabled') === 'true' : null; })()`); const field = () => evaluate(`(() => { const input = document.querySelector('input'); return input ? { value: input.value, disabled: input.disabled, label: input.getAttribute('aria-label') } : null; })()`); 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.templateCreateProbe ?? 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}` }, session); await waitFor( () => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 'semantics placeholder', ); await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); await delay(900); await assertBody('Create pipeline from this template'); await clickLabel('Create pipeline from this template'); await waitFor(promptOpen, 'the create prompt'); await assertBody('Create from Release workflow'); await assertBody('New pipeline name (required)'); await assertBody("Creates a new draft pipeline on the server from this template's source; it is not run."); // CupertinoDialogAction does not expose aria-disabled on the web semantics // layer; its disabled state is covered by the widget tests. if (host !== 'cupertino' && (await createDisabled()) !== true) { throw new Error('Create was not disabled with a blank name'); } const blank = await field(); if (blank?.value !== '' || blank?.disabled !== false) { throw new Error(`The field is not blank and editable: ${JSON.stringify(blank)}`); } if (blank?.label !== 'New pipeline name (required)') { throw new Error(`The name field has no accessible name: ${JSON.stringify(blank)}`); } captures.push(await capture(`${capturePrefix}-blank-name-disabled`)); checks.push({ check: 'blank name keeps Create disabled', field: blank }); await typeName('Probe release'); if (host !== 'cupertino' && (await createDisabled()) !== false) { throw new Error('Create stayed disabled after a non-blank name'); } await clickLabel('Create'); await assertBody('invalid definition: fixture stage "añadir" has no type'); const refused = await state(); if (refused?.validates !== 1 || refused?.submits !== 0 || refused?.created?.length !== 0) { throw new Error(`A validation refusal sent more than validate: ${JSON.stringify(refused)}`); } if ((await field())?.value !== 'Probe release') { throw new Error(`The typed name was lost: ${JSON.stringify(await field())}`); } captures.push(await capture(`${capturePrefix}-validation-refusal`)); checks.push({ check: 'validation refusal sent no submit and kept the typed name', validates: refused.validates, submits: refused.submits, created: refused.created.length, field: await field(), }); // Retrying the same name is the normal correction after a server refusal. await clickLabel('Create'); await assertBody('Created "Probe release". It is in the Pipelines list.'); const accepted = await state(); if (accepted?.validates !== 2 || accepted?.submits !== 1 || accepted?.created?.length !== 1) { throw new Error(`The accepted create was not reported exactly once: ${JSON.stringify(accepted)}`); } if (accepted.lastSubmit?.name !== 'Probe release' || accepted.lastSubmit?.create_only !== true) { throw new Error(`Submit payload drifted: ${JSON.stringify(accepted.lastSubmit)}`); } const verbatim = ' name: "release"\n stages:\n - key: "añadir" 😀 \n'; if (accepted.lastSubmit?.source !== accepted.lastSource || accepted.lastSource !== verbatim) { throw new Error(`Source was not sent verbatim: ${JSON.stringify(accepted)}`); } const done = await field(); if (done?.disabled !== true) throw new Error(`The field did not lock after success: ${JSON.stringify(done)}`); await assertBody('Close'); captures.push(await capture(`${capturePrefix}-accepted-success`)); checks.push({ check: 'accepted create showed the exact line, reported once and locked the field', validates: accepted.validates, submits: accepted.submits, created: accepted.created.length, lastSubmit: accepted.lastSubmit, field: done, }); await clickLabel('Close'); await waitFor(async () => !(await promptOpen()), 'the prompt to close'); checks.push({ check: 'Close dismissed the prompt without another request', state: await state(), }); console.log(JSON.stringify({ ok: true, origin, viewport: { width, height }, textScale, host, 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 }); }