Files
pipelines/app/tool/changes_probe.mjs
T

420 lines
17 KiB
JavaScript

// Real-browser evidence for the Changes viewer fixture.
//
// Usage (from app/):
// flutter build web --target tool/changes_probe_entry.dart --output build/probe-web
// node tool/changes_probe.mjs --root build/probe-web --out DIR
//
// Serves the fixture over loopback with an explicit text/html index, drives
// headless Chrome through the Chrome DevTools Protocol, enables Flutter
// semantics, clicks exact labels, verifies the body/labels change, copies the
// raw patch and writes real PNG captures at phone and desktop widths.
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-web');
const out = resolve(args.get('out') ?? 'build/changes-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);
const type = mime[extname(filePath)] ?? 'application/octet-stream';
res.writeHead(200, { 'content-type': type }).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 origin = `http://127.0.0.1:${port}`;
const profile = await mkdtemp(join(tmpdir(), 'd1-changes-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)}`);
};
let ws;
const evidence = { origin, captures: [], checks: [] };
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 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.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 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(450);
};
// File rows announce their visible kind and meta after the path, so click by
// the stable action+path prefix rather than a fully spelled label.
const clickPrefix = async (prefix) => {
const clicked = await evaluate(`(() => {
const wanted = ${JSON.stringify(prefix)};
const nodes = [...document.querySelectorAll('flt-semantics')];
// A container's innerText also starts with its first row, so click the
// most specific (shortest-text) node that starts with the row prefix.
const candidates = nodes.filter((x) =>
(x.getAttribute('aria-label') || '').startsWith(wanted)
|| (x.innerText || '').trim().startsWith(wanted));
if (candidates.length === 0) return false;
candidates.sort((a, b) => (a.innerText || '').length - (b.innerText || '').length);
candidates[0].click();
return true;
})()`);
if (!clicked) throw new Error(`No semantics label starting ${JSON.stringify(prefix)}: ${JSON.stringify(await labels())}`);
await delay(450);
};
const activate = async () => {
await delay(3500);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(1000);
const current = await labels();
if (!current.some((label) => label.includes('Changes 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, height, 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 file = join(out, `${name}.png`);
await writeFile(file, Buffer.from(data, 'base64'));
evidence.captures.push(file);
return file;
};
await navigate(`${origin}/`);
const contentType = await evaluate(
"fetch('/').then(r => r.headers.get('content-type'))",
);
evidence.checks.push({ check: 'index content-type is text/html', value: contentType });
if (!String(contentType).toLowerCase().startsWith('text/html')) {
throw new Error(`Root index is not text/html: ${contentType}`);
}
// Diff text inherits native host typography; it must not add a custom face.
const manifest = await evaluate("fetch('/assets/FontManifest.json').then(r => r.json())");
const codeEntry = (Array.isArray(manifest) ? manifest : []).find(
(entry) => entry.family === 'RobotoMono',
);
if (codeEntry) throw new Error('Diff build still overrides native typography');
evidence.checks.push({
check: 'the web build does not register a custom diff font',
});
const firstLabels = await labels();
const firstBody = await bodyText();
const firstRow = 'Open diff for lib/features/changes/domain/unified_patch.dart';
const firstRowPresent = firstLabels.some((label) => label.startsWith(firstRow));
const firstStates = ['Changes probe', 'Capture unavailable'].filter((label) =>
firstBody.includes(label),
);
evidence.checks.push({
check: 'run summary and capture states are present',
value: firstStates,
fileRow: firstRowPresent,
});
if (firstStates.length !== 2 || !firstRowPresent) {
throw new Error(`Run summary or capture states missing: ${JSON.stringify(firstBody)}`);
}
await capture('d1-changes-phone-list', 390, 844);
const before = await bodyText();
await clickPrefix('Open diff for lib/features/changes/domain/unified_patch.dart');
const after = await bodyText();
if (after === before) throw new Error('Opening the diff did not change the document body');
for (const label of ['Rendered', 'Raw patch', 'Copy patch', 'Close diff']) {
if (!(await hasLabel(label))) throw new Error(`Open diff is missing ${label}: ${JSON.stringify(await labels())}`);
}
evidence.checks.push({ check: 'body changed and diff controls appeared', bodyChanged: true });
const renderedFile = await capture('d1-changes-phone-diff', 390, 844);
const renderedBody = await bodyText();
await clickExact('Raw patch');
const rawSemantics = (await labels()).some((label) =>
label.startsWith('Raw patch for lib/features/changes/domain/unified_patch.dart'),
);
if (!rawSemantics) {
throw new Error(`Raw patch view semantics missing: ${JSON.stringify(await labels())}`);
}
const rawBody = await bodyText();
const rawFile = await capture('d1-changes-phone-raw', 390, 844);
const [renderedBytes, rawBytes] = await Promise.all([
readFile(renderedFile),
readFile(rawFile),
]);
if (renderedBytes.equals(rawBytes)) {
throw new Error('Raw patch capture is identical to the rendered capture');
}
evidence.checks.push({
check: 'Raw patch switched the body and the rendered pixels',
bodyChanged: rawBody !== renderedBody,
pixelsChanged: true,
rawSemantics,
});
await send('Browser.grantPermissions', {
origin,
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
});
await clickExact('Copy patch');
let clipboard = null;
try {
clipboard = await evaluate('navigator.clipboard.readText()');
} catch (error) {
evidence.checks.push({ check: 'clipboard readback unavailable', value: String(error) });
}
if (clipboard != null) {
const startsWithHeader = String(clipboard).startsWith(
'--- "a/lib/features/changes/domain/unified_patch.dart"',
);
if (!startsWithHeader) throw new Error('Clipboard did not contain the recorded raw patch');
evidence.checks.push({
check: 'Copy patch copied the raw patch',
startsWithHeader,
length: String(clipboard).length,
});
}
await capture('d1-changes-phone-copied', 390, 844);
await clickExact('Close diff');
if (await hasLabel('Raw patch')) throw new Error('Close diff left the viewer open');
evidence.checks.push({ check: 'Close diff returned to the list', bodyChanged: (await bodyText()) !== after });
// Filter changed filenames through the real text field when Flutter exposes
// it as a semantics input; record honestly when it does not.
const focused = await evaluate(`(() => {
const nodes = [...document.querySelectorAll('input, textarea')];
const node = nodes.find(x => ((x.getAttribute('aria-label') || '') + ' ' + (x.placeholder || '')).toLowerCase().includes('filter changed files'));
if (!node) return false;
node.focus();
node.click();
return true;
})()`);
if (focused) {
await send('Input.insertText', { text: 'unified' }, session);
await delay(500);
const filtered = await labels();
// A row is one single-line semantics node; a list container repeats its
// rows with newlines and must not be counted as an extra match.
const rows = (prefix) =>
filtered.filter((label) => label.startsWith(prefix) && !label.includes('\n'));
const unifiedRows = rows('Open diff for lib/features/changes/domain/unified_patch.dart');
const binaryRows = rows('Open diff for assets/pipelines-mark.png');
evidence.checks.push({
check: 'filtering by changed filename',
unifiedRows: unifiedRows.length,
binaryRows: binaryRows.length,
});
if (unifiedRows.length === 0) {
throw new Error('Filtering removed the matching file');
}
if (binaryRows.length !== 0) {
throw new Error('Filtering left a non-matching file visible');
}
await capture('d1-changes-phone-filtered', 390, 844);
} else {
evidence.checks.push({ check: 'filter field not exposed as a semantics input', value: false });
}
// Visit numbering: two stages appear twice in this compact fixture. The
// implementer's visits are position-derived (1, 2); the resumed reviewer
// visit records control.visit 6, which must win over its position.
await navigate(`${origin}/?visits=1`);
const visitText = await bodyText();
const visitsFor = (stage) => [
...visitText.matchAll(new RegExp(`${stage}\\s+Visit (\\d+)`, 'g')),
].map((match) => match[1]);
const implementerVisits = visitsFor('implementer');
const reviewerVisits = visitsFor('reviewer');
evidence.checks.push({
check: 'repeated stages show their own visit numbers',
implementerVisits,
reviewerVisits,
});
if (implementerVisits.join(',') !== '1,2') {
throw new Error(`implementer visits are not 1,2: ${JSON.stringify(visitText)}`);
}
if (reviewerVisits.join(',') !== '1,6') {
throw new Error(`reviewer visits are not 1,6 (control.visit): ${JSON.stringify(visitText)}`);
}
const visitFile = await capture('d1-changes-phone-visits', 390, 844);
await navigate(`${origin}/`);
await clickPrefix('Open diff for lib/features/changes/widgets/file_diff_viewer.dart');
await capture('d1-changes-desktop-diff', 1280, 800);
await clickExact('Close diff');
await capture('d1-changes-desktop-list', 1280, 800);
// Presentation revision: the light theme and a narrow Cupertino surface, at
// 2x so glyph advance, line tints and the popup inset are inspectable.
await navigate(`${origin}/?theme=light`);
await capture('d1-changes-phone-light-list', 390, 844);
await clickPrefix('Open diff for lib/features/changes/domain/unified_patch.dart');
await capture('d1-changes-phone-light-diff', 390, 844);
await clickExact('Raw patch');
await capture('d1-changes-phone-light-raw', 390, 844);
await clickExact('Rendered');
// The 2x capture is last in its state: Chrome only re-lays out Flutter when
// the CSS size changes, so scaling back down on the same page would capture a
// half-size frame.
await capture('d1-changes-phone-light-diff-2x', 390, 844, 2);
await navigate(`${origin}/?theme=light`);
await capture('d1-changes-desktop-light-list', 1280, 800);
await clickPrefix('Open diff for lib/features/changes/widgets/file_diff_viewer.dart');
await capture('d1-changes-desktop-light-diff', 1280, 800);
await clickExact('Close diff');
await navigate(`${origin}/?platform=cupertino&theme=light`);
await capture('d1-changes-cupertino-light-list', 390, 844);
await clickPrefix('Open diff for lib/features/changes/domain/unified_patch.dart');
await capture('d1-changes-cupertino-light-diff', 390, 844);
await capture('d1-changes-cupertino-light-diff-2x', 390, 844, 2);
await navigate(`${origin}/?platform=cupertino`);
const cupertinoLabels = await labels();
if (!cupertinoLabels.some((label) => label.includes('Changes probe'))) {
throw new Error('Cupertino fixture did not load');
}
await clickPrefix('Open diff for lib/features/changes/domain/unified_patch.dart');
await capture('d1-changes-cupertino-diff', 1280, 800);
evidence.checks.push({ check: 'Cupertino viewer opened', value: true });
evidence.checks.push({ check: 'visit capture written', value: visitFile });
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 });
}