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
settle.mjs · 10.9 KB · 122 lines
| 1 | // "Has the page finished loading?" — the verdict says whether it settled, which rule ended the wait, and what |
| 2 | // actually happened. Every case here is a page that fools a timeout or a "network quiet" guess. |
| 3 | export default async function (ctx) { |
| 4 | const { call: T, check, base } = ctx; |
| 5 | const settle = (url, opts = {}) => T("page.goto", { url, settle: opts }).then((r) => r.settle); |
| 6 | const quick = { frameTimeoutMs: 1500 }; |
| 7 | |
| 8 | // ---- the plain case, and the shape of the answer ---- |
| 9 | let v = await settle(base + "/free"); |
| 10 | check(v.settled === true && v.outcome === "ok" && v.status === 200, "an ordinary page settles: outcome ok, status 200", `${v.rule} in ${v.waitedMs} ms`); |
| 11 | check(v.rule === "frames-match", "…and it says WHICH rule ended the wait (two identical frames)", v.rule); |
| 12 | check(v.final.url === base + "/free" && v.requests >= 1 && v.bytes > 0 && v.hosts.includes(new URL(base).host), "…and returns facts about the load: final URL, requests, bytes, hosts", `${v.requests} req, ${v.bytes} B`); |
| 13 | check(v.waitedMs < 3000, "…without waiting longer than it needs to", `${v.waitedMs} ms`); |
| 14 | v = await settle(base + "/free", { frames: false }); |
| 15 | check(v.settled && v.rule === "quiet", "with the frame test off, it settles by 'quiet'", v.rule); |
| 16 | |
| 17 | // ---- redirects are part of the facts ---- |
| 18 | v = await settle(base + "/redir/chain"); |
| 19 | check(v.settled && v.status === 200 && v.final.url === base + "/free" && v.redirects.length === 2, "a redirect chain: the final URL, its status (200, not the first hop's 302) and each hop are reported", v.redirects.map((r) => `${r.status}→${r.to.replace(base, "")}`).join(", ")); |
| 20 | |
| 21 | // ---- how much of the page arrived ---- |
| 22 | v = await settle(base + "/free", quick); |
| 23 | check(v.resources?.mainDocument === "answered" && v.resources.failed === 0 && v.resources.answered === v.resources.requested && v.resources.waiting === 0, "a complete page: everything it asked for was answered, nothing failed", JSON.stringify(v.resources)); |
| 24 | v = await settle(base + "/resources", quick); |
| 25 | const rs = v.resources; |
| 26 | check(v.outcome === "ok" && rs.mainDocument === "answered", "a page that loses files is still 'ok' (the main document settled)…", `${v.outcome}`); |
| 27 | check(rs.requested >= 5 && rs.errorStatus === 1 && rs.failed === 2 && rs.failedByKind["could-not-connect"] === 1 && rs.failedByKind.blocked === 1, "…and the counts say how much arrived: 2 failed (one dropped connection, one port the browser refuses), 1 answered with an error status", JSON.stringify(rs)); |
| 28 | check(rs.answered >= 3 && rs.waiting === 0, "…the rest were answered, none still waiting", `${rs.answered} answered, ${rs.waiting} waiting`); |
| 29 | v = await settle(base + "/redir/chain", quick); |
| 30 | check(v.resources.requested === 3 && v.resources.answered === 3 && v.resources.failed === 0, "a redirect chain counts every hop as answered", JSON.stringify(v.resources)); |
| 31 | v = await settle("http://nonexistent-host.invalid/", quick); |
| 32 | check(v.resources.mainDocument === "failed" && v.resources.failed >= 1 && v.resources.failedByKind["could-not-find-site"] >= 1, "a load that fails: the main document is 'failed', with the reason counted", JSON.stringify(v.resources)); |
| 33 | |
| 34 | // ---- console and cookie facts ---- |
| 35 | v = await settle(base + "/console", quick); |
| 36 | check(v.console?.counts?.error === 25 && v.console.counts.warning === 1 && v.console.counts.info === 1, "console messages are counted by level", JSON.stringify(v.console?.counts)); |
| 37 | check(v.console?.errors?.length === 20 && v.console.errors[0].text.startsWith("error 0 ") && v.console.errors.every((e) => e.text.length <= 200), "…the first 20 errors are kept, each cut to 200 characters", `${v.console?.errors?.length} kept, longest ${Math.max(...(v.console?.errors ?? []).map((e) => e.text.length))}`); |
| 38 | const named = (n) => v.cookies?.find((c) => c.name === n); |
| 39 | check(named("visitor")?.httpOnly === true && named("visitor")?.session === true && named("js")?.httpOnly === false, "cookies at the verdict are listed with name, domain, path and flags", JSON.stringify(v.cookies)); |
| 40 | check(!JSON.stringify(v).includes("SECRET-VALUE-123"), "…and no cookie value appears anywhere in the verdict"); |
| 41 | v = await settle(base + "/free", quick); |
| 42 | check(v.console.counts.error === 0 && v.console.errors.length === 0, "the next visit starts with an empty console record"); |
| 43 | |
| 44 | // ---- the blank test ---- |
| 45 | v = await settle(base + "/tiny", quick); |
| 46 | check(v.settled && v.outcome === "blank", "a tiny page (under 20 characters of text) is 'blank' by default", v.outcome); |
| 47 | v = await settle(base + "/tiny", { ...quick, minText: 0 }); |
| 48 | check(v.settled && v.outcome === "ok", "…with minText 0 the same page is ok", v.outcome); |
| 49 | v = await settle(base + "/late-text", quick); |
| 50 | check(v.settled && v.outcome === "blank" && v.waitedMs < 2500, "a page that draws late settles as blank before it draws, by default", `${v.outcome} at ${v.waitedMs} ms`); |
| 51 | v = await settle(base + "/late-text", { ...quick, blankWaitMs: 6000 }); |
| 52 | check(v.settled && v.outcome === "ok" && v.waitedMs >= 2400, "with blankWaitMs it keeps waiting while blank, and settles ok once the text is drawn", `${v.outcome} at ${v.waitedMs} ms`); |
| 53 | v = await settle(base + "/blank", { ...quick, blankWaitMs: 1500 }); |
| 54 | check(v.settled && v.outcome === "blank" && v.waitedMs >= 1400, "a page that stays blank is still reported blank, and the wait is in waitedMs", `${v.outcome} at ${v.waitedMs} ms`); |
| 55 | |
| 56 | // ---- a page that moves itself after loading ---- |
| 57 | v = await settle(base + "/free"); |
| 58 | check(Array.isArray(v.moves) && v.moves.length === 0, "an ordinary page reports no client-side moves"); |
| 59 | const slowQuiet = { quietMs: 1500 }; |
| 60 | v = await settle(base + "/moves-js", slowQuiet); |
| 61 | check(v.settled && v.final.url === base + "/free", "a page that moves itself (script): the wait restarts and settles on the page it ended up on", v.final.url.replace(base, "")); |
| 62 | check(v.moves.length === 1 && v.moves[0].to === base + "/redir/chain", "…the PAGE's own move is reported, separately from…", JSON.stringify(v.moves.map((m) => [m.to.replace(base, ""), m.afterMs]))); |
| 63 | check(v.redirects.length === 2 && v.redirects.every((r) => r.status === 301 || r.status === 302), "…the server redirects it then went through (two hops)", v.redirects.map((r) => r.status).join(",")); |
| 64 | v = await settle(base + "/moves-meta", slowQuiet); |
| 65 | check(v.settled && v.final.url === base + "/free" && v.moves.length === 1, "a real <meta refresh>: also a client-side move, and the wait settles on the final page", `${v.moves.length} move(s), final ${v.final.url.replace(base, "")}`); |
| 66 | |
| 67 | // ---- pages that fool a guess ---- |
| 68 | const t0 = Date.now(); |
| 69 | v = await settle(base + "/slow?ms=1500"); |
| 70 | check(v.settled && v.outcome === "ok" && Date.now() - t0 >= 1500 && v.bytes > 5000, "a page whose body trickles in is not called finished early", `${v.waitedMs} ms, ${v.bytes} B`); |
| 71 | |
| 72 | v = await settle(base + "/stream-open"); |
| 73 | check(v.settled && v.outcome === "ok" && v.streams >= 1, "a finished page holding an event stream open still settles (the stream is ignored and reported)", `streams=${v.streams}, ${v.rule}`); |
| 74 | |
| 75 | v = await settle(base + "/late"); |
| 76 | const decoded = (await T("page.eval", { js: "[...document.images].every((i) => i.complete && i.naturalWidth > 0) && document.images.length" })).value; |
| 77 | // When a timing check fails, say whether the page thought it was on screen: a hidden page has its timers slowed and its animations stopped. |
| 78 | const seenAs = async () => JSON.stringify((await T("page.eval", { js: "({ visibility: document.visibilityState, focus: document.hasFocus() })" })).value); |
| 79 | check(v.settled && v.waitedMs >= 1700 && decoded >= 1, "content added AFTER the load event: it waits for the late image to arrive and decode", `${v.waitedMs} ms, ${decoded} image(s) decoded, page ${await seenAs()}`); |
| 80 | |
| 81 | v = await settle(base + "/animated", quick); |
| 82 | check(v.settled === true && v.rule === "animating" && v.animating === true && v.outcome === "ok", "a page that never stops moving settles as 'animating' instead of waiting for the deadline", `${v.rule} in ${v.waitedMs} ms, page ${await seenAs()}`); |
| 83 | |
| 84 | // ---- what actually happened ---- |
| 85 | v = await settle(base + "/blank", quick); |
| 86 | check(v.settled && v.outcome === "blank", "a loaded page with nothing on it is reported as 'blank'", v.outcome); |
| 87 | v = await settle(base + "/status/404", quick); |
| 88 | check(v.settled && v.outcome === "error-status" && v.status === 404, "an error status is an outcome, with the number", `${v.outcome} ${v.status}`); |
| 89 | v = await settle(base + "/status/429", quick); |
| 90 | check(v.outcome === "error-status" && v.status === 429 && v.slowDown === true, "HTTP 429 is flagged: the site says slow down", `slowDown=${v.slowDown}`); |
| 91 | v = await settle(base + "/status/500", quick); |
| 92 | check(v.outcome === "error-status" && v.status === 500, "HTTP 500 is reported with its number", `${v.status}`); |
| 93 | |
| 94 | v = await settle(base + "/download"); |
| 95 | check(v.outcome === "not-a-page" && v.rule === "download" && v.download === "hello-report.txt", "an address that is a download is 'not-a-page', and names the file", `${v.outcome} (${v.download})`); |
| 96 | |
| 97 | await T("x402.approval", { mode: "deny" }); |
| 98 | v = await settle(base + "/premium"); |
| 99 | await T("x402.approval", { mode: "ask", maxUsd: 0 }); |
| 100 | check(v.outcome === "payment-required" && v.rule === "payment", "a payment request that was refused ends the wait as 'payment-required'", `${v.outcome}/${v.rule}`); |
| 101 | |
| 102 | // ---- failures say why ---- |
| 103 | v = await settle(base + "/fail", quick); |
| 104 | check(v.settled === false && v.outcome === "failed" && v.failure.kind === "could-not-connect", "a dropped connection: failed, 'could-not-connect'", `${v.failure?.kind} (${v.failure?.description})`); |
| 105 | v = await settle("http://nonexistent-host.invalid/", quick); |
| 106 | check(v.outcome === "failed" && v.failure.kind === "could-not-find-site", "an unknown host: failed, 'could-not-find-site'", `${v.failure?.kind} (${v.failure?.description})`); |
| 107 | v = await settle(base + "/redir/loop", quick); |
| 108 | check(v.outcome === "failed" && v.failure.kind === "redirect-loop", "a redirect loop: failed, 'redirect-loop'", `${v.failure?.kind} (${v.failure?.description})`); |
| 109 | |
| 110 | // ---- the deadline: always returns, and says what was still unmet ---- |
| 111 | const d0 = Date.now(); |
| 112 | v = await settle(base + "/hang", { timeoutMs: 2500, ...quick }); |
| 113 | const took = Date.now() - d0; |
| 114 | check(v.settled === false && v.outcome === "deadline" && v.rule === "deadline", "a request that never answers: the deadline ends the wait", `${v.outcome}/${v.rule}`); |
| 115 | check(v.blockedBy?.[0]?.rule === "network" && /\/hang/.test(v.blockedBy[0].detail), "…and names what was still unmet: the request that never answered", v.blockedBy?.[0]?.detail?.slice(0, 80)); |
| 116 | check(took < 2500 + 4000, "…and the call returns promptly after the deadline (it cannot hang)", `${took} ms`); |
| 117 | await T("page.goto", { url: base + "/free" }); |
| 118 | |
| 119 | // ---- page.settle on an already-loaded page ---- |
| 120 | v = await T("page.settle", {}); |
| 121 | check(v.settled && v.outcome === "ok", "page.settle works on its own on a loaded page", v.rule); |
| 122 | } |