A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/test/extensions.test.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
extensions.test.ts · 3.9 KB · 75 lines
1import { chmodSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
2import { tmpdir } from "node:os";
3import { join } from "node:path";
4import { describe, expect, it } from "vitest";
5import { ExtensionRegistry, loadExtensions, parseExtensionPaths } from "../src/extensions.js";
6
7const dir = mkdtempSync(join(tmpdir(), "dbb-ext-"));
8const file = (name: string, body: string) => {
9 const p = join(dir, name);
10 writeFileSync(p, body);
11 return p;
12};
13
14describe("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
45describe("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});