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
extensions.test.ts · 3.9 KB · 75 lines
| 1 | import { chmodSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 2 | import { tmpdir } from "node:os"; |
| 3 | import { join } from "node:path"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { ExtensionRegistry, loadExtensions, parseExtensionPaths } from "../src/extensions.js"; |
| 6 | |
| 7 | const dir = mkdtempSync(join(tmpdir(), "dbb-ext-")); |
| 8 | const file = (name: string, body: string) => { |
| 9 | const p = join(dir, name); |
| 10 | writeFileSync(p, body); |
| 11 | return p; |
| 12 | }; |
| 13 | |
| 14 | describe("parseExtensionPaths", () => { |
| 15 | it("nothing set means no extensions", () => { |
| 16 | expect(parseExtensionPaths(undefined)).toEqual([]); |
| 17 | expect(parseExtensionPaths(" ")).toEqual([]); |
| 18 | }); |
| 19 | it("accepts absolute paths to local .mjs/.js files, separated by colons", () => { |
| 20 | const a = file("a.mjs", "export function register() {}"); |
| 21 | const b = file("b.js", "export function register() {}"); |
| 22 | expect(parseExtensionPaths(`${a}:${b}`)).toEqual([a, b]); |
| 23 | }); |
| 24 | it.each([["relative/path.mjs"], ["./x.mjs"], ["/no/such/file.mjs"], ["/tmp"]])("refuses %s", (p) => { |
| 25 | expect(() => parseExtensionPaths(p)).toThrow(/DBB_EXTENSIONS/); |
| 26 | }); |
| 27 | it("refuses a file other users can write to, or one in a folder they can write to", () => { |
| 28 | const f = file("open.mjs", "export function register() {}"); |
| 29 | chmodSync(f, 0o666); |
| 30 | expect(() => parseExtensionPaths(f)).toThrow(/can be written by other users/); |
| 31 | const shared = join(dir, "shared"); |
| 32 | mkdirSync(shared); |
| 33 | const g = join(shared, "g.mjs"); |
| 34 | writeFileSync(g, "export function register() {}"); |
| 35 | chmodSync(shared, 0o777); |
| 36 | expect(() => parseExtensionPaths(g)).toThrow(/can be written by other users/); |
| 37 | chmodSync(shared, 0o755); |
| 38 | expect(parseExtensionPaths(g)).toEqual([g]); |
| 39 | }); |
| 40 | it("refuses a file that is not .mjs or .js", () => { |
| 41 | expect(() => parseExtensionPaths(file("x.txt", "hi"))).toThrow(/not a \.mjs or \.js/); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("loading and calling", () => { |
| 46 | it("registers methods under ext. and calls them with the parameters and tools", async () => { |
| 47 | const f = file("ok.mjs", 'export function register(api) { api.method("hello", (p, t) => ({ got: p.x, hasTools: typeof t.webContents })); }'); |
| 48 | const reg = new ExtensionRegistry<string>(); |
| 49 | const announced: string[] = []; |
| 50 | await loadExtensions([f], reg, (x) => announced.push(x)); |
| 51 | expect(reg.names()).toEqual(["ext.hello"]); |
| 52 | expect(reg.loaded).toEqual([f]); |
| 53 | expect(reg.loadedNames()).toEqual(["ok.mjs"]); // the status report shows names only |
| 54 | expect(announced).toEqual([f]); |
| 55 | expect(await reg.call("ext.hello", { x: 5 }, { webContents: async () => "page" })).toEqual({ got: 5, hasTools: "function" }); |
| 56 | expect(reg.has("page.goto")).toBe(false); // a built-in name is never reachable this way |
| 57 | }); |
| 58 | it("a handler that throws rejects", async () => { |
| 59 | const f = file("throws.mjs", 'export function register(api) { api.method("boom", () => { throw new Error("no"); }); }'); |
| 60 | const reg = new ExtensionRegistry<string>(); |
| 61 | await loadExtensions([f], reg); |
| 62 | await expect(reg.call("ext.boom", {}, { webContents: async () => "" })).rejects.toThrow("no"); |
| 63 | }); |
| 64 | it("refuses a module with no register function", async () => { |
| 65 | await expect(loadExtensions([file("none.mjs", "export const x = 1;")], new ExtensionRegistry())).rejects.toThrow(/register/); |
| 66 | }); |
| 67 | it.each([["Bad Name"], ["../x"], ["ext.double"], ["UPPER"], [""], ["a.b.c"]])("refuses the method name %j", async (name) => { |
| 68 | const f = file("name.mjs", `export function register(api) { api.method(${JSON.stringify(name)}, () => 1); }`); |
| 69 | await expect(loadExtensions([f], new ExtensionRegistry())).rejects.toThrow(/not a valid method name|already/); |
| 70 | }); |
| 71 | it("refuses a duplicate method", async () => { |
| 72 | const f = file("dup.mjs", 'export function register(api) { api.method("same", () => 1); api.method("same", () => 2); }'); |
| 73 | await expect(loadExtensions([f], new ExtensionRegistry())).rejects.toThrow(/already registered/); |
| 74 | }); |
| 75 | }); |