422 lines
18 KiB
JavaScript
422 lines
18 KiB
JavaScript
// Real-browser evidence for the run-detail Artifacts action and gallery page.
|
|
//
|
|
// Usage (from app/):
|
|
// flutter build web --target tool/artifact_gallery_probe.dart --output build/probe-gallery
|
|
// node tool/artifact_gallery_probe.mjs --root build/probe-gallery --out DIR
|
|
//
|
|
// Serves the built fixture on an OS-assigned loopback port, drives headless
|
|
// Chrome, and records the actual recorded rows, the selection dialog, the safe
|
|
// external Open, the empty/unavailable states, both native hosts, dark
|
|
// Cupertino and the 320/2x states. 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-gallery');
|
|
const out = resolve(args.get('out') ?? 'build/artifact-gallery-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) => {
|
|
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
|
|
if (pathname.startsWith('/fixture/')) {
|
|
res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' });
|
|
res.end(`recorded artifact ${pathname}\n`);
|
|
return;
|
|
}
|
|
try {
|
|
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(), 'a1w-chrome-'));
|
|
const chrome = spawn(
|
|
chromePath,
|
|
['--headless=new', '--no-first-run', '--no-default-browser-check', '--disable-gpu',
|
|
'--disable-popup-blocking', '--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 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' });
|
|
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('Browser.grantPermissions', {
|
|
origin,
|
|
permissions: ['clipboardReadWrite', 'clipboardSanitizedWrite'],
|
|
});
|
|
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 text = (x) => x.getAttribute('aria-label') || (x.innerText || '').trim();
|
|
const nodes = [...document.querySelectorAll('flt-semantics')];
|
|
const tappable = nodes.filter(x => x.hasAttribute('flt-tappable'));
|
|
// The tappable node is the one the engine listens to; a labelled wrapper
|
|
// above it would swallow a synthetic click.
|
|
const node = tappable.find(x => text(x) === wanted)
|
|
|| tappable.find(x => text(x).startsWith(wanted))
|
|
|| nodes.find(x => text(x) === 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);
|
|
};
|
|
// The lowest matching row is the later visit's duplicate filename.
|
|
const clickLowestPrefix = async (prefix) => {
|
|
const clicked = await evaluate(`(() => {
|
|
const wanted = ${JSON.stringify(prefix)};
|
|
const text = (x) => x.getAttribute('aria-label') || (x.innerText || '').trim();
|
|
const candidates = [...document.querySelectorAll('flt-semantics')]
|
|
.filter(x => text(x).startsWith(wanted))
|
|
.filter(x => x.hasAttribute('flt-tappable'));
|
|
if (!candidates.length) return false;
|
|
candidates.sort((a, b) => b.getBoundingClientRect().y - a.getBoundingClientRect().y);
|
|
candidates[0].click();
|
|
return true;
|
|
})()`);
|
|
if (!clicked) throw new Error(`No tappable label starting ${JSON.stringify(prefix)}: ${JSON.stringify(await labels())}`);
|
|
await delay(600);
|
|
};
|
|
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('Artifacts gallery 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);
|
|
};
|
|
const rectOfPrefix = (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);
|
|
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;
|
|
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;
|
|
}
|
|
return { max, width: img.width, height: img.height };
|
|
})()`);
|
|
};
|
|
const openGallery = async (query) => {
|
|
await navigate(`${origin}/?${query}`);
|
|
if (!(await hasLabel('Artifacts'))) {
|
|
throw new Error(`Run detail has no Artifacts action: ${JSON.stringify(await labels())}`);
|
|
}
|
|
await clickExact('Artifacts');
|
|
await delay(600);
|
|
};
|
|
|
|
// Material: recorded rows, duplicate filename, visit-2 capture facts and the
|
|
// safe external Open.
|
|
await navigate(`${origin}/?origin=${encodeURIComponent(origin)}`);
|
|
if (!(await hasLabel('Artifacts'))) {
|
|
throw new Error(`Material run detail has no Artifacts action: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({ check: 'the run detail offers the native Artifacts action', value: true });
|
|
await capture('a1w-material-detail');
|
|
|
|
await clickExact('Artifacts');
|
|
const galleryBody = await bodyText();
|
|
for (const recorded of ['build.log', 'shot.png', 'text/csv', 'implementer · visit 1', 'reviewer · visit 2']) {
|
|
if (!galleryBody.includes(recorded)) {
|
|
throw new Error(`Gallery is missing recorded row ${recorded}: ${JSON.stringify(galleryBody)}`);
|
|
}
|
|
}
|
|
if (!galleryBody.includes('Artifacts · 4 recorded')) {
|
|
throw new Error(`Gallery summary missing: ${JSON.stringify(galleryBody)}`);
|
|
}
|
|
const resultRows = (await labels()).filter((label) => label.startsWith('result.csv')).length;
|
|
if (resultRows < 2) {
|
|
throw new Error(`Duplicate result.csv rows missing: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({ check: 'the gallery lists the actual recorded rows, not nav labels', value: true });
|
|
await capture('a1w-material-gallery');
|
|
|
|
await clickLowestPrefix('result.csv');
|
|
const selectedBody = await bodyText();
|
|
for (const fact of ['reviewer · visit 2', 'text/csv · 4.0\u00a0KiB', 'Artifacts · 1 recorded · partial capture', '2 files excluded from artifact capture.']) {
|
|
if (!selectedBody.includes(fact)) {
|
|
throw new Error(`Selected visit-2 facts missing ${fact}: ${JSON.stringify(selectedBody)}`);
|
|
}
|
|
}
|
|
if (!(await hasLabel('Open')) || !(await hasLabel('Copy link'))) {
|
|
throw new Error(`Selection offers no Open/Copy link: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({ check: 'the duplicate filename selected visit 2 with its own capture facts', value: true });
|
|
await capture('a1w-material-selected');
|
|
|
|
// The selection offers the safe external Open and a separate Copy link.
|
|
// Headless Chrome cannot carry a user activation through a synthetic
|
|
// semantics click, so the tab itself is proven by the widget test that
|
|
// records the exact uri handed to the opener; the browser proves the actions
|
|
// appear only for a url that resolves through the safe rules.
|
|
if (!(await hasLabel('Open')) || !(await hasLabel('Copy link'))) {
|
|
throw new Error(`Selection offers no Open/Copy link: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({
|
|
check: 'the selection offers the native Open and separate Copy link for a safe recorded url',
|
|
value: true,
|
|
});
|
|
await capture('a1w-material-open');
|
|
|
|
await navigate(`${origin}/?origin=${encodeURIComponent(origin)}&state=unsafe`);
|
|
await clickExact('Artifacts');
|
|
await clickLowestPrefix('result.csv');
|
|
const unsafeBody = await bodyText();
|
|
if (!unsafeBody.includes('The recorded artifact URL cannot be opened safely.')) {
|
|
throw new Error(`Unsafe url wording missing: ${JSON.stringify(unsafeBody)}`);
|
|
}
|
|
if ((await hasLabel('Open')) || (await hasLabel('Copy link'))) {
|
|
throw new Error(`Unsafe url still offered an action: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({ check: 'a protocol-relative url offers no open or copy action', value: true });
|
|
await capture('a1w-material-unsafe');
|
|
|
|
await navigate(`${origin}/?origin=${encodeURIComponent(origin)}&state=nourl`);
|
|
await clickExact('Artifacts');
|
|
await clickLowestPrefix('result.csv');
|
|
const nourlBody = await bodyText();
|
|
if (!nourlBody.includes('No artifact URL recorded.')) {
|
|
throw new Error(`No-url wording missing: ${JSON.stringify(nourlBody)}`);
|
|
}
|
|
if ((await hasLabel('Open')) || (await hasLabel('Copy link'))) {
|
|
throw new Error(`A url-less record still offered an action: ${JSON.stringify(await labels())}`);
|
|
}
|
|
evidence.checks.push({ check: 'a record with no url is explicit and offers no action', value: true });
|
|
await capture('a1w-material-nourl');
|
|
|
|
await navigate(`${origin}/?origin=${encodeURIComponent(origin)}&state=empty`);
|
|
await clickExact('Artifacts');
|
|
const emptyBody = await bodyText();
|
|
if (!emptyBody.includes('No artifacts recorded for this run.')) {
|
|
throw new Error(`Recorded empty capture wording missing: ${JSON.stringify(emptyBody)}`);
|
|
}
|
|
evidence.checks.push({ check: 'a recorded empty capture says nothing was recorded', value: true });
|
|
await capture('a1w-material-empty');
|
|
|
|
await navigate(`${origin}/?origin=${encodeURIComponent(origin)}&state=unavailable`);
|
|
await clickExact('Artifacts');
|
|
const unavailableBody = await bodyText();
|
|
if (!unavailableBody.includes('Artifacts · capture unavailable')) {
|
|
throw new Error(`Unavailable capture wording missing: ${JSON.stringify(unavailableBody)}`);
|
|
}
|
|
if (unavailableBody.includes('No artifacts recorded for this run.')) {
|
|
throw new Error('Unavailable capture claimed nothing was recorded');
|
|
}
|
|
evidence.checks.push({ check: 'an unavailable capture is never reported as empty', value: true });
|
|
await capture('a1w-material-unavailable');
|
|
|
|
// Cupertino light: the tinted action, the native dialog and its actions.
|
|
await openGallery(`platform=cupertino&theme=light&origin=${encodeURIComponent(origin)}`);
|
|
const cupertinoBody = await bodyText();
|
|
if (!cupertinoBody.includes('shot.png') || !cupertinoBody.includes('reviewer · visit 2')) {
|
|
throw new Error(`Cupertino gallery missing recorded rows: ${JSON.stringify(cupertinoBody)}`);
|
|
}
|
|
evidence.checks.push({ check: 'Cupertino lists the same recorded rows', value: true });
|
|
await capture('a1w-cupertino-gallery');
|
|
await clickLowestPrefix('result.csv');
|
|
if (!(await hasLabel('Open')) || !(await hasLabel('Copy link'))) {
|
|
throw new Error(`Cupertino selection offers no actions: ${JSON.stringify(await labels())}`);
|
|
}
|
|
const cupertinoSelected = await bodyText();
|
|
if (!cupertinoSelected.includes('Artifacts · 1 recorded · partial capture')) {
|
|
throw new Error(`Cupertino selection lost its capture facts: ${JSON.stringify(cupertinoSelected)}`);
|
|
}
|
|
evidence.checks.push({ check: 'Cupertino selection shows the recorded facts and native actions', value: true });
|
|
await capture('a1w-cupertino-selected');
|
|
|
|
// Cupertino dark: the gallery and its dialog on the dark host.
|
|
await openGallery(`platform=cupertino&theme=dark&origin=${encodeURIComponent(origin)}`);
|
|
await capture('a1w-cupertino-dark-gallery');
|
|
await clickLowestPrefix('result.csv');
|
|
const darkLabels = await labels();
|
|
const darkTitle = darkLabels.find((label) => label.startsWith('result.csv'));
|
|
const titleRect = await rectOfPrefix('result.csv');
|
|
const darkText = [await bodyText(), ...darkLabels].join('\n');
|
|
if (!darkText.includes('Artifacts · 1 recorded · partial capture')) {
|
|
throw new Error(`Dark Cupertino dialog did not render: ${JSON.stringify(darkText)}`);
|
|
}
|
|
if (!titleRect) throw new Error(`Dark Cupertino dialog title missing: ${JSON.stringify(darkLabels)}`);
|
|
const luminance = await brightestPixel(titleRect);
|
|
if (luminance.max < 0.5) {
|
|
throw new Error(`Dark Cupertino dialog text is not light: ${JSON.stringify(luminance)}`);
|
|
}
|
|
evidence.checks.push({
|
|
check: 'dark Cupertino dialog text is light on the popup surface',
|
|
value: true,
|
|
luminance: luminance.max,
|
|
title: darkTitle,
|
|
});
|
|
await capture('a1w-cupertino-dark-selected');
|
|
|
|
// 320 px at 2x text on both hosts: the recorded rows and facts stay whole.
|
|
const openScaled = async (platform, prefix) => {
|
|
await openGallery(`platform=${platform}&theme=light&scale=2&origin=${encodeURIComponent(origin)}`);
|
|
const scaled = await bodyText();
|
|
for (const recorded of ['build.log', 'shot.png', 'reviewer · visit 2']) {
|
|
if (!scaled.includes(recorded)) {
|
|
throw new Error(`2x gallery missing ${recorded}: ${JSON.stringify(scaled)}`);
|
|
}
|
|
}
|
|
await capture(`${prefix}-gallery`, 320, 720, 1);
|
|
await clickLowestPrefix('result.csv');
|
|
const facts = await bodyText();
|
|
if (!facts.includes('Artifacts · 1 recorded · partial capture')) {
|
|
throw new Error(`2x selection lost its facts: ${JSON.stringify(facts)}`);
|
|
}
|
|
if (!(await hasLabel('Open')) || !(await hasLabel('Copy link'))) {
|
|
throw new Error(`2x selection lost its actions: ${JSON.stringify(await labels())}`);
|
|
}
|
|
await capture(`${prefix}-selected`, 320, 720, 1);
|
|
};
|
|
await openScaled('material', 'a1w-material-2x-320');
|
|
evidence.checks.push({ check: 'Material 2x/320 keeps the recorded rows, facts and actions', value: true });
|
|
await openScaled('cupertino', 'a1w-cupertino-2x-320');
|
|
evidence.checks.push({ check: 'Cupertino 2x/320 keeps the recorded rows, facts and actions', 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 });
|
|
}
|