Files
pipelines/app/tool/graph_canvas_probe.mjs
T

203 lines
12 KiB
JavaScript

// Browser acceptance for the isolated DefinitionGraphView fixture.
//
// Serves the probe build (`flutter build web -t tool/graph_canvas_probe.dart`)
// on an OS-assigned loopback port, drives a real headless Chrome, asserts the
// native node/label semantics, selection identity, measured node rectangles and
// that a real drag pans the stock InteractiveViewer, then writes captures.
//
// PROBE_VARIANT=material|cupertino PROBE_SCHEME=dark|light PROBE_SCALE=1|2 \
// PROBE_WEB_ROOT=<probe build> OPENCODE_ARTIFACT_DIR=<dir> \
// node tool/graph_canvas_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/graph_canvas_probe'));
const variant = process.env.PROBE_VARIANT ?? 'material';
const scheme = process.env.PROBE_SCHEME ?? 'dark';
const scale = process.env.PROBE_SCALE ?? '1';
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR ?? join(tmpdir(), 'graph-canvas-capture');
const prefix = scheme === 'light' ? 'light-' : '';
const longKey = 'implementer-with-an-unusually-long-stage-label';
const nodes = ['plan', 'build', longKey, 'audit', 'Success', 'Failure'];
const edgeLabels = ['success', 'error*', 'retry', 'blocked'];
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(), 'graph-canvas-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 => {
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 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 msg = JSON.parse(event.data);
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' });
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);
const url = `http://127.0.0.1:${port}/?style=${variant}&scale=${scale}`;
const navigation = await send('Page.navigate', { url }, session);
if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation));
await send('Emulation.setDeviceMetricsOverride', { width: 1280, height: 800, deviceScaleFactor: 1, mobile: false }, 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;
};
await delay(3500);
const loaded = await evaluate('({url:location.href, ready:document.readyState})');
if (!loaded?.url.includes(`127.0.0.1:${port}/?style=${variant}&scale=${scale}`)) throw new Error(`Fixture did not load: ${JSON.stringify(loaded)}`);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(2500);
const semantics = () => evaluate("[...document.querySelectorAll('flt-semantics')].map(x=>({label:(x.getAttribute('aria-label')||x.innerText||'').trim(), selected:x.getAttribute('aria-current')}))");
const nodeAt = label => `[...document.querySelectorAll('flt-semantics')].find(x => (x.getAttribute('aria-label')||x.innerText||'').trim() === ${JSON.stringify(label)})`;
const body = await evaluate('document.body.innerText');
if (!body.includes('Selected: build')) throw new Error(`Initial selection missing: ${body}`);
const initial = await semantics();
// Flutter web maps Semantics(selected:) to aria-current, not aria-selected.
const current = initial.find(x => x.selected === 'true');
if (!current?.label.startsWith('build')) throw new Error(`Selected node is not announced: ${JSON.stringify(initial)}`);
const nodeRect = async label => evaluate(`(() => { const e=${nodeAt(label)}; if (!e) return null; const r=e.getBoundingClientRect(); return {x:r.x,y:r.y,width:r.width,height:r.height}; })()`);
const rects = {};
for (const label of nodes) {
const rect = await nodeRect(label);
if (rect?.width > 0 && rect?.height > 0) rects[label] = rect;
}
// Measured native nodes occupy separate rectangles: no fixed-size overlap.
const visibleNodes = Object.keys(rects);
for (let a = 0; a < visibleNodes.length; a++) {
for (let b = a + 1; b < visibleNodes.length; b++) {
const one = rects[visibleNodes[a]], two = rects[visibleNodes[b]];
const overlap = one.x < two.x + two.width && two.x < one.x + one.width &&
one.y < two.y + two.height && two.y < one.y + one.height;
if (overlap) throw new Error(`Nodes overlap: ${visibleNodes[a]} ${JSON.stringify(one)} vs ${visibleNodes[b]} ${JSON.stringify(two)}`);
}
}
if (!(rects[longKey].height > rects.plan.height || rects[longKey].width > rects.plan.width)) {
throw new Error(`Long key did not determine its own native extent: ${JSON.stringify(rects)}`);
}
const clickLabel = async label => {
const point = await evaluate(`(() => { const e=${nodeAt(label)}; 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);
return true;
};
const capture = async (name, width, height) => {
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: width < 600 }, session);
await delay(2500);
const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data;
const file = `${prefix}${variant}-graph-${scale}x-${name}.png`;
await writeFile(`${artifactDir}/${file}`, Buffer.from(data, 'base64'));
return file;
};
const captures = [];
captures.push(await capture('desktop', 1280, 800));
captures.push(await capture('phone', 390, 844));
if (!await clickLabel('plan')) throw new Error('Could not click plan node');
await delay(800);
const afterPlan = await evaluate('document.body.innerText');
if (!afterPlan.includes('Selected: plan')) throw new Error(`plan callback did not carry its key: ${afterPlan}`);
const planCurrent = (await semantics()).find(x => x.selected === 'true');
if (planCurrent?.label !== 'plan') throw new Error(`plan is not announced selected: ${JSON.stringify(planCurrent)}`);
captures.push(await capture('selected-plan', 1280, 800));
const seen = new Set();
const drag = async (start, dx, dy = 0) => {
await send('Input.dispatchMouseEvent', {type:'mousePressed', ...start, button:'left', buttons:1, clickCount:1}, session);
for (let step=1; step<=5; step++) {
await send('Input.dispatchMouseEvent', {type:'mouseMoved', x:start.x+dx*step/5, y:start.y+dy*step/5, button:'left', buttons:1}, session);
await delay(40);
}
await send('Input.dispatchMouseEvent', {type:'mouseReleased', x:start.x+dx, y:start.y+dy, button:'left', clickCount:1}, session);
await delay(800);
};
// Native content-sized graphs need not fit on one screen. Verify offscreen
// nodes through actual pan gestures rather than assuming fixed node boxes.
for (let pass=0; pass<10; pass++) {
const labels = (await semantics()).map(item => item.label);
for (const needle of [...nodes, ...edgeLabels]) {
if (labels.some(label => label.includes(needle))) seen.add(needle);
}
if ([...nodes, ...edgeLabels].every(label => seen.has(label)) &&
(await nodeRect('Success'))?.width > 0) break;
await drag({x:900,y:300}, -500);
}
for (const label of [...nodes, ...edgeLabels]) {
if (!seen.has(label)) throw new Error(`Graph pan never revealed ${label}`);
}
if (!await clickLabel('Success')) throw new Error('Could not click Success terminal');
await delay(800);
const afterTerminal = await evaluate('document.body.innerText');
if (!afterTerminal.includes('Selected: #success')) throw new Error(`terminal callback did not carry its key: ${afterTerminal}`);
captures.push(await capture('selected-terminal',1280,800));
await send('Page.reload', {}, session);
await delay(2500);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(1500);
// A real drag on a phone viewport, where the measured graph is wider than
// the viewport, must pan the stock InteractiveViewer: the node moves.
await send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true }, session);
await delay(1500);
const before = await nodeRect('plan');
const start = { x: before.x + before.width / 2, y: before.y + before.height / 2 };
await drag(start, -40, -10);
const after = await nodeRect('plan');
if (!after || (Math.abs(after.x - before.x) < 20 && Math.abs(after.y - before.y) < 20)) {
throw new Error(`Drag did not pan the graph: ${JSON.stringify({ before, after })}`);
}
captures.push(await capture('panned-phone', 390, 844));
console.log(JSON.stringify({ variant, scheme, scale, rects, captures }, 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(resolve => {
server.close(resolve);
server.closeAllConnections();
});
await rm(profile, { recursive: true, force: true });
}