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 Chrome acceptance for composer paste. // // Usage (from app/): // flutter build web -t tool/chat_composer_probe_entry.dart -o build/composer_probe_paste // OPENCODE_ARTIFACT_DIR= node tool/chat_paste_probe.mjs build/composer_probe_paste // // The fixture is served over loopback only. Flutter paints text on a canvas, so // assertions read the semantics tree; the browser also reports whether the // document paste listener called preventDefault. Captures are PNGs. const artifactDir = process.env.OPENCODE_ARTIFACT_DIR; if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the captures'); await mkdir(artifactDir, { recursive: true }); const root = resolve(process.argv[2] ?? join(process.cwd(), 'build/composer_probe_paste')); 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', '.woff': 'font/woff', '.woff2': 'font/woff2', }; 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(), 'chat-paste-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; }); let ws; try { const waitFor = async (fn, what) => { for (let i = 0; i < 120; i++) { const value = await fn(); if (value) return value; await delay(100); } throw new Error(`Timed out waiting for ${what}`); }; 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 msg = JSON.parse(event.data); if (msg.method === 'Runtime.exceptionThrown') { console.error('PAGE EXCEPTION', JSON.stringify(msg.params?.exceptionDetails?.exception?.description ?? msg.params?.exceptionDetails?.text)); } 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 session = (await send('Target.attachToTarget', { targetId: created.result.targetId, flatten: true })).result.sessionId; await send('Page.enable', {}, session); await send('Runtime.enable', {}, session); const navigation = await send('Page.navigate', { url: `http://127.0.0.1:${port}/` }, session); if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation)); 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; }; for (let i = 0; i < 120; i++) { if (await evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))")) break; await delay(100); } await evaluate("document.querySelector('flt-semantics-placeholder')?.click()"); await delay(1800); const labels = async () => evaluate("[...document.querySelectorAll('flt-semantics')].map(x => x.getAttribute('aria-label') || x.innerText || '').filter(Boolean)"); const semanticText = async () => (await labels()).join('\n'); const assertSemantics = async (text, what) => { const read = await semanticText(); if (!read.toLowerCase().replace(/\s+/g, ' ').includes(text.toLowerCase())) { throw new Error(`${what}: semantics lacks ${JSON.stringify(text)}\n${read.slice(0, 2000)}`); } }; const assertMissing = async (text, what) => { const read = await semanticText(); if (read.toLowerCase().replace(/\s+/g, ' ').includes(text.toLowerCase())) { throw new Error(`${what}: semantics still has ${JSON.stringify(text)}\n${read.slice(0, 2000)}`); } }; const editingValue = async () => evaluate(`(() => { const match = n => (n.getAttribute('aria-label') || n.getAttribute('placeholder') || '').includes('Message the agent'); const node = document.querySelector('input.flt-text-editing, textarea.flt-text-editing') || [...document.querySelectorAll('textarea, input')].find(match) || null; return node ? { tag: node.tagName, value: node.value } : null; })()`); const focusField = async () => { const focused = await evaluate(`(() => { const needle = 'Message the agent'; const node = [...document.querySelectorAll('textarea, input')] .find(n => (n.getAttribute('aria-label') || n.placeholder || '').includes(needle)); if (!node) return null; node.focus(); return { tag: node.tagName }; })()`); if (!focused) throw new Error('Composer field was not exposed to semantics'); await delay(500); return focused; }; const dispatchPaste = async ({ text = null, files = [] }) => evaluate(`(() => { const data = new DataTransfer(); if (${JSON.stringify(text)} != null) data.setData('text/plain', ${JSON.stringify(text)}); const specs = ${JSON.stringify(files)}; for (const spec of specs) { const bytes = new Uint8Array(spec.size).fill(7); data.items.add(new File([bytes], spec.name, { type: spec.mime })); } const target = document.activeElement || document.body; const event = new ClipboardEvent('paste', { clipboardData: data, bubbles: true, cancelable: true, }); target.dispatchEvent(event); return event.defaultPrevented; })()`); const type = async text => { await send('Input.insertText', { text }, session); await delay(250); }; const capture = async name => { const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data; const file = `paste-${name}.png`; await writeFile(`${artifactDir}/${file}`, Buffer.from(data, 'base64')); return file; }; const setSize = async (width, height, scale = 1) => { await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: scale, mobile: width < 600, }, session); await delay(400); }; const checks = []; await setSize(390, 844); await assertSemantics('Composer fixture', 'The fixture thread'); await focusField(); await type('keep me'); const textPrevented = await dispatchPaste({ text: ' clipboard words' }); if (textPrevented) throw new Error('A text-only paste was prevented'); checks.push({ check: 'a text-only paste is not intercepted', prevented: textPrevented }); const filePrevented = await dispatchPaste({ files: [{ name: 'shot.png', mime: 'image/png', size: 3 }], }); if (!filePrevented) throw new Error('A file paste was not consumed'); await waitFor(async () => (await semanticText()).includes('shot.png'), 'the pasted chip'); const draftAfterFile = await editingValue(); if (draftAfterFile?.value !== 'keep me') { throw new Error(`The paste changed the draft: ${JSON.stringify(draftAfterFile)}`); } await capture('file-chip'); checks.push({ check: 'a pasted file becomes a chip and leaves text alone', prevented: filePrevented, value: draftAfterFile.value }); const hugePrevented = await dispatchPaste({ files: [{ name: 'huge.bin', mime: 'application/octet-stream', size: 1048577 }], }); await waitFor( async () => (await semanticText()).includes('1 MiB total'), 'the over-limit refusal', ); await assertSemantics('Attachments are too large for web chat (1 MiB total).', 'the over-limit refusal'); await assertMissing('huge.bin', 'the refused file'); await capture('over-limit'); checks.push({ check: 'an over-limit paste shows the refusal and adds no chip', prevented: hugePrevented }); await dispatchPaste({ files: [{ name: '', mime: 'image/png', size: 4 }] }); await waitFor(async () => (await semanticText()).includes('pasted-1.png'), 'the generated name'); await capture('nameless-file'); checks.push({ check: 'a nameless clipboard image takes the web pasted name' }); console.log(JSON.stringify({ checks }, 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 }); }