315 lines
14 KiB
JavaScript
315 lines
14 KiB
JavaScript
// Real-browser acceptance for the attention notification capability.
|
|
//
|
|
// flutter build web -t tool/notification_probe.dart -o build/notification_probe_web
|
|
// OPENCODE_ARTIFACT_DIR=<dir> node tool/notification_probe.mjs
|
|
//
|
|
// The page is served from an isolated loopback server. `window.Notification` is
|
|
// replaced with a recording spy before the app loads, and the real browser
|
|
// permission is granted (or denied) through CDP, so the drive proves the real
|
|
// bridge's behaviour: the initial seed raises nothing, one live transition
|
|
// raises exactly one notification with the entry's own words and run+kind
|
|
// identity, a dismissed transition and a reconnect replay raise none, a second
|
|
// decision gate of one run raises a fresh notification (the waiting shape has no
|
|
// finished_at), and the Settings tile reads honestly in every state on both
|
|
// hosts at 320 logical px and 2x text. One PNG is written per captured state.
|
|
import { createServer } from 'node:http';
|
|
import { spawn } from 'node:child_process';
|
|
import { once } from 'node:events';
|
|
import { existsSync } from 'node:fs';
|
|
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 ?? 'build/notification_probe_web');
|
|
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR;
|
|
if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
|
|
await mkdir(artifactDir, { recursive: true });
|
|
const defaultChrome = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
|
|
const chromePath = process.env.CHROME_PATH ?? defaultChrome;
|
|
if (!existsSync(chromePath)) throw new Error(`Chrome not found at ${chromePath}; set CHROME_PATH`);
|
|
|
|
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 origin = `http://127.0.0.1:${port}`;
|
|
|
|
// The spy is installed before every document, so the app only ever sees it. It
|
|
// delegates `permission` to the real constructor (which CDP granted or denied)
|
|
// and records each `new Notification(...)` call.
|
|
const spy = `(() => {
|
|
const mode = new URLSearchParams(location.search).get('mode') || '';
|
|
window.__notifications = [];
|
|
window.__probeReport = '';
|
|
if (mode === 'unsupported') { window.Notification = undefined; return; }
|
|
const Real = window.Notification;
|
|
function Spy(title, options) {
|
|
window.__notifications.push({
|
|
title: String(title ?? ''),
|
|
body: String((options && options.body) || ''),
|
|
tag: String((options && options.tag) || ''),
|
|
});
|
|
return { onclick: null, close() {} };
|
|
}
|
|
Object.defineProperty(Spy, 'permission', {
|
|
get: () => (Real ? Real.permission : 'granted'),
|
|
});
|
|
Spy.requestPermission = () => Promise.resolve(Spy.permission);
|
|
window.Notification = Spy;
|
|
})();`;
|
|
|
|
const profile = await mkdtemp(join(tmpdir(), 'notification-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) => {
|
|
for (let i = 0; i < 120; i++) {
|
|
const value = await check();
|
|
if (value) return value;
|
|
await delay(100);
|
|
}
|
|
throw new Error(`Timed out waiting for Chrome DevTools\n${stderr.slice(-1200)}`);
|
|
};
|
|
|
|
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 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, rejectCall) => {
|
|
const callId = ++id;
|
|
pending.set(callId, (message) => {
|
|
if (message.error) rejectCall(new Error(`${method}: ${message.error.message}`));
|
|
else resolveCall(message.result);
|
|
});
|
|
ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) }));
|
|
});
|
|
|
|
const target = (await send('Target.createTarget', { url: 'about:blank' })).targetId;
|
|
const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).sessionId;
|
|
await send('Page.enable', {}, session);
|
|
await send('Runtime.enable', {}, session);
|
|
await send('Page.addScriptToEvaluateOnNewDocument', { source: spy }, session);
|
|
|
|
const evaluate = async (expression) => {
|
|
const response = await send(
|
|
'Runtime.evaluate',
|
|
{ expression, returnByValue: true, awaitPromise: true },
|
|
session,
|
|
);
|
|
if (response.exceptionDetails) {
|
|
throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response.exceptionDetails)}`);
|
|
}
|
|
return response.result?.value;
|
|
};
|
|
const semantics = () => evaluate(
|
|
"[...document.querySelectorAll('flt-semantics')].map(x=>{const r=x.getBoundingClientRect();return{label:(x.getAttribute('aria-label')||x.innerText||'').trim(),role:x.getAttribute('role'),x:r.x,y:r.y,w:r.width,h:r.height}})",
|
|
);
|
|
const requireLabels = (all, needles, state) => {
|
|
for (const needle of needles) {
|
|
if (!all.some((label) => label.includes(needle))) {
|
|
throw new Error(`Missing ${JSON.stringify(needle)} in ${state}: ${JSON.stringify(all)}`);
|
|
}
|
|
}
|
|
};
|
|
let viewport = { width: 320, height: 640 };
|
|
const navigate = async ({ mode, host = 'material', scale = 1, theme = 'dark', width = 320, height = 640, semantics: enableSemantics = false, state } = {}) => {
|
|
viewport = { width, height };
|
|
await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: theme }] }, session);
|
|
const query = new URLSearchParams({ mode, host, scale: String(scale), theme });
|
|
if (state) query.set('state', state);
|
|
await send('Page.navigate', { url: `${origin}/?${query}` }, session);
|
|
await delay(2500);
|
|
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 2, mobile: true }, session);
|
|
await delay(1000);
|
|
if (enableSemantics) {
|
|
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
|
|
await delay(1200);
|
|
}
|
|
};
|
|
const waitForReport = async (mode) => {
|
|
for (let attempt = 0; attempt < 80; attempt++) {
|
|
const report = await evaluate('window.__probeReport');
|
|
if (report && report.includes(`mode=${mode}`)) return report;
|
|
await delay(150);
|
|
}
|
|
throw new Error(`The ${mode} scenario never reported`);
|
|
};
|
|
const notifications = () => evaluate('JSON.stringify(window.__notifications)');
|
|
const capture = async (name) => {
|
|
const data = (await send('Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, session)).data;
|
|
await writeFile(join(artifactDir, name), Buffer.from(data, 'base64'));
|
|
};
|
|
|
|
const grant = () => send('Browser.grantPermissions', { origin, permissions: ['notifications'] });
|
|
const deny = () => send('Browser.setPermission', {
|
|
permission: { name: 'notifications' },
|
|
setting: 'denied',
|
|
origin,
|
|
});
|
|
const prompt = () => send('Browser.setPermission', {
|
|
permission: { name: 'notifications' },
|
|
setting: 'prompt',
|
|
origin,
|
|
});
|
|
|
|
const checks = [];
|
|
const captures = [];
|
|
const check = (name, condition, detail) => {
|
|
if (!condition) throw new Error(`${name}: ${detail}`);
|
|
checks.push(name);
|
|
};
|
|
|
|
await prompt();
|
|
|
|
// 1. The initial list seed never notifies.
|
|
await navigate({ mode: 'seed' });
|
|
const seedReport = await waitForReport('seed');
|
|
check('seed report', seedReport.includes('posted=0'), seedReport);
|
|
check('seed silent', (await notifications()) === '[]', await notifications());
|
|
|
|
// 2. One live transition notifies exactly once, with the entry's own words.
|
|
await grant();
|
|
await navigate({ mode: 'transition' });
|
|
const transitionReport = await waitForReport('transition');
|
|
check('transition posted once', transitionReport.includes('posted=1'), transitionReport);
|
|
const posted = JSON.parse(await notifications());
|
|
check('transition count', posted.length === 1, JSON.stringify(posted));
|
|
check('transition title', posted[0].title === 'A run failed', JSON.stringify(posted[0]));
|
|
check('transition body', posted[0].body === 'Nightly audit · nightly', JSON.stringify(posted[0]));
|
|
check('transition identity', posted[0].tag === '1:Failed', JSON.stringify(posted[0]));
|
|
await capture('notification-transition-material-dark-320-2x.png');
|
|
captures.push('notification-transition-material-dark-320-2x.png');
|
|
|
|
// 3. A dismissed transition never notifies.
|
|
await navigate({ mode: 'dismissed' });
|
|
const dismissedReport = await waitForReport('dismissed');
|
|
check('dismissed report', dismissedReport.includes('posted=0'), dismissedReport);
|
|
check('dismissed silent', (await notifications()) === '[]', await notifications());
|
|
|
|
// 4. A reconnect replay never notifies again.
|
|
await navigate({ mode: 'reconnect' });
|
|
const reconnectReport = await waitForReport('reconnect');
|
|
check('reconnect posted once', reconnectReport.includes('posted=1'), reconnectReport);
|
|
check('reconnect count', JSON.parse(await notifications()).length === 1, await notifications());
|
|
|
|
// 4b. A second decision gate of one run notifies again. The waiting frames
|
|
// carry no finished_at, so the run's timestamps cannot tell the gates apart.
|
|
await navigate({ mode: 'secondgate' });
|
|
const secondGateReport = await waitForReport('secondgate');
|
|
check('second gate posted twice', secondGateReport.includes('posted=2'), secondGateReport);
|
|
const gates = JSON.parse(await notifications());
|
|
check('second gate count', gates.length === 2, JSON.stringify(gates));
|
|
check(
|
|
'second gate identity',
|
|
gates.every((item) => item.tag === '7:Decision'),
|
|
JSON.stringify(gates),
|
|
);
|
|
check(
|
|
'second gate title',
|
|
gates.every((item) => item.title === 'A run needs your decision'),
|
|
JSON.stringify(gates),
|
|
);
|
|
|
|
// 5. A denied page is honest and posts nothing.
|
|
await deny();
|
|
await navigate({ mode: 'denied', semantics: true });
|
|
const deniedReport = await waitForReport('denied');
|
|
check('denied report', deniedReport.includes('state=denied') && deniedReport.includes('posted=0'), deniedReport);
|
|
check('denied silent', (await notifications()) === '[]', await notifications());
|
|
requireLabels(await semantics().then((rows) => rows.map((r) => r.label)), [
|
|
'Notifications are blocked for this site.',
|
|
], 'denied tile');
|
|
await capture('notification-denied-material-dark-320-2x.png');
|
|
captures.push('notification-denied-material-dark-320-2x.png');
|
|
|
|
// 6. An unsupported browser is honest and posts nothing.
|
|
await grant();
|
|
await navigate({ mode: 'unsupported', semantics: true });
|
|
const unsupportedReport = await waitForReport('unsupported');
|
|
check('unsupported report', unsupportedReport.includes('state=unsupported'), unsupportedReport);
|
|
check('unsupported silent', (await notifications()) === '[]', await notifications());
|
|
requireLabels(await semantics().then((rows) => rows.map((r) => r.label)), [
|
|
'This browser does not support notifications.',
|
|
], 'unsupported tile');
|
|
await capture('notification-unsupported-material-dark-320-2x.png');
|
|
captures.push('notification-unsupported-material-dark-320-2x.png');
|
|
|
|
// 7. Each tile state reads honestly on both hosts, at the hardest case:
|
|
// 320 logical px and 2x text.
|
|
await prompt();
|
|
const states = {
|
|
unsupported: 'This browser does not support notifications.',
|
|
insecure: 'Notifications need a secure context (https or localhost).',
|
|
default: 'Notifications are off. Enable them to be told when a run fails',
|
|
granted: 'Notifications are on. Failed and waiting runs will raise a browser notification.',
|
|
denied: 'Notifications are blocked for this site.',
|
|
};
|
|
for (const host of ['material', 'cupertino']) {
|
|
for (const [state, copy] of Object.entries(states)) {
|
|
await navigate({ mode: 'tile', host, scale: 2, semantics: true, state });
|
|
const labels = (await semantics()).map((row) => row.label);
|
|
requireLabels(labels, [
|
|
'Pipelines can notify you when a run is waiting',
|
|
copy,
|
|
], `${host} ${state} tile`);
|
|
if (state === 'default') {
|
|
requireLabels(labels, ['Enable notifications'], `${host} ${state} action`);
|
|
}
|
|
checks.push(`${host} ${state} tile`);
|
|
const name = `notification-tile-${state}-${host}-dark-320-2x.png`;
|
|
await capture(name);
|
|
captures.push(name);
|
|
}
|
|
}
|
|
|
|
console.log(JSON.stringify({ checks, captures }, 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 });
|
|
}
|