Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 2 to 318

Compare

+67 −0 in 1 place
new file, 67 lines
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.
3+
4+import assert from "node:assert/strict";
5+import type { spawn } from "node:child_process";
6+import { EventEmitter } from "node:events";
7+import { test } from "node:test";
8+import { browserSkip, isWebLink, openCommand, openInBrowser } from "../src/open-browser.ts";
9+
10+const LINK = "https://abso.dev/login/box?code=WXYZ-BCDF&from=cli";
12+test("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+});
20+test("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+});
29+test("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+});
35+test("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+});
42+type Call = { command: string; args: readonly string[]; options: unknown };
43+function 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+}
53+test("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+});
MCP
compare on this site's /mcp
CLI
abso diff 2 318 client/test/open-browser.test.ts --json
JSON
/christopher/abso-core/compare/2..318?file=client%2Ftest%2Fopen-browser.test.ts&format=json