askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
e2e.mjs · 10.6 KB · 161 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * End-to-end suites: drive the REAL browser through its own automation API. |
| 4 | * |
| 5 | * npm run e2e -w @dbbasic/browser # every suite |
| 6 | * npm run e2e -w @dbbasic/browser -- api states # just those |
| 7 | * E2E_LIVE_NET=1 adds suites/checks that touch real sites (askrobots.com's bot check) |
| 8 | * E2E_LIVE_AI=1 adds checks that call the AI providers with your real keys (a few cents) |
| 9 | * |
| 10 | * Hermetic by construction: it picks free ports, makes a temp profile and a temp vault (seeded with the |
| 11 | * demo login), runs its own copy of the demo server, and keeps its control token in the temp dir — so it |
| 12 | * never touches your running browser, your real vault, or ~/.dbbasic/control.json. Each suite gets a freshly |
| 13 | * launched app, so suites can't contaminate each other. |
| 14 | * |
| 15 | * Needs a desktop session (it opens real windows): macOS, or Linux with a display. On a Linux machine with no screen, run it |
| 16 | * under a virtual one (`xvfb-run -a npm run e2e …`, as CI does) and without E2E_BACKGROUND, which hides the window in a way a |
| 17 | * virtual display stops drawing. Exit code is non-zero if any check fails. |
| 18 | */ |
| 19 | import { spawn } from "node:child_process"; |
| 20 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 21 | import { createServer } from "node:net"; |
| 22 | import { tmpdir } from "node:os"; |
| 23 | import { dirname, join } from "node:path"; |
| 24 | import { fileURLToPath, pathToFileURL } from "node:url"; |
| 25 | import { makeCtx } from "../test/e2e/lib.mjs"; |
| 26 | |
| 27 | const here = dirname(fileURLToPath(import.meta.url)); |
| 28 | const root = join(here, ".."); |
| 29 | const SUITES = ["api", "ui", "transport", "states", "reader", "downloads", "settle", "provider", "escape", "example", "extensions", "forms", "chat", "mcp", "resources", "bookmarks", "keep", "omnibox", "menu", "history", "live-net"]; |
| 30 | const wanted = process.argv.slice(2).filter((a) => !a.startsWith("-")); |
| 31 | const run = wanted.length ? wanted : SUITES; |
| 32 | for (const s of run) if (!SUITES.includes(s)) { console.error(`unknown suite "${s}" (have: ${SUITES.join(", ")})`); process.exit(2); } |
| 33 | |
| 34 | const freePort = () => new Promise((res, rej) => { const s = createServer(); s.listen(0, "127.0.0.1", () => { const p = s.address().port; s.close(() => res(p)); }); s.on("error", rej); }); |
| 35 | const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 36 | async function waitFor(fn, what, ms = 40000) { |
| 37 | const end = Date.now() + ms; |
| 38 | for (;;) { |
| 39 | try { const v = await fn(); if (v) return v; } catch { /* not yet */ } |
| 40 | if (Date.now() > end) throw new Error(`timed out waiting for ${what}`); |
| 41 | await sleep(250); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // E2E_PARALLEL=n runs up to n suites at once. Each suite is its own process with its own demo server, ports, profile and vault, |
| 46 | // so nothing is shared; output is printed per suite when it finishes. (Costs memory: one browser per running suite.) |
| 47 | const parallel = Number(process.env.E2E_PARALLEL || 1); |
| 48 | if (parallel > 1 && run.length > 1 && process.env.E2E_CHILD !== "1") { |
| 49 | const { spawn: sp } = await import("node:child_process"); |
| 50 | const queue = [...run]; |
| 51 | const results = []; |
| 52 | const runOne = (name) => new Promise((resolve) => { |
| 53 | if (name === "live-net" && process.env.E2E_LIVE_NET !== "1") return resolve({ name, out: `\n━━ ${name} ━━ (skipped: set E2E_LIVE_NET=1)\n`, pass: 0, fail: 0 }); |
| 54 | const child = sp(process.execPath, [fileURLToPath(import.meta.url), name], { env: { ...process.env, E2E_CHILD: "1" }, stdio: ["ignore", "pipe", "pipe"] }); |
| 55 | let out = ""; |
| 56 | child.stdout.on("data", (d) => (out += d)); |
| 57 | child.stderr.on("data", (d) => (out += d)); |
| 58 | child.on("close", () => { |
| 59 | const m = /(\d+) passed, (\d+) failed/.exec(out); |
| 60 | resolve({ name, out, pass: m ? Number(m[1]) : 0, fail: m ? Number(m[2]) : 1 }); |
| 61 | }); |
| 62 | }); |
| 63 | const worker = async () => { for (let n = queue.shift(); n; n = queue.shift()) { const r = await runOne(n); results.push(r); console.log(r.out.replace(/\n[✓✗] \d+ passed.*\n?$/, "\n")); } }; |
| 64 | await Promise.all(Array.from({ length: Math.min(parallel, run.length) }, worker)); |
| 65 | const pass = results.reduce((a, r) => a + r.pass, 0), fail = results.reduce((a, r) => a + r.fail, 0); |
| 66 | console.log(`\n${fail ? "✗" : "✓"} ${pass} passed, ${fail} failed (${run.length} suites, ${parallel} at a time)`); |
| 67 | process.exit(fail ? 1 : 0); |
| 68 | } |
| 69 | |
| 70 | const tmp = mkdtempSync(join(tmpdir(), "dbb-e2e-")); |
| 71 | const procs = []; |
| 72 | const cleanup = () => { for (const p of procs) { try { p.kill("SIGTERM"); } catch {} } try { rmSync(tmp, { recursive: true, force: true }); } catch {} }; |
| 73 | process.on("exit", cleanup); |
| 74 | for (const sig of ["SIGINT", "SIGTERM"]) process.on(sig, () => { cleanup(); process.exit(130); }); |
| 75 | |
| 76 | // ---- the demo server (own port, loopback only) --------------------------------------------------- |
| 77 | const demoPort = await freePort(); |
| 78 | const demo = spawn(process.execPath, [join(root, "demo", "x402-demo-server.mjs")], { env: { ...process.env, DEMO_PORT: String(demoPort), DEMO_HOST: "127.0.0.1" }, stdio: "ignore" }); |
| 79 | procs.push(demo); |
| 80 | const base = `http://127.0.0.1:${demoPort}`; |
| 81 | await waitFor(() => fetch(base + "/free").then((r) => r.ok), "the demo server"); |
| 82 | |
| 83 | // ---- a vault seeded with the demo login (via the standalone package — which also tests interop) -- |
| 84 | const { Vault } = await import(pathToFileURL(join(root, "..", "..", "packages", "vault", "src", "index.ts")).href); |
| 85 | const VAULT_MASTER = "e2e master pw"; |
| 86 | |
| 87 | async function launchApp(suite) { |
| 88 | const profile = join(tmp, `profile-${suite}`); |
| 89 | const vaultPath = join(tmp, `vault-${suite}.json`); |
| 90 | const v = await Vault.create(vaultPath, VAULT_MASTER); |
| 91 | await v.add({ origin: base, username: "demo", password: "demo-pass" }); |
| 92 | v.lock(); |
| 93 | const controlPort = await freePort(); |
| 94 | const debugPort = await freePort(); |
| 95 | const token = "e2e-" + Math.random().toString(16).slice(2); |
| 96 | const log = []; |
| 97 | const mcpServersFile = join(tmp, `mcp-servers-${suite}.json`); |
| 98 | if (suite === "history") { |
| 99 | // The askrobots entry pointed at the stand-in, with its history tools: searching for the assistant, the rest the browser's. |
| 100 | writeFileSync(mcpServersFile, JSON.stringify([{ id: "askrobots", name: "askrobots (stand-in)", url: `${base}/mcp`, tools: ["list_links", "search_history"], readOnly: ["list_links"], addsOnly: ["search_history"], browserOnly: ["add_history", "list_history", "forget_history", "history_settings"], extraScopes: ["history"] }])); |
| 101 | } |
| 102 | if (suite === "bookmarks" || suite === "omnibox") { |
| 103 | // The built-in askrobots entry, replaced by one that points at the stand-in server: the bookmarks use the "askrobots" entry. |
| 104 | writeFileSync(mcpServersFile, JSON.stringify([{ id: "askrobots", name: "askrobots (stand-in)", url: `${base}/mcp`, tools: ["list_links", "create_link"], readOnly: ["list_links"], browserOnly: ["upload_file"], fixedArgs: { create_link: { project_id: "P-links", is_public: false }, upload_file: { project_id: "P-links", is_public: false, category: "browser/kept" } } }])); |
| 105 | } |
| 106 | if (suite === "mcp") { |
| 107 | writeFileSync(mcpServersFile, JSON.stringify([{ id: "demo", name: "Demo notes", url: `${base}/mcp`, tools: ["lookup", "save", "person"], readOnly: ["lookup", "person"], fixedArgs: { save: { project_id: "P-1" } }, dropFields: { person: ["notes", "interactions"] } }])); |
| 108 | } |
| 109 | // E2E_APP_ARGS: extra switches for the browser, e.g. "--disable-gpu" on a machine with no graphics card. |
| 110 | const extra = (process.env.E2E_APP_ARGS ?? "").split(/\s+/).filter(Boolean); |
| 111 | const app = spawn(process.execPath, [join(here, "launch.mjs"), `--remote-debugging-port=${debugPort}`, `--user-data-dir=${profile}`, ...extra], { |
| 112 | env: { |
| 113 | ...process.env, |
| 114 | DBB_CONTROL: "1", DBB_CONTROL_PORT: String(controlPort), DBB_CONTROL_TOKEN: token, DBB_CONTROL_FILE: join(tmp, `control-${suite}.json`), |
| 115 | ...(suite === "extensions" ? { DBB_EXTENSIONS: join(here, "..", "test", "fixtures", "ext-echo.mjs") } : {}), |
| 116 | // The chat suite talks to a scripted model on the demo server (never a real provider or the user's own keys). |
| 117 | ...(suite === "chat" || suite === "mcp" || suite === "bookmarks" || suite === "keep" || suite === "history" ? { DBB_AI_BASE_URL: `${base}/v1`, DBB_AI_PROVIDER: "openai", DBB_AI_MODEL: "fake-model", OPENAI_API_KEY: "e2e-fake-key", ANTHROPIC_API_KEY: "", DBB_AI_CONSENT: "1" } : {}), |
| 118 | // The mcp suite signs in to a stand-in MCP server on the demo server; sign-ins stay in memory, never in the keychain. |
| 119 | ...(suite === "history" ? { DBB_HISTORY_LOCAL_TEST: "1" } : {}), // the stand-in runs on this machine |
| 120 | ...(suite === "mcp" || suite === "bookmarks" || suite === "omnibox" || suite === "history" ? { DBB_MCP_STORE: "memory", DBB_MCP_SERVERS: mcpServersFile } : {}), |
| 121 | ...(process.env.E2E_BACKGROUND === "1" ? { DBB_BACKGROUND: "1" } : {}), |
| 122 | ...(suite === "keep" || suite === "omnibox" ? { DBB_KEEP_DIR: join(tmp, "kept-" + suite) } : {}), |
| 123 | DBB_RECORD_MENUS: "1", // right-click menus are recorded for the menu suite, never shown |
| 124 | DBB_SUGGEST_URL: `${base}/suggest?q=%s`, // never the real search site from a test |
| 125 | DBB_VAULT: vaultPath, HOMEPAGE: `${base}/free`, DBB_DOWNLOAD_DIR: join(tmp, `downloads-${suite}`), |
| 126 | }, |
| 127 | stdio: ["ignore", "pipe", "pipe"], |
| 128 | }); |
| 129 | app.stdout.on("data", (d) => log.push(String(d))); |
| 130 | app.stderr.on("data", (d) => log.push(String(d))); |
| 131 | procs.push(app); |
| 132 | const url = `http://127.0.0.1:${controlPort}/rpc`; |
| 133 | await waitFor(async () => { |
| 134 | const r = await fetch(url, { method: "POST", headers: { authorization: `Bearer ${token}` }, body: JSON.stringify({ method: "status" }) }).then((x) => x.json()); |
| 135 | return r.ok && r.result.tabs.some((t) => t.url.startsWith(base) && t.wcId != null && !t.loading); // up, and the first tab has loaded |
| 136 | }, "the browser to start"); |
| 137 | return { app, url, token, debugPort, controlPort, log, downloadDir: join(tmp, `downloads-${suite}`), quit: () => { try { app.kill("SIGTERM"); } catch {} } }; |
| 138 | } |
| 139 | |
| 140 | const totals = { pass: 0, fail: 0, skipped: [] }; |
| 141 | for (const name of run) { |
| 142 | if (name === "live-net" && process.env.E2E_LIVE_NET !== "1") { totals.skipped.push("live-net (set E2E_LIVE_NET=1)"); continue; } |
| 143 | console.log(`\n━━ ${name} ━━`); |
| 144 | const inst = await launchApp(name); |
| 145 | const ctx = makeCtx({ ...inst, base, tmp, vaultMaster: VAULT_MASTER, liveAi: process.env.E2E_LIVE_AI === "1" }); |
| 146 | try { |
| 147 | const mod = await import(pathToFileURL(join(root, "test", "e2e", `${name}.mjs`)).href); |
| 148 | await mod.default(ctx); |
| 149 | } catch (e) { |
| 150 | ctx.check(false, `suite "${name}" crashed`, String(e?.stack || e).split("\n").slice(0, 3).join(" | ")); |
| 151 | } finally { |
| 152 | await ctx.close(); |
| 153 | inst.quit(); |
| 154 | await sleep(800); |
| 155 | } |
| 156 | totals.pass += ctx.counts.pass; |
| 157 | totals.fail += ctx.counts.fail; |
| 158 | } |
| 159 | |
| 160 | console.log(`\n${totals.fail ? "✗" : "✓"} ${totals.pass} passed, ${totals.fail} failed${totals.skipped.length ? ` · skipped: ${totals.skipped.join(", ")}` : ""}`); |
| 161 | process.exit(totals.fail ? 1 : 0); |