// 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 report.html (the run report) go to // e2e/out//. --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 -- \nAll: task e2e:check -- all\nWatch: task e2e:check -- --headed `); 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) => { if (m.type() !== 'error') return; const url = m.location().url; errors.push(url ? `${m.text()} (${url})` : 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 artifacts = []; // -[-x].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) }); artifacts.push({ file }); return file; }; // Keep a non-image artifact for this case: written as -. const attach = (fileName, data) => { const file = `${name}-${fileName}`; fs.writeFileSync(path.join(out, file), data); artifacts.push({ file }); }; const fail = (msg) => notes.push(msg); // Drop console errors matching re: a response the page is designed to survive. const tolerated = []; const tolerate = (re) => tolerated.push(re); 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, attach, tolerate }); } catch (e) { if (e instanceof SkipCheck) skipped = e.message; else error = String(e && e.message || e); } if (error || notes.length) await shot('failure').catch(() => {}); await page.close(); const problems = skipped ? [] : [error, ...notes, ...errors.filter((e) => !tolerated.some((re) => re.test(e)))].filter(Boolean); const result = { check: name, ok: problems.length === 0, ms: Date.now() - started, problems, artifacts }; 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)); const commit = require('child_process').execFileSync('git', ['rev-parse', '--short', 'HEAD'], { cwd: __dirname, encoding: 'utf8' }).trim(); fs.writeFileSync(path.join(out, 'report.html'), report(results, { time: new Date().toISOString(), commit })); 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, '"'); } // One self-contained page per run: a test plan and verification record. Each // case carries the artifacts its own run produced; images are inlined. function report(results, meta) { const status = (r) => (r.skipped ? 'SKIP' : r.ok ? 'ok' : 'FAIL'); const count = (s) => results.filter((r) => status(r) === s).length; const artifact = (a) => { const data = fs.readFileSync(path.join(out, a.file)); if (!a.file.endsWith('.png')) return `${esc(a.file)}`; const src = `data:image/png;base64,${data.toString('base64')}`; return `
${esc(a.file)}
`; }; const rows = results.map((r) => { const s = status(r); const msg = r.skipped ? [r.skipReason] : r.problems; return `${esc(r.check)}${s}${r.ms}ms` + `${msg.map((m) => `
${esc(m)}
`).join('')}` + `${r.artifacts.map(artifact).join('')}`; }).join('\n'); return ` e2e ${esc(meta.time)}

e2e ${esc(meta.time)}

commit ${esc(meta.commit)} against ${esc(base)}: ${results.length} cases, ${count('ok')} ok, ${count('FAIL')} FAIL, ${count('SKIP')} SKIP

${rows}
CaseStatusTimeMessageArtifacts
`; } 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('
').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')); }