401 lines
15 KiB
JavaScript
401 lines
15 KiB
JavaScript
// Real Chrome evidence for the registered Search entry and /search route.
|
|
//
|
|
// Build the normal app, then run from app/:
|
|
// flutter build web -o build/search_wiring_web
|
|
// OPENCODE_ARTIFACT_DIR=<dir> node tool/search_wiring_probe.mjs \
|
|
// build/search_wiring_web
|
|
//
|
|
// The fixture is the same isolated websocket server the Runs probe uses, with a
|
|
// deterministic `search` branch; no production server or operator data is used.
|
|
import { spawn } from 'node:child_process';
|
|
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
|
|
import { once } from 'node:events';
|
|
import { tmpdir } from 'node:os';
|
|
import { join, resolve } from 'node:path';
|
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
|
|
import { startFixture } from './runs_probe_server.mjs';
|
|
|
|
const output = process.env.OPENCODE_ARTIFACT_DIR;
|
|
if (!output) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
|
|
await mkdir(output, { recursive: true });
|
|
|
|
const root = resolve(process.argv[2] ?? 'build/search_wiring_web');
|
|
const fixture = await startFixture({ root });
|
|
const result = { captures: [], evidence: {} };
|
|
|
|
const profile = await mkdtemp(join(tmpdir(), 'x1-wire-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}`);
|
|
}
|
|
|
|
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));
|
|
}
|
|
if (message.method === 'Runtime.consoleAPICalled') {
|
|
const text = (message.params?.args ?? [])
|
|
.map((arg) => arg.value ?? arg.description ?? '')
|
|
.join(' ');
|
|
if (text.includes('X1FOCUS')) console.error(text);
|
|
}
|
|
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);
|
|
// A headless target can otherwise keep the page unfocused, which would make
|
|
// the browser refuse the field's autofocus request rather than test the app.
|
|
await send('Page.bringToFront', {}, 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 enableSemantics = async () => {
|
|
await waitFor(
|
|
() => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),
|
|
'Flutter semantics placeholder',
|
|
);
|
|
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
|
|
await delay(600);
|
|
};
|
|
|
|
const text = () =>
|
|
evaluate(`(() => {
|
|
const chunks = [];
|
|
for (const el of document.querySelectorAll('flt-semantics, [aria-label], [aria-valuetext]')) {
|
|
const label = el.getAttribute('aria-label');
|
|
if (label) chunks.push(label);
|
|
if (!el.children.length && el.innerText) chunks.push(el.innerText);
|
|
}
|
|
if (document.body?.innerText) chunks.push(document.body.innerText);
|
|
return [...new Set(chunks)].join('\\n');
|
|
})()`);
|
|
|
|
const assertBody = (needle) =>
|
|
waitFor(async () => (await text()).includes(needle), JSON.stringify(needle));
|
|
|
|
const path = () => evaluate('location.pathname');
|
|
|
|
const clickSemantics = (needle) =>
|
|
evaluate(`(() => {
|
|
const wanted = ${JSON.stringify(needle)};
|
|
const nodes = [...document.querySelectorAll('flt-semantics')];
|
|
const candidates = nodes.filter((el) => {
|
|
const label = (el.getAttribute('aria-label') || el.innerText || '').trim();
|
|
return label === wanted || label.includes(wanted);
|
|
});
|
|
const interactive = candidates.find((el) =>
|
|
el.hasAttribute('flt-tappable') && ['button', 'link', 'menuitem', 'tab'].includes(el.getAttribute('role')));
|
|
const node = interactive || candidates.find((el) => el.hasAttribute('flt-tappable')) || candidates.at(-1);
|
|
if (!node) return { clicked: false, candidates: [] };
|
|
node.click();
|
|
return { clicked: true, candidates: candidates.map((el) => ({
|
|
label: (el.getAttribute('aria-label') || el.innerText || '').trim().slice(0, 120),
|
|
role: el.getAttribute('role'), tappable: el.hasAttribute('flt-tappable'),
|
|
})) };
|
|
})()`);
|
|
|
|
const clickAction = async (label) => {
|
|
// A real pointer event keeps user activation and browser focus, unlike a
|
|
// synthetic el.click() on the semantics node.
|
|
const centre = await evaluate(`(() => {
|
|
const wanted = ${JSON.stringify(label)};
|
|
const nodes = [...document.querySelectorAll('flt-semantics')]
|
|
.filter((el) => ['button', 'link', 'menuitem', 'tab'].includes(el.getAttribute('role')));
|
|
const node = nodes.find((el) =>
|
|
(el.getAttribute('aria-label') || el.innerText || '').trim() === wanted);
|
|
if (!node) return null;
|
|
const rect = node.getBoundingClientRect();
|
|
return { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
|
|
})()`);
|
|
if (!centre) {
|
|
const labels = await evaluate(`[...document.querySelectorAll('flt-semantics')]
|
|
.filter((el) => ['button', 'link', 'menuitem', 'tab'].includes(el.getAttribute('role')))
|
|
.map((el) => (el.getAttribute('aria-label') || el.innerText || '').trim())`);
|
|
throw new Error(`Could not click ${JSON.stringify(label)}: ${JSON.stringify(labels)}`);
|
|
}
|
|
await send(
|
|
'Input.dispatchMouseEvent',
|
|
{ type: 'mousePressed', x: centre.x, y: centre.y, button: 'left', clickCount: 1 },
|
|
session,
|
|
);
|
|
await send(
|
|
'Input.dispatchMouseEvent',
|
|
{ type: 'mouseReleased', x: centre.x, y: centre.y, button: 'left', clickCount: 1 },
|
|
session,
|
|
);
|
|
await delay(350);
|
|
};
|
|
|
|
const setSize = async (width, height, mobile) => {
|
|
await send(
|
|
'Emulation.setDeviceMetricsOverride',
|
|
{ width, height, deviceScaleFactor: 1, mobile },
|
|
session,
|
|
);
|
|
await delay(500);
|
|
};
|
|
|
|
const capture = async (name) => {
|
|
const data = (
|
|
await send(
|
|
'Page.captureScreenshot',
|
|
{ format: 'png', captureBeyondViewport: false },
|
|
session,
|
|
)
|
|
).result.data;
|
|
const file = join(output, `${name}.png`);
|
|
await writeFile(file, Buffer.from(data, 'base64'));
|
|
result.captures.push(file);
|
|
};
|
|
|
|
const open = async (url, { width, height, mobile }) => {
|
|
await setSize(width, height, mobile);
|
|
const navigation = await send('Page.navigate', { url }, session);
|
|
if (navigation.error || navigation.result?.errorText) {
|
|
throw new Error(JSON.stringify(navigation));
|
|
}
|
|
await enableSemantics();
|
|
};
|
|
|
|
const fieldFocused = () =>
|
|
evaluate(`(() => {
|
|
const active = document.activeElement;
|
|
const isField = active && (active.tagName === 'INPUT' || active.tagName === 'TEXTAREA');
|
|
const node = isField ? active : document.querySelector('input.flt-text-editing, textarea.flt-text-editing, input, textarea');
|
|
if (!node) return { found: false, focused: false };
|
|
return { found: true, focused: document.activeElement === node, tag: node.tagName };
|
|
})()`);
|
|
|
|
const typeQuery = async (value) => {
|
|
const field = await fieldFocused();
|
|
if (!field.focused) {
|
|
const focused = await evaluate(`(() => {
|
|
const nodes = [...document.querySelectorAll('input.flt-text-editing, textarea.flt-text-editing, input, textarea')];
|
|
const node = nodes.find((el) => el.checkVisibility?.() ?? true) || nodes.at(-1);
|
|
if (!node) return false; node.focus(); return true;
|
|
})()`);
|
|
if (!focused) throw new Error('Search field not exposed as a semantics input');
|
|
await delay(300);
|
|
}
|
|
const inserted = await send('Input.insertText', { text: value }, session);
|
|
if (inserted.error) throw new Error(JSON.stringify(inserted));
|
|
await delay(1400);
|
|
return field;
|
|
};
|
|
|
|
const fixtureState = async () =>
|
|
(await fetch(`${fixture.url}/fixture/state`)).json();
|
|
|
|
// Search mounts its screen after the native page transition so autofocus is
|
|
// not swallowed; wait for the real field before typing.
|
|
const openSearch = async () => {
|
|
await clickAction('Search');
|
|
await waitFor(async () => (await path()) === '/search', 'the /search route');
|
|
await waitFor(async () => (await fieldFocused()).focused, 'the focused search field');
|
|
};
|
|
|
|
const stateBefore = await fixtureState();
|
|
|
|
// Material host (a Linux browser UA picks the Material composition).
|
|
await send(
|
|
'Emulation.setUserAgentOverride',
|
|
{
|
|
userAgent:
|
|
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' +
|
|
'(KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
|
|
platform: 'Linux x86_64',
|
|
},
|
|
session,
|
|
);
|
|
await open(`${fixture.url}/runs`, { width: 390, height: 844, mobile: true });
|
|
await assertBody('Fixture live run');
|
|
if ((await path()) !== '/runs') throw new Error(`Boot moved to ${await path()}`);
|
|
const listRunsBefore = (await fixtureState()).listRuns;
|
|
|
|
await openSearch();
|
|
result.evidence.materialFocus = await fieldFocused();
|
|
|
|
await typeQuery('fixture');
|
|
await assertBody('run #1 · Fixture live run');
|
|
result.evidence.materialResults = (await text()).includes('Fixture pipeline');
|
|
const afterSearch = await fixtureState();
|
|
const request = afterSearch.searchRequests.at(-1);
|
|
if (JSON.stringify(request) !== JSON.stringify({ query: 'fixture', kind: '', limit: 0 })) {
|
|
throw new Error(`Search request mismatch: ${JSON.stringify(request)}`);
|
|
}
|
|
result.evidence.searchRequest = request;
|
|
if (afterSearch.connections !== 1) {
|
|
throw new Error(`Search opened another socket: ${JSON.stringify(afterSearch)}`);
|
|
}
|
|
await capture('x1-wire-material-search-phone');
|
|
|
|
// A run hit opens through the existing run route and detail.
|
|
const openedHit = await clickSemantics('run #1 · Fixture live run');
|
|
if (!openedHit.clicked) throw new Error('Could not open the run result');
|
|
await waitFor(async () => (await path()) === '/runs/1', 'the run route from search');
|
|
await assertBody('Original prompt');
|
|
await capture('x1-wire-material-run-from-search-phone');
|
|
|
|
// Escape closes Search back onto the same detail without re-reading it.
|
|
const getRunBeforeEscape = (await fixtureState()).getRun;
|
|
await openSearch();
|
|
const composeFocus = await fieldFocused();
|
|
if (!composeFocus.focused) throw new Error('Search did not focus from the run detail');
|
|
await send(
|
|
'Input.dispatchKeyEvent',
|
|
{ type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 },
|
|
session,
|
|
);
|
|
await send(
|
|
'Input.dispatchKeyEvent',
|
|
{ type: 'keyUp', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 },
|
|
session,
|
|
);
|
|
await waitFor(async () => (await path()) === '/runs/1', 'Escape back to the run');
|
|
await assertBody('Original prompt');
|
|
const afterEscape = await fixtureState();
|
|
if (afterEscape.getRun !== getRunBeforeEscape) {
|
|
throw new Error('Escape re-read the retained run detail');
|
|
}
|
|
result.evidence.escapeRestored = { path: await path(), getRun: afterEscape.getRun };
|
|
await capture('x1-wire-material-escape-restored-phone');
|
|
|
|
await setSize(1280, 800, false);
|
|
await openSearch();
|
|
await typeQuery('fixture');
|
|
await assertBody('run #1 · Fixture live run');
|
|
await capture('x1-wire-material-search-desktop');
|
|
|
|
// Cupertino host: the native navigation-bar action and system Back.
|
|
await send(
|
|
'Emulation.setUserAgentOverride',
|
|
{
|
|
userAgent:
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 ' +
|
|
'(KHTML, like Gecko) Version/17.0 Safari/605.1.15',
|
|
platform: 'MacIntel',
|
|
},
|
|
session,
|
|
);
|
|
await open(`${fixture.url}/runs`, { width: 390, height: 844, mobile: true });
|
|
await assertBody('Fixture live run');
|
|
const cupertinoListBefore = (await fixtureState()).listRuns;
|
|
|
|
await openSearch();
|
|
result.evidence.cupertinoFocus = await fieldFocused();
|
|
await typeQuery('fixture');
|
|
await assertBody('run #1 · Fixture live run');
|
|
await capture('x1-wire-cupertino-search-phone');
|
|
|
|
// Browser Back is the shell's own history: the loaded list must still be
|
|
// there rather than being re-read or replaced.
|
|
await evaluate('history.back()');
|
|
await waitFor(async () => (await path()) === '/runs', 'browser Back to /runs');
|
|
// Wait for the transient page to finish leaving, not just for the URL.
|
|
await waitFor(
|
|
async () => !(await text()).includes('Kind: All kinds'),
|
|
'the search page to close',
|
|
);
|
|
await assertBody('Fixture live run');
|
|
const cupertinoListAfter = (await fixtureState()).listRuns;
|
|
if (cupertinoListAfter !== cupertinoListBefore) {
|
|
throw new Error(
|
|
`Back re-read the retained list: ${cupertinoListBefore} -> ${cupertinoListAfter}`,
|
|
);
|
|
}
|
|
result.evidence.cupertinoBack = {
|
|
path: await path(),
|
|
listRuns: cupertinoListAfter,
|
|
};
|
|
await capture('x1-wire-cupertino-back-restored-phone');
|
|
|
|
await setSize(1280, 800, false);
|
|
await openSearch();
|
|
await typeQuery('fixture');
|
|
await assertBody('run #1 · Fixture live run');
|
|
await capture('x1-wire-cupertino-search-desktop');
|
|
|
|
const finalState = await fixtureState();
|
|
if (finalState.subscribed < 2) {
|
|
throw new Error(`Expected the shared subscription on both boots: ${JSON.stringify(finalState)}`);
|
|
}
|
|
if (stateBefore.connections !== 0) {
|
|
throw new Error('The isolated fixture already had connections');
|
|
}
|
|
result.evidence.finalState = finalState;
|
|
console.log(JSON.stringify(result, 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 });
|
|
}
|