Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/chrome-cdp/__serial__/binary-version.test.ts

Raw
import { afterEach, expect, it, vi } from "vitest";

const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
vi.mock("node:child_process", async (importOriginal) => ({
	...(await importOriginal<typeof import("node:child_process")>()),
	execFile: execFileMock,
}));

import { Browser } from "../browser.ts";

afterEach(() => {
	vi.unstubAllGlobals();
	vi.resetAllMocks();
});

function platform(value: string): void {
	vi.stubGlobal("process", { ...process, platform: value });
}

function output(text: string): void {
	execFileMock.mockImplementation((_file, _args, _options, callback) => {
		callback(null, text, "");
	});
}

it("reads Windows executable metadata without starting a GUI or interpolating its path", async () => {
	platform("win32");
	output("153.0.8010.12\r\n");
	const file = "C:\\Browser's directory\\chrome.exe";
	await expect(Browser.binaryMajor(file)).resolves.toBe("153.0.8010.12");
	const [command, args, options] = execFileMock.mock.calls[0]!;
	expect(command).toBe("powershell.exe");
	expect(args).toContain("-NoProfile");
	expect(args.at(-1)).toContain("$env:PI_EXT_CHROME_VERSION_FILE");
	expect(args.join(" ")).not.toContain(file);
	expect(options).toMatchObject({
		timeout: 3000,
		maxBuffer: 64 * 1024,
		env: { PI_EXT_CHROME_VERSION_FILE: file },
	});
});

it("reads non-Windows Chrome version through its CLI", async () => {
	platform("linux");
	output("Google Chrome for Testing 153.0.8010.12\n");
	await expect(Browser.binaryMajor("/browser/chrome")).resolves.toBe(
		"153.0.8010.12",
	);
	expect(execFileMock.mock.calls[0]?.slice(0, 2)).toEqual([
		"/browser/chrome",
		["--version"],
	]);
});

it("rejects malformed version metadata", async () => {
	platform("win32");
	output("unknown version");
	await expect(Browser.binaryMajor("chrome.exe")).rejects.toThrow(
		"cannot parse Chrome version",
	);
});

it("propagates bounded subprocess failures", async () => {
	platform("win32");
	execFileMock.mockImplementation((_file, _args, _options, callback) =>
		callback(new Error("version lookup timed out"), "", ""),
	);
	await expect(Browser.binaryMajor("chrome.exe")).rejects.toThrow(
		"version lookup timed out",
	);
});