Files
pipelines/app/tool/run_detail_graph_probe.mjs
T

299 lines
14 KiB
JavaScript

// Real-browser acceptance for the run detail's Graph tab.
//
// Serves the probe build (`flutter build web -t tool/run_detail_graph_probe.dart`)
// on an OS-assigned loopback port and drives the actual RunDetailView over a
// server-shaped fixture. It checks the Graph tab's recorded evidence and legend,
// that an unvisited node does nothing, that a visited stage click selects the
// recorded visit in Timeline, and the exact missing-graph wording. Captures are
// written for both hosts.
//
// PROBE_VARIANT=material|cupertino PROBE_SCHEME=dark|light PROBE_SCALE=1|2 \
// PROBE_WIDTH=1280 PROBE_HEIGHT=900 PROBE_WEB_ROOT=<probe build> \
// OPENCODE_ARTIFACT_DIR=<dir> node tool/run_detail_graph_probe.mjs
import { createServer } from 'node:http';
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, readFile, rm, writeFile } from 'node:fs/promises';
const root = resolve(
process.env.PROBE_WEB_ROOT ??
join(process.cwd(), 'build/probe-run-detail-graph'),
);
const variant = process.env.PROBE_VARIANT ?? 'material';
const scheme = process.env.PROBE_SCHEME ?? 'dark';
const scale = process.env.PROBE_SCALE ?? '1';
const width = Number(process.env.PROBE_WIDTH ?? 1280);
const height = Number(process.env.PROBE_HEIGHT ?? 900);
const chromePath = process.env.CHROME_PATH ??
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR;
if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
await mkdir(artifactDir, { recursive: true });
const prefix = `run-detail-graph-${variant}-${scheme}-${scale}x-${width}x${height}`;
const mime = {
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.otf': 'font/otf',
'.ttf': 'font/ttf',
};
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(), 'run-detail-graph-chrome-'));
const chrome = spawn(chromePath, [
'--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) => {
for (let i = 0; i < 140; i++) {
const value = await fn();
if (value) return value;
await delay(100);
}
throw new Error('Timed out waiting for Chrome DevTools');
};
let ws;
try {
const endpoint = await waitFor(() => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1]);
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?.exception?.description));
}
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 created = await send('Target.createTarget', { url: 'about:blank' });
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);
await send('Emulation.setEmulatedMedia', {
features: [{ name: 'prefers-color-scheme', value: scheme }],
}, session);
await send('Emulation.setDeviceMetricsOverride', {
width, height, deviceScaleFactor: 1, mobile: width < 600,
}, 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 semantics = () => evaluate(`[...document.querySelectorAll('flt-semantics')].map(x => {
const r = x.getBoundingClientRect();
return {
label: (x.getAttribute('aria-label') || (x.innerText || '')).trim(),
selected: x.getAttribute('aria-selected') || x.getAttribute('aria-current'),
tappable: x.hasAttribute('flt-tappable'),
x: r.x, y: r.y, width: r.width, height: r.height,
visible: r.width > 0 && r.height > 0 && r.bottom > 0 && r.top < innerHeight,
};
})`);
const labels = async () => (await semantics()).map((item) => item.label).filter(Boolean);
const bodyText = async () => [await evaluate('document.body.innerText'), ...(await labels())].join('\n');
const waitForText = async (needle, context) => {
for (let attempt = 0; attempt < 60; attempt++) {
if ((await bodyText()).includes(needle)) return;
await delay(250);
}
throw new Error(`${context}: missing ${JSON.stringify(needle)}\n${await bodyText()}`);
};
const clickItem = async (item) => {
const x = item.x + item.width / 2;
const y = item.y + item.height / 2;
await send('Input.dispatchMouseEvent', {
type: 'mousePressed', x, y, button: 'left', clickCount: 1,
}, session);
await send('Input.dispatchMouseEvent', {
type: 'mouseReleased', x, y, button: 'left', clickCount: 1,
}, session);
await delay(1000);
};
const findVisible = async (predicate) => {
const items = (await semantics()).filter((item) => item.visible && predicate(item));
items.sort((a, b) => a.width * a.height - b.width * b.height);
return items[0] ?? null;
};
// A native control's semantics node is the one the engine listens to; a
// synthetic DOM click avoids coordinate ambiguity in a horizontally scrolled
// tab strip and in a panned graph canvas.
const clickSemantics = async (wanted, prefix = false) => {
const clicked = await evaluate(`(() => {
const wanted = ${JSON.stringify(wanted)};
const prefix = ${prefix ? 'true' : 'false'};
const text = (x) => (x.getAttribute('aria-label') || x.innerText || '').trim();
const matches = [...document.querySelectorAll('flt-semantics')]
.filter((x) => x.hasAttribute('flt-tappable'))
.filter((x) => prefix ? text(x).startsWith(wanted) : text(x) === wanted);
if (!matches.length) return false;
matches.sort((a, b) => {
const ra = a.getBoundingClientRect();
const rb = b.getBoundingClientRect();
return ra.width * ra.height - rb.width * rb.height;
});
matches[0].click();
return true;
})()`);
if (!clicked) throw new Error(`Missing tappable ${JSON.stringify(wanted)}: ${JSON.stringify(await labels())}`);
await delay(1000);
};
const clickLabel = (label) => clickSemantics(label);
const clickPrefix = (prefix) => clickSemantics(prefix, true);
const capture = async (name) => {
const data = (await send('Page.captureScreenshot', {
format: 'png', captureBeyondViewport: false,
}, session)).result.data;
const file = `${prefix}-${name}.png`;
await writeFile(join(artifactDir, file), Buffer.from(data, 'base64'));
return file;
};
const evidence = { variant, scheme, scale, width, height, checks: [], captures: [] };
const check = (text, value) => evidence.checks.push({ check: text, value });
const activate = async () => {
await delay(3200);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(900);
};
const navigate = async (query, needle) => {
const navigation = await send('Page.navigate', { url: `http://127.0.0.1:${port}/${query}` }, session);
if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation));
await activate();
await waitForText(needle, 'fixture load');
};
await navigate(
`?platform=${variant}&theme=${scheme === 'dark' ? 1 : 0}&scale=${scale}&case=default`,
'Release verification',
);
check('the real run detail opened over the recorded fixture', true);
await clickLabel('Graph');
await waitForText('Executed', 'graph legend');
await waitForText('Not executed', 'graph legend');
const graphLabels = await labels();
const plan = await findVisible((entry) => entry.label.startsWith('plan') && entry.tappable);
const build = await findVisible((entry) => entry.label.startsWith('build') && entry.tappable);
const audit = await findVisible((entry) => entry.label === 'audit');
if (!plan) throw new Error(`Visited stage node is missing: ${JSON.stringify(graphLabels)}`);
if (audit?.tappable) throw new Error('An unvisited node is a tap target');
if (plan.y + plan.height > height) {
throw new Error(`Graph node falls outside the bounded viewport: ${JSON.stringify({ plan, height })}`);
}
check(
'Graph draws the recorded stages inside the viewport',
`${Math.round(plan.width)}x${Math.round(plan.height)} plan${build ? ', build visible' : ''}`,
);
check('Graph legend shows Executed and Not executed', true);
evidence.captures.push(await capture('graph'));
// An unvisited node is not a control: clicking its label leaves the Graph tab
// exactly as it was and creates no visit. At 320/2x the canvas is panned, so
// the later node may sit outside the visible viewport; the widget test covers
// that case there.
if (audit) {
await clickItem(audit);
await waitForText('Executed', 'graph after unvisited click');
if ((await bodyText()).includes('plan · visit 1')) {
throw new Error('An unvisited node invented a timeline visit');
}
check('an unvisited node does nothing and invents no visit', true);
} else {
check('unvisited node is outside this bounded viewport; widget test covers it', 'skipped');
}
await clickPrefix('plan');
await waitForText('plan · visit 1', 'timeline visit pane after stage click');
await waitForText('Status: completed', 'recorded visit facts');
await waitForText('Timeline', 'timeline tab');
const timeline = await semantics();
const titleCandidates = timeline
.filter((entry) => entry.visible && entry.label.startsWith('plan · visit 1'))
.sort((a, b) => a.width * a.height - b.width * b.height);
if (!titleCandidates.length) {
throw new Error(`Opened visit pane missing: ${JSON.stringify(timeline.map((entry) => entry.label))}`);
}
const paneLabels = timeline.map((entry) => entry.label);
for (const section of ['Input', 'Output', 'Artifacts', 'Session', 'Definition']) {
if (!paneLabels.includes(section)) {
throw new Error(`Visit section ${section} is missing: ${JSON.stringify(paneLabels)}`);
}
}
if ((await bodyText()).includes('audit · visit')) {
throw new Error('A visit was invented for the unvisited stage');
}
check('a stage click opens the recorded visit pane', true);
check('the visit pane offers its recorded sections', true);
// The programmatic switch must reveal its own tab, not leave an offscreen
// selection behind a strip still showing Summary/Graph.
const timelineTab = await findVisible((entry) => entry.label === 'Timeline' && entry.tappable);
if (!timelineTab) throw new Error(`Timeline tab is not visible: ${JSON.stringify(await labels())}`);
if (timelineTab.x < 0 || timelineTab.x + timelineTab.width > width) {
throw new Error(`Selected Timeline tab is clipped: ${JSON.stringify({ timelineTab, width })}`);
}
check('the selected Timeline tab is revealed in the strip', `${Math.round(timelineTab.x)}..${Math.round(timelineTab.x + timelineTab.width)}`);
evidence.openedPane = titleCandidates[0];
evidence.captures.push(await capture('selected-visit'));
await navigate(
`?platform=${variant}&theme=${scheme === 'dark' ? 1 : 0}&scale=${scale}&case=missing`,
'Release verification',
);
await clickLabel('Graph');
await waitForText('This server did not include the run graph.', 'missing graph');
if ((await semantics()).some((entry) => entry.label === 'audit')) {
throw new Error('A run without a graph still drew stage nodes');
}
check('a run without a graph states the exact missing wording', true);
evidence.captures.push(await capture('missing-graph'));
console.log(JSON.stringify(evidence, null, 2));
} finally {
if (ws?.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ id: 2147483647, method: 'Browser.close' }));
}
await Promise.race([once(chrome, 'exit'), delay(3000)]);
ws?.close();
if (chrome.exitCode === null) chrome.kill('SIGTERM');
chrome.stderr.destroy();
chrome.unref();
await new Promise((resolveClose) => {
server.close(resolveClose);
server.closeAllConnections();
});
await rm(profile, { recursive: true, force: true });
}