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
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
downloads-store.test.ts · 2.8 KB · 64 lines
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { DownloadList, MAX_KEPT, type DownloadRecord } from "../src/downloads-store"; |
| 3 | |
| 4 | const base = (over: Partial<DownloadRecord> = {}): Omit<DownloadRecord, "id"> => ({ |
| 5 | name: "report.pdf", |
| 6 | host: "example.com", |
| 7 | url: "https://example.com/report.pdf", |
| 8 | total: 100, |
| 9 | received: 0, |
| 10 | state: "progressing", |
| 11 | path: "/downloads/report.pdf", |
| 12 | startedAt: 1000, |
| 13 | ...over, |
| 14 | }); |
| 15 | |
| 16 | describe("the downloads list", () => { |
| 17 | it("is saved when a download starts and ends, and read back after a restart", () => { |
| 18 | let file: string | null = null; |
| 19 | let saves = 0; |
| 20 | const a = new DownloadList(null, (t) => ((file = t), saves++), () => 5000); |
| 21 | const r = a.add(base()); |
| 22 | expect(r.id).toBe(1); |
| 23 | a.update(r.id, { received: 50 }); // progress alone: not saved each time |
| 24 | expect(saves).toBe(1); |
| 25 | a.update(r.id, { received: 100, state: "completed" }); |
| 26 | expect(saves).toBe(2); |
| 27 | expect(a.get(r.id)?.endedAt).toBe(5000); |
| 28 | const b = new DownloadList(file, () => {}); |
| 29 | expect(b.list().map((x) => [x.id, x.name, x.state, x.received])).toEqual([[1, "report.pdf", "completed", 100]]); |
| 30 | expect(b.add(base({ name: "next.zip" })).id).toBe(2); // ids carry on after a restart |
| 31 | }); |
| 32 | |
| 33 | it("lists one still running when the browser closed as interrupted", () => { |
| 34 | const file = JSON.stringify({ v: 1, items: [{ ...base(), id: 3, state: "progressing" }, { ...base(), id: 4, state: "paused" }] }); |
| 35 | expect(new DownloadList(file, () => {}).list().map((x) => x.state)).toEqual(["interrupted", "interrupted"]); |
| 36 | }); |
| 37 | |
| 38 | it("ignores a damaged file and damaged entries, and keeps only web addresses for Retry", () => { |
| 39 | expect(new DownloadList("{nope", () => {}).list()).toEqual([]); |
| 40 | const file = JSON.stringify({ v: 1, items: [{ id: "x", name: "a" }, { id: 2 }, { ...base(), id: 5, url: "javascript:alert(1)", state: "bogus", extra: "dropped" }] }); |
| 41 | const l = new DownloadList(file, () => {}).list(); |
| 42 | expect(l.length).toBe(1); |
| 43 | expect(l[0]).toMatchObject({ id: 5, url: "", state: "interrupted" }); |
| 44 | expect("extra" in l[0]!).toBe(false); |
| 45 | }); |
| 46 | |
| 47 | it("removes one or clears the finished ones, never a running download", () => { |
| 48 | const a = new DownloadList(null, () => {}); |
| 49 | const done = a.add(base({ state: "completed" })); |
| 50 | const run = a.add(base({ name: "big.bin" })); |
| 51 | expect(a.remove(run.id)).toBe(false); |
| 52 | expect(a.remove(done.id)).toBe(true); |
| 53 | a.add(base({ state: "cancelled" })); |
| 54 | expect(a.clear()).toBe(1); |
| 55 | expect(a.list().map((x) => x.name)).toEqual(["big.bin"]); |
| 56 | }); |
| 57 | |
| 58 | it("keeps the newest MAX_KEPT", () => { |
| 59 | const a = new DownloadList(null, () => {}); |
| 60 | for (let i = 0; i <= MAX_KEPT; i++) a.add(base({ name: `f${i}`, state: "completed" })); |
| 61 | expect(a.list().length).toBe(MAX_KEPT); |
| 62 | expect(a.list()[0]!.name).toBe("f1"); |
| 63 | }); |
| 64 | }); |