import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { resolveFirefoxBinary } from "../index.ts"; import { findExecutable, findExecutableFile, } from "../src/pi-ext-executable.ts"; vi.mock("../src/pi-ext-executable.ts", () => ({ findExecutable: vi.fn(), findExecutableFile: vi.fn(), })); const roots = [ "ProgramW6432", "ProgramFiles", "ProgramFiles(x86)", "LOCALAPPDATA", ]; describe("Firefox binary discovery", () => { const platform = process.platform; afterEach(() => { Object.defineProperty(process, "platform", { value: platform }); vi.restoreAllMocks(); vi.unstubAllEnvs(); vi.resetAllMocks(); }); function windows() { Object.defineProperty(process, "platform", { value: "win32" }); for (const root of roots) vi.stubEnv(root, undefined); } it.each(roots)("finds Firefox under %s without PATH", (root) => { windows(); vi.stubEnv(root, "C:/Firefox installation root"); const binary = join( "C:/Firefox installation root", "Mozilla Firefox", "firefox.exe", ); vi.mocked(findExecutableFile).mockImplementation((candidate) => candidate === binary ? candidate : undefined, ); expect(resolveFirefoxBinary(undefined)).toBe(binary); }); it("prefers PATH over Windows installation locations", () => { windows(); vi.stubEnv("ProgramFiles", "C:/Programs"); vi.mocked(findExecutable).mockReturnValue("C:/custom/firefox.exe"); expect(resolveFirefoxBinary(undefined)).toBe("C:/custom/firefox.exe"); expect(findExecutableFile).not.toHaveBeenCalled(); }); it("skips missing candidates and prefers system over per-user installs", () => { windows(); for (const root of roots) vi.stubEnv(root, `C:/${root}`); const binary = join("C:/ProgramFiles", "Mozilla Firefox", "firefox.exe"); vi.mocked(findExecutableFile).mockImplementation((candidate) => candidate === binary ? candidate : undefined, ); expect(resolveFirefoxBinary(undefined)).toBe(binary); expect(findExecutableFile).toHaveBeenCalledTimes(2); }); it("does not search Windows locations on other platforms", () => { Object.defineProperty(process, "platform", { value: "linux" }); vi.stubEnv("ProgramFiles", "C:/Programs"); expect(() => resolveFirefoxBinary(undefined)).toThrow("firefox not found"); expect(findExecutableFile).not.toHaveBeenCalled(); }); it("reports missing Firefox after exhausting Windows locations", () => { windows(); vi.stubEnv("ProgramFiles", "C:/Programs"); expect(() => resolveFirefoxBinary(undefined)).toThrow("set FIREFOX_BIN"); expect(findExecutableFile).toHaveBeenCalledTimes(1); }); it("does not fall back when an explicitly configured executable is missing", () => { windows(); expect(() => resolveFirefoxBinary("/nonexistent/firefox-bin")).toThrow( "firefox not found", ); expect(findExecutable).not.toHaveBeenCalled(); expect(findExecutableFile).not.toHaveBeenCalled(); }); });