Files
archie/e2e/run.cjs
T

75 lines
3.8 KiB
JavaScript

// Drive the dashboard of a running local stack (e2e/stack.sh) in headless
// Chromium. Each module in checks/ exports async ({ page, go, shot, fail }).
// A check fails on a thrown error or any console or page error.
//
// node e2e/run.cjs [--record VERSION] [check ...]
//
// Screenshots and results.json 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 { chromium } = require('playwright');
const base = process.env.ARCHIE_E2E_URL || 'http://localhost:5173';
const args = process.argv.slice(2);
let record = '';
if (args[0] === '--record') { record = args[1]; args.splice(0, 2); }
const names = args.length ? args : fs.readdirSync(path.join(__dirname, 'checks')).filter((f) => f.endsWith('.cjs')).map((f) => f.slice(0, -4)).sort();
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();
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) {
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); };
const shot = (label) => page.screenshot({ path: path.join(out, `${name}-${label.replace(/[^a-z0-9-]+/gi, '_') || 'root'}.png`) });
const fail = (msg) => notes.push(msg);
const started = Date.now();
let error = '';
try { await check({ page, go, shot, fail }); } catch (e) { error = String(e && e.message || e); }
await page.close();
const problems = [error, ...notes, ...errors].filter(Boolean);
results.push({ check: name, ok: problems.length === 0, ms: Date.now() - started, problems });
console.log(`${problems.length ? 'FAIL' : 'ok '} ${name}${problems.length ? '\n ' + problems.join('\n ') : ''}`);
}
await browser.close();
fs.writeFileSync(path.join(out, 'results.json'), JSON.stringify(results, null, 2));
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 writeRecord(version, results) {
const dir = path.join(__dirname, 'results');
fs.mkdirSync(dir, { recursive: true });
const rows = results.map((r) => `| ${r.check} | ${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'));
}