Files
pipelines/app/tool/diff_wiring_probe.mjs
T

499 lines
21 KiB
JavaScript

// Real-browser evidence for the run-detail Changes action wiring.
//
// Usage (from app/):
// flutter build web --target tool/diff_wiring_probe.dart --output build/probe-wire
// node tool/diff_wiring_probe.mjs --root build/probe-wire --out DIR
//
// The fixture serves the actual RunDetailView over a fake gateway, then drives
// headless Chrome over loopback: the native Changes button, the recorded file,
// its diff, Close diff, Back, the failed-run evidence without a run-wide
// summary, the absent action while executing, and the Cupertino raw/dark
// surfaces. No server or operator run is touched.
import { createServer } from 'node:http';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { spawn } from 'node:child_process';
import { once } from 'node:events';
import { setTimeout as delay } from 'node:timers/promises';
import { extname, join, resolve, sep } from 'node:path';
import { tmpdir } from 'node:os';
const args = new Map();
for (let i = 2; i < process.argv.length; i++) {
const value = process.argv[i];
if (value.startsWith('--')) args.set(value.slice(2), process.argv[++i]);
}
const root = resolve(args.get('root') ?? 'build/probe-wire');
const out = resolve(args.get('out') ?? 'build/diff-wiring-probe');
const chromePath =
process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
await mkdir(out, { recursive: true });
const mime = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.wasm': 'application/wasm',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.ttf': 'font/ttf',
'.otf': 'font/otf',
'.woff': 'font/woff',
'.woff2': 'font/woff2',
'.bin': 'application/octet-stream',
'.frag': 'application/octet-stream',
};
const server = createServer(async (req, res) => {
try {
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
let filePath = resolve(root, '.' + pathname);
if (filePath !== root && !filePath.startsWith(root + sep)) {
res.writeHead(403).end();
return;
}
if (pathname === '/' || pathname.endsWith('/')) filePath = join(filePath, 'index.html');
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 origin = `http://127.0.0.1:${server.address().port}`;
const profile = await mkdtemp(join(tmpdir(), 'd1-wire-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, what) => {
for (let i = 0; i < 120; i++) {
const value = await fn();
if (value) return value;
await delay(100);
}
throw new Error(`Timed out waiting for ${what}: ${stderr.slice(-1500)}`);
};
const file = 'lib/features/runs/ui/run_changes_action.dart';
const rowPrefix = `Open diff for ${file}`;
const evidence = { origin, captures: [], checks: [] };
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.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' });
if (!created.result) throw new Error(`Target creation failed: ${JSON.stringify(created)}`);
const session = (await send('Target.attachToTarget', {
targetId: created.result.targetId,
flatten: true,
})).result.sessionId;
await send('Page.enable', {}, session);
await send('Runtime.enable', {}, session);
await send('Emulation.setDeviceMetricsOverride', {
width: 390, height: 844, deviceScaleFactor: 1, mobile: true,
}, 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 labels = () =>
evaluate(
"[...document.querySelectorAll('flt-semantics')].map(x => x.getAttribute('aria-label') || (x.innerText || '').trim()).filter(Boolean)",
);
const bodyText = () => evaluate('document.body.innerText');
const hasLabel = async (label) => (await labels()).includes(label);
const hasPrefix = async (prefix) =>
(await labels()).some((label) => label.startsWith(prefix));
const clickExact = async (label) => {
const clicked = await evaluate(`(() => {
const wanted = ${JSON.stringify(label)};
const nodes = [...document.querySelectorAll('flt-semantics')];
const node = nodes.find(x => x.getAttribute('aria-label') === wanted)
|| nodes.find(x => (x.innerText || '').trim() === wanted);
if (!node) return false;
node.click();
return true;
})()`);
if (!clicked) throw new Error(`No exact semantics label ${JSON.stringify(label)}: ${JSON.stringify(await labels())}`);
await delay(500);
};
const clickPrefix = async (prefix) => {
const clicked = await evaluate(`(() => {
const wanted = ${JSON.stringify(prefix)};
const nodes = [...document.querySelectorAll('flt-semantics')];
const candidates = nodes.filter((x) =>
(x.getAttribute('aria-label') || '').startsWith(wanted)
|| (x.innerText || '').trim().startsWith(wanted));
if (candidates.length === 0) return false;
// Click the actionable row, not an ancestor semantics container whose
// innerText happens to start with the same text.
const actionable = candidates.filter((x) => x.hasAttribute('flt-tappable'));
const target = (actionable.length > 0 ? actionable : candidates)
.sort((a, b) => (a.innerText || '').length - (b.innerText || '').length)[0];
target.click();
return true;
})()`);
if (!clicked) throw new Error(`No semantics label starting ${JSON.stringify(prefix)}: ${JSON.stringify(await labels())}`);
await delay(500);
};
const activate = async () => {
await delay(3200);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(900);
const current = await labels();
if (!current.some((label) => label.includes('Changes wiring probe'))) {
throw new Error(`Flutter semantics unavailable: ${JSON.stringify(current)}`);
}
};
const navigate = async (url) => {
const navigation = await send('Page.navigate', { url }, session);
if (navigation.error || navigation.result?.errorText) throw new Error(JSON.stringify(navigation));
await activate();
};
const capture = async (name, width = 390, height = 844, scale = 1) => {
await send('Emulation.setDeviceMetricsOverride', {
width, height, deviceScaleFactor: scale, mobile: width < 600,
}, session);
await delay(350);
const data = (await send('Page.captureScreenshot', { format: 'png' }, session)).result.data;
const path = join(out, `${name}.png`);
await writeFile(path, Buffer.from(data, 'base64'));
evidence.captures.push(path);
};
// Semantics expose a widget's box in CSS pixels; the pixel probe then reads
// the rendered screen so a text colour can be asserted, not just presence.
const rectOfLabel = (prefix) =>
evaluate(`(() => {
const wanted = ${JSON.stringify(prefix)};
const text = (x) => x.getAttribute('aria-label') || (x.innerText || '').trim();
const node = [...document.querySelectorAll('flt-semantics')]
.find(x => text(x).startsWith(wanted));
if (!node) return null;
const r = node.getBoundingClientRect();
return { x: r.x, y: r.y, width: r.width, height: r.height };
})()`);
const brightestPixel = async (rect) => {
const shot = await send('Page.captureScreenshot', {
format: 'png',
clip: { ...rect, scale: 2 },
}, session);
if (!shot.result?.data) throw new Error(`Screenshot clip failed: ${JSON.stringify(shot)}`);
return evaluate(`(async () => {
const img = new Image();
img.src = 'data:image/png;base64,${shot.result.data}';
await img.decode();
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const data = ctx.getImageData(0, 0, img.width, img.height).data;
let max = 0;
let bright = 0;
for (let i = 0; i < data.length; i += 4) {
const lum = (0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2]) / 255;
if (lum > max) max = lum;
if (lum > 0.5) bright++;
}
return { max, bright, pixels: data.length / 4, width: img.width, height: img.height };
})()`);
};
await navigate(`${origin}/`);
const detailBody = await bodyText();
if (!detailBody.includes('Changes wiring probe')) {
throw new Error(`Run detail did not render: ${JSON.stringify(detailBody)}`);
}
if (!(await hasLabel('Changes'))) {
throw new Error(`Run detail has no Changes action: ${JSON.stringify(await labels())}`);
}
evidence.checks.push({ check: 'run detail shows the native Changes action', value: true });
await capture('d1-wire-material-detail');
await clickExact('Changes');
const changesBody = await bodyText();
if (changesBody === detailBody) throw new Error('Changes did not open a new page');
if (!changesBody.includes(file)) throw new Error(`Recorded filename missing: ${JSON.stringify(changesBody)}`);
if (!changesBody.includes('1 file changed · +2 -1')) {
throw new Error(`Recorded stat missing: ${JSON.stringify(changesBody)}`);
}
evidence.checks.push({ check: 'Changes opened the recorded filename and stat', value: true });
await capture('d1-wire-material-changes');
await delay(1200);
await clickPrefix(rowPrefix);
const diffBody = await bodyText();
if (!diffBody.includes('const recorded = true;')
|| !diffBody.includes('const label = "Old";')) {
throw new Error(`Recorded diff lines missing: ${JSON.stringify(diffBody)}`);
}
for (const label of ['Rendered', 'Raw patch', 'Copy patch', 'Close diff']) {
if (!(await hasLabel(label))) throw new Error(`Diff is missing ${label}: ${JSON.stringify(await labels())}`);
}
evidence.checks.push({ check: 'the recorded file opened its actual diff', value: true });
await capture('d1-wire-material-diff');
await clickExact('Close diff');
if (!(await bodyText()).includes(file)) throw new Error('Close diff did not return to Changes');
await clickExact('Back');
const backBody = await bodyText();
if (!backBody.includes('Changes wiring probe') || !(await hasLabel('Changes'))) {
throw new Error(`Back did not return to the run detail: ${JSON.stringify(backBody)}`);
}
evidence.checks.push({ check: 'Close diff and Back returned to the previous pages', value: true });
await capture('d1-wire-material-back');
await navigate(`${origin}/?capture=unavailable`);
await clickExact('Changes');
const unavailableBody = await bodyText();
if (!unavailableBody.includes('Changes · capture unavailable')) {
throw new Error(`Unavailable capture wording missing: ${JSON.stringify(unavailableBody)}`);
}
if (!unavailableBody.includes('Workspace snapshot unavailable; changes could not be compared.')) {
throw new Error('Unavailable diagnostic missing');
}
if (unavailableBody.includes('No changes observed')) {
throw new Error('Unavailable capture claimed no changes');
}
evidence.checks.push({ check: 'unavailable capture keeps its gap wording', value: true });
await capture('d1-wire-material-unavailable');
await navigate(`${origin}/?run=failed`);
await clickExact('Changes');
await capture('d1-wire-material-failed');
const failedBody = await bodyText();
const failedText = [failedBody, ...(await labels())].join('\n');
if (!failedText.includes(file)) {
throw new Error(`Failed run lost its visit capture: ${JSON.stringify(failedText)}`);
}
if (failedText.includes('file changed')) {
throw new Error(`Failed run showed a settled run-wide summary: ${JSON.stringify(failedText)}`);
}
if (!failedText.includes('Visit 1')) {
throw new Error(`Failed run visit section missing: ${JSON.stringify(failedText)}`);
}
evidence.checks.push({ check: 'a failed run keeps visit evidence without a summary', value: true });
await navigate(`${origin}/?run=running`);
if (await hasLabel('Changes')) {
throw new Error('An executing run offered the snapshot Changes page');
}
const runningBody = await bodyText();
if (!runningBody.includes('running')) {
throw new Error(`Running run detail did not render its status: ${JSON.stringify(runningBody)}`);
}
evidence.checks.push({ check: 'an executing run offers no snapshot Changes page', value: true });
await capture('d1-wire-material-running');
await navigate(`${origin}/?platform=cupertino&theme=light`);
if (!(await hasLabel('Changes'))) {
throw new Error(`Cupertino run detail has no Changes action: ${JSON.stringify(await labels())}`);
}
await capture('d1-wire-cupertino-detail');
await clickExact('Changes');
const cupertinoChanges = await bodyText();
if (!cupertinoChanges.includes(file)) {
throw new Error(`Cupertino Changes did not open the recorded file: ${JSON.stringify(cupertinoChanges)}`);
}
evidence.checks.push({ check: 'Cupertino Changes opened the recorded file', value: true });
await capture('d1-wire-cupertino-changes');
await clickPrefix(rowPrefix);
if (!(await bodyText()).includes('const recorded = true;')) {
throw new Error('Cupertino recorded diff missing');
}
await capture('d1-wire-cupertino-diff');
await clickExact('Raw patch');
const rawLabels = await labels();
const rawBody = await bodyText();
if (!rawLabels.some((label) => label.includes('Raw patch for'))
&& !rawBody.includes('const recorded = true;')) {
throw new Error(`Cupertino raw patch did not render: ${JSON.stringify(rawLabels)}`);
}
if (!(await hasLabel('Copy patch'))) {
throw new Error(`Cupertino raw view lost its actions: ${JSON.stringify(await labels())}`);
}
evidence.checks.push({ check: 'Cupertino Raw patch uses a native selectable body', value: true });
await capture('d1-wire-cupertino-raw');
await clickExact('Close diff');
await delay(600);
await clickExact('Back');
await delay(800);
if (!(await bodyText()).includes('Changes wiring probe')) {
throw new Error('Cupertino Back did not return to the run detail');
}
evidence.checks.push({ check: 'Cupertino diff and Back returned', value: true });
await capture('d1-wire-cupertino-back');
await navigate(`${origin}/?platform=cupertino&theme=dark`);
await clickExact('Changes');
const darkText = [await bodyText(), ...(await labels())].join('\n');
if (!darkText.includes(file) || !darkText.includes('file changed')) {
throw new Error(`Cupertino dark Changes did not render: ${JSON.stringify(darkText)}`);
}
evidence.checks.push({ check: 'Cupertino dark Changes renders the recorded summary', value: true });
await capture('d1-wire-cupertino-dark-changes');
await clickPrefix(rowPrefix);
if (!(await bodyText()).includes('const recorded = true;')) {
throw new Error('Cupertino dark recorded diff missing');
}
// The header is a merged semantics label, so anchor the pixel crop on the
// controls below it: the form control and the title share the 16 px inset,
// and the close control shares the header's top line.
const formRect = await rectOfLabel('Rendered');
const closeRect = await rectOfLabel('Close diff');
if (!formRect || !closeRect) {
throw new Error(`Dark diff controls are missing: ${JSON.stringify(await labels())}`);
}
const titleLuminance = await brightestPixel({
x: formRect.x,
y: closeRect.y,
width: 60,
height: 22,
});
if (titleLuminance.max < 0.5) {
throw new Error(`Dark Cupertino diff header is not light: ${JSON.stringify(titleLuminance)}`);
}
evidence.checks.push({
check: 'Cupertino dark diff header text is light on the popup surface',
value: true,
luminance: titleLuminance.max,
});
evidence.checks.push({ check: 'Cupertino dark diff renders its recorded lines', value: true });
await capture('d1-wire-cupertino-dark-diff');
// A 320-px host at 2x text is the narrowest documented state. The native
// controls must keep the whole browser words; the strip scrolls and reveals
// the selected segment instead of clipping or breaking a label.
const scroll = async ({ x, y, deltaX = 0, deltaY = 0 }) => {
await send('Input.dispatchMouseEvent', {
type: 'mouseWheel', x, y, deltaX, deltaY,
}, session);
await delay(400);
};
const openScaledDiff = async ({ platform, prefix }) => {
await send('Emulation.setDeviceMetricsOverride', {
width: 320, height: 720, deviceScaleFactor: 1, mobile: true,
}, session);
const query = platform === 'cupertino'
? '?platform=cupertino&theme=light&scale=2'
: '?theme=light&scale=2';
await navigate(`${origin}/${query}`);
await clickExact('Changes');
// The 2x summary pushes the recorded file below the fold: scroll the
// Changes list until its row is built and offered.
for (let i = 0; i < 8 && !(await hasPrefix(rowPrefix)); i++) {
await scroll({ x: 160, y: 600, deltaY: 400 });
}
if (!(await hasPrefix(rowPrefix))) {
throw new Error(`2x Changes did not offer the recorded file: ${JSON.stringify(await labels())}`);
}
await clickPrefix(rowPrefix);
await delay(800);
const body = await bodyText();
if (!body.includes('const recorded = true;')) {
throw new Error(`2x diff did not render: ${JSON.stringify(body)}`);
}
for (const label of ['Copy patch', 'Close diff', 'Diff form']) {
if (!(await hasLabel(label))) {
throw new Error(`2x diff is missing ${label}: ${JSON.stringify(await labels())}`);
}
}
await capture(`${prefix}-diff`, 320, 720, 1);
// The native menu carries both fixed words on a host too narrow for the
// segmented control; the sheet/list must show each of them whole. A real
// pointer tap is used because the Cupertino label sits on a semantic
// wrapper above the actual button.
const menuRect = await rectOfLabel('Diff form');
if (!menuRect) {
throw new Error(`2x diff has no Diff form control: ${JSON.stringify(await labels())}`);
}
const tap = {
x: menuRect.x + menuRect.width / 2,
y: menuRect.y + menuRect.height / 2,
};
await send('Input.dispatchMouseEvent', {
type: 'mousePressed', ...tap, button: 'left', clickCount: 1,
}, session);
await send('Input.dispatchMouseEvent', {
type: 'mouseReleased', ...tap, button: 'left', clickCount: 1,
}, session);
await delay(900);
for (const label of ['Rendered', 'Raw patch']) {
if (!(await hasLabel(label))) {
throw new Error(`2x diff menu is missing ${label}: ${JSON.stringify(await labels())}`);
}
}
await capture(`${prefix}-menu`, 320, 720, 1);
await clickExact('Raw patch');
await delay(900);
const raw = await bodyText();
const rawLabels = await labels();
if (!rawLabels.some((label) => label.includes('Raw patch for'))
&& !raw.includes('const recorded = true;')) {
throw new Error(`2x Raw patch did not render: ${JSON.stringify(rawLabels)}`);
}
await capture(`${prefix}-raw`, 320, 720, 1);
await clickExact('Close diff');
await delay(600);
};
await openScaledDiff({ platform: 'material', prefix: 'd1-wire-material-2x-320' });
evidence.checks.push({
check: 'Material 2x/320 diff keeps its controls and opens Raw patch',
value: true,
});
await openScaledDiff({ platform: 'cupertino', prefix: 'd1-wire-cupertino-2x-320' });
evidence.checks.push({
check: 'Cupertino 2x/320 diff keeps its controls and opens Raw patch',
value: true,
});
console.log(JSON.stringify(evidence, 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 });
}