












@@ -0,0 +1,86 @@
1+import { afterEach, describe, expect, it, vi } from "vitest";
2+3+const execFileSyncMock = vi.hoisted(() => vi.fn());
4+5+vi.mock("node:child_process", async () => {
6+const actual = await vi.importActual<typeof import("node:child_process")>("node:child_process");
7+return {
8+ ...actual,
9+execFileSync: (...args: unknown[]) => execFileSyncMock(...args),
10+};
11+});
12+13+import { readBrowserVersion } from "./chrome.executables.js";
14+15+function stubPlatform(platform: NodeJS.Platform): void {
16+Object.defineProperty(process, "platform", {
17+configurable: true,
18+value: platform,
19+});
20+}
21+22+describe("readBrowserVersion", () => {
23+const originalPlatform = process.platform;
24+25+afterEach(() => {
26+stubPlatform(originalPlatform);
27+execFileSyncMock.mockReset();
28+vi.restoreAllMocks();
29+});
30+31+it("reads macOS app bundle versions from Info.plist before spawning Chrome", () => {
32+stubPlatform("darwin");
33+execFileSyncMock.mockReturnValue("148.0.7778.179\n");
34+35+const version = readBrowserVersion(
36+"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
37+);
38+39+expect(version).toBe("148.0.7778.179");
40+expect(execFileSyncMock).toHaveBeenCalledTimes(1);
41+expect(execFileSyncMock).toHaveBeenCalledWith(
42+"/usr/libexec/PlistBuddy",
43+[
44+"-c",
45+"Print :CFBundleShortVersionString",
46+"/Applications/Google Chrome.app/Contents/Info.plist",
47+],
48+expect.objectContaining({ timeout: 800 }),
49+);
50+});
51+52+it("falls back to a slower --version probe when macOS bundle metadata is unavailable", () => {
53+stubPlatform("darwin");
54+execFileSyncMock
55+.mockImplementationOnce(() => {
56+throw new Error("plist unavailable");
57+})
58+.mockReturnValueOnce("Google Chrome 148.0.7778.179\n");
59+60+const version = readBrowserVersion(
61+"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
62+);
63+64+expect(version).toBe("Google Chrome 148.0.7778.179");
65+expect(execFileSyncMock).toHaveBeenNthCalledWith(
66+2,
67+"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
68+["--version"],
69+expect.objectContaining({ timeout: 6000 }),
70+);
71+});
72+73+it("uses the slower --version probe for non-bundle paths", () => {
74+stubPlatform("darwin");
75+execFileSyncMock.mockReturnValue("Chromium 148.0.7778.179\n");
76+77+const version = readBrowserVersion("/opt/chromium/chrome");
78+79+expect(version).toBe("Chromium 148.0.7778.179");
80+expect(execFileSyncMock).toHaveBeenCalledWith(
81+"/opt/chromium/chrome",
82+["--version"],
83+expect.objectContaining({ timeout: 6000 }),
84+);
85+});
86+});
此內容由慣性聚合(RSS閱讀器)自動聚合整理,僅供閱讀參考。 原文來自 — 版權歸原作者所有。