369 lines
14 KiB
JavaScript
369 lines
14 KiB
JavaScript
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 { join, extname, resolve, sep } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import { deepStrictEqual } from 'node:assert/strict';
|
|
|
|
// Real-browser acceptance for the native saved-schedule form.
|
|
//
|
|
// flutter build web -t tool/schedule_editor_probe_app.dart \
|
|
// -o build/schedule_editor_probe_web
|
|
// OPENCODE_ARTIFACT_DIR=<dir> node tool/schedule_editor_probe.mjs
|
|
//
|
|
// The fixture callback and record are in-process; no Pipelines server,
|
|
// authentication or real schedule is contacted.
|
|
|
|
const root = resolve(process.env.PROBE_WEB_ROOT ?? 'build/schedule_editor_probe_web');
|
|
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 chromePath = process.env.CHROME_PATH
|
|
?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
|
|
const mime = {
|
|
'.html': 'text/html; charset=utf-8',
|
|
'.js': 'text/javascript; charset=utf-8',
|
|
'.mjs': 'text/javascript; charset=utf-8',
|
|
'.json': 'application/json',
|
|
'.wasm': 'application/wasm',
|
|
'.png': 'image/png',
|
|
'.otf': 'font/otf',
|
|
'.ttf': 'font/ttf',
|
|
'.woff': 'font/woff',
|
|
'.woff2': 'font/woff2',
|
|
'.ico': 'image/x-icon',
|
|
'.bin': 'application/octet-stream',
|
|
};
|
|
|
|
const server = createServer(async (req, res) => {
|
|
try {
|
|
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
|
|
const path = resolve(root, `.${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',
|
|
'cache-control': 'no-store',
|
|
})
|
|
.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(), 's1-form-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, label, attempts = 220) => {
|
|
for (let i = 0; i < attempts; i++) {
|
|
const value = await fn();
|
|
if (value) return value;
|
|
await delay(100);
|
|
}
|
|
throw new Error(`Timed out waiting for ${label}\n${stderr.slice(-1200)}`);
|
|
};
|
|
|
|
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();
|
|
const pageErrors = [];
|
|
ws.addEventListener('message', (event) => {
|
|
const message = JSON.parse(event.data);
|
|
if (message.method === 'Runtime.exceptionThrown') {
|
|
pageErrors.push(message.params?.exceptionDetails?.exception?.description ?? 'page exception');
|
|
}
|
|
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);
|
|
|
|
let width = 390;
|
|
let height = 844;
|
|
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(`Runtime exception: ${JSON.stringify(response.result.exceptionDetails)}`);
|
|
}
|
|
return response.result?.result?.value;
|
|
};
|
|
|
|
const text = () => evaluate(`(() => {
|
|
const chunks = [];
|
|
for (const el of document.querySelectorAll('flt-semantics')) {
|
|
const label = el.getAttribute('aria-label');
|
|
if (label) chunks.push(label);
|
|
if (!el.children.length && el.innerText) chunks.push(el.innerText);
|
|
}
|
|
for (const el of document.querySelectorAll('input, textarea')) {
|
|
if (el.getAttribute('aria-label')) chunks.push(el.getAttribute('aria-label') + '=' + el.value);
|
|
}
|
|
if (document.body?.innerText) chunks.push(document.body.innerText);
|
|
return [...new Set(chunks)].join('\\n');
|
|
})()`);
|
|
|
|
const assertText = async (needle) => {
|
|
try {
|
|
await waitFor(async () => (await text()).includes(needle), `text ${JSON.stringify(needle)}`);
|
|
} catch (error) {
|
|
throw new Error(`${error.message}\nText now:\n${(await text()).slice(0, 3000)}`);
|
|
}
|
|
};
|
|
|
|
const click = async (needle, optional = false) => {
|
|
const clicked = await evaluate(`(() => {
|
|
const wanted = ${JSON.stringify(needle)};
|
|
const nodes = [...document.querySelectorAll('flt-semantics')];
|
|
const node = nodes.find((x) => (x.getAttribute('aria-label') || '').trim() === wanted)
|
|
|| nodes.find((x) => (x.innerText || '').trim() === wanted)
|
|
|| nodes.find((x) => (x.getAttribute('aria-label') || x.innerText || '').trim().startsWith(wanted));
|
|
if (!node) return false;
|
|
node.click();
|
|
return true;
|
|
})()`);
|
|
if (!clicked && !optional) {
|
|
throw new Error(`Could not click ${JSON.stringify(needle)}\n${(await text()).slice(0, 3000)}`);
|
|
}
|
|
await delay(350);
|
|
return clicked;
|
|
};
|
|
|
|
const inputs = () => evaluate(`[...document.querySelectorAll('input,textarea')].map((el) => ({
|
|
label: el.getAttribute('aria-label') || '',
|
|
value: el.value,
|
|
disabled: el.disabled,
|
|
}))`);
|
|
|
|
const typeField = async (label, value) => {
|
|
const focused = await evaluate(`(() => {
|
|
const wanted = ${JSON.stringify(label)};
|
|
const el = [...document.querySelectorAll('input, textarea')].find((input) => {
|
|
const name = input.getAttribute('aria-label') || '';
|
|
return name === wanted || name.startsWith(wanted + '\\n');
|
|
});
|
|
if (!el) return false;
|
|
el.focus();
|
|
el.select?.();
|
|
el.setSelectionRange?.(0, (el.value || '').length);
|
|
return true;
|
|
})()`);
|
|
if (!focused) throw new Error(`No field ${JSON.stringify(label)}\n${(await text()).slice(0, 3000)}`);
|
|
await delay(250);
|
|
await send('Input.insertText', { text: value }, session);
|
|
await delay(350);
|
|
};
|
|
|
|
const state = () => evaluate('globalThis.scheduleEditorProbe ?? null');
|
|
const wheel = async (delta) => {
|
|
await send(
|
|
'Input.dispatchMouseEvent',
|
|
{ type: 'mouseWheel', x: Math.floor(width / 2), y: Math.floor(height / 2), deltaX: 0, deltaY: delta },
|
|
session,
|
|
);
|
|
await delay(400);
|
|
};
|
|
const capture = async (name) => {
|
|
const data = (await send('Page.captureScreenshot', { format: 'png' }, session)).result.data;
|
|
const file = `${name}.png`;
|
|
await writeFile(join(output, file), Buffer.from(data, 'base64'));
|
|
captures.push({ name, file, width, height });
|
|
return file;
|
|
};
|
|
|
|
const load = async (query, nextWidth, nextHeight) => {
|
|
width = nextWidth;
|
|
height = nextHeight;
|
|
await send(
|
|
'Emulation.setDeviceMetricsOverride',
|
|
{ width, height, deviceScaleFactor: 1, mobile: width < 600 },
|
|
session,
|
|
);
|
|
const navigation = await send('Page.navigate', { url: `${origin}/?${query}` }, session);
|
|
if (navigation.error || navigation.result?.errorText) {
|
|
throw new Error(`Navigation failed: ${JSON.stringify(navigation)}`);
|
|
}
|
|
await waitFor(
|
|
() => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),
|
|
'semantics placeholder',
|
|
);
|
|
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
|
|
await delay(1200);
|
|
await assertText('Add schedule');
|
|
};
|
|
|
|
const checks = [];
|
|
const captures = [];
|
|
|
|
// Material phone: invalid -> valid -> refusal -> exact record.
|
|
await load('host=material&scale=1', 390, 844);
|
|
await assertText('Future runs only; does not publish or change the workflow.');
|
|
await assertText('Weekdays (none means every day)');
|
|
await assertText('Enabled schedules start future runs automatically');
|
|
await click('Save schedule');
|
|
await delay(500);
|
|
let current = await state();
|
|
let fields = await inputs();
|
|
const nameField = fields.find((field) => field.label.startsWith('Name'));
|
|
const promptField = fields.find((field) => field.label.startsWith('Prompt'));
|
|
if (current?.submits !== 0) throw new Error(`Invalid form submitted: ${JSON.stringify(current)}`);
|
|
if (!nameField?.label.includes('Enter a schedule name.')) {
|
|
throw new Error(`Name error missing: ${JSON.stringify(fields)}`);
|
|
}
|
|
if (!promptField?.label.includes('Enter a prompt.')) {
|
|
throw new Error(`Prompt error missing: ${JSON.stringify(fields)}`);
|
|
}
|
|
await capture('s1-form-material-390-invalid');
|
|
checks.push({
|
|
check: 'invalid submit showed both field errors and sent nothing',
|
|
submits: current.submits,
|
|
fields,
|
|
});
|
|
|
|
await typeField('Name', 'Probe schedule');
|
|
await typeField('Prompt', 'Summarise the release notes.');
|
|
await click('Save schedule');
|
|
await delay(200);
|
|
current = await state();
|
|
if (current?.pending !== true || current?.submits !== 1) {
|
|
throw new Error(`First submit did not enter the sending state: ${JSON.stringify(current)}`);
|
|
}
|
|
await click('Saving…', true);
|
|
await delay(150);
|
|
current = await state();
|
|
if (current?.submits !== 1) {
|
|
throw new Error(`A second submit escaped while sending: ${JSON.stringify(current)}`);
|
|
}
|
|
checks.push({ check: 'sending disabled a duplicate submit', submits: current.submits });
|
|
|
|
await delay(1100);
|
|
current = await state();
|
|
fields = await inputs();
|
|
if (current?.pending !== false || current?.records !== 0) {
|
|
throw new Error(`Refusal state drifted: ${JSON.stringify(current)}`);
|
|
}
|
|
if (current?.lastError !== 'conflict: revision 13 is stale') {
|
|
throw new Error(`Refusal was not the fixture's: ${JSON.stringify(current)}`);
|
|
}
|
|
if (
|
|
fields.find((field) => field.label.startsWith('Name'))?.value !== 'Probe schedule'
|
|
|| fields.find((field) => field.label.startsWith('Prompt'))?.value
|
|
!== 'Summarise the release notes.'
|
|
) {
|
|
throw new Error(`Refusal lost typed values: ${JSON.stringify(fields)}`);
|
|
}
|
|
await wheel(900);
|
|
await assertText('Your draft is preserved.');
|
|
await capture('s1-form-material-390-refusal');
|
|
checks.push({
|
|
check: 'refusal kept both typed values, showed the preservation copy and re-enabled the form',
|
|
lastError: current.lastError,
|
|
refusalCopy: 'Your draft is preserved.',
|
|
values: fields.map((field) => [field.label.split('\n')[0], field.value]),
|
|
});
|
|
|
|
await click('Save schedule');
|
|
await waitFor(async () => (await state())?.records === 1, 'the accepted record');
|
|
current = await state();
|
|
deepStrictEqual(current.lastRecord, {
|
|
id: 0,
|
|
name: 'Probe schedule',
|
|
pipeline_id: 4,
|
|
version: 2,
|
|
enabled: true,
|
|
input: { prompt: 'Summarise the release notes.' },
|
|
workspace: '/srv/research',
|
|
rule: { every: '24h', timezone: 'Europe/London' },
|
|
notify_mode: 'auto',
|
|
});
|
|
await delay(500);
|
|
await wheel(1400);
|
|
await capture('s1-form-material-390-record');
|
|
checks.push({
|
|
check: 'retry submitted the exact create record once',
|
|
submits: current.submits,
|
|
records: current.records,
|
|
record: current.lastRecord,
|
|
});
|
|
|
|
// Narrow phone at 2x text: labels and helpers wrap, nothing overflows.
|
|
await load('host=material&scale=2', 320, 844);
|
|
await click('Save schedule');
|
|
await delay(600);
|
|
await capture('s1-form-material-320-2x-top');
|
|
for (let i = 0; i < 5; i++) await wheel(600);
|
|
await capture('s1-form-material-320-2x-bottom');
|
|
await assertText('Enabled schedules start future runs automatically');
|
|
await assertText('Save schedule');
|
|
checks.push({ check: '320px at 2x text kept every label and the actions', width: 320, scale: 2 });
|
|
|
|
// Actual Cupertino dark host, not a Material fallback.
|
|
await load('host=cupertino&brightness=dark&scale=1', 390, 844);
|
|
await assertText('Add schedule');
|
|
await capture('s1-form-cupertino-dark-390-top');
|
|
for (let i = 0; i < 3; i++) await wheel(600);
|
|
await capture('s1-form-cupertino-dark-390-bottom');
|
|
await assertText('Notifications');
|
|
checks.push({ check: 'Cupertino dark rendered the native form', host: 'cupertino', brightness: 'dark' });
|
|
|
|
if (pageErrors.length) throw new Error(`Page exceptions: ${JSON.stringify(pageErrors)}`);
|
|
const manifest = {
|
|
url: origin,
|
|
fixture: 'tool/schedule_editor_probe_app.dart',
|
|
output,
|
|
checks,
|
|
captures,
|
|
};
|
|
await writeFile(join(output, 'manifest.json'), JSON.stringify(manifest, null, 2));
|
|
console.log(JSON.stringify(manifest, 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 });
|
|
}
|