Files
pipelines/app/tool/chat_sessions_probe.mjs
alan b60ad6d98f Remove alternate hosts from tooling probes
Keep the probe suite on the Material composition so browser fixtures and capture drivers no longer render or select Cupertino hosts. Record the final residual legacy arguments and implementation checks for the delivery gate.
2026-11-11 21:38:30 +00:00

335 lines
18 KiB
JavaScript

import { createServer } from 'node:http';
import { readFile } 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 { mkdir, mkdtemp, rm } from 'node:fs/promises';
// Real-browser acceptance for the chat session list's shared actions.
//
// Usage (from app/):
// flutter build web -t tool/chat_sessions_probe_entry.dart \
// --dart-define=CHAT_PROBE_STYLE=material -o build/chat-list-material
// PROBE_WEB_ROOT=$PWD/build/chat-list-material \
// OPENCODE_ARTIFACT_DIR=<dir> node tool/chat_sessions_probe.mjs
//
// The fixture answers the app's real protocol in-process: accepted create,
// rename, star and close calls change the list the next chat_sessions read
// returns, and the title `Refused title` is refused with the server's words.
const root = resolve(process.env.PROBE_WEB_ROOT ?? join(process.cwd(), 'build/web'));
const variant = 'material';
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR ?? join(tmpdir(), 'chat-sessions-capture');
await mkdir(artifactDir, { recursive: true });
const mime = { '.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.wasm': 'application/wasm', '.png': 'image/png', '.ico': 'image/x-icon' };
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-sessions-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; });
const waitFor = async (fn, label = 'condition') => {
for (let i = 0; i < 100; i++) { const value = await fn(); 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 msg = JSON.parse(event.data);
if (msg.method?.includes('detached') || msg.method?.includes('crashed')) console.error('CDP event', JSON.stringify(msg));
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 target = created.result.targetId;
const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).result.sessionId;
await send('Page.enable', {}, session);
await send('Runtime.enable', {}, 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)}`);
return response.result?.result?.value;
};
const load = async (suffix = '') => {
const navigation = await send('Page.navigate', { url: `http://127.0.0.1:${port}/${suffix}` }, session);
if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation));
await delay(3500);
const loaded = await evaluate("({url:location.href, ready:document.readyState, body:document.body.innerText, html:document.body.innerHTML.slice(0,500)})");
if (!loaded?.url?.includes(`127.0.0.1:${port}`)) throw new Error(`Fixture did not load: ${JSON.stringify(loaded)}`);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(2500);
};
await load();
const semantics = async () => evaluate("[...document.querySelectorAll('flt-semantics')].map(x => x.getAttribute('aria-label') || x.innerText || '').filter(Boolean)");
const bodyText = async () => evaluate('document.body.innerText');
const visibleText = async () => `${await bodyText()}\n${(await semantics()).join('\n')}`;
const clickLabel = async label => {
const point = await evaluate(`(() => { const needle = ${JSON.stringify(label)}; const e=[...document.querySelectorAll('flt-semantics')].find(x => (x.getAttribute('aria-label') || x.innerText || '') === needle); if (!e) return null; const r=e.getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+r.height/2}; })()`);
if (!point) return false;
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 true;
};
const click = async (label, what = label) => {
if (!await clickLabel(label)) throw new Error(`Could not click ${what}`);
};
const clickCreateConfirm = async () => {
const point = await evaluate(`(() => {
const dialog = [...document.querySelectorAll('[role="dialog"], [role="alertdialog"]')]
.find(node => node.innerText.includes('Start a chat'));
if (!dialog) return null;
const confirm = [...dialog.querySelectorAll('flt-semantics')]
.find(node => (node.getAttribute('aria-label') || node.innerText || '') === 'New chat');
if (!confirm) return null;
const rect = confirm.getBoundingClientRect();
return {x: rect.x + rect.width / 2, y: rect.y + rect.height / 2};
})()`);
if (!point) throw new Error('Could not find New chat confirm inside the Start a chat dialog');
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);
};
const clickFirst = async label => {
const point = await evaluate(`(() => { const needle = ${JSON.stringify(label)}; const e=[...document.querySelectorAll('flt-semantics')].find(x => (x.getAttribute('aria-label') || x.innerText || '') === needle); if (!e) return null; e.scrollIntoView({block:'center'}); const r=e.getBoundingClientRect(); return {x:r.x+r.width/2,y:r.y+r.height/2}; })()`);
if (!point) throw new Error(`Could not find ${label}`);
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);
};
const waitText = (needle, label = needle) => waitFor(async () => (await visibleText()).includes(needle), label);
const focusInput = async () => {
const focused = await evaluate(`(() => {
const node = document.querySelector('input.flt-text-editing, flt-semantics input, input, textarea');
if (!node) return false;
node.focus();
node.select?.();
node.setSelectionRange?.(0, (node.value || '').length);
return true;
})()`);
if (!focused) throw new Error('No semantics text field available');
await delay(250);
return evaluate(`(() => { const node = document.querySelector('input.flt-text-editing, flt-semantics input, input, textarea'); return node ? node.value : null; })()`);
};
const type = async text => {
await send('Input.insertText', { text }, session);
await delay(400);
};
const resize = async (width, height, scale = 1) => {
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: scale, mobile: width < 600 }, session);
await delay(500);
};
const capture = async (name, width, height, scale = 1) => {
await resize(width, height, scale);
const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data;
await (await import('node:fs/promises')).writeFile(`${artifactDir}/${variant}-${name}.png`, Buffer.from(data, 'base64'));
};
const captures = [];
const shot = async (name, width, height, scale = 1) => {
await capture(name, width, height, scale);
captures.push(`${variant}-${name}.png`);
};
const labels = await semantics();
if (!labels.some(x => x.includes('New chat'))) throw new Error(`Chat fixture semantics unavailable: ${JSON.stringify(labels)}`);
await resize(390, 844);
await shot('list-phone', 390, 844);
await load('?dark=1');
await shot('list-dark-320-2x', 320, 640, 2);
await load();
await resize(390, 844);
// Hide is device-local: the row leaves with an Undo and no server call.
await click('Chat actions for Open fixture', 'the open row actions');
await click('Hide on this device', 'Hide on this device');
await waitText('Hidden on this device', 'the hide Undo');
if ((await visibleText()).includes('Open fixture')) {
throw new Error('The hidden row stayed in the list');
}
await shot('hidden-undo', 390, 844);
await shot('hidden-undo-320-2x', 320, 640, 2);
if (!(await visibleText()).includes('Hidden on this device')) {
throw new Error('The Undo bar left the 320px view');
}
await resize(390, 844);
await click('Undo', 'Undo');
await delay(600);
if (!(await visibleText()).includes('Open fixture')) {
// Flutter web's semantics DOM swallows the coordinate hit for this
// Material button; activate the same rendered node through its semantics
// action instead of pretending the Undo happened.
const activated = await evaluate(`(() => { const e = [...document.querySelectorAll('flt-semantics')].find(x => (x.getAttribute('aria-label') || x.innerText || '') === 'Undo'); e?.click(); return !!e; })()`);
if (!activated) throw new Error('Could not activate Undo');
await delay(600);
}
await waitText('Open fixture', 'the restored row');
await waitText('Open fixture', 'the restored row');
// Dialog chrome at the smallest size the list supports (320 CSS px, 2x DPR).
await click('New chat', 'the New chat action');
await waitText('Start a chat', 'the create dialog at 320');
await waitText('Where should this chat live?', 'the directory picker at 320');
await shot('create-320-2x', 320, 640, 2);
await click('Cancel', 'Cancel the create dialog');
await resize(390, 844);
await click('Chat actions for Open fixture', 'the open row actions');
await click('Rename…', 'Rename…');
await waitText('Rename chat', 'the rename dialog at 320');
await shot('rename-320-2x', 320, 640, 2);
await click('Cancel', 'Cancel the rename dialog');
await resize(390, 844);
// The New marker follows this device's read watermark and never a closed row.
await load('?unseen=1');
await resize(390, 844);
await click('Refresh', 'Refresh');
await waitText('Open fixture', 'the open row');
await waitText('/workspace/demo', 'the open row directory');
const unseenRows = await evaluate(`[...document.querySelectorAll('flt-semantics')].map(node => ({
text: node.getAttribute('aria-label') || node.innerText || '',
rect: (() => { const r = node.getBoundingClientRect(); return {x:r.x, y:r.y, width:r.width, height:r.height}; })(),
})).filter(node => node.text && node.rect.width && node.rect.height)`);
const openRow = unseenRows.find(node => {
const lines = node.text.split('\n');
return lines.includes('Open fixture') && lines.includes('/workspace/demo');
});
const closedRow = unseenRows.find(node => {
const lines = node.text.split('\n');
return lines.includes('Closed fixture') && lines.includes('/workspace/archive');
});
if (!openRow || !openRow.text.split('\n').includes('New')) {
throw new Error('The open row did not report New');
}
if (!closedRow || !closedRow.text.split('\n').includes('Closed') || closedRow.text.split('\n').includes('New')) {
throw new Error('The closed row reported New');
}
await shot('new-marker', 390, 844);
await load();
await resize(390, 844);
// A create reply without `chat_session` uses the web's exact sentence.
await click('New chat', 'the New chat action');
await waitText('Start a chat', 'the create dialog');
await waitText('Where should this chat live?', 'the directory picker');
await clickCreateConfirm();
await waitText(
'The server did not return the new chat session.',
'the missing-session wording',
);
if ((await visibleText()).includes('Bad state')) {
throw new Error('The missing-session refusal leaked a Dart StateError');
}
await shot('create-missing-session', 390, 844);
await click('Cancel', 'Cancel the refused create');
// Create: the dialog sends once and the accepted row arrives from a list read.
await click('New chat', 'the New chat action');
await waitText('Start a chat', 'the create dialog');
await waitText('Where should this chat live?', 'the directory picker');
await clickCreateConfirm();
if (process.env.PROBE_DEBUG) console.log(JSON.stringify({ afterCreate: await bodyText(), labels: await semantics() }, null, 2));
await waitText('New chat', 'the created row');
if ((await visibleText()).includes('no chat harness')) throw new Error('Create did not reach the fixture');
await shot('created-list', 390, 844);
// Rename: the server-assigned fixed title is changed and read back.
await click('Chat actions for New chat', 'the created row actions');
await click('Rename…', 'Rename…');
await waitText('Rename chat', 'the rename dialog');
const seeded = await focusInput();
if (seeded !== 'New chat') throw new Error(`Rename was seeded with ${JSON.stringify(seeded)}`);
await type('Probe renamed');
await click('Save', 'the rename Save');
await waitText('Probe renamed', 'the renamed row');
// The list's own `New chat` action stays on screen, so the old title is
// checked on the row's actions label, which names the row's title.
if ((await semantics()).includes('Chat actions for New chat')) throw new Error('The old title survived the accepted rename');
if (!(await semantics()).includes('Chat actions for Probe renamed')) throw new Error('The renamed row has no actions under its new title');
await shot('renamed-list', 390, 844);
// Star the renamed row (the first unstarred row in server order).
const unstarredBefore = (await semantics()).filter(label => label === 'Unstar chat').length;
await clickFirst('Star chat');
await waitFor(async () => (await semantics()).filter(label => label === 'Unstar chat').length === unstarredBefore + 1, 'the shared star to refresh');
const starredBody = await visibleText();
if (starredBody.indexOf('Probe renamed') > starredBody.indexOf('Streaming fixture')) {
throw new Error('The starred row did not move to the Starred group');
}
await shot('starred-list', 390, 844);
// Close: accepted close changes the row's available action to Reopen.
await click('Chat actions for Probe renamed', 'the starred row actions');
await click('Close chat…', 'Close chat…');
await click('Close chat', 'the close confirmation');
await click('Chat actions for Probe renamed', 'the closed row actions');
if (!(await semantics()).includes('Reopen')) throw new Error('The closed row does not offer Reopen');
await click('Rename…', 'Rename… on the closed row');
// Refusal: the dialog keeps the field and the server's own words.
await waitText('Rename chat', 'the rename dialog');
await focusInput();
await type('Refused title');
await click('Save', 'the refused Save');
await waitText('a title with usable text is required', 'the rename refusal');
const kept = await evaluate(`(() => { const node = document.querySelector('input.flt-text-editing, flt-semantics input, input, textarea'); return node ? node.value : null; })()`);
const keptLabels = await semantics();
if (kept !== 'Refused title' && !keptLabels.some(label => label.includes('Refused title'))) {
throw new Error(`The refusal lost the dialog draft: ${JSON.stringify({ kept, keptLabels })}`);
}
if (!keptLabels.includes('Cancel')) throw new Error('The refusal closed the dialog');
await shot('rename-refusal', 390, 844);
await click('Cancel', 'Cancel after the refusal');
// A refused row action keeps the server's whole reason under its own row on
// both hosts — never one ellipsised line inside a Material subtitle.
await clickFirst('Chat actions for Closed fixture');
await click('Reopen', 'Reopen on the closed fixture');
await waitText('the chat is locked by another operator', 'the row refusal');
if (
!(await visibleText()).includes('refresh the list to see its current state')
) {
throw new Error('The row refusal lost the end of the server reason');
}
await shot('row-refusal', 390, 844);
// Filter to the refused row so the 320px capture keeps the whole message
// clear of the floating action button.
await focusInput();
await type('Closed fixture');
await delay(600);
await shot('row-refusal-320-2x', 320, 640, 2);
console.log(JSON.stringify({ variant, captures, hiddenUndo: true, newMarker: true, missingSessionWording: true, refusalVisible: true, rowRefusal: true, renamedFromServer: true, starredFromServer: true, closedFromServer: true }, 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 });
}