{"draft":null,"from":7,"to":318,"files":[{"path":"client/test/open-browser.test.ts","added":67,"removed":0,"diff":"--- /dev/null\n+++ b/client/test/open-browser.test.ts\n@@ -0,0 +1,67 @@\n+// Opening the approve page in the person's browser: when it's skipped, which program opens it on each\n+// platform, and that a failed open is silent.\n+\n+import assert from \"node:assert/strict\";\n+import type { spawn } from \"node:child_process\";\n+import { EventEmitter } from \"node:events\";\n+import { test } from \"node:test\";\n+import { browserSkip, isWebLink, openCommand, openInBrowser } from \"../src/open-browser.ts\";\n+\n+const LINK = \"https://abso.dev/login/box?code=WXYZ-BCDF&from=cli\";\n+\n+test(\"a desktop opens a browser\", () => {\n+\tassert.equal(browserSkip({}, \"win32\"), null);\n+\tassert.equal(browserSkip({}, \"darwin\"), null);\n+\tassert.equal(browserSkip({ DISPLAY: \":0\" }, \"linux\"), null);\n+\tassert.equal(browserSkip({ WAYLAND_DISPLAY: \"wayland-0\" }, \"linux\"), null);\n+\tassert.equal(browserSkip({ CI: \"false\" }, \"darwin\"), null, \"CI=false isn't CI\");\n+});\n+\n+test(\"no browser with ABSO_NO_BROWSER, over SSH, under CI, or on Linux without a display\", () => {\n+\tassert.equal(browserSkip({ ABSO_NO_BROWSER: \"1\" }, \"win32\"), \"ABSO_NO_BROWSER is set\");\n+\tassert.equal(browserSkip({ SSH_CONNECTION: \"10.0.0.2 51234 10.0.0.1 22\" }, \"darwin\"), \"this is an SSH session\");\n+\tassert.equal(browserSkip({ SSH_TTY: \"/dev/pts/0\", DISPLAY: \":0\" }, \"linux\"), \"this is an SSH session\");\n+\tassert.equal(browserSkip({ CI: \"true\" }, \"win32\"), \"this is CI\");\n+\tassert.equal(browserSkip({}, \"linux\"), \"there's no display\");\n+\tassert.equal(browserSkip({}, \"freebsd\"), \"there's no display\");\n+});\n+\n+test(\"each platform's opener takes the link whole, with no shell to split it at &\", () => {\n+\tassert.deepEqual(openCommand(LINK, \"win32\"), [\"rundll32\", [\"url.dll,FileProtocolHandler\", LINK]]);\n+\tassert.deepEqual(openCommand(LINK, \"darwin\"), [\"open\", [LINK]]);\n+\tassert.deepEqual(openCommand(LINK, \"linux\"), [\"xdg-open\", [LINK]]);\n+});\n+\n+test(\"only web pages are opened\", () => {\n+\tassert.equal(isWebLink(LINK), true);\n+\tassert.equal(isWebLink(\"http://localhost:8787/login/box?code=A\"), true);\n+\tfor (const bad of [\"file:///C:/Windows/System32/calc.exe\", \"javascript:alert(1)\", \"C:\\\\x.exe\", \"not a link\", \"\"])\n+\t\tassert.equal(isWebLink(bad), false, bad);\n+});\n+\n+type Call = { command: string; args: readonly string[]; options: unknown };\n+function fakeSpawn(calls: Call[], fail?: \"throw\" | \"error\"): typeof spawn {\n+\treturn ((command: string, args: readonly string[], options: unknown) => {\n+\t\tcalls.push({ command, args, options });\n+\t\tif (fail === \"throw\") throw new Error(\"spawn EACCES\");\n+\t\tconst child = Object.assign(new EventEmitter(), { unref: () => {} });\n+\t\tif (fail === \"error\") queueMicrotask(() => child.emit(\"error\", new Error(\"spawn xdg-open ENOENT\")));\n+\t\treturn child;\n+\t}) as unknown as typeof spawn;\n+}\n+\n+test(\"openInBrowser starts the opener without waiting on it, and a failure is silent\", async () => {\n+\tconst calls: Call[] = [];\n+\tassert.equal(openInBrowser(LINK, \"win32\", fakeSpawn(calls)), true);\n+\tassert.deepEqual(calls[0]?.command, \"rundll32\");\n+\tassert.deepEqual(calls[0]?.args, [\"url.dll,FileProtocolHandler\", LINK]);\n+\tassert.deepEqual(calls[0]?.options, { detached: true, stdio: \"ignore\", windowsHide: true });\n+\n+\tassert.equal(openInBrowser(LINK, \"linux\", fakeSpawn([], \"throw\")), false);\n+\tassert.equal(openInBrowser(LINK, \"linux\", fakeSpawn([], \"error\")), true);\n+\tawait new Promise((done) => setTimeout(done, 0)); // the error event fires, and nothing throws\n+\n+\tconst none: Call[] = [];\n+\tassert.equal(openInBrowser(\"file:///etc/passwd\", \"linux\", fakeSpawn(none)), false);\n+\tassert.equal(none.length, 0, \"a link that isn't a web page never reaches the opener\");\n+});"}],"unmatched":[]}