#!/usr/bin/env node // EVERY E2E SUITE, SIDE BY SIDE, ONE SUMMARY (release 24, plans/e2e-speed.md // S4) — the release gate's e2e in one command: // // pnpm e2e:all [--only editor,umtool] [--logs ] // // Each package's suite queues under its own name (`queue-lock.mjs --name // e2e-`), so they no longer wait for one another — only for the heavy // slot, which admits each by its memory estimate (an e2e suite 1.5 GB; a // start-mode rebuild is a 5 GB build, one at a time). This starts one LANE per // package at once; a lane runs its package's suites in order (export has four // configs, which share one queue). Every suite still goes through its own // package script, so the queue, the port preflight, the start-mode build and // every bypass (E2E_QUEUE=0, HEAVY=0, E2E_MODE=dev, …) behave exactly as when // it is run alone. The wall time becomes the longest lane, not the sum — except // that a lane sharing a tree of the checkout with another starts after it // (`after`: export's after the editor's, both use export/public). // // Output: one line as each suite starts and ends; each suite's full output in // `/.log` (default `test-results-e2e-all/`, gitignored); then one // summary — per suite: passed / failed / flaky / skipped from its own json // report (only a report written by THIS run counts), its wall time, how long // it waited for the queue and the slot, and every failed test by file:line. // Exit 0 only when every suite exited 0. import { spawn } from "node:child_process"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const SELF = fileURLToPath(import.meta.url); export const REPO_ROOT = path.resolve(path.dirname(SELF), ".."); export const DEFAULT_LOG_DIR = "test-results-e2e-all"; // The suites, by lane. `report` is the json reporter's outputFile, relative // to the repo root (each config's `["json", { outputFile }]`). export const LANES = Object.freeze([ { pkg: "editor", suites: [{ id: "editor", script: "e2e", report: "editor/test-results/timings.json" }] }, { pkg: "export", // SHARED TREES: the editor suite's export dev server serves the checkout's // `export/public` and its build specs point `export/out`; export's suites // rewrite both (e2e:2origin's hub build composes into export/public and // builds export/out; the default config writes public/sw.js). The first // `pnpm e2e:all` (release 24) ran them at once: the hub build replaced // export/public under the editor suite, 20 export specs timed out and the // editor's next start could not boot. So this lane starts after the // editor's ends (and the editor suite holds the e2e-export queue too). after: [{ pkg: "editor", shares: "export/public, export/out" }], suites: [ { id: "export", script: "e2e", report: "export/test-results/timings.json" }, { id: "export-hub", script: "e2e:hub", report: "export/test-results-hub/timings.json" }, { id: "export-2origin", script: "e2e:2origin", report: "export/test-results-2origin/timings.json" }, { id: "export-report", script: "e2e:report", report: "export/test-results-report/timings.json" }, ], }, { pkg: "homepage", suites: [{ id: "homepage", script: "e2e", report: "homepage/test-results/timings.json" }] }, { pkg: "umtool", suites: [{ id: "umtool", script: "e2e", report: "umtool/test-results/timings.json" }] }, ]); /** * Which selected lanes each lane waits for: `pkg → [pkg…]`, from the lanes' * `after` (a tree they share). A lane not selected is not waited for. */ export function laneWaits(lanes) { const selected = new Set(lanes.map((l) => l.pkg)); return new Map(lanes.map((l) => [l.pkg, (l.after ?? []).map((a) => a.pkg).filter((p) => selected.has(p))])); } /** The lanes `--only` names (a package, or a suite id); all of them by default. */ export function selectLanes(only, lanes = LANES) { if (!only || !only.length) return lanes; const want = new Set(only); const out = []; for (const lane of lanes) { const suites = want.has(lane.pkg) ? lane.suites : lane.suites.filter((s) => want.has(s.id)); if (suites.length) out.push({ ...lane, suites }); } const known = new Set(lanes.flatMap((l) => [l.pkg, ...l.suites.map((s) => s.id)])); const unknown = only.filter((o) => !known.has(o)); if (unknown.length) throw new Error(`e2e:all: no suite named ${unknown.join(", ")} (one of: ${[...known].join(", ")})`); return out; } /** * A Playwright json report → counts and failures. `startedAfter` (ms) rejects * a report left by an earlier run: a suite that died before writing its own * would otherwise be summarised from last week's. */ export function readReport(report, { startedAfter = 0 } = {}) { if (!report?.stats) return null; const start = Date.parse(report.stats.startTime ?? ""); if (startedAfter && !(start >= startedAfter - 1000)) return null; const failures = []; const walk = (suite) => { for (const spec of suite.specs ?? []) { for (const t of spec.tests ?? []) { if (t.status === "unexpected") { failures.push(`${spec.file ?? suite.file ?? "?"}:${spec.line ?? "?"} ${spec.title}`); } } } for (const child of suite.suites ?? []) walk({ ...child, file: child.file ?? suite.file }); }; for (const s of report.suites ?? []) walk(s); return { passed: Number(report.stats.expected) || 0, failed: Number(report.stats.unexpected) || 0, flaky: Number(report.stats.flaky) || 0, skipped: Number(report.stats.skipped) || 0, durationMs: Number(report.stats.duration) || 0, startMs: Number.isFinite(start) ? start : null, failures, }; } export function humanMs(ms) { if (ms == null || !Number.isFinite(ms)) return "—"; const s = Math.max(0, Math.round(ms / 1000)); return s < 60 ? `${s}s` : `${Math.floor(s / 60)}m${String(s % 60).padStart(2, "0")}s`; } /** * The summary, from one result per suite: * `{id, code, launchedMs, endedMs, report: readReport(…)|null, log}`. */ export function renderSummary(results, { wallMs } = {}) { const rows = [["suite", "result", "passed", "failed", "flaky", "skipped", "tests", "waited", "log"]]; for (const r of results) { const rep = r.report; const result = r.code === 0 ? "ok" : rep ? `FAIL (exit ${r.code})` : `FAIL (exit ${r.code}, no report)`; const waited = rep?.startMs != null && r.launchedMs != null ? rep.startMs - r.launchedMs : null; rows.push([ r.id, result, rep ? String(rep.passed) : "—", rep ? String(rep.failed) : "—", rep ? String(rep.flaky) : "—", rep ? String(rep.skipped) : "—", rep ? humanMs(rep.durationMs) : humanMs(r.endedMs - r.launchedMs), humanMs(waited), r.log ?? "", ]); } const widths = rows[0].map((_, i) => Math.max(...rows.map((row) => row[i].length))); const lines = rows.map((row) => row.map((c, i) => c.padEnd(widths[i])).join(" ").trimEnd()); lines.splice(1, 0, widths.map((w) => "-".repeat(w)).join(" ")); const total = (k) => results.reduce((n, r) => n + (r.report?.[k] ?? 0), 0); const sumMs = results.reduce((n, r) => n + (r.report?.durationMs ?? r.endedMs - r.launchedMs), 0); const ok = results.every((r) => r.code === 0); lines.push(""); lines.push( `e2e:all ${ok ? "PASSED" : "FAILED"} — ${total("passed")} passed, ${total("failed")} failed, ` + `${total("flaky")} flaky, ${total("skipped")} skipped; wall ${humanMs(wallMs)} ` + `(the suites' own times add up to ${humanMs(sumMs)})`, ); const failed = results.filter((r) => r.report?.failures?.length); for (const r of failed) { lines.push(""); lines.push(`${r.id} — failed:`); for (const f of r.report.failures.slice(0, 20)) lines.push(` ${f}`); if (r.report.failures.length > 20) lines.push(` … and ${r.report.failures.length - 20} more`); } return `${lines.join("\n")}\n`; } // ------------------------------------------------------------------ the run function parseArgs(argv) { const opts = { only: null, logs: DEFAULT_LOG_DIR }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--only") opts.only = String(argv[++i] ?? "").split(",").map((s) => s.trim()).filter(Boolean); else if (a === "--logs") opts.logs = argv[++i]; else if (a === "--help" || a === "-h") opts.help = true; else throw new Error(`e2e:all: unknown argument '${a}'`); } return opts; } const children = new Set(); function runSuite(lane, suite, logDir) { const log = path.join(logDir, `${suite.id}.log`); const out = fs.openSync(log, "w"); const launchedMs = Date.now(); process.stdout.write(`e2e:all: ${suite.id} started (pnpm --filter ${lane.pkg} run ${suite.script}) — ${path.relative(REPO_ROOT, log)}\n`); // Through the worktree wrapper, so a worktree's suites use its own ports. const child = spawn( process.execPath, [path.join(REPO_ROOT, "scripts", "worktree.mjs"), "run", "--", "pnpm", "--filter", lane.pkg, "run", suite.script], { cwd: REPO_ROOT, stdio: ["ignore", out, out], env: process.env }, ); children.add(child); return new Promise((resolve) => { const done = (code) => { children.delete(child); fs.closeSync(out); const endedMs = Date.now(); let report = null; try { report = readReport(JSON.parse(fs.readFileSync(path.join(REPO_ROOT, suite.report), "utf8")), { startedAfter: launchedMs, }); } catch { /* no report: the suite died before writing one */ } const counts = report ? `${report.passed} passed, ${report.failed} failed, ${report.skipped} skipped` : "no report"; process.stdout.write( `e2e:all: ${suite.id} ${code === 0 ? "done" : `FAILED (exit ${code})`} in ${humanMs(endedMs - launchedMs)} — ${counts}\n`, ); resolve({ id: suite.id, code, launchedMs, endedMs, report, log: path.relative(REPO_ROOT, log) }); }; child.on("error", () => done(1)); child.on("exit", (code, signal) => done(signal ? 1 : (code ?? 1))); }); } async function runLane(lane, logDir) { const results = []; for (const suite of lane.suites) results.push(await runSuite(lane, suite, logDir)); return results; } async function main(argv) { const opts = parseArgs(argv); if (opts.help) { process.stdout.write("usage: pnpm e2e:all [--only ,…] [--logs ]\n"); return 0; } const lanes = selectLanes(opts.only); const logDir = path.resolve(REPO_ROOT, opts.logs); fs.mkdirSync(logDir, { recursive: true }); // A terminal's Ctrl-C reaches every suite already (one process group); a // SIGTERM sent to this process alone is passed on, so nothing is orphaned. for (const sig of ["SIGTERM", "SIGHUP"]) { process.on(sig, () => { for (const c of children) { try { if (c.pid !== undefined) c.kill(sig); } catch { /* already gone */ } } }); } process.on("SIGINT", () => process.stdout.write("\ne2e:all: SIGINT — waiting for the suites to stop\n")); const t0 = Date.now(); const waits = laneWaits(lanes); const running = new Map(); for (const lane of lanes) { const before = waits.get(lane.pkg).map((p) => running.get(p)); running.set( lane.pkg, Promise.all(before).then(() => { if (before.length) { const reasons = lane.after.filter((a) => waits.get(lane.pkg).includes(a.pkg)); process.stdout.write( `e2e:all: ${lane.pkg} lane starts after ${reasons.map((a) => `${a.pkg} (they share ${a.shares})`).join(", ")}\n`, ); } return runLane(lane, logDir); }), ); } const results = (await Promise.all(lanes.map((l) => running.get(l.pkg)))).flat(); process.stdout.write(`\n${renderSummary(results, { wallMs: Date.now() - t0 })}`); for (const r of results.filter((x) => x.code !== 0 && !x.report)) { const tail = fs.readFileSync(path.join(REPO_ROOT, r.log), "utf8").trimEnd().split("\n").slice(-12); process.stdout.write(`\n${r.id} — last lines of ${r.log}:\n${tail.map((l) => ` ${l}`).join("\n")}\n`); } return results.every((r) => r.code === 0) ? 0 : 1; } if (process.argv[1] && fs.realpathSync(process.argv[1]) === fs.realpathSync(SELF)) { main(process.argv.slice(2)).then( (code) => process.exit(code), (err) => { process.stderr.write(`${err?.message ?? err}\n`); process.exit(2); }, ); }