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

9 downloads

dbbasic-browser/apps/browser/test/history.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 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
history.test.ts · 13.8 KB · 277 lines
1import { describe, expect, it } from "vitest";
2import { HistoryService, MAX_TEXT_BYTES, QUEUE_CAP, cleanHistoryUrl, clipText, onNeverList, skipReason } from "../src/history.js";
3
4describe("history: which address is kept", () => {
5 it("drops the fragment, user info and every parameter except the few that say which page it is", () => {
6 expect(cleanHistoryUrl("https://example.com/a?utm_source=x&id=5#top")).toBe("https://example.com/a");
7 expect(cleanHistoryUrl("https://www.youtube.com/watch?v=abc123&t=42s&utm_campaign=y")).toBe("https://www.youtube.com/watch?v=abc123");
8 expect(cleanHistoryUrl("https://m.youtube.com/playlist?list=PL1&si=track")).toBe("https://m.youtube.com/playlist?list=PL1");
9 expect(cleanHistoryUrl("https://news.ycombinator.com/item?id=42&p=2")).toBe("https://news.ycombinator.com/item?id=42");
10 expect(cleanHistoryUrl("https://www.google.com/search?q=private+thing")).toBe("https://www.google.com/search");
11 expect(cleanHistoryUrl("https://user:pw@example.com/x")).toBe("https://example.com/x");
12 for (const bad of ["file:///etc/passwd", "about:blank", "dbb-reader://x", "javascript:alert(1)", "not a url"]) expect(cleanHistoryUrl(bad)).toBeNull();
13 });
14});
15
16describe("history: what is never recorded", () => {
17 it("sign-in and payment pages, by address, by title or by a password field", () => {
18 expect(skipReason({ url: "https://accounts.google.com/v3/signin" })).toBe("sign-in-or-payment");
19 expect(skipReason({ url: "https://login.example.com/" })).toBe("sign-in-or-payment");
20 expect(skipReason({ url: "https://shop.example/checkout" })).toBe("sign-in-or-payment");
21 expect(skipReason({ url: "https://example.com/account/security/" })).toBe("sign-in-or-payment");
22 expect(skipReason({ url: "https://example.com/welcome", title: "Sign in to Example" })).toBe("sign-in-or-payment");
23 expect(skipReason({ url: "https://example.com/welcome", hasPassword: true })).toBe("sign-in-or-payment");
24 expect(skipReason({ url: "https://www.paypal.com/myaccount" })).toBe("sign-in-or-payment");
25 // ordinary pages that only look close
26 expect(skipReason({ url: "https://example.com/blog/how-to-pay-less-tax" })).toBeNull();
27 expect(skipReason({ url: "https://example.com/paying-guide" })).toBeNull();
28 expect(skipReason({ url: "https://news.example/story", title: "Payments startup raises money" })).toBeNull();
29 });
30
31 it("this machine and the private network, unless a test says so", () => {
32 for (const u of ["http://localhost:3000/", "http://127.0.0.1:8080/x", "http://192.168.1.1/", "http://10.0.0.5/", "http://printer.local/", "http://[::1]/"]) expect(skipReason({ url: u })).toBe("private-network");
33 expect(skipReason({ url: "http://127.0.0.1:8080/x" }, [], true)).toBeNull();
34 });
35
36 it("never-record sites cover their subdomains, without matching look-alikes", () => {
37 expect(onNeverList("www.example.com", ["example.com"])).toBe(true);
38 expect(onNeverList("mail.example.com", ["example.com"])).toBe(true);
39 expect(onNeverList("notexample.com", ["example.com"])).toBe(false);
40 expect(skipReason({ url: "https://m.example.com/x" }, ["www.example.com"])).toBe("never-this-site");
41 });
42
43 it("page text is cut at 50,000 bytes on a character boundary, without NULs", () => {
44 expect(clipText("a\u0000b")).toBe("ab");
45 const long = "é".repeat(40_000); // 2 bytes each
46 const c = clipText(long);
47 expect(Buffer.byteLength(c, "utf8")).toBeLessThanOrEqual(MAX_TEXT_BYTES);
48 expect(c.endsWith("é")).toBe(true);
49 });
50});
51
52function setup(o: { connected?: boolean; caller?: boolean; reply?: (tool: string, args: Record<string, unknown>) => unknown } = {}) {
53 let now = Date.parse("2026-11-21T10:00:00Z");
54 const calls: Array<{ tool: string; args: Record<string, unknown> }> = [];
55 const st = { connected: o.connected ?? true, caller: o.caller ?? false, fail: false };
56 const svc = new HistoryService({
57 call: async (tool, args) => {
58 calls.push({ tool, args });
59 if (st.fail) throw new Error("offline");
60 const r = o.reply?.(tool, args);
61 if (r !== undefined) return JSON.stringify(r);
62 if (tool === "add_history") return JSON.stringify({ accepted: (args.visits as unknown[]).length, duplicates: 0, dropped: {}, paused: false });
63 if (tool === "history_settings") return JSON.stringify({ paused: args.paused ?? false, never_sites: args.never_sites ?? [], forget_count: 0, stored_bytes: 0 });
64 if (tool === "forget_history") return JSON.stringify({ forgotten: 3, scope: args.scope });
65 return "{}";
66 },
67 connected: () => st.connected,
68 whyOff: () => "not signed in to askrobots",
69 callerNetworkOn: () => st.caller,
70 now: () => now,
71 });
72 const tick = (s: number) => (now += s * 1000);
73 const visit = (tab: number, url: string, secs: number, extra: { title?: string; text?: string; pw?: boolean } = {}) => {
74 svc.shown(tab, url, extra.title ?? "A page");
75 if (extra.text !== undefined || extra.pw) svc.text(tab, url, extra.text ?? "", !!extra.pw);
76 tick(secs);
77 svc.left(tab);
78 };
79 return { svc, calls, st, tick, visit };
80}
81
82describe("history: titles", () => {
83 it("a page without a title (shown as its address, parameters and all) is kept with no title", async () => {
84 const { svc, calls, tick } = setup();
85 svc.shown(1, "http://example.com:8080/headers?utm_source=news#top", "example.com:8080/headers?utm_source=news#top");
86 tick(3);
87 svc.shown(2, "https://example.com/a", "https://example.com/a");
88 svc.shown(3, "https://example.com/b", "Example: the B page");
89 tick(3);
90 svc.leaveAll();
91 await svc.flush();
92 const sent = calls[0]!.args.visits as Array<{ url: string; title?: string }>;
93 expect(sent.map((v) => v.title)).toEqual([undefined, undefined, "Example: the B page"]);
94 });
95});
96
97describe("history: recording and sending", () => {
98 it("a visit becomes one record with its cleaned address, title, start time, seconds and text", async () => {
99 const { svc, calls, visit } = setup();
100 visit(1, "https://example.com/story?utm_source=x#c", 12, { title: "Solar batteries", text: "Lithium iron phosphate…" });
101 expect(svc.state().queued).toBe(1);
102 await svc.flush();
103 expect(calls).toHaveLength(1);
104 expect(calls[0]!.tool).toBe("add_history");
105 expect(calls[0]!.args.visits).toEqual([{ url: "https://example.com/story", title: "Solar batteries", visited_at: "2026-11-21T10:00:00.000Z", seconds_on_page: 12, text: "Lithium iron phosphate…" }]);
106 expect(svc.state()).toMatchObject({ queued: 0, sent: 1, on: true });
107 });
108
109 it("glances, sign-in pages, password pages and the private network are not visits", async () => {
110 const { svc, visit } = setup();
111 visit(1, "https://example.com/quick", 1);
112 visit(1, "https://example.com/login", 30);
113 visit(1, "https://example.com/members", 30, { pw: true });
114 visit(1, "http://localhost:8000/admin", 30);
115 expect(svc.state().queued).toBe(0);
116 });
117
118 it("the same page at a new fragment is one visit; a new YouTube video is a new one", () => {
119 const { svc, tick } = setup();
120 svc.shown(1, "https://www.youtube.com/watch?v=one");
121 tick(5);
122 svc.shown(1, "https://www.youtube.com/watch?v=one#t=30"); // same page
123 tick(5);
124 svc.shown(1, "https://www.youtube.com/watch?v=two"); // the first one ends here, 10 s
125 tick(5);
126 svc.left(1);
127 expect(svc.state().queued).toBe(2);
128 });
129
130 it("text for a page the tab has already left is ignored", async () => {
131 const { svc, calls, tick } = setup();
132 svc.shown(1, "https://example.com/a");
133 tick(3);
134 svc.shown(1, "https://example.com/b");
135 svc.text(1, "https://example.com/a", "late text from a", false);
136 tick(3);
137 svc.left(1);
138 await svc.flush();
139 const sent = calls[0]!.args.visits as Array<{ url: string; text?: string }>;
140 expect(sent.find((v) => v.url.endsWith("/b"))?.text).toBeUndefined();
141 });
142
143 it("nothing is recorded when signed out, while paused, or while the caller network is on", async () => {
144 const off = setup({ connected: false });
145 off.visit(1, "https://example.com/a", 10);
146 expect(off.svc.state()).toMatchObject({ queued: 0, on: false, why: "not signed in to askrobots" });
147 const caller = setup({ caller: true });
148 caller.visit(1, "https://example.com/a", 10);
149 expect(caller.svc.state().queued).toBe(0);
150 const p = setup();
151 await p.svc.setPaused(true);
152 p.visit(1, "https://example.com/a", 10);
153 expect(p.svc.state()).toMatchObject({ queued: 0, paused: true });
154 expect(p.calls.at(-1)).toEqual({ tool: "history_settings", args: { paused: true } });
155 });
156
157 it("offline: visits wait and go on the next try; the queue is capped, oldest first out", async () => {
158 const { svc, st, visit } = setup();
159 st.fail = true;
160 visit(1, "https://example.com/a", 5);
161 await svc.flush();
162 expect(svc.state()).toMatchObject({ queued: 1, error: "offline" });
163 st.fail = false;
164 await svc.flush();
165 expect(svc.state()).toMatchObject({ queued: 0, sent: 1 });
166 expect(svc.state().error).toBeUndefined();
167 st.fail = true;
168 for (let i = 0; i < QUEUE_CAP + 5; i++) visit(1, `https://example.com/p${i}`, 3);
169 expect(svc.state().queued).toBe(QUEUE_CAP);
170 });
171
172 it("an error from the account (over storage) keeps the batch here and says so", async () => {
173 const { svc, visit } = setup({ reply: (t) => (t === "add_history" ? { error: "over the storage limit" } : undefined) });
174 visit(1, "https://example.com/a", 5);
175 await svc.flush();
176 expect(svc.state()).toMatchObject({ queued: 1, error: "over the storage limit" });
177 });
178
179 it("a flush with nothing to send does not block the next one", async () => {
180 const { svc, calls, visit } = setup();
181 await svc.flush(); // nothing waiting
182 visit(1, "https://example.com/a", 5);
183 await svc.flush();
184 expect(calls.map((c) => c.tool)).toEqual(["add_history"]);
185 expect(svc.state().queued).toBe(0);
186 });
187
188 it("a page kept open is sent once it has been open 30 seconds, once, with the seconds so far", async () => {
189 const { svc, calls, tick } = setup();
190 svc.shown(1, "https://www.houstonchronicle.com/news/article/x-22521312.php?taid=1&utm_source=twitter", "A story");
191 svc.text(1, "https://www.houstonchronicle.com/news/article/x-22521312.php", "Story text", false);
192 tick(10);
193 await svc.flush();
194 expect(calls).toHaveLength(0); // not yet
195 tick(25);
196 await svc.flush();
197 expect(calls[0]!.args.visits).toEqual([{ url: "https://www.houstonchronicle.com/news/article/x-22521312.php", title: "A story", visited_at: "2026-11-21T10:00:00.000Z", seconds_on_page: 35, text: "Story text" }]);
198 tick(100);
199 svc.left(1);
200 await svc.flush();
201 expect(calls).toHaveLength(1); // leaving it later adds nothing
202 });
203
204 it("pages already open when history switches on (signing in, resuming) count from that moment", async () => {
205 const { svc, calls, tick } = setup();
206 svc.shown(1, "https://example.com/open-before");
207 tick(60);
208 svc.resetAccount(); // signed in
209 tick(5);
210 svc.left(1);
211 await svc.flush();
212 expect(calls[0]!.args.visits).toMatchObject([{ url: "https://example.com/open-before", visited_at: "2026-11-21T10:01:00.000Z", seconds_on_page: 5 }]);
213 const p = setup();
214 p.svc.shown(1, "https://example.com/read");
215 await p.svc.setPaused(true);
216 p.tick(40);
217 await p.svc.flush(); // paused: not sent, however long it is open
218 await p.svc.setPaused(false);
219 p.tick(4);
220 p.svc.left(1);
221 await p.svc.flush();
222 const adds = p.calls.filter((c) => c.tool === "add_history");
223 expect(adds.map((c) => c.args.visits)).toMatchObject([[{ url: "https://example.com/read", seconds_on_page: 4 }]]);
224 });
225
226 it("quitting ends every open visit", () => {
227 const { svc, tick } = setup();
228 svc.shown(1, "https://example.com/a");
229 svc.shown(2, "https://example.com/b");
230 tick(5);
231 svc.leaveAll();
232 expect(svc.state().queued).toBe(2);
233 });
234
235 it("sends in batches of 100", async () => {
236 const { svc, calls, visit } = setup();
237 for (let i = 0; i < 250; i++) visit(1, `https://example.com/p${i}`, 3);
238 await svc.flush();
239 expect(calls.map((c) => (c.args.visits as unknown[]).length)).toEqual([100, 100, 50]);
240 });
241
242 it("signing out or in again empties the queue, so nothing reaches another account", () => {
243 const { svc, visit } = setup();
244 visit(1, "https://example.com/a", 5);
245 svc.resetAccount();
246 expect(svc.state().queued).toBe(0);
247 });
248
249 it("forget takes matching visits out of the queue and asks the account to forget the rest", async () => {
250 const { svc, calls, visit, st } = setup();
251 st.fail = true; // keep them queued
252 visit(1, "https://example.com/a", 5);
253 visit(1, "https://sub.other.com/b", 5);
254 st.fail = false;
255 const r = await svc.forget("site", "https://www.other.com/");
256 expect(calls.at(-1)).toEqual({ tool: "forget_history", args: { scope: "site", site: "other.com" } });
257 expect(r.forgotten).toBe(4); // one from the queue + three on the account
258 expect(svc.state().queued).toBe(1);
259 await svc.forget("url", "https://example.com/a#x");
260 expect(calls.at(-1)).toEqual({ tool: "forget_history", args: { scope: "url", url: "https://example.com/a" } });
261 expect(svc.state().queued).toBe(0);
262 });
263
264 it("never-record a site: saved to the account, applied to the queue and to later visits", async () => {
265 const { svc, calls, visit } = setup();
266 await svc.neverRecord("www.example.com");
267 expect(calls.at(-1)).toEqual({ tool: "history_settings", args: { never_sites: ["example.com"] } });
268 visit(1, "https://shop.example.com/x", 9);
269 expect(svc.state().queued).toBe(0);
270 });
271
272 it("settings from the account: pause and the never list", async () => {
273 const { svc } = setup({ reply: (t) => (t === "history_settings" ? { paused: true, never_sites: ["bank.example"], forget_count: 0, stored_bytes: 0 } : undefined) });
274 await svc.loadSettings();
275 expect(svc.state()).toMatchObject({ paused: true, neverSites: ["bank.example"] });
276 });
277});