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/bookmarks.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
bookmarks.test.ts · 12.1 KB · 213 lines
1import { describe, expect, it } from "vitest";
2import { BookmarksService, LIMIT, normalizeUrl, parseLinks, type BookmarksDeps, type BookmarksState } from "../src/bookmarks-service";
3
4const LINKS = JSON.stringify([
5 { id: "a", url: "https://www.djangotricks.com/", title: "www.djangotricks.com", tags: "", is_public: false, project_id: null, created_at: "2026-11-19T04:24:52Z" },
6 { id: "b", url: "https://facebook.com", title: "Facebook", tags: "social, people", is_public: false, project_id: null, created_at: "2026-11-17T16:42:31Z" },
7 { id: "c", url: "javascript:alert(1)", title: "bad" },
8 { id: "d", url: "https://untitled.example/page", title: "" },
9]);
10
11function make(over: Partial<BookmarksDeps> & { signedIn?: boolean; loaded?: boolean; reply?: string; copy?: string | null } = {}) {
12 const calls: Array<[string, Record<string, unknown>, { fixed: boolean }]> = [];
13 const changed: BookmarksState[] = [];
14 let saved: string | null = over.copy ?? null;
15 let reply = over.reply ?? LINKS;
16 const deps: BookmarksDeps = {
17 call: async (tool, args, opts) => {
18 calls.push([tool, args, opts]);
19 if (tool === "create_link") {
20 const list = JSON.parse(reply) as unknown[];
21 reply = JSON.stringify([{ id: "new", url: args.url, title: args.title }, ...list]);
22 return JSON.stringify({ id: "new" });
23 }
24 return reply;
25 },
26 connected: () => over.signedIn ?? true,
27 ready: () => over.loaded ?? true,
28 loadCopy: () => saved,
29 saveCopy: (t) => void (saved = t),
30 changed: (s) => void changed.push(s),
31 now: () => 1_000_000,
32 ...over,
33 };
34 return { svc: new BookmarksService(deps), calls, changed, saved: () => saved };
35}
36
37describe("reading the service's links", () => {
38 it("keeps web addresses only, falls back to the host for a missing title, and splits tags", () => {
39 const b = parseLinks(LINKS);
40 expect(b.map((x) => x.id)).toEqual(["a", "b", "d"]);
41 expect(b[1]).toMatchObject({ title: "Facebook", tags: ["social", "people"], createdAt: "2026-11-17T16:42:31Z" });
42 expect(b[2]?.title).toBe("untitled.example");
43 expect(parseLinks(JSON.stringify({ links: [{ url: "https://x.example", title: "X" }] }))[0]?.url).toBe("https://x.example");
44 expect(parseLinks("not json")).toEqual([]);
45 });
46
47 it("compares addresses the way people mean them", () => {
48 expect(normalizeUrl("https://Facebook.com/")).toBe(normalizeUrl("https://facebook.com"));
49 expect(normalizeUrl("https://x.example/a/#top")).toBe(normalizeUrl("https://x.example/a"));
50 expect(normalizeUrl("https://x.example/a?q=1")).not.toBe(normalizeUrl("https://x.example/a"));
51 expect(normalizeUrl("https://x.example/a?utm_source=news&fbclid=1&guccounter=2")).toBe(normalizeUrl("https://x.example/a")); // tracking only
52 expect(normalizeUrl("https://www.youtube.com/watch?v=abc")).not.toBe(normalizeUrl("https://www.youtube.com/watch?v=xyz")); // a real query is the page
53 });
54});
55
56describe("the bookmarks list", () => {
57 it("loads from the service when asked, keeps a local copy, and says when the list changed", async () => {
58 const { svc, calls, changed, saved } = make();
59 expect(svc.state()).toMatchObject({ items: [], source: "none" });
60 const s = await svc.refresh();
61 expect(calls).toEqual([["list_links", { limit: LIMIT }, { fixed: false }]]); // reading never applies the entry's fixed project
62 expect(s).toMatchObject({ source: "service", connected: true, updatedAt: 1_000_000 });
63 expect(s.items).toHaveLength(3);
64 expect(changed).toHaveLength(1);
65 expect(JSON.parse(saved() as string).items).toHaveLength(3);
66 await svc.refresh();
67 expect(changed).toHaveLength(2); // the same list again is still announced: the window may not know it is connected yet
68 expect(svc.has("https://facebook.com/")).toBe(true);
69 expect(svc.has("https://elsewhere.example")).toBe(false);
70 });
71
72 it("shows the local copy at start-up without reaching the service (or the keychain)", async () => {
73 const first = make();
74 await first.svc.refresh();
75 const again = make({ copy: first.saved(), loaded: false, connected: () => { throw new Error("must not read the sign-in at start-up"); } });
76 expect(again.svc.state()).toMatchObject({ source: "copy", updatedAt: 1_000_000, connected: false });
77 expect(again.svc.state().items).toHaveLength(3);
78 expect(again.svc.has("https://facebook.com")).toBe(true);
79 expect(again.calls).toEqual([]);
80 });
81
82 it("asks nothing of the service when not signed in, and reports errors instead of throwing", async () => {
83 const out = make({ signedIn: false });
84 expect(await out.svc.refresh()).toMatchObject({ connected: false, items: [] });
85 expect(out.calls).toEqual([]);
86 const bad = make({ call: async () => { throw new Error("askrobots.com wants a fresh sign-in"); } });
87 expect((await bad.svc.refresh()).error).toMatch(/fresh sign-in/);
88 });
89
90 it("two refreshes at once make one request", async () => {
91 const { svc, calls } = make();
92 await Promise.all([svc.refresh(), svc.refresh()]);
93 expect(calls).toHaveLength(1);
94 });
95});
96
97describe("saving with one click", () => {
98 it("saves with the page's title, applies the entry's fixed arguments, and refreshes the list", async () => {
99 const { svc, calls } = make();
100 await svc.refresh();
101 const r = await svc.add("https://new.example/page", "A new page");
102 expect(r).toEqual({ ok: true });
103 expect(calls[1]).toEqual(["create_link", { url: "https://new.example/page", title: "A new page" }, { fixed: true }]);
104 expect(svc.has("https://new.example/page")).toBe(true);
105 });
106
107 it("loads the list before saving when it has not been loaded yet, so a page is never saved twice", async () => {
108 const { svc, calls } = make();
109 expect(await svc.add("https://facebook.com/", "Facebook")).toEqual({ ok: true, already: true });
110 expect(calls.map(([t]) => t)).toEqual(["list_links"]);
111 });
112
113 it("does not save twice, refuses what is not a web page, and says to connect when not signed in", async () => {
114 const { svc, calls } = make();
115 await svc.refresh();
116 expect(await svc.add("https://facebook.com", "Facebook")).toEqual({ ok: true, already: true });
117 expect(calls.filter(([t]) => t === "create_link")).toHaveLength(0);
118 expect((await svc.add("file:///etc/passwd", "x")).ok).toBe(false);
119 expect(await make({ signedIn: false }).svc.add("https://x.example", "X")).toMatchObject({ ok: false, message: expect.stringMatching(/Connect askrobots/) });
120 });
121});
122
123describe("searching all the links", () => {
124 it("sends only what was typed, to the service's own search, and leaves the local list alone", async () => {
125 const { svc, calls } = make();
126 await svc.refresh();
127 const r = await svc.search(" face ");
128 expect(calls.at(-1)).toEqual(["list_links", { search: "face", limit: LIMIT }, { fixed: false }]);
129 expect(r.items.length).toBeGreaterThan(0);
130 expect(svc.state().items).toHaveLength(3);
131 expect(await svc.search("f")).toEqual({ items: [] }); // too short: nothing sent
132 expect(await make({ signedIn: false }).svc.search("face")).toMatchObject({ items: [], error: expect.stringMatching(/Connect/) });
133 });
134
135 it("remembers older bookmarks a search turned up, so ☆ knows those pages are saved, and keeps them in the local copy", async () => {
136 const old = JSON.stringify([{ id: "z", url: "https://finance.yahoo.com/news/old-story", title: "Old story" }]);
137 const changed: BookmarksState[] = [];
138 let copy: string | null = null;
139 let calls = 0;
140 const svc = new BookmarksService({
141 call: async (_t, args) => (calls++, args.search ? old : LINKS), connected: () => true, ready: () => true,
142 loadCopy: () => copy, saveCopy: (t) => void (copy = t), changed: (s) => void changed.push(s),
143 });
144 await svc.refresh();
145 expect(svc.has("https://finance.yahoo.com/news/old-story")).toBe(false);
146 await svc.search("yahoo");
147 expect(svc.has("https://finance.yahoo.com/news/old-story?guccounter=1")).toBe(true);
148 expect(svc.state().known.map((b) => b.title)).toEqual(["Old story"]);
149 expect(changed.at(-1)?.known).toHaveLength(1);
150 const again = new BookmarksService({ call: async () => "[]", connected: () => true, ready: () => false, loadCopy: () => copy, saveCopy: () => {}, changed: () => {} });
151 expect(again.has("https://finance.yahoo.com/news/old-story")).toBe(true); // remembered across a restart
152 expect(calls).toBe(2);
153 });
154
155 it("checks the site of a page that is not known locally, once per site, and remembers what it finds", async () => {
156 const siteLinks = JSON.stringify([{ id: "s1", url: "https://finance.yahoo.com/news/old-story", title: "Old story" }, { id: "s2", url: "https://elsewhere.example/x", title: "Not this site" }]);
157 const calls: Array<Record<string, unknown>> = [];
158 let copy: string | null = null;
159 const svc = new BookmarksService({
160 call: async (_t, args) => (calls.push(args), args.search ? siteLinks : "[]"), connected: () => true, ready: () => true,
161 loadCopy: () => copy, saveCopy: (t) => void (copy = t), changed: () => {},
162 });
163 expect(await svc.checkPage("https://finance.yahoo.com/news/old-story?guccounter=1")).toEqual({ checked: true, saved: true });
164 expect(calls).toEqual([{ search: "finance.yahoo.com", limit: LIMIT }]); // only the site's name was sent
165 expect(svc.has("https://elsewhere.example/x")).toBe(false); // results for other sites are not taken
166 await svc.checkPage("https://finance.yahoo.com/news/other");
167 expect(calls).toHaveLength(1); // once per site: the first answer held every saved page there
168 });
169
170 it("asks about the page itself when the site has more saved pages than one answer holds", async () => {
171 const many = JSON.stringify(Array.from({ length: LIMIT }, (_, i) => ({ id: "m" + i, url: `https://big.example/p${i}`, title: "p" + i })));
172 const calls: Array<Record<string, unknown>> = [];
173 const svc = new BookmarksService({
174 call: async (_t, args) => (calls.push(args), String(args.search).startsWith("big.example/old") ? JSON.stringify([{ id: "o", url: "https://big.example/old", title: "old" }]) : many),
175 connected: () => true, ready: () => true, loadCopy: () => null, saveCopy: () => {}, changed: () => {},
176 });
177 expect(await svc.checkPage("https://big.example/old")).toEqual({ checked: true, saved: true });
178 expect(calls.map((c) => c.search)).toEqual(["big.example", "big.example/old"]);
179 });
180
181 it("asks nothing when the setting is off, or before the connection has been opened this session", async () => {
182 const calls: unknown[] = [];
183 const deps = { call: async () => (calls.push(1), "[]"), connected: () => true, loadCopy: () => null, saveCopy: () => {}, changed: () => {} };
184 const off = new BookmarksService({ ...deps, ready: () => true });
185 off.setCheckPages(false);
186 expect(await off.checkPage("https://x.example/a")).toEqual({ checked: false, saved: false });
187 const notYet = new BookmarksService({ ...deps, ready: () => false });
188 expect(await notYet.checkPage("https://x.example/a")).toEqual({ checked: false, saved: false });
189 expect(calls).toHaveLength(0);
190 expect(off.state().checkPages).toBe(false);
191 });
192
193 it("tells the window after every load, so a window that started from the local copy learns it is connected", async () => {
194 const first = make();
195 await first.svc.refresh();
196 const again = make({ copy: first.saved() });
197 expect(again.svc.state().source).toBe("copy");
198 await again.svc.refresh(); // the same list as the copy
199 expect(again.changed.at(-1)).toMatchObject({ source: "service", connected: true });
200 });
201
202 it("a check that fails is not remembered as done, and says why", async () => {
203 let fail = true;
204 const svc = new BookmarksService({
205 call: async () => { if (fail) throw new Error("network down"); return JSON.stringify([{ id: "s", url: "https://slashdot.org/", title: "slashdot" }]); },
206 connected: () => true, ready: () => true, loadCopy: () => null, saveCopy: () => {}, changed: () => {},
207 });
208 expect(await svc.checkPage("https://slashdot.org/")).toEqual({ checked: false, saved: false });
209 expect(svc.state().error).toMatch(/could not check slashdot.org: network down/);
210 fail = false;
211 expect(await svc.checkPage("https://slashdot.org/")).toEqual({ checked: true, saved: true }); // tried again
212 });
213});