A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

0 downloads

dbbasic-browser/apps/browser/test/settle.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 15 hours, 50 minutes ago History
settle.test.ts · 18.5 KB · 276 lines
1import { describe, expect, it } from "vitest";
2import { SETTLE_DEFAULTS, blockers, classifyFailure, newMemo, resolveConfig, step, summarizeResources, type Snapshot } from "../src/settle.js";
3
4const CFG = { ...SETTLE_DEFAULTS, timeoutMs: 10_000, quietMs: 500, frameTimeoutMs: 3_000 };
5const snap = (over: Partial<Snapshot> & { nowMs?: number } = {}): Snapshot => ({
6 nowMs: 1000,
7 lifecycle: { crashed: false, failed: null, download: null, status: 200 },
8 net: { inFlight: [], sinceActivityMs: 2000, state: null, pay: null },
9 page: { readyState: "complete", fonts: "loaded", imagesPending: 0, textLength: 500, hasMedia: true },
10 ...over,
11});
12const run = (s: Snapshot, cfg = CFG, memo = newMemo()) => step(s, cfg, memo);
13
14describe("settled: what has to hold", () => {
15 it("everything holds + matching frames → settled by 'frames-match', outcome ok", () => {
16 const r = run(snap({ frames: { matched: true } }));
17 expect(r).toMatchObject({ done: true, verdict: { settled: true, rule: "frames-match", outcome: "ok", status: 200 } });
18 });
19 it("with the frame test off, everything holding settles as 'quiet'", () => {
20 expect(run(snap(), { ...CFG, frames: false })).toMatchObject({ done: true, verdict: { settled: true, rule: "quiet" } });
21 });
22 it("asks for a frame comparison before declaring a page settled", () => {
23 expect(run(snap())).toEqual({ done: false, action: "check-frames" });
24 });
25 it("a page that never stops changing settles as 'animating' after frameTimeoutMs", () => {
26 const memo = newMemo();
27 expect(step(snap({ nowMs: 1000, frames: { matched: false } }), CFG, memo)).toEqual({ done: false, action: "check-frames" });
28 const r = step(snap({ nowMs: 4100, frames: { matched: false } }), CFG, memo);
29 expect(r).toMatchObject({ done: true, verdict: { settled: true, rule: "animating", animating: true, outcome: "ok" } });
30 });
31});
32
33describe("each unmet rule blocks, and is named", () => {
34 const cases: Array<[string, Partial<Snapshot>, string]> = [
35 ["the load event hasn't fired", { page: { readyState: "interactive", fonts: "loaded", imagesPending: 0, textLength: 500, hasMedia: true } }, "load-event"],
36 ["a request is waiting for the server", { net: { inFlight: [{ url: "https://a.test/x", phase: "waiting", ageMs: 900, longLived: false }], sinceActivityMs: 5000, state: null, pay: null } }, "network"],
37 ["a response body is still arriving", { net: { inFlight: [{ url: "https://a.test/big", phase: "receiving", ageMs: 900, longLived: false }], sinceActivityMs: 5000, state: null, pay: null } }, "network"],
38 ["the network moved a moment ago", { net: { inFlight: [], sinceActivityMs: 100, state: null, pay: null } }, "network-quiet"],
39 ["web fonts are still loading", { page: { readyState: "complete", fonts: "loading", imagesPending: 0, textLength: 500, hasMedia: true } }, "fonts"],
40 ["visible images are still loading", { page: { readyState: "complete", fonts: "loaded", imagesPending: 2, textLength: 500, hasMedia: true } }, "images"],
41 ["the page can't be asked", { page: null }, "page-unreachable"],
42 ];
43 for (const [name, over, rule] of cases) {
44 it(`${name} → blocked by '${rule}'`, () => {
45 expect(blockers(snap(over), CFG).map((b) => b.rule)).toContain(rule);
46 expect(run(snap(over))).toEqual({ done: false });
47 });
48 }
49 it("resets the 'everything holds' clock when something moves again", () => {
50 const memo = newMemo();
51 step(snap({ nowMs: 1000, frames: { matched: false } }), CFG, memo);
52 expect(memo.okSince).toBe(1000);
53 step(snap({ nowMs: 1500, net: { inFlight: [], sinceActivityMs: 50, state: null, pay: null } }), CFG, memo);
54 expect(memo.okSince).toBeNull();
55 });
56});
57
58describe("the blank test: minText and blankWaitMs", () => {
59 const tiny = { readyState: "complete", fonts: "loaded", imagesPending: 0, textLength: 5, hasMedia: false };
60 const text = { ...tiny, textLength: 500 };
61 const settled = (page: typeof tiny, nowMs: number, cfg = CFG, memo = newMemo()) => step({ ...snap({ page, nowMs }), frames: { matched: true } }, cfg, memo);
62 it("a page with under 20 characters of text and no media is blank by default", () => {
63 expect(settled(tiny, 1000)).toMatchObject({ done: true, verdict: { outcome: "blank" } });
64 });
65 it("minText sets the threshold, and 0 turns the test off", () => {
66 expect(settled(tiny, 1000, { ...CFG, minText: 5 })).toMatchObject({ verdict: { outcome: "ok" } });
67 expect(settled(tiny, 1000, { ...CFG, minText: 6 })).toMatchObject({ verdict: { outcome: "blank" } });
68 expect(settled({ ...tiny, textLength: 0 }, 1000, { ...CFG, minText: 0 })).toMatchObject({ verdict: { outcome: "ok" } });
69 });
70 it("blankWaitMs keeps checking while the page would settle as blank, then reports blank if it stayed blank", () => {
71 const cfg = { ...CFG, blankWaitMs: 5000 };
72 const memo = newMemo();
73 expect(settled(tiny, 1000, cfg, memo)).toEqual({ done: false });
74 expect(memo.blankSince).toBe(1000);
75 expect(settled(tiny, 5999, cfg, memo)).toEqual({ done: false });
76 expect(settled(tiny, 6000, cfg, memo)).toMatchObject({ done: true, verdict: { outcome: "blank", settled: true, waitedMs: 6000 } });
77 });
78 it("…and settles at once when text turns up during the wait", () => {
79 const cfg = { ...CFG, blankWaitMs: 5000 };
80 const memo = newMemo();
81 settled(tiny, 1000, cfg, memo);
82 expect(settled(text, 2500, cfg, memo)).toMatchObject({ done: true, verdict: { outcome: "ok", waitedMs: 2500 } });
83 expect(memo.blankSince).toBeNull();
84 });
85 it("media counts as content, so a page of only an image is not waited on", () => {
86 expect(settled({ ...tiny, textLength: 0, hasMedia: true }, 1000, { ...CFG, blankWaitMs: 5000 })).toMatchObject({ done: true, verdict: { outcome: "ok" } });
87 });
88 it("a page that moves itself starts the blank wait over", () => {
89 const cfg = { ...CFG, blankWaitMs: 5000 };
90 const memo = newMemo();
91 const lc = (n: number) => ({ crashed: false, failed: null, download: null, status: 200, navigations: n });
92 step({ ...snap({ page: tiny, nowMs: 1000, lifecycle: lc(1) }), frames: { matched: true } }, cfg, memo);
93 expect(memo.blankSince).toBe(1000);
94 step({ ...snap({ page: tiny, nowMs: 4000, lifecycle: lc(2) }), frames: { matched: true } }, cfg, memo);
95 expect(memo.blankSince).toBe(4000);
96 });
97 it("resolveConfig clamps both", () => {
98 expect(resolveConfig({ minText: -5 }).minText).toBe(0);
99 expect(resolveConfig({ minText: 1e9 }).minText).toBe(10_000);
100 expect(resolveConfig({ blankWaitMs: 1e9 }).blankWaitMs).toBe(120_000);
101 expect(resolveConfig({}).minText).toBe(20);
102 expect(resolveConfig({}).blankWaitMs).toBe(0);
103 });
104});
105
106describe("a page that moves itself restarts the wait", () => {
107 it("a changed navigation count resets the everything-holds clock, so 'animating' isn't declared too early", () => {
108 const memo = newMemo();
109 const lc = (navigations: number) => ({ crashed: false, failed: null, download: null, status: 200, navigations });
110 // On page 1 everything held from t=1000.
111 expect(step(snap({ nowMs: 1000, lifecycle: lc(1), frames: { matched: false } }), CFG, memo)).toEqual({ done: false, action: "check-frames" });
112 expect(memo.okSince).toBe(1000);
113 // The page moved itself (navigations 1 -> 2). Without the reset, 4500-1000 >= frameTimeoutMs would settle as "animating".
114 const r = step(snap({ nowMs: 4500, lifecycle: lc(2), frames: { matched: false } }), CFG, memo);
115 expect(r).toEqual({ done: false, action: "check-frames" });
116 expect(memo.okSince).toBe(4500);
117 });
118 it("an unchanged count changes nothing", () => {
119 const memo = newMemo();
120 const lc = { crashed: false, failed: null, download: null, status: 200, navigations: 1 };
121 step(snap({ nowMs: 1000, lifecycle: lc, frames: { matched: false } }), CFG, memo);
122 expect(step(snap({ nowMs: 4500, lifecycle: lc, frames: { matched: false } }), CFG, memo)).toMatchObject({ done: true, verdict: { rule: "animating" } });
123 });
124});
125
126describe("waiting on a caller-supplied network layer", () => {
127 const withCaller = { url: "https://a.test/page", phase: "caller" as const, ageMs: 5000, longLived: false };
128 it("is its own state: named 'caller', not counted as a slow network request", () => {
129 const b = blockers(snap({ net: { inFlight: [withCaller], sinceActivityMs: 5000, state: null, pay: null } }), CFG);
130 expect(b.map((x) => x.rule)).toEqual(["caller"]);
131 expect(b[0]!.detail).toContain("https://a.test/page");
132 });
133 it("time spent on the caller does not count toward the deadline", () => {
134 const s = (callerWaitMs: number) => snap({ nowMs: 12_000, callerWaitMs, net: { inFlight: [withCaller], sinceActivityMs: 5000, state: null, pay: null } });
135 expect(run(s(0))).toMatchObject({ done: true, verdict: { rule: "deadline" } }); // 12 s waited, none of it on the caller
136 expect(run(s(1_500))).toMatchObject({ done: true, verdict: { rule: "deadline" } }); // 10.5 s of its own time: over 10 s
137 expect(run(s(2_500))).toEqual({ done: false }); // 9.5 s of its own time: still going
138 expect(run(s(11_000))).toEqual({ done: false }); // nearly all of it on the caller
139 expect(run({ ...s(30_000), nowMs: 40_000 })).toMatchObject({ done: true, verdict: { rule: "deadline" } }); // forgiven only up to twice the deadline: it cannot freeze for ever
140 });
141 it("a slow SERVER is still caught by the deadline even when the caller answered", () => {
142 const slow = { url: "https://a.test/slow", phase: "waiting" as const, ageMs: 12_000, longLived: false };
143 expect(run(snap({ nowMs: 12_000, callerWaitMs: 500, net: { inFlight: [slow], sinceActivityMs: 12_000, state: null, pay: null } }))).toMatchObject({ done: true, verdict: { rule: "deadline", blockedBy: [{ rule: "network" }] } });
144 });
145});
146
147describe("a failure says who decided it", () => {
148 const failed = (f: object) => run(snap({ lifecycle: { crashed: false, failed: { description: "x", ...f }, download: null, status: null } }));
149 it("one the browser saw is 'observed'", () => {
150 expect(failed({ description: "ERR_CONNECTION_RESET" })).toMatchObject({ verdict: { failure: { kind: "could-not-connect", source: "observed" } } });
151 });
152 it("one the caller gave keeps the caller's kind and source", () => {
153 expect(failed({ description: "the caller's own rules say no", kind: "blocked", source: "caller" })).toMatchObject({ verdict: { failure: { kind: "blocked", description: "the caller's own rules say no", source: "caller" } } });
154 });
155 it("'caller-silent' is its own kind, apart from 'timed-out'", () => {
156 expect(failed({ description: "the caller did not answer within 20000 ms", kind: "caller-silent", source: "observed" })).toMatchObject({ verdict: { failure: { kind: "caller-silent" } } });
157 });
158});
159
160describe("a stuck navigation: the page can't be asked, but the request that never answered is named", () => {
161 it("reports the network blocker first and page-unreachable after it", () => {
162 const hang = { url: "https://a.test/hang", phase: "waiting" as const, ageMs: 5000, longLived: false };
163 const b = blockers(snap({ page: null, net: { inFlight: [hang], sinceActivityMs: 5000, state: null, pay: null } }), CFG);
164 expect(b.map((x) => x.rule)).toEqual(["network", "page-unreachable"]);
165 expect(b[0]!.detail).toContain("https://a.test/hang");
166 });
167});
168
169describe("long-lived streams don't block (a finished page can hold one open)", () => {
170 const sse = { url: "https://a.test/events", phase: "receiving" as const, ageMs: 9000, longLived: true };
171 it("an open event stream is ignored and reported", () => {
172 const r = run(snap({ net: { inFlight: [sse], sinceActivityMs: 3000, state: null, pay: null }, frames: { matched: true } }));
173 expect(r).toMatchObject({ done: true, verdict: { settled: true, rule: "frames-match", streams: 1 } });
174 });
175 it("but a request still WAITING for its headers does block", () => {
176 const waiting = { url: "https://a.test/hang", phase: "waiting" as const, ageMs: 9000, longLived: false };
177 expect(run(snap({ net: { inFlight: [sse, waiting], sinceActivityMs: 3000, state: null, pay: null } }))).toEqual({ done: false });
178 });
179});
180
181describe("the deadline says what was still unmet", () => {
182 it("returns settled:false, rule 'deadline', and blockedBy with the request that never answered", () => {
183 const hang = { url: "https://a.test/hang", phase: "waiting" as const, ageMs: 10_000, longLived: false };
184 const r = run(snap({ nowMs: 10_000, net: { inFlight: [hang], sinceActivityMs: 10_000, state: null, pay: null } }));
185 expect(r).toMatchObject({ done: true, verdict: { settled: false, outcome: "deadline", rule: "deadline", waitedMs: 10_000 } });
186 if (r.done) expect(r.verdict.blockedBy?.[0]).toMatchObject({ rule: "network" });
187 if (r.done) expect(r.verdict.blockedBy?.[0]?.detail).toContain("https://a.test/hang");
188 });
189 it("a deadline during a bot check is reported as 'bot-check', not a generic timeout", () => {
190 const r = run(snap({ nowMs: 10_000, net: { inFlight: [{ url: "https://a.test/.within.website/x", phase: "waiting", ageMs: 9000, longLived: false }], sinceActivityMs: 100, state: "challenge", pay: null } }));
191 expect(r).toMatchObject({ done: true, verdict: { outcome: "bot-check", rule: "deadline", settled: false } });
192 });
193});
194
195describe("outcomes: what actually happened", () => {
196 const settledWith = (over: Partial<Snapshot>) => run({ ...snap(over), frames: { matched: true } });
197 it("an error status is an outcome with the number, even though the page settled", () => {
198 expect(settledWith({ lifecycle: { crashed: false, failed: null, download: null, status: 404 } })).toMatchObject({ done: true, verdict: { settled: true, outcome: "error-status", status: 404 } });
199 });
200 it("HTTP 429 is flagged as 'slow down'", () => {
201 const r = settledWith({ lifecycle: { crashed: false, failed: null, download: null, status: 429 } });
202 expect(r).toMatchObject({ done: true, verdict: { outcome: "error-status", status: 429, slowDown: true } });
203 });
204 it("a settled page with nothing on it is 'blank'", () => {
205 expect(settledWith({ page: { readyState: "complete", fonts: "loaded", imagesPending: 0, textLength: 3, hasMedia: false } })).toMatchObject({ done: true, verdict: { settled: true, outcome: "blank" } });
206 });
207 it("but a page that is just an image or canvas is not blank", () => {
208 expect(settledWith({ page: { readyState: "complete", fonts: "loaded", imagesPending: 0, textLength: 0, hasMedia: true } })).toMatchObject({ verdict: { outcome: "ok" } });
209 });
210 it("a download is 'not-a-page' and ends the wait at once", () => {
211 expect(run(snap({ lifecycle: { crashed: false, failed: null, download: "report.pdf", status: null } }))).toMatchObject({ done: true, verdict: { outcome: "not-a-page", rule: "download", download: "report.pdf" } });
212 });
213 it("a crash ends the wait at once, unsettled", () => {
214 expect(run(snap({ lifecycle: { crashed: true, failed: null, download: null, status: null } }))).toMatchObject({ done: true, verdict: { settled: false, outcome: "crashed", rule: "crashed" } });
215 });
216 it("a payment waiting on a decision ends the wait with 'payment-required'", () => {
217 expect(run(snap({ net: { inFlight: [], sinceActivityMs: 10, state: null, pay: "pending" } }))).toMatchObject({ done: true, verdict: { outcome: "payment-required", rule: "payment" } });
218 });
219 it("a failed navigation names why", () => {
220 const r = run(snap({ lifecycle: { crashed: false, failed: { description: "ERR_CERT_DATE_INVALID" }, download: null, status: null } }));
221 expect(r).toMatchObject({ done: true, verdict: { settled: false, outcome: "failed", rule: "navigation-failed", failure: { kind: "certificate", description: "ERR_CERT_DATE_INVALID" } } });
222 });
223});
224
225describe("summarizeResources: how much of the page arrived", () => {
226 const stats = (over: object = {}) => ({ requests: 10, answered: 6, errorStatus: 1, cancelled: 1, failures: [{ kind: "blocked", error: "x" }, { kind: null, error: "ERR_CONNECTION_REFUSED" }], ...over });
227 it("counts what was requested, answered, failed (by reason) and cancelled, and what is still waiting", () => {
228 expect(summarizeResources(stats(), { failed: false, status: 200 })).toEqual({
229 requested: 10, answered: 6, errorStatus: 1, failed: 2, failedByKind: { blocked: 1, "could-not-connect": 1 }, cancelled: 1, waiting: 1, mainDocument: "answered",
230 });
231 });
232 it("a reason the caller gave is kept; one the browser saw is classified from the error text", () => {
233 const r = summarizeResources(stats({ failures: [{ kind: "caller-silent", error: "x" }, { kind: null, error: "ERR_NAME_NOT_RESOLVED" }, { kind: "bogus", error: "ERR_CERT_DATE_INVALID" }] }), { failed: false, status: 200 });
234 expect(r.failedByKind).toEqual({ "caller-silent": 1, "could-not-find-site": 1, certificate: 1 });
235 });
236 it("the main document is failed, answered or pending", () => {
237 expect(summarizeResources(stats(), { failed: true, status: null }).mainDocument).toBe("failed");
238 expect(summarizeResources(stats(), { failed: false, status: 404 }).mainDocument).toBe("answered");
239 expect(summarizeResources(stats(), { failed: false, status: null }).mainDocument).toBe("pending");
240 });
241 it("a complete page has no failures and nothing waiting", () => {
242 const r = summarizeResources({ requests: 3, answered: 3, errorStatus: 0, cancelled: 0, failures: [] }, { failed: false, status: 200 });
243 expect(r).toMatchObject({ failed: 0, waiting: 0, failedByKind: {} });
244 });
245});
246
247describe("classifyFailure", () => {
248 const cases: Array<[string, string]> = [
249 ["ERR_NAME_NOT_RESOLVED", "could-not-find-site"],
250 ["ERR_CONNECTION_REFUSED", "could-not-connect"],
251 ["ERR_CONNECTION_RESET", "could-not-connect"],
252 ["ERR_EMPTY_RESPONSE", "could-not-connect"],
253 ["ERR_CERT_AUTHORITY_INVALID", "certificate"],
254 ["ERR_SSL_PROTOCOL_ERROR", "certificate"],
255 ["ERR_TOO_MANY_REDIRECTS", "redirect-loop"],
256 ["ERR_INTERNET_DISCONNECTED", "offline"],
257 ["ERR_CONNECTION_TIMED_OUT", "timed-out"],
258 ["ERR_BLOCKED_BY_CLIENT", "blocked"],
259 ["ERR_UNSAFE_PORT", "blocked"],
260 ["ERR_SOMETHING_NEW", "other"],
261 ["", "other"],
262 ];
263 for (const [desc, kind] of cases) it(`${desc || "(empty)"} → ${kind}`, () => expect(classifyFailure(desc)).toBe(kind));
264});
265
266describe("resolveConfig", () => {
267 it("fills defaults, clamps ranges, and ignores junk", () => {
268 expect(resolveConfig(undefined)).toEqual(SETTLE_DEFAULTS);
269 expect(resolveConfig({ timeoutMs: 1 }).timeoutMs).toBe(500);
270 expect(resolveConfig({ timeoutMs: 9e9 }).timeoutMs).toBe(300_000);
271 expect(resolveConfig({ quietMs: "abc" }).quietMs).toBe(SETTLE_DEFAULTS.quietMs);
272 expect(resolveConfig({ frames: false }).frames).toBe(false);
273 expect(resolveConfig({ frames: "no" }).frames).toBe(true);
274 expect(resolveConfig("nonsense")).toEqual(SETTLE_DEFAULTS);
275 });
276});