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
keep.test.ts · 4.6 KB · 71 lines
| 1 | import { mkdtempSync } from "node:fs"; |
| 2 | import { tmpdir } from "node:os"; |
| 3 | import { join } from "node:path"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { KeepStore, keptFilename, keptPageHtml, keptUrl, parseKeptUrl } from "../src/keep-store"; |
| 6 | |
| 7 | const store = () => new KeepStore(join(mkdtempSync(join(tmpdir(), "keep-")), "kept")); |
| 8 | |
| 9 | describe("kept copies", () => { |
| 10 | it("keeps the text, the picture and a record, and lists newest first", async () => { |
| 11 | const s = store(); |
| 12 | const a = await s.add({ url: "https://a.example/x", title: "A page", text: "alpha words here", png: Buffer.from([1, 2, 3]), now: 1000 }); |
| 13 | const b = await s.add({ url: "https://b.example/y", title: "B page", text: "beta words", png: null, reason: "read later", tags: [" news ", ""], now: 2000 }); |
| 14 | expect(a).toMatchObject({ title: "A page", textChars: 16, imageBytes: 3, tags: [] }); |
| 15 | expect(b).toMatchObject({ reason: "read later", tags: ["news"], imageBytes: 0 }); |
| 16 | expect((await s.list()).map((r) => r.id)).toEqual([b.id, a.id]); |
| 17 | expect((await s.get(a.id))?.text).toBe("alpha words here"); |
| 18 | expect(await s.image(a.id)).toEqual(Buffer.from([1, 2, 3])); |
| 19 | expect(await s.image(b.id)).toBeNull(); |
| 20 | }); |
| 21 | |
| 22 | it("finds pages by every word of a search, in title, address, reason, tags or text, with the words around the match", async () => { |
| 23 | const s = store(); |
| 24 | await s.add({ url: "https://r.example/marmalade", title: "Recipes", text: "First line.\nThe quick brown zebra keeps a secret recipe for marmalade.", png: null }); |
| 25 | await s.add({ url: "https://n.example/", title: "News", text: "nothing here", png: null, tags: ["zebra"] }); |
| 26 | const hit = await s.list("zebra marmalade"); |
| 27 | expect(hit.map((r) => r.title)).toEqual(["Recipes"]); |
| 28 | expect(hit[0]?.snippet).toMatch(/quick brown zebra keeps/); |
| 29 | expect((await s.list("zebra")).length).toBe(2); // the tag counts too |
| 30 | expect(await s.list("pineapple")).toEqual([]); |
| 31 | }); |
| 32 | |
| 33 | it("removes a copy, and never reaches outside its folder", async () => { |
| 34 | const s = store(); |
| 35 | const a = await s.add({ url: "https://a.example/", title: "A", text: "x", png: null }); |
| 36 | expect(await s.remove(a.id)).toBe(true); |
| 37 | expect(await s.get(a.id)).toBeNull(); |
| 38 | expect(await s.get("../../etc")).toBeNull(); |
| 39 | expect(await s.remove("../x")).toBe(false); |
| 40 | }); |
| 41 | |
| 42 | it("only its own addresses are served, and the page shown has nothing that runs and escapes what the page said", () => { |
| 43 | expect(parseKeptUrl(keptUrl("0123456789abcdef"))).toEqual({ id: "0123456789abcdef", file: "" }); |
| 44 | expect(parseKeptUrl(keptUrl("0123456789abcdef", "page.png"))).toEqual({ id: "0123456789abcdef", file: "page.png" }); |
| 45 | expect(parseKeptUrl("dbb-kept://0123456789abcdef/../../secret")).toBeNull(); |
| 46 | expect(parseKeptUrl("dbb-kept://short/")).toBeNull(); |
| 47 | expect(parseKeptUrl("https://0123456789abcdef/")).toBeNull(); |
| 48 | const html = keptPageHtml({ id: "0123456789abcdef", url: "https://x.example/\"><script>", title: "<b>T</b>", keptAt: 0, tags: [], textChars: 5, imageBytes: 0 }, "a <script>alert(1)</script> b"); |
| 49 | expect(html).not.toMatch(/<script>/); |
| 50 | expect(html).toMatch(/default-src 'none'/); |
| 51 | expect(html).toContain("<script>alert(1)</script>"); |
| 52 | }); |
| 53 | |
| 54 | it("names files readably: title, site and date, without characters file systems dislike", () => { |
| 55 | const at = Date.UTC(2026, 10, 20, 12); |
| 56 | expect(keptFilename({ title: "Amazon Fights the Delivery Protection Act", url: "https://slashdot.org/story/1", keptAt: at }, "png")).toBe("Amazon Fights the Delivery Protection Act (slashdot.org, 2026-11-20).png"); |
| 57 | expect(keptFilename({ title: "a/b: c? <d> | e", url: "https://www.x.example/", keptAt: at }, "txt")).toBe("a b c d e (x.example, 2026-11-20).txt"); |
| 58 | expect(keptFilename({ title: "slashdot.org", url: "https://slashdot.org/", keptAt: at }, "png")).toBe("slashdot.org (2026-11-20).png"); // the site is not said twice |
| 59 | expect(keptFilename({ title: "", url: "https://y.example/", keptAt: at }, "png")).toBe("y.example (2026-11-20).png"); |
| 60 | expect(keptFilename({ title: "x".repeat(200), url: "https://z.example/", keptAt: at }, "png").length).toBeLessThan(130); |
| 61 | }); |
| 62 | |
| 63 | it("updates a record (what was saved to the account)", async () => { |
| 64 | const s = store(); |
| 65 | const a = await s.add({ url: "https://a.example/", title: "A", text: "x", png: null }); |
| 66 | const u = await s.update(a.id, { remote: { files: [{ kind: "text", filename: "A (a.example, 2026-11-20).txt", id: "f1" }] } }); |
| 67 | expect(u?.remote?.files[0]?.id).toBe("f1"); |
| 68 | expect((await s.get(a.id))?.record.remote?.files).toHaveLength(1); |
| 69 | expect(await s.update("0000000000000000", { title: "x" })).toBeNull(); |
| 70 | }); |
| 71 | }); |