// Real-browser acceptance for the registered Templates destination. // // From app/: // flutter build web -o build/templates_wiring_web // OPENCODE_ARTIFACT_DIR= \ // node tool/templates_wiring_probe.mjs build/templates_wiring_web // // PROBE_HOST (material|cupertino) selects the host. The fixture server is an // isolated loopback implementation of the Pipelines websocket protocol used by // this flow; no production server, database or operator data is involved. import { createServer } from 'node:http'; import { readFile, mkdir, mkdtemp, rm, stat, 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'; import { createHash } from 'node:crypto'; const root = resolve(process.argv[2] ?? 'build/templates_wiring_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 host = process.env.PROBE_HOST === 'cupertino' ? 'cupertino' : 'material'; const width = Number(process.env.PROBE_WIDTH ?? 390); const height = Number(process.env.PROBE_HEIGHT ?? 844); const deviceScaleFactor = Number(process.env.PROBE_DPR ?? 1); const capturePrefix = process.env.PROBE_CAPTURE_PREFIX ?? `t1-wire-${host}`; const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8', '.json': 'application/json; charset=utf-8', '.wasm': 'application/wasm', '.png': 'image/png', '.svg': 'image/svg+xml', '.ico': 'image/x-icon', '.ttf': 'font/ttf', '.otf': 'font/otf', '.woff': 'font/woff', '.woff2': 'font/woff2', }; const PIPELINES = [ { id: 1, name: 'Alpha review', version: 2, stages: 2, created_at: '2026-10-21T09:00:00', updated_at: '2026-10-21T10:00:00', scheduled: true, schedule_count: 1, enabled_schedule_count: 1, }, { id: 2, name: 'Beta deploy', version: 1, stages: 1, created_at: '2026-10-20T09:00:00', updated_at: '2026-10-20T10:00:00', scheduled: false, schedule_count: 0, enabled_schedule_count: 0, }, ]; const RUNS = [ { id: 42, pipeline_id: 1, status: 'succeeded', created_at: '2026-10-21T09:30:00', finished_at: '2026-10-21T09:35:00', executing: false, }, ]; const TEMPLATE = { key: 'release', name: 'Release workflow', description: 'Ship a version', source: 'name: "release"\nstages:\n build:\n type: model\n', scheduled: true, }; function accept(key) { return createHash('sha1') .update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`) .digest('base64'); } function frame(opcode, payload) { const length = payload.length; let header; if (length < 126) { header = Buffer.from([0x80 | opcode, length]); } else if (length < 65536) { header = Buffer.alloc(4); header[0] = 0x80 | opcode; header[1] = 126; header.writeUInt16BE(length, 2); } else { header = Buffer.alloc(10); header[0] = 0x80 | opcode; header[1] = 127; header.writeBigUInt64BE(BigInt(length), 2); } return Buffer.concat([header, payload]); } class Connection { constructor(socket, owner) { this.socket = socket; this.owner = owner; this.buffer = Buffer.alloc(0); this.fragments = []; this.fragmentOpcode = 0; } push(chunk) { if (chunk?.length) this.buffer = Buffer.concat([this.buffer, chunk]); this.parse(); } parse() { while (this.buffer.length >= 2) { const first = this.buffer[0]; const second = this.buffer[1]; const fin = (first & 0x80) !== 0; const opcode = first & 0x0f; const masked = (second & 0x80) !== 0; let length = second & 0x7f; let offset = 2; if (length === 126) { if (this.buffer.length < 4) return; length = this.buffer.readUInt16BE(2); offset = 4; } else if (length === 127) { if (this.buffer.length < 10) return; const big = this.buffer.readBigUInt64BE(2); if (big > BigInt(Number.MAX_SAFE_INTEGER)) return this.close(); length = Number(big); offset = 10; } const maskOffset = offset; if (masked) { if (this.buffer.length < offset + 4) return; offset += 4; } if (this.buffer.length < offset + length) return; let payload = Buffer.from(this.buffer.subarray(offset, offset + length)); if (masked) { const mask = this.buffer.subarray(maskOffset, maskOffset + 4); for (let index = 0; index < payload.length; index++) { payload[index] ^= mask[index % 4]; } } this.buffer = this.buffer.subarray(offset + length); this.handleFrame(fin, opcode, payload); } } handleFrame(fin, opcode, payload) { if (opcode === 0x8) return this.close(); if (opcode === 0x9) return void this.socket.write(frame(0x0a, payload)); if (opcode === 0x0a) return; if (opcode === 0x0) { this.fragments.push(payload); if (fin) { const data = Buffer.concat(this.fragments); this.fragments = []; this.message(this.fragmentOpcode, data); } return; } if (opcode !== 0x1) return; if (!fin) { this.fragmentOpcode = opcode; this.fragments = [payload]; return; } this.message(opcode, payload); } message(_opcode, payload) { let request; try { request = JSON.parse(payload.toString('utf8')); } catch { return; } this.owner.onMessage(this, request); } send(value) { if (this.socket.destroyed) return; this.socket.write(frame(0x1, Buffer.from(JSON.stringify(value)))); } close() { if (!this.socket.destroyed) this.socket.end(); } } async function staticFile(base, pathname, req, res) { let file = resolve(base, `.${pathname}`); if (file !== base && !file.startsWith(base + sep)) { res.writeHead(403).end(); return; } try { if ((await stat(file)).isDirectory()) file = join(file, 'index.html'); } catch { if (extname(pathname)) { res.writeHead(404).end('not found'); return; } file = join(base, 'index.html'); } try { const body = await readFile(file); res .writeHead(200, { 'content-type': MIME[extname(file)] ?? 'application/octet-stream', 'cache-control': 'no-store', }) .end(req.method === 'HEAD' ? undefined : body); } catch { res.writeHead(404).end('not found'); } } async function startFixture(base) { const connections = new Set(); const sockets = new Set(); const state = { connections: 0, subscribed: 0, ping: 0, listPipelines: 0, listRuns: 0, listTemplates: 0, validates: 0, submits: 0, submitsBody: [], }; const created = []; const pipelines = () => [ ...PIPELINES, ...created.map((item) => ({ id: item.id, name: item.name, version: 1, stages: 1, created_at: '2026-10-22T00:00:00', updated_at: '2026-10-22T00:00:00', scheduled: false, schedule_count: 0, enabled_schedule_count: 0, })), ]; const server = createServer(async (req, res) => { const pathname = new URL(req.url, 'http://fixture').pathname; if (pathname === '/fixture/state') { res .writeHead(200, { 'content-type': 'application/json' }) .end(JSON.stringify({ ...state, pipelines: pipelines().length })); return; } if (req.method !== 'GET' && req.method !== 'HEAD') { res.writeHead(405).end(); return; } await staticFile(base, pathname, req, res); }); server.on('upgrade', (req, socket, head) => { const url = new URL(req.url, 'http://fixture'); if ( url.pathname !== '/connect' || req.headers.upgrade?.toLowerCase() !== 'websocket' || !req.headers['sec-websocket-key'] ) { socket.end('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'); return; } socket.write( 'HTTP/1.1 101 Switching Protocols\r\n' + 'Upgrade: websocket\r\n' + 'Connection: Upgrade\r\n' + `Sec-WebSocket-Accept: ${accept(req.headers['sec-websocket-key'])}\r\n\r\n`, ); const connection = new Connection(socket, { onMessage: (client, request) => { if (request.type === 'subscribe') { // Like the real server in this app's probes, the watch stays silent. state.subscribed++; return; } if (request.type === 'unsubscribe') return; if (request.type === 'ping') { state.ping++; client.send({ id: request.id, type: 'pong', build: { version: 'fixture templates build' }, time: '2026-10-22T00:00:00Z', }); return; } if (request.type === 'list_pipelines') { state.listPipelines++; client.send({ id: request.id, type: 'pipelines', pipelines: pipelines() }); return; } if (request.type === 'list_runs') { state.listRuns++; client.send({ id: request.id, type: 'runs', runs: RUNS }); return; } if (request.type === 'list_templates') { state.listTemplates++; client.send({ id: request.id, type: 'templates', templates: [TEMPLATE] }); return; } if (request.type === 'validate') { state.validates++; client.send({ id: request.id, type: 'graph', graph: { stages: [] } }); return; } if (request.type === 'submit') { state.submits++; state.submitsBody.push({ name: request.name, source: request.source, create_only: request.create_only, }); const id = 41 + created.length; const name = String(request.name ?? '').trim() || 'Unnamed'; created.push({ id, name, source: request.source }); client.send({ id: request.id, type: 'pipeline', detail: { id, name }, }); return; } client.send({ id: request.id, type: 'error', error: `Fixture does not implement ${request.type}`, }); }, }); connections.add(connection); state.connections = connections.size; socket.on('data', (chunk) => connection.push(chunk)); socket.on('close', () => { connections.delete(connection); state.connections = connections.size; }); socket.on('error', () => connections.delete(connection)); connection.push(head); }); server.listen(0, '127.0.0.1'); await once(server, 'listening'); server.on('connection', (socket) => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); }); const port = server.address().port; return { server, origin: `http://127.0.0.1:${port}`, created, close: () => new Promise((resolveClose) => { for (const connection of connections) connection.close(); server.close(() => resolveClose()); for (const socket of sockets) socket.destroy(); }), }; } const fixture = await startFixture(await realpathOrSelf(root)); const profile = await mkdtemp(join(tmpdir(), 'templates-wiring-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; }); function realpathOrSelf(path) { return resolve(path); } async function waitFor(check, label, attempts = 180) { 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(JSON.stringify(response)); if (response.result?.exceptionDetails) { throw new Error(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 pathname = () => evaluate('location.pathname'); const assertPath = async (path) => { await waitFor(async () => (await pathname()) === path, `path ${path}`); }; const labelPoint = (label, contains) => 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 }; })()`); const wheel = async (deltaY) => { await send( 'Input.dispatchMouseEvent', { type: 'mouseWheel', x: width / 2, y: height / 2, deltaX: 0, deltaY, }, session, ); await delay(250); }; const clickLabel = async (label, { contains = false } = {}) => { for (let attempt = 0; attempt < 12; attempt++) { const point = await labelPoint(label, contains); if (point && point.y > 4 && point.y < height - 4) { 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(500); return; } // The Settings page scrolls; bring an off-screen row into the viewport. await wheel(point && point.y < 0 ? -350 : 350); } throw new Error( `Could not click ${JSON.stringify(label)}. Labels: ${JSON.stringify(await labels())}`, ); }; const typeIntoFocused = async (value) => { const focused = await evaluate(`(() => { const inputs = [...document.querySelectorAll('input, textarea')].filter((el) => { const r = el.getBoundingClientRect(); return r.width > 0 && r.height > 0; }); const input = inputs[inputs.length - 1]; if (!input) return false; input.focus(); input.select?.(); input.setSelectionRange?.(0, (input.value || '').length); return true; })()`); if (!focused) throw new Error('No semantics input was available to type into'); await delay(300); await send('Input.insertText', { text: value }, session); await delay(400); }; const state = () => evaluate(`fetch('${fixture.origin}/fixture/state').then((r) => r.json())`); 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: `${fixture.origin}/?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('Alpha review'); captures.push(await capture(`${capturePrefix}-pipelines`)); await clickLabel('Settings'); await assertPath('/settings'); await assertBody('Server URL'); await assertBody('Browse templates'); const settingsState = await state(); captures.push(await capture(`${capturePrefix}-settings`)); await clickLabel('Browse templates', { contains: true }); await assertPath('/templates'); await assertBody('Release workflow'); const opened = await state(); if (opened.listTemplates !== 1) { throw new Error(`Templates did not read the catalogue once: ${JSON.stringify(opened)}`); } captures.push(await capture(`${capturePrefix}-gallery`)); await clickLabel('Release workflow', { contains: true }); await assertBody('Definition source'); await assertBody('Create pipeline from this template'); const detail = await state(); if (detail.validates !== 0 || detail.submits !== 0) { throw new Error(`Opening a template sent a write: ${JSON.stringify(detail)}`); } captures.push(await capture(`${capturePrefix}-detail`)); await clickLabel('Create pipeline from this template'); await assertBody('Create from Release workflow'); await typeIntoFocused('Probe release'); await clickLabel('Create'); await assertBody('Created "Probe release". It is in the Pipelines list.'); const createdState = await state(); if ( createdState.validates !== 1 || createdState.submits !== 1 || createdState.submitsBody?.[0]?.create_only !== true || createdState.submitsBody?.[0]?.source !== TEMPLATE.source ) { throw new Error(`The accepted create drifted: ${JSON.stringify(createdState)}`); } if (createdState.listPipelines !== 2) { throw new Error( `The accepted detail did not request exactly one fresh list: ${JSON.stringify(createdState)}`, ); } captures.push(await capture(`${capturePrefix}-created`)); checks.push({ check: 'created detail triggered one fresh list read', state: createdState }); await clickLabel('Close'); await assertBody('Definition source'); // Detail Back returns to the gallery; the page Back restores Settings. await clickLabel(host === 'cupertino' ? 'Back Templates' : 'Back'); await assertBody('Release workflow'); await clickLabel('Back to Settings'); await assertPath('/settings'); await assertBody('Server URL'); await assertBody('Browse templates'); captures.push(await capture(`${capturePrefix}-back-settings`)); const restored = await state(); if (restored.listTemplates !== 1 || restored.submits !== 1) { throw new Error(`Returning to Settings re-read or re-submitted: ${JSON.stringify(restored)}`); } if (restored.ping !== settingsState.ping) { throw new Error(`Settings was remounted instead of preserved: ${JSON.stringify(restored)}`); } checks.push({ check: 'Back restored the same Settings screen', state: restored }); await clickLabel('Pipelines'); await assertPath('/pipelines'); await assertBody('Probe release'); const refreshed = await state(); if (refreshed.listPipelines !== 2 || refreshed.listRuns !== 2) { throw new Error(`Showing Pipelines added a read: ${JSON.stringify(refreshed)}`); } if (refreshed.connections !== 1) { throw new Error(`Templates opened a second socket: ${JSON.stringify(refreshed)}`); } captures.push(await capture(`${capturePrefix}-refreshed-pipelines`)); checks.push({ check: 'Pipelines showed the server row over one socket', state: refreshed }); console.log( JSON.stringify( { ok: true, origin: fixture.origin, host, viewport: { width, height, deviceScaleFactor }, checks, captures, }, null, 2, ), ); } finally { ws?.close(); chrome.kill('SIGTERM'); await Promise.race([once(chrome, 'exit'), delay(3000)]); await fixture.close(); await rm(profile, { recursive: true, force: true }); }