163 lines
8.7 KiB
JavaScript
163 lines
8.7 KiB
JavaScript
// Drive the dashboard of a running local stack (e2e/stack.sh) in Chromium. Each module in checks/ exports async ({ page, go, shot, fail, skip, settled }).
|
|
// A check fails on a thrown error or any console or page error. skip(reason)
|
|
// records a skip for a missing precondition and never fails.
|
|
//
|
|
// node e2e/run.cjs [--headed] [--record VERSION] [list | all | check ...]
|
|
//
|
|
// Screenshots, results.json (the manifest) and index.html (the gallery) go to
|
|
// e2e/out/<stamp>/. --record also writes e2e/results/VERSION.md, the committed
|
|
// evidence for a release.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const DESKTOP = { width: 1440, height: 900 };
|
|
|
|
// Stable label naming: kebab-case, so a route like /org/members becomes
|
|
// org-members and / becomes home.
|
|
function slug(label) {
|
|
return String(label).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') || 'home';
|
|
}
|
|
|
|
class SkipCheck extends Error {}
|
|
|
|
const base = process.env.ARCHIE_E2E_URL || 'http://localhost:5173';
|
|
const args = process.argv.slice(2);
|
|
const headed = args.includes('--headed');
|
|
if (headed) args.splice(args.indexOf('--headed'), 1);
|
|
let record = '';
|
|
if (args[0] === '--record') {
|
|
record = args[1];
|
|
if (!record || record.startsWith('-')) {
|
|
console.error('Usage: --record VERSION all|check ...');
|
|
process.exit(1);
|
|
}
|
|
args.splice(0, 2);
|
|
}
|
|
const available = fs.readdirSync(path.join(__dirname, 'checks')).filter((f) => f.endsWith('.cjs')).map((f) => f.slice(0, -4)).sort();
|
|
if (!args.length || (args.length === 1 && args[0] === 'list')) {
|
|
console.log(`Available browser checks:\n${available.map(name => ` ${name}`).join('\n')}\n\nRun: task e2e:check -- <check ...>\nAll: task e2e:check -- all\nWatch: task e2e:check -- --headed <check ...>`);
|
|
process.exit(0);
|
|
}
|
|
const names = args.length === 1 && args[0] === 'all' ? available : args;
|
|
const unknown = names.filter(name => !available.includes(name));
|
|
if (unknown.length) {
|
|
console.error(`Unknown checks: ${unknown.join(', ')}. Run task e2e:check to list available checks.`);
|
|
process.exit(1);
|
|
}
|
|
const { chromium } = require('playwright');
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
const out = path.join(__dirname, 'out', stamp);
|
|
fs.mkdirSync(out, { recursive: true });
|
|
|
|
(async () => {
|
|
const browser = await chromium.launch({ headless: !headed, slowMo: headed ? 150 : 0 });
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
|
|
const login = await context.request.post(base + '/api/auth/login', {
|
|
headers: { 'X-Archie-CSRF': '1', Origin: base },
|
|
data: { email: process.env.ARCHIE_E2E_EMAIL || 'operator@example.test', password: process.env.ARCHIE_E2E_PASSWORD || 'archie-local-e2e-password' },
|
|
});
|
|
if (!login.ok()) {
|
|
const error = `Test account sign-in failed (${login.status()}): ${await login.text()}`;
|
|
await browser.close();
|
|
throw new Error(error);
|
|
}
|
|
const storageState = await context.storageState();
|
|
await context.close();
|
|
const results = [];
|
|
for (const name of names) {
|
|
console.log(`run ${name}`);
|
|
const check = require(path.join(__dirname, 'checks', name + '.cjs'));
|
|
// A fresh page per check: open event streams pile up across navigations.
|
|
const page = await browser.newPage({ viewport: { width: 1440, height: 900 }, storageState });
|
|
const errors = [];
|
|
const notes = [];
|
|
page.on('pageerror', (e) => errors.push(String(e)));
|
|
page.on('console', (m) => m.type() === 'error' && errors.push(m.text()));
|
|
// Wait for load, never networkidle: the event stream never goes idle.
|
|
const go = async (p) => { await page.goto(base + p, { waitUntil: 'load' }); await page.waitForTimeout(1200); };
|
|
// Harness-owned wait: wait for a selector or URL, then settle briefly.
|
|
const settled = async (sel, timeout = 10000) => {
|
|
if (sel) {
|
|
if (/^https?:/.test(sel)) await page.waitForURL(sel, { timeout });
|
|
else if (sel.startsWith('/')) await page.waitForURL('**' + sel, { timeout });
|
|
else await page.locator(sel).first().waitFor({ timeout });
|
|
}
|
|
await page.waitForTimeout(150);
|
|
};
|
|
const shots = [];
|
|
// <check>-<kebab>[-<w>x<h>].png; the viewport suffix marks non-desktop
|
|
// shots. A short settle precedes every capture so checks need no capture
|
|
// wrapper of their own. Every capture is kept: a repeated label gets -2, -3...
|
|
const shot = async (label) => {
|
|
await page.waitForTimeout(150);
|
|
const vp = page.viewportSize() || DESKTOP;
|
|
const suffix = (vp.width === DESKTOP.width && vp.height === DESKTOP.height) ? '' : `-${vp.width}x${vp.height}`;
|
|
const stem = `${name}-${slug(label)}${suffix}`;
|
|
let file = `${stem}.png`;
|
|
for (let n = 2; fs.existsSync(path.join(out, file)); n++) file = `${stem}-${n}.png`;
|
|
await page.screenshot({ path: path.join(out, file) });
|
|
shots.push({ file, w: vp.width, h: vp.height, check: name });
|
|
return file;
|
|
};
|
|
const fail = (msg) => notes.push(msg);
|
|
const skip = (reason) => { throw new SkipCheck(reason || 'precondition missing'); };
|
|
const started = Date.now();
|
|
let error = '';
|
|
let skipped = '';
|
|
try { await check({ page, go, shot, fail, skip, settled }); } catch (e) {
|
|
if (e instanceof SkipCheck) skipped = e.message;
|
|
else error = String(e && e.message || e);
|
|
}
|
|
await page.close();
|
|
const problems = skipped ? [] : [error, ...notes, ...errors].filter(Boolean);
|
|
const result = { check: name, ok: problems.length === 0, ms: Date.now() - started, problems, shots };
|
|
if (skipped) { result.skipped = true; result.skipReason = skipped; }
|
|
results.push(result);
|
|
console.log(`${skipped ? 'SKIP' : problems.length ? 'FAIL' : 'ok '} ${name}${skipped ? ` (${skipped})` : problems.length ? '\n ' + problems.join('\n ') : ''}`);
|
|
}
|
|
await browser.close();
|
|
fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify(results, null, 2));
|
|
fs.writeFileSync(path.join(out, 'index.html'), gallery(results));
|
|
if (record) writeRecord(record, results);
|
|
console.log(`evidence: ${out}`);
|
|
process.exit(results.every((r) => r.ok) ? 0 : 1);
|
|
})().catch((e) => { console.error(e); process.exit(2); });
|
|
|
|
function esc(s) {
|
|
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
}
|
|
|
|
// Per-run gallery for human image review, grouped by check.
|
|
function gallery(results) {
|
|
const sections = results.map((r) => {
|
|
const status = r.skipped ? 'skip' : r.ok ? 'pass' : 'fail';
|
|
const head = `<h2>${esc(r.check)} <span class="${status}">${status}</span> <small>${r.ms}ms</small></h2>`;
|
|
const skipNote = r.skipped ? `<p class="skip-reason">skipped: ${esc(r.skipReason || '')}</p>` : '';
|
|
const probs = r.problems.length
|
|
? `<ul class="problems">${r.problems.map((p) => `<li>${esc(p)}</li>`).join('')}</ul>` : '';
|
|
const shots = r.shots.length
|
|
? `<div class="shots">${r.shots.map((s) => `<figure><a href="${esc(s.file)}"><img src="${esc(s.file)}" loading="lazy"></a><figcaption>${esc(s.file)} ${s.w}x${s.h}</figcaption></figure>`).join('')}</div>`
|
|
: '<p class="no-shots">no screenshots</p>';
|
|
return `<section class="${status}">${head}${skipNote}${probs}${shots}</section>`;
|
|
}).join('\n');
|
|
const passed = results.filter((r) => r.ok && !r.skipped).length;
|
|
const skipped = results.filter((r) => r.skipped).length;
|
|
return `<!doctype html>
|
|
<html><head><meta charset="utf-8"><title>e2e ${esc(stamp)}</title>
|
|
<style>body{font-family:system-ui,sans-serif;max-width:1200px;margin:0 auto;padding:1em}h2 small{color:#666;font-weight:normal}.pass>h2 .pass,.fail>h2 .fail,.skip>h2 .skip{padding:.1em .5em;border-radius:.3em;font-size:.8em}.pass>h2 .pass{background:#dfd}.fail>h2 .fail{background:#fdd}.skip>h2 .skip{background:#eef}.problems{color:#a00}.skip-reason{color:#555}.no-shots{color:#888;font-style:italic}.shots{display:flex;flex-wrap:wrap;gap:1em}figure{margin:0;width:320px}img{width:100%;border:1px solid #ccc}figcaption{font-size:.8em;color:#555;word-break:break-all}section{border-top:1px solid #ddd;padding-bottom:1em}</style>
|
|
</head><body><h1>e2e run ${esc(stamp)}</h1><p>${passed} passed, ${skipped} skipped, ${results.length - passed - skipped} failed against ${esc(base)}.</p>${sections}</body></html>
|
|
`;
|
|
}
|
|
|
|
function writeRecord(version, results) {
|
|
const dir = path.join(__dirname, 'results');
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
const rows = results.map((r) => `| ${r.check} | ${r.skipped ? 'skip' : r.ok ? 'pass' : 'fail'} | ${r.problems.join('<br>').replace(/\|/g, '\\|') || ''} |`);
|
|
const passed = results.filter((r) => r.ok).length;
|
|
fs.writeFileSync(path.join(dir, `${version}.md`), [
|
|
`# ${version}`, '',
|
|
`Run ${new Date().toISOString()} against ${base}: ${passed} of ${results.length} checks passed.`, '',
|
|
'| Check | Result | Problems |', '| --- | --- | --- |', ...rows, '',
|
|
].join('\n'));
|
|
}
|