Files
pipelines/app/tool/chat_mode_probe.mjs
T

83 lines
4.3 KiB
JavaScript

// Switches an open chat's permission mode in a real browser against a loopback
// fixture, never operator data.
// PROBE_WIDTH=1280 PROBE_HOST=material|cupertino node tool/chat_mode_probe.mjs <web-build> <out>
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { startFixture } from './runs_probe_server.mjs';
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
import { axNodes, enableSemantics, waitFor, capture } from './pipelines_probe_cdp_input.mjs';
const width = Number(process.env.PROBE_WIDTH ?? 1280);
const host = process.env.PROBE_HOST ?? 'material';
const output = resolve(process.argv[3] ?? 'build/chat-mode-probe');
await mkdir(output, { recursive: true });
const messages = [
{ id: 'u0', role: 'user', parts: [{ id: 'p0', type: 'text', text: 'Switch this chat to auto mode.' }] },
{ id: 'a0', role: 'assistant', parts: [{ id: 'p1', type: 'text', text: 'Recorded fixture reply.' }] },
];
const fixture = await startFixture({ root: resolve(process.argv[2]), chatMessages: messages });
const profile = await mkdtemp(join(tmpdir(), 'chat-mode-'));
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', profile, 'about:blank');
const log = { width, host, checks: [], captures: [] };
let c;
const named = async (pattern) => (await axNodes(c)).filter((n) => pattern.test(n.name?.value ?? ''));
const press = async (node) => {
const { model } = await c.send('DOM.getBoxModel', { backendNodeId: node.backendDOMNodeId });
const [x1, y1, , , x3, y3] = model.content;
for (const type of ['mousePressed', 'mouseReleased']) {
await c.send('Input.dispatchMouseEvent', { type, x: (x1 + x3) / 2, y: (y1 + y3) / 2, button: 'left', clickCount: 1 });
}
};
const first = async (pattern, what) => {
let found;
assert.ok(await waitFor(async () => (found = (await named(pattern)).find((n) => n.backendDOMNodeId)), 30000), what);
return found;
};
try {
c = await connect((await waitForPage(await waitForPort(join(profile, 'DevToolsActivePort')))).webSocketDebuggerUrl);
await c.send('Page.enable');
if (host === 'cupertino') {
await c.send('Emulation.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15', platform: 'MacIntel',
});
} else {
await c.send('Emulation.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36', platform: 'Win32',
});
}
await c.send('Emulation.setDeviceMetricsOverride', { width, height: 800, deviceScaleFactor: 1, mobile: false });
await c.send('Page.navigate', { url: fixture.url + '/chat/ses_probechat' });
assert.ok(await waitFor(() => c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 30000));
await enableSemantics(c);
await first(/Change mode\b.*Ask for permission/, 'mode control shows the row mode');
await sleep(800);
await capture(c, output, log, `${host}-mode-default`);
log.checks.push('bar shows the stored/default mode before any chat_modes request');
await press(await first(/Change mode\b/, 'mode control'));
await first(/^Auto/, 'Auto option');
await sleep(600);
await capture(c, output, log, `${host}-mode-picker`);
await press(await first(/^Auto/, 'Auto option'));
await first(/Change mode\b.*Auto/, 'bar shows Auto');
await sleep(600);
await capture(c, output, log, `${host}-mode-auto`);
const state = await (await fetch(fixture.url + '/fixture/state')).json();
assert.deepEqual(state.chatModeRequests, [{ session_id: 'ses_probechat', mode: 'auto' }]);
log.checks.push('choosing Auto sent chat_mode and the bar shows Auto');
console.log(JSON.stringify(log, null, 2));
} catch (error) {
if (c) {
await capture(c, output, log, 'failure');
await writeFile(join(output, 'failure-ax.json'), JSON.stringify(await axNodes(c), null, 2));
}
throw error;
} finally {
await writeFile(join(output, `${host}-result.json`), JSON.stringify(log, null, 2));
c?.close(); const exited = once(chrome, 'exit'); chrome.kill(); await exited;
await fixture.close();
}