import { createServer } from 'node:http';
import { readFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { spawn, execFile } from 'node:child_process';
import { promisify } from 'node:util';
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 check that the composer regains focus (caret and selection
// kept) when the browser window comes back, and stays unfocused when it was
// not focused before. Window switching is reproduced by the same DOM events
// the browser sends: the editing element's blur (relatedTarget null), then
// window blur, later window focus.
//
// Usage (from app/), with the composer probe build of chat_composer_probe.mjs:
// OPENCODE_ARTIFACT_DIR=
node tool/composer_focus_restore_probe.mjs build/composer_probe_material
//
// The fixture is served over loopback only. Flutter paints text on a canvas, so
// assertions read the semantics tree (aria-label/innerText) and the focused
// editing element; captures are PNGs for visual inspection.
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_material'));
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-composer-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?.includes('detached') || msg.method?.includes('crashed')) {
console.error('CDP event', JSON.stringify(msg));
}
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;
};
// Wait for the app, but leave semantics off: that is the operator's normal
// mode, where the engine's own editing element (and its blur handling)
// backs the field.
for (let i = 0; i < 120; i++) {
if (await evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))")) break;
await delay(100);
}
await delay(1800);
const click = async (x, y) => {
for (const type of ['mousePressed', 'mouseReleased']) {
await send('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1 }, session);
}
};
// The composer sits at the bottom; click upward until an editing element
// takes focus.
const focusField = async () => {
for (let y = 790; y > 500; y -= 10) {
await click(640, y);
await delay(150);
if ((await state()).editing) return y;
}
throw new Error('No click focused the composer');
};
const type = async text => { await send('Input.insertText', { text }, session); await delay(250); };
const setSize = async (width, height, scale = 1) => {
await send('Emulation.setDeviceMetricsOverride', {
width, height, deviceScaleFactor: scale, mobile: width < 600,
}, session);
await delay(400);
};
const state = () => evaluate(`(() => {
const a = document.activeElement;
// The composer is the fixture's only text field, so a focused editing
// element (engine textarea or semantics textarea) is the composer.
const editing = Boolean(a && (a.tagName === 'TEXTAREA' || a.tagName === 'INPUT'));
return { editing, value: editing ? a.value : null,
start: editing ? a.selectionStart : null, end: editing ? a.selectionEnd : null };
})()`);
const switchWindow = async () => {
// Two evaluations, as two event dispatches in the browser: microtasks
// (the framework handling the engine's closed text connection) run in
// between, so Flutter has already unfocused the field before the window
// blur reaches it. One script doing both would hide that.
await evaluate('document.activeElement?.blur()');
await evaluate("window.dispatchEvent(new FocusEvent('blur'))");
await delay(600);
const away = await state();
await evaluate("window.dispatchEvent(new FocusEvent('focus'))");
await delay(800);
return { away, back: await state() };
};
const failures = [];
const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) failures.push(what); };
await setSize(1280, 800);
console.log('composer at y', await focusField());
await type('hello world');
await evaluate(`(() => { const a = document.activeElement; a.setSelectionRange(2, 5); a.dispatchEvent(new Event('selectionchange')); document.dispatchEvent(new Event('selectionchange')); })()`);
await delay(400);
const before = await state();
console.log('before', JSON.stringify(before));
const focused = await switchWindow();
console.log('focused case', JSON.stringify(focused));
check(!focused.away.editing, 'composer loses DOM focus while the window is away');
check(focused.back.editing && focused.back.value === 'hello world', 'composer regains focus with its text');
check(focused.back.start === before.start && focused.back.end === before.end, `selection kept (${before.start}-${before.end})`);
await send('Page.captureScreenshot', { format: 'png' }, session).then(r =>
writeFile(join(artifactDir, 'focus-restored.png'), Buffer.from(r.result.data, 'base64')));
// Control: composer not focused when the window leaves.
// The operator clicks the transcript (tap outside unfocuses the field),
// then switches windows a moment later.
await click(640, 200);
await delay(1000);
console.log('after click away', JSON.stringify(await state()));
const unfocused = await switchWindow();
console.log('unfocused case', JSON.stringify(unfocused));
check(!unfocused.back.editing, 'composer stays unfocused when it was not focused');
if (failures.length) throw new Error(`Failed: ${failures.join('; ')}`);
console.log('composer focus restore probe passed');
} finally {
ws?.close();
chrome.kill('SIGTERM');
await Promise.race([once(chrome, 'exit'), delay(3000)]);
server.close();
await rm(profile, { recursive: true, force: true });
}