Files
pipelines/app/tool/attention_wiring_probe.mjs
T

221 lines
9.8 KiB
JavaScript

// Real-browser acceptance for the Needs-you bell in the Runs destination.
//
// flutter build web
// OPENCODE_ARTIFACT_DIR=<dir> node tool/attention_wiring_probe.mjs build/web
//
// The fixture serves the normal app bundle and answers list_runs over the same
// origin, so this is the real Runs screen and the real shared socket. It touches
// no production server, operator run or browser profile. The list carries one
// failed run, one human wait, plus a running and a machine export wait that must
// not reach the bell. Each native host is captured at 320 logical px / 2x.
import { spawn, execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { once } from 'node:events';
import { existsSync } from 'node:fs';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { tmpdir } from 'node:os';
import { setTimeout as delay } from 'node:timers/promises';
import { startFixture } from './runs_probe_server.mjs';
const output = process.env.OPENCODE_ARTIFACT_DIR;
if (!output) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
await mkdir(output, { recursive: true });
const root = resolve(process.argv[2] ?? 'build/web');
const now = Date.now();
const dayStart = new Date(now);
dayStart.setHours(0, 0, 0, 0);
const at = (minutes) =>
new Date(Math.max(now - minutes * 60_000, dayStart.getTime() + 30_000)).toISOString();
const runs = [
{ id: 1, title: 'Running fixture', name: 'running-fixture', status: 'running',
created_at: at(6), pipeline_id: 10 },
{ id: 2, title: 'Approve the release candidate', name: 'release',
status: 'waiting', created_at: at(5), finished_at: '', pipeline_id: 10 },
{ id: 3, title: 'Nightly audit stopped', name: 'nightly-audit',
status: 'failed', diagnostic: 'Stage 3 stopped at the export archive step because the credential is missing.',
created_at: at(4), finished_at: at(1), pipeline_id: 10 },
{ id: 6, title: 'Machine export wait', name: 'export-handoff',
status: 'waiting', export_pending_sequence: 2, created_at: at(3), pipeline_id: 10 },
];
const fixture = await startFixture({ root, runs });
// Prove the fixture answered before Chrome starts, so a missing build fails with
// a clear message instead of a blank page.
const probe = await fetch(`${fixture.url}/`);
if (probe.status !== 200 || !(probe.headers.get('content-type') ?? '').startsWith('text/html')) {
throw new Error(`Fixture index must be HTML: ${probe.status} ${probe.headers.get('content-type')}`);
}
await probe.text();
const chromePath = process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
if (!existsSync(chromePath)) throw new Error(`Chrome not found at ${chromePath}; set CHROME_PATH`);
const profile = await mkdtemp(join(tmpdir(), 'attention-wiring-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 (check, what) => {
for (let i = 0; i < 160; i++) {
const value = await check();
if (value) return value;
await delay(100);
}
throw new Error(`Timed out waiting for ${what}\n${stderr.slice(-1200)}`);
};
let ws;
const captures = [];
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 target = (await send('Target.createTarget', { url: 'about:blank' })).result.targetId;
const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).result.sessionId;
await send('Page.enable', {}, session);
await send('Runtime.enable', {}, 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)}`);
if (response.result?.exceptionDetails) {
throw new Error(`Page exception: ${JSON.stringify(response.result.exceptionDetails)}`);
}
return response.result?.result?.value;
};
const path = () => evaluate('location.pathname');
const semantics = () => evaluate(`[...document.querySelectorAll('flt-semantics')].map((el) => {
const r = el.getBoundingClientRect();
return { label: (el.getAttribute('aria-label') || el.innerText || '').trim(),
role: el.getAttribute('role'), x: r.x, y: r.y, w: r.width, h: r.height };
})`);
const labels = async () => (await semantics()).map((item) => item.label).filter(Boolean);
const text = () => evaluate(`(() => {
const chunks = [...document.querySelectorAll('flt-semantics, [aria-label]')]
.map((el) => el.getAttribute('aria-label') || el.innerText || '');
if (document.body?.innerText) chunks.push(document.body.innerText);
return [...new Set(chunks)].join('\\n');
})()`);
const requireLabel = async (needle) => {
await waitFor(async () => (await labels()).some((label) => label.includes(needle)), `label ${JSON.stringify(needle)}`);
};
const requireText = async (needle) => {
await waitFor(async () => (await text()).includes(needle), `text ${JSON.stringify(needle)}`);
};
const enableSemantics = async () => {
await waitFor(
() => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),
'semantics placeholder',
);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await delay(600);
};
const clickLabel = async (needle, exact = false) => {
const clicked = await evaluate(`(() => {
const wanted = ${JSON.stringify(needle)};
const all = [...document.querySelectorAll('flt-semantics')];
const match = (el) => {
const label = (el.getAttribute('aria-label') || el.innerText || '').trim();
return ${exact ? 'label === wanted' : 'label.includes(wanted)'};
};
const element = all.find((el) => ['button', 'tab', 'link', 'menuitem'].includes(el.getAttribute('role')) && match(el))
|| all.find(match);
if (!element) return false;
element.click();
return true;
})()`);
if (!clicked) throw new Error(`Could not click ${JSON.stringify(needle)}. Labels: ${JSON.stringify(await labels())}`);
await delay(400);
};
const capture = async (name) => {
const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).result.data;
const pathName = join(output, `${name}.png`);
await writeFile(pathName, Buffer.from(data, 'base64'));
captures.push(pathName);
return pathName;
};
const ocr = async (file) => (await promisify(execFile)('tesseract', [file, 'stdout'])).stdout;
const requireOcr = async (file, needle) => {
const read = await ocr(file);
if (!read.toLowerCase().includes(needle.toLowerCase())) {
throw new Error(`OCR of ${file} lacks ${JSON.stringify(needle)}:\n${read.slice(0, 2000)}`);
}
};
const setPhone = async () => {
await send('Emulation.setDeviceMetricsOverride', {
width: 320, height: 640, deviceScaleFactor: 2, mobile: true,
}, session);
await delay(500);
};
const drive = async (host, userAgent) => {
if (userAgent) {
await send('Emulation.setUserAgentOverride', userAgent, session);
}
await setPhone();
await send('Page.navigate', { url: `${fixture.url}/` }, session);
await enableSemantics();
await waitFor(async () => (await path()) === '/pipelines', 'boot route /pipelines');
await clickLabel('Runs', true);
await waitFor(async () => (await path()) === '/runs', 'runs route');
await requireLabel('Notifications, 2 needing attention');
const bell = await capture(`attention-runs-${host}-bell-320x2`);
await requireOcr(bell, 'Nightly audit stopped');
await clickLabel('Notifications, 2 needing attention');
await requireText('Needs you');
await requireText('Approve the release candidate');
await requireText('Stage 3 stopped at the export archive step');
await requireLabel('Dismiss Approve the release candidate');
await requireLabel('Dismiss Nightly audit stopped');
await requireLabel('Clear all');
const sheetLabels = await labels();
if (sheetLabels.some((label) => label.includes('Machine export wait'))) {
throw new Error('an export wait leaked into the bell sheet');
}
if (sheetLabels.some((label) => label.includes('Running fixture'))) {
throw new Error('a running run leaked into the bell sheet');
}
const sheet = await capture(`attention-runs-${host}-sheet-320x2`);
await requireOcr(sheet, 'Needs you');
return { host, bell, sheet };
};
const cupertino = await drive('cupertino');
const material = await drive('material', {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36',
platform: 'Linux x86_64',
});
const state = await (await fetch(`${fixture.url}/fixture/state`)).json();
console.log(JSON.stringify({ captures, cupertino, material, fixture: state }, null, 2));
} finally {
try { ws?.close(); } catch {}
chrome.kill('SIGTERM');
await rm(profile, { recursive: true, force: true }).catch(() => {});
await fixture.close();
}