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
reader-pages.test.ts · 2.4 KB · 54 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { EXPIRED_HTML, READER_SCHEME, ReaderPages, readerIdFromUrl, readerUrl } from "../src/reader-pages.js"; |
| 3 | |
| 4 | describe("ReaderPages", () => { |
| 5 | it("stores a page under a short opaque id and serves it back", () => { |
| 6 | const pages = new ReaderPages(); |
| 7 | const id = pages.put("<p>hi</p>"); |
| 8 | expect(id).toMatch(/^[0-9a-f]{18}$/); |
| 9 | expect(pages.get(id)).toBe("<p>hi</p>"); |
| 10 | expect(readerUrl(id)).toBe(`${READER_SCHEME}://${id}/`); |
| 11 | }); |
| 12 | it("update() replaces the content (the summary arrived), so a later load shows the finished page", () => { |
| 13 | const pages = new ReaderPages(); |
| 14 | const id = pages.put("pending skeleton"); |
| 15 | expect(pages.update(id, "finished page with TL;DR")).toBe(true); |
| 16 | expect(pages.get(id)).toBe("finished page with TL;DR"); |
| 17 | }); |
| 18 | it("update() on an evicted page is a harmless no-op", () => { |
| 19 | const pages = new ReaderPages(2); |
| 20 | const first = pages.put("a"); |
| 21 | pages.put("b"); |
| 22 | pages.put("c"); // evicts the oldest |
| 23 | expect(pages.get(first)).toBeUndefined(); |
| 24 | expect(pages.update(first, "late")).toBe(false); |
| 25 | expect(pages.get(first)).toBeUndefined(); |
| 26 | }); |
| 27 | it("is bounded: only the newest `max` pages are kept", () => { |
| 28 | const pages = new ReaderPages(3); |
| 29 | const ids = ["1", "2", "3", "4", "5"].map((x) => pages.put(x)); |
| 30 | expect(pages.size).toBe(3); |
| 31 | expect(ids.map((i) => pages.get(i))).toEqual([undefined, undefined, "3", "4", "5"]); |
| 32 | }); |
| 33 | it("ids are unique and unguessable-looking", () => { |
| 34 | const pages = new ReaderPages(1000); |
| 35 | expect(new Set(Array.from({ length: 500 }, () => pages.put("x"))).size).toBe(500); |
| 36 | }); |
| 37 | }); |
| 38 | |
| 39 | describe("readerIdFromUrl — only our own URLs resolve", () => { |
| 40 | it("accepts a well-formed reader URL", () => { |
| 41 | const id = "0123456789abcdef01"; |
| 42 | expect(readerIdFromUrl(readerUrl(id))).toBe(id); |
| 43 | expect(readerIdFromUrl(`${readerUrl(id)}?x=1#frag`)).toBe(id); |
| 44 | }); |
| 45 | it("rejects other schemes, odd hosts and garbage", () => { |
| 46 | for (const bad of ["https://0123456789abcdef01/", "dbb-reader://evil/", "dbb-reader://0123456789abcdef0123/", "dbb-reader://../../etc/passwd", "dbb-reader://", "not a url", ""]) { |
| 47 | expect(readerIdFromUrl(bad)).toBeNull(); |
| 48 | } |
| 49 | }); |
| 50 | it("the expired notice is a script-free page", () => { |
| 51 | expect(EXPIRED_HTML).not.toMatch(/<script/i); |
| 52 | expect(EXPIRED_HTML).toMatch(/default-src 'none'/); |
| 53 | }); |
| 54 | }); |