import { describe, expect, it, vi } from "vitest"; import { openUrl, resolveBrowserOpenCommand, resolveControlUiLinks } from "./onboard-helpers.js"; const mocks = vi.hoisted(() => ({ runCommandWithTimeout: vi.fn(async () => ({ stdout: "", stderr: "", code: 0, signal: null, killed: false, })), pickPrimaryTailnetIPv4: vi.fn(() => undefined), })); vi.mock("../process/exec.js", () => ({ runCommandWithTimeout: mocks.runCommandWithTimeout, })); vi.mock("../infra/tailnet.js", () => ({ pickPrimaryTailnetIPv4: mocks.pickPrimaryTailnetIPv4, })); describe("openUrl", () => { it("quotes URLs on win32 so '&' is not treated as cmd separator", async () => { vi.spyOn(process, "platform", "get").mockReturnValue("win32"); const url = "https://accounts.google.com/o/oauth2/v2/auth?client_id=abc&response_type=code&redirect_uri=http%3A%2F%2Flocalhost"; const ok = await openUrl(url); expect(ok).toBe(true); expect(mocks.runCommandWithTimeout).toHaveBeenCalledTimes(1); const [argv, options] = mocks.runCommandWithTimeout.mock.calls[0] ?? []; expect(argv?.slice(0, 4)).toEqual(["cmd", "/c", "start", '""']); expect(argv?.at(-1)).toBe(`"${url}"`); expect(options).toMatchObject({ timeoutMs: 5_000, windowsVerbatimArguments: true, }); }); }); describe("resolveBrowserOpenCommand", () => { it("marks win32 commands as quoteUrl=true", async () => { vi.spyOn(process, "platform", "get").mockReturnValue("win32"); const resolved = await resolveBrowserOpenCommand(); expect(resolved.argv).toEqual(["cmd", "/c", "start", ""]); expect(resolved.quoteUrl).toBe(true); }); }); describe("resolveControlUiLinks", () => { it("uses customBindHost for custom bind", () => { const links = resolveControlUiLinks({ port: 18789, bind: "custom", customBindHost: "192.168.1.100", }); expect(links.httpUrl).toBe("http://192.168.1.100:18789/"); expect(links.wsUrl).toBe("ws://192.168.1.100:18789"); }); it("falls back to loopback for invalid customBindHost", () => { const links = resolveControlUiLinks({ port: 18789, bind: "custom", customBindHost: "192.168.001.100", }); expect(links.httpUrl).toBe("http://127.0.0.1:18789/"); expect(links.wsUrl).toBe("ws://127.0.0.1:18789"); }); });