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
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
history-copy.test.ts · 7.2 KB · 163 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { HistoryCopy, MAX_VISITS } from "../src/history-copy.js"; |
| 3 | import { forgetMatch } from "../src/history.js"; |
| 4 | |
| 5 | const day = 86_400_000; |
| 6 | const NOW = Date.parse("2026-11-21T12:00:00Z"); |
| 7 | |
| 8 | /** A stand-in for the account's list_history: a change log, read a page at a time from a cursor. */ |
| 9 | function account() { |
| 10 | let seq = 0; |
| 11 | let resetAt = 0; |
| 12 | const visits: Array<{ id: string; url: string; title: string; visited_at: string; seq: number }> = []; |
| 13 | const gone: Array<{ id: string; seq: number }> = []; |
| 14 | const calls: Array<Record<string, unknown>> = []; |
| 15 | let n = 1; |
| 16 | return { |
| 17 | calls, |
| 18 | add(url: string, title: string, at = NOW) { |
| 19 | visits.push({ id: "v" + n++, url, title, visited_at: new Date(at).toISOString(), seq: ++seq }); |
| 20 | }, |
| 21 | forget(pred: (v: { url: string }) => boolean, all = false) { |
| 22 | for (let i = visits.length - 1; i >= 0; i--) if (pred(visits[i]!)) (gone.push({ id: visits[i]!.id, seq: ++seq }), visits.splice(i, 1)); |
| 23 | if (all) resetAt = seq; |
| 24 | }, |
| 25 | list(a: Record<string, unknown>) { |
| 26 | calls.push(a); |
| 27 | const since = a.cursor === undefined ? 0 : Number(String(a.cursor).slice(1)); |
| 28 | if (Number.isNaN(since) || since > seq) return { error: "unknown cursor" }; |
| 29 | if (a.cursor !== undefined && since < resetAt) return { visits: [], forgotten: [], full_copy: true, cursor: null, has_more: false }; |
| 30 | const limit = Number(a.limit) || 200; |
| 31 | const newer = visits.filter((v) => v.seq > since).sort((x, y) => x.seq - y.seq); |
| 32 | const page = newer.slice(0, limit); |
| 33 | const upTo = page.length ? page[page.length - 1]!.seq : seq; |
| 34 | return { |
| 35 | visits: page.map(({ seq: _s, ...v }) => v), |
| 36 | forgotten: a.cursor === undefined ? [] : gone.filter((g) => g.seq > since && g.seq <= upTo).map((g) => g.id), |
| 37 | full_copy: false, |
| 38 | cursor: "c" + upTo, |
| 39 | has_more: newer.length > limit, |
| 40 | }; |
| 41 | }, |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | function setup(o: { connected?: boolean; saved?: string | null } = {}) { |
| 46 | const acct = account(); |
| 47 | const disk = { text: o.saved ?? (null as string | null) }; |
| 48 | const st = { connected: o.connected ?? true, now: NOW }; |
| 49 | const copy = new HistoryCopy({ |
| 50 | call: async (tool, args) => { |
| 51 | if (tool !== "list_history") throw new Error("only list_history"); |
| 52 | return JSON.stringify(acct.list(args)); |
| 53 | }, |
| 54 | connected: () => st.connected, |
| 55 | load: () => disk.text, |
| 56 | save: (t) => (disk.text = t), |
| 57 | now: () => st.now, |
| 58 | }); |
| 59 | return { acct, copy, disk, st }; |
| 60 | } |
| 61 | |
| 62 | describe("history: the copy for the address bar", () => { |
| 63 | it("reads the account's history, then only what changed, from the cursor it was given", async () => { |
| 64 | const { acct, copy } = setup(); |
| 65 | acct.add("https://a.example/1", "One"); |
| 66 | acct.add("https://b.example/2", "Two"); |
| 67 | await copy.sync(); |
| 68 | expect(copy.pages().map((p) => p.url).sort()).toEqual(["https://a.example/1", "https://b.example/2"]); |
| 69 | acct.add("https://c.example/3", "Three"); // from another computer |
| 70 | await copy.sync(); |
| 71 | expect(acct.calls.map((c) => c.cursor)).toEqual([undefined, "c2"]); |
| 72 | expect(copy.state().visits).toBe(3); |
| 73 | }); |
| 74 | |
| 75 | it("a forget on any computer reaches the copy; a forget-all starts it again", async () => { |
| 76 | const { acct, copy } = setup(); |
| 77 | acct.add("https://a.example/1", "One"); |
| 78 | acct.add("https://b.example/2", "Two"); |
| 79 | await copy.sync(); |
| 80 | acct.forget((v) => v.url.includes("a.example")); |
| 81 | await copy.sync(); |
| 82 | expect(copy.pages().map((p) => p.url)).toEqual(["https://b.example/2"]); |
| 83 | acct.forget(() => true, true); |
| 84 | acct.add("https://d.example/4", "Four"); |
| 85 | await copy.sync(); |
| 86 | expect(copy.pages().map((p) => p.url)).toEqual(["https://d.example/4"]); |
| 87 | }); |
| 88 | |
| 89 | it("a cursor the account no longer knows: starts again from nothing", async () => { |
| 90 | const { acct, copy } = setup({ saved: JSON.stringify({ v: 1, cursor: "c999", visits: [{ id: "old", url: "https://gone.example/", title: "Gone", at: NOW }] }) }); |
| 91 | acct.add("https://a.example/1", "One"); |
| 92 | await copy.sync(); |
| 93 | expect(copy.pages().map((p) => p.url)).toEqual(["https://a.example/1"]); |
| 94 | }); |
| 95 | |
| 96 | it("reads page after page when there is more", async () => { |
| 97 | const { acct, copy } = setup(); |
| 98 | for (let i = 0; i < 1200; i++) acct.add(`https://many.example/${i}`, `Page ${i}`); |
| 99 | await copy.sync(); |
| 100 | expect(copy.state().visits).toBe(1200); |
| 101 | expect(acct.calls.length).toBe(3); |
| 102 | expect(acct.calls.every((c) => c.limit === 500)).toBe(true); |
| 103 | }); |
| 104 | |
| 105 | it("one page however many visits; most visited and most recent first", async () => { |
| 106 | const { acct, copy } = setup(); |
| 107 | acct.add("https://once.example/", "Once, today", NOW - 1000); |
| 108 | for (let i = 0; i < 5; i++) acct.add("https://often.example/", "Often", NOW - 3 * day - i); |
| 109 | acct.add("https://old.example/", "Old", NOW - 90 * day); |
| 110 | acct.add("https://old.example/", "Old, retitled", NOW - 80 * day); |
| 111 | await copy.sync(); |
| 112 | const pages = copy.pages(); |
| 113 | expect(pages.map((p) => p.url)).toEqual(["https://often.example/", "https://once.example/", "https://old.example/"]); |
| 114 | expect(pages[0]).toMatchObject({ visits: 5 }); |
| 115 | expect(pages[2]).toMatchObject({ visits: 2, title: "Old, retitled" }); |
| 116 | }); |
| 117 | |
| 118 | it("is saved without the pages' text, and comes back when the browser opens", async () => { |
| 119 | const a = setup(); |
| 120 | a.acct.add("https://a.example/1", "One"); |
| 121 | await a.copy.sync(); |
| 122 | const saved = a.disk.text!; |
| 123 | expect(JSON.parse(saved)).toMatchObject({ v: 1, cursor: "c1", visits: [{ id: "v1", url: "https://a.example/1", title: "One" }] }); |
| 124 | expect(saved).not.toMatch(/text/); |
| 125 | const b = setup({ connected: false, saved }); |
| 126 | expect(b.copy.pages().map((p) => p.url)).toEqual(["https://a.example/1"]); |
| 127 | }); |
| 128 | |
| 129 | it("signing out deletes it; nothing is read while signed out", async () => { |
| 130 | const { acct, copy, disk, st } = setup(); |
| 131 | acct.add("https://a.example/1", "One"); |
| 132 | await copy.sync(); |
| 133 | copy.clear(); |
| 134 | expect(disk.text).toBeNull(); |
| 135 | expect(copy.pages()).toEqual([]); |
| 136 | st.connected = false; |
| 137 | await copy.sync(); |
| 138 | expect(acct.calls).toHaveLength(1); |
| 139 | }); |
| 140 | |
| 141 | it("a forget here takes the visits out at once, by page, site or the last hour", async () => { |
| 142 | const { acct, copy } = setup(); |
| 143 | acct.add("https://www.other.com/a", "A", NOW - 10 * 60_000); |
| 144 | acct.add("https://sub.other.com/b", "B", NOW - 2 * 3600_000); |
| 145 | acct.add("https://example.com/c", "C", NOW - 2 * 3600_000); |
| 146 | await copy.sync(); |
| 147 | copy.remove(forgetMatch("last_hour", undefined, NOW)); |
| 148 | expect(copy.pages().map((p) => p.url).sort()).toEqual(["https://example.com/c", "https://sub.other.com/b"]); |
| 149 | copy.remove(forgetMatch("site", "https://www.other.com/", NOW)); |
| 150 | expect(copy.pages().map((p) => p.url)).toEqual(["https://example.com/c"]); |
| 151 | copy.remove(forgetMatch("url", "https://example.com/c?utm_source=x#y", NOW)); |
| 152 | expect(copy.pages()).toEqual([]); |
| 153 | }); |
| 154 | |
| 155 | it("keeps at most MAX_VISITS visits, the oldest go first", async () => { |
| 156 | const { acct, copy } = setup(); |
| 157 | for (let i = 0; i < MAX_VISITS + 10; i++) acct.add(`https://p.example/${i}`, "", NOW - (MAX_VISITS + 10 - i) * 1000); |
| 158 | await copy.sync(); // (one read takes up to 20,000; the rest come on the next) |
| 159 | await copy.sync(); |
| 160 | expect(copy.state().visits).toBe(MAX_VISITS); |
| 161 | expect(copy.pages().some((p) => p.url === "https://p.example/0")).toBe(false); |
| 162 | }); |
| 163 | }); |