// 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//. --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) => 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 = []; // -[-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) }); 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, '"'); } // 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 = `

${esc(r.check)} ${status} ${r.ms}ms

`; const skipNote = r.skipped ? `

skipped: ${esc(r.skipReason || '')}

` : ''; const probs = r.problems.length ? `
    ${r.problems.map((p) => `
  • ${esc(p)}
  • `).join('')}
` : ''; const shots = r.shots.length ? `
${r.shots.map((s) => `
${esc(s.file)} ${s.w}x${s.h}
`).join('')}
` : '

no screenshots

'; return `
${head}${skipNote}${probs}${shots}
`; }).join('\n'); const passed = results.filter((r) => r.ok && !r.skipped).length; const skipped = results.filter((r) => r.skipped).length; return ` e2e ${esc(stamp)}

e2e run ${esc(stamp)}

${passed} passed, ${skipped} skipped, ${results.length - passed - skipped} failed against ${esc(base)}.

${sections} `; } 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')); }