- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/open-browser.test.ts?format=json
67 linesspeed 100%Last change speed in version 60, 1 version changed it
1// Opening the approve page in the person's browser: when it's skipped, which program opens it on each
2// platform, and that a failed open is silent.
4import assert from "node:assert/strict";
5import type { spawn } from "node:child_process";
6import { EventEmitter } from "node:events";
7import { test } from "node:test";
8import { browserSkip, isWebLink, openCommand, openInBrowser } from "../src/open-browser.ts";
10const LINK = "https://abso.dev/login/box?code=WXYZ-BCDF&from=cli";
12test("a desktop opens a browser", () => {
13 assert.equal(browserSkip({}, "win32"), null);
14 assert.equal(browserSkip({}, "darwin"), null);
15 assert.equal(browserSkip({ DISPLAY: ":0" }, "linux"), null);
16 assert.equal(browserSkip({ WAYLAND_DISPLAY: "wayland-0" }, "linux"), null);
17 assert.equal(browserSkip({ CI: "false" }, "darwin"), null, "CI=false isn't CI");
18});
20test("no browser with ABSO_NO_BROWSER, over SSH, under CI, or on Linux without a display", () => {
21 assert.equal(browserSkip({ ABSO_NO_BROWSER: "1" }, "win32"), "ABSO_NO_BROWSER is set");
22 assert.equal(browserSkip({ SSH_CONNECTION: "10.0.0.2 51234 10.0.0.1 22" }, "darwin"), "this is an SSH session");
23 assert.equal(browserSkip({ SSH_TTY: "/dev/pts/0", DISPLAY: ":0" }, "linux"), "this is an SSH session");
24 assert.equal(browserSkip({ CI: "true" }, "win32"), "this is CI");
25 assert.equal(browserSkip({}, "linux"), "there's no display");
26 assert.equal(browserSkip({}, "freebsd"), "there's no display");
27});
29test("each platform's opener takes the link whole, with no shell to split it at &", () => {
30 assert.deepEqual(openCommand(LINK, "win32"), ["rundll32", ["url.dll,FileProtocolHandler", LINK]]);
31 assert.deepEqual(openCommand(LINK, "darwin"), ["open", [LINK]]);
32 assert.deepEqual(openCommand(LINK, "linux"), ["xdg-open", [LINK]]);
33});
35test("only web pages are opened", () => {
36 assert.equal(isWebLink(LINK), true);
37 assert.equal(isWebLink("http://localhost:8787/login/box?code=A"), true);
38 for (const bad of ["file:///C:/Windows/System32/calc.exe", "javascript:alert(1)", "C:\\x.exe", "not a link", ""])
39 assert.equal(isWebLink(bad), false, bad);
40});
42type Call = { command: string; args: readonly string[]; options: unknown };
43function fakeSpawn(calls: Call[], fail?: "throw" | "error"): typeof spawn {
44 return ((command: string, args: readonly string[], options: unknown) => {
45 calls.push({ command, args, options });
46 if (fail === "throw") throw new Error("spawn EACCES");
47 const child = Object.assign(new EventEmitter(), { unref: () => {} });
48 if (fail === "error") queueMicrotask(() => child.emit("error", new Error("spawn xdg-open ENOENT")));
49 return child;
50 }) as unknown as typeof spawn;
51}
53test("openInBrowser starts the opener without waiting on it, and a failure is silent", async () => {
54 const calls: Call[] = [];
55 assert.equal(openInBrowser(LINK, "win32", fakeSpawn(calls)), true);
56 assert.deepEqual(calls[0]?.command, "rundll32");
57 assert.deepEqual(calls[0]?.args, ["url.dll,FileProtocolHandler", LINK]);
58 assert.deepEqual(calls[0]?.options, { detached: true, stdio: "ignore", windowsHide: true });
60 assert.equal(openInBrowser(LINK, "linux", fakeSpawn([], "throw")), false);
61 assert.equal(openInBrowser(LINK, "linux", fakeSpawn([], "error")), true);
62 await new Promise((done) => setTimeout(done, 0)); // the error event fires, and nothing throws
64 const none: Call[] = [];
65 assert.equal(openInBrowser("file:///etc/passwd", "linux", fakeSpawn(none)), false);
66 assert.equal(none.length, 0, "a link that isn't a web page never reaches the opener");
67});