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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
lib.mjs · 3.0 KB · 64 lines
| 1 | // Shared helpers for the end-to-end suites. A suite is `export default async (ctx) => { … }`. |
| 2 | // |
| 3 | // ctx.call(method, params) JSON-RPC to the browser's automation API (throws {code} on error) |
| 4 | // ctx.check(cond, msg, more) record a pass/fail |
| 5 | // ctx.base the demo server origin, e.g. http://127.0.0.1:54321 |
| 6 | // ctx.chrome() a CDP session on the browser's own UI (for checking what the chrome renders) |
| 7 | // ctx.sleep(ms) |
| 8 | // ctx.liveAi true when E2E_LIVE_AI=1 (checks that call a real AI provider) |
| 9 | |
| 10 | export const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 11 | |
| 12 | export function makeCtx({ url, token, debugPort, controlPort, base, tmp, vaultMaster, liveAi, log, downloadDir }) { |
| 13 | const counts = { pass: 0, fail: 0 }; |
| 14 | const sessions = []; |
| 15 | |
| 16 | async function call(method, params = {}) { |
| 17 | const res = await fetch(url, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify({ method, params }) }); |
| 18 | const body = await res.json(); |
| 19 | if (!body.ok) throw Object.assign(new Error(`${body.error?.code}: ${body.error?.message}`), { code: body.error?.code }); |
| 20 | return body.result; |
| 21 | } |
| 22 | |
| 23 | function check(cond, msg, more = "") { |
| 24 | counts[cond ? "pass" : "fail"]++; |
| 25 | console.log(`${cond ? " ✓" : " ✗ FAIL"} ${msg}${more ? ` → ${more}` : ""}`); |
| 26 | return !!cond; |
| 27 | } |
| 28 | |
| 29 | /** Chrome DevTools Protocol on the browser's own UI document (not on a page). */ |
| 30 | async function chrome() { |
| 31 | const targets = await (await fetch(`http://127.0.0.1:${debugPort}/json`)).json(); |
| 32 | const ws = new WebSocket(targets.find((t) => t.type === "page").webSocketDebuggerUrl); |
| 33 | await new Promise((r) => (ws.onopen = r)); |
| 34 | let id = 0; |
| 35 | const pend = new Map(); |
| 36 | ws.onmessage = (m) => { const d = JSON.parse(m.data); pend.get(d.id)?.(d); }; |
| 37 | const send = (method, params = {}) => new Promise((r) => { const i = ++id; pend.set(i, r); ws.send(JSON.stringify({ id: i, method, params })); }); |
| 38 | const ev = (expr) => send("Runtime.evaluate", { expression: expr, awaitPromise: true, returnByValue: true }).then((d) => d.result?.result?.value); |
| 39 | const s = { send, ev, close: () => ws.close() }; |
| 40 | sessions.push(s); |
| 41 | return s; |
| 42 | } |
| 43 | |
| 44 | return { |
| 45 | call, check, chrome, sleep, base, tmp, vaultMaster, liveAi, counts, downloadDir, controlUrl: url, controlToken: token, controlPort, |
| 46 | appLog: () => (log || []).join(""), |
| 47 | close: async () => { for (const s of sessions) { try { s.close(); } catch {} } }, |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | /** Navigate without waiting and record every distinct tab state until `ms` elapsed. */ |
| 52 | export async function traceStates(ctx, url, ms, tab) { |
| 53 | const seen = []; |
| 54 | const t0 = Date.now(); |
| 55 | const nav = ctx.call("page.goto", { url, wait: false, ...(tab ? { tab } : {}) }).catch(() => {}); |
| 56 | while (Date.now() - t0 < ms) { |
| 57 | const t = (await ctx.call("tabs.list")).find((x) => (tab ? x.id === tab : x.active)); |
| 58 | const s = t?.state ?? "idle"; |
| 59 | if (seen[seen.length - 1] !== s) seen.push(s); |
| 60 | await sleep(40); |
| 61 | } |
| 62 | await nav; |
| 63 | return seen; |
| 64 | } |