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
favicons.test.ts · 5.4 KB · 109 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { FaviconStore, MAX_HOSTS, REFRESH_MS, hostOf, sniff, type Fetched } from "../src/favicons.js"; |
| 3 | |
| 4 | const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]); |
| 5 | const ICO = new Uint8Array([0, 0, 1, 0, 9, 9]); |
| 6 | |
| 7 | function store(over: { fetch?: (u: string) => Promise<Fetched | null>; file?: string | null } = {}) { |
| 8 | const log = { fetched: [] as string[], saved: [] as (string | null)[] }; |
| 9 | let now = 1_000_000; |
| 10 | const s = new FaviconStore({ |
| 11 | fetch: async (u) => { log.fetched.push(u); return over.fetch ? over.fetch(u) : { bytes: ICO, type: "text/plain" }; }, |
| 12 | shrink: () => "data:image/png;base64,c21hbGw=", |
| 13 | load: () => over.file ?? null, |
| 14 | save: (t) => log.saved.push(t), |
| 15 | now: () => now, |
| 16 | }); |
| 17 | return { s, log, tick: (ms: number) => (now += ms) }; |
| 18 | } |
| 19 | |
| 20 | describe("favicons", () => { |
| 21 | it("knows the format from the bytes, whatever the server calls it", () => { |
| 22 | expect(sniff(PNG)).toBe("image/png"); |
| 23 | expect(sniff(ICO)).toBe("image/x-icon"); |
| 24 | expect(sniff(new TextEncoder().encode('<?xml version="1.0"?>\n<svg xmlns="x"></svg>'))).toBe("image/svg+xml"); |
| 25 | expect(sniff(new TextEncoder().encode("<!DOCTYPE html><title>Not found</title>"))).toBe("text/html"); |
| 26 | }); |
| 27 | |
| 28 | it("learns a site's icon once, from the page's first web or data: candidate, and keeps it by host", async () => { |
| 29 | const { s, log } = store(); |
| 30 | const icon = await s.learn("https://www.example.com/a/b?x=1", ["chrome://x", "https://www.example.com/favicon.ico"]); |
| 31 | expect(log.fetched).toEqual(["https://www.example.com/favicon.ico"]); |
| 32 | expect(icon).toBe("data:image/x-icon;base64," + Buffer.from(ICO).toString("base64")); // an ICO is kept as it is |
| 33 | expect(s.get("www.example.com")).toBe(icon); |
| 34 | expect(await s.learn("https://www.example.com/other", ["https://www.example.com/favicon.ico"])).toBeNull(); |
| 35 | expect(log.fetched.length).toBe(1); // fresh: not fetched again |
| 36 | }); |
| 37 | |
| 38 | it("shrinks PNG and JPEG icons", async () => { |
| 39 | const { s } = store({ fetch: async () => ({ bytes: PNG, type: "image/png" }) }); |
| 40 | expect(await s.learn("https://a.test/", ["https://a.test/i.png"])).toBe("data:image/png;base64,c21hbGw="); |
| 41 | }); |
| 42 | |
| 43 | it("takes an icon written into the page as data:", async () => { |
| 44 | const { s, log } = store(); |
| 45 | const svg = "data:image/svg+xml," + encodeURIComponent("<svg xmlns='http://www.w3.org/2000/svg'/>"); |
| 46 | expect(await s.learn("https://b.test/", [svg])).toMatch(/^data:image\/svg\+xml;base64,/); |
| 47 | expect(log.fetched).toEqual([]); |
| 48 | }); |
| 49 | |
| 50 | it("refuses what is not an image (a not-found page), and does not try that site again for a day", async () => { |
| 51 | const { s, log, tick } = store({ fetch: async () => ({ bytes: new TextEncoder().encode("<html>404</html>"), type: "image/x-icon" }) }); |
| 52 | expect(await s.learn("https://c.test/", ["https://c.test/favicon.ico"])).toBeNull(); |
| 53 | expect(s.get("c.test")).toBeNull(); |
| 54 | await s.learn("https://c.test/", ["https://c.test/favicon.ico"]); |
| 55 | expect(log.fetched.length).toBe(1); |
| 56 | tick(25 * 3600 * 1000); |
| 57 | await s.learn("https://c.test/", ["https://c.test/favicon.ico"]); |
| 58 | expect(log.fetched.length).toBe(2); |
| 59 | }); |
| 60 | |
| 61 | it("refuses a huge icon", async () => { |
| 62 | const big = new Uint8Array(200 * 1024); big.set(ICO); |
| 63 | const { s } = store({ fetch: async () => ({ bytes: big, type: "image/x-icon" }) }); |
| 64 | expect(await s.learn("https://d.test/", ["https://d.test/favicon.ico"])).toBeNull(); |
| 65 | }); |
| 66 | |
| 67 | it("looks again after a week, and only reports a change", async () => { |
| 68 | const { s, log, tick } = store(); |
| 69 | await s.learn("https://e.test/", ["https://e.test/favicon.ico"]); |
| 70 | tick(REFRESH_MS + 1); |
| 71 | expect(await s.learn("https://e.test/", ["https://e.test/favicon.ico"])).toBeNull(); // same icon |
| 72 | expect(log.fetched.length).toBe(2); |
| 73 | }); |
| 74 | |
| 75 | it("only web pages have icons", async () => { |
| 76 | const { s, log } = store(); |
| 77 | expect(await s.learn("dbb-kept://1/", ["https://x.test/favicon.ico"])).toBeNull(); |
| 78 | expect(await s.learn("about:blank", ["https://x.test/favicon.ico"])).toBeNull(); |
| 79 | expect(log.fetched).toEqual([]); |
| 80 | expect(hostOf("HTTP://Example.COM:8080/x")).toBe("example.com"); |
| 81 | }); |
| 82 | |
| 83 | it("forgets one site, or all of them (an empty store deletes its file)", async () => { |
| 84 | const { s, log } = store(); |
| 85 | await s.learn("https://f.test/", ["https://f.test/favicon.ico"]); |
| 86 | await s.learn("https://g.test/", ["https://g.test/favicon.ico"]); |
| 87 | s.forget("F.test"); |
| 88 | expect(s.get("f.test")).toBeNull(); |
| 89 | expect(JSON.parse(log.saved.at(-1)!).icons["g.test"]).toBeTruthy(); |
| 90 | s.forget(); |
| 91 | expect(s.size()).toBe(0); |
| 92 | expect(log.saved.at(-1)).toBeNull(); |
| 93 | }); |
| 94 | |
| 95 | it("survives a restart, ignores a damaged file, and keeps only the sites seen most recently", async () => { |
| 96 | const a = store(); |
| 97 | await a.s.learn("https://h.test/", ["https://h.test/favicon.ico"]); |
| 98 | a.s.flush(); |
| 99 | const b = store({ file: a.log.saved.at(-1)! }); |
| 100 | expect(b.s.get("h.test")).toMatch(/^data:image\//); |
| 101 | expect(store({ file: "{not json" }).s.size()).toBe(0); |
| 102 | expect(store({ file: JSON.stringify({ v: 1, icons: { "x.test": { icon: "javascript:alert(1)", at: 1 } } }) }).s.size()).toBe(0); |
| 103 | const c = store(); |
| 104 | for (let i = 0; i < MAX_HOSTS + 5; i++) { c.tick(1); await c.s.learn(`https://s${i}.test/`, [`https://s${i}.test/favicon.ico`]); } |
| 105 | expect(c.s.size()).toBe(MAX_HOSTS); |
| 106 | expect(c.s.get("s0.test")).toBeNull(); |
| 107 | expect(c.s.get(`s${MAX_HOSTS + 4}.test`)).not.toBeNull(); |
| 108 | }); |
| 109 | }); |