From d8745d928dd30d3f2436c23a0b73e5609e121ed4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 21:42:48 +0100 Subject: [PATCH] test: share browser facade fixtures --- src/plugin-sdk/browser-facade-test-helpers.ts | 59 +++++++++++++++++++ src/plugin-sdk/browser-facades.test.ts | 35 ++++------- .../browser-host-inspection.test.ts | 46 +++++---------- 3 files changed, 84 insertions(+), 56 deletions(-) create mode 100644 src/plugin-sdk/browser-facade-test-helpers.ts diff --git a/src/plugin-sdk/browser-facade-test-helpers.ts b/src/plugin-sdk/browser-facade-test-helpers.ts new file mode 100644 index 00000000000..4e0853f6567 --- /dev/null +++ b/src/plugin-sdk/browser-facade-test-helpers.ts @@ -0,0 +1,59 @@ +import { expect, vi } from "vitest"; + +type FacadeLoaderMock = ReturnType; + +type ChromeExecutableFixture = { + kind: string; + path: string; +}; + +const BROWSER_HOST_INSPECTION_ARTIFACT = { + dirName: "browser", + artifactBasename: "browser-host-inspection.js", +} as const; + +const BROWSER_VERSION = "Google Chrome 144.0.7534.0"; + +export function mockBrowserHostInspectionFacade( + loadBundledPluginPublicSurfaceModuleSync: FacadeLoaderMock, + executable: ChromeExecutableFixture, +) { + const resolveGoogleChromeExecutableForPlatform = vi.fn().mockReturnValue(executable); + const readBrowserVersion = vi.fn().mockReturnValue(BROWSER_VERSION); + const parseBrowserMajorVersion = vi.fn().mockReturnValue(144); + + loadBundledPluginPublicSurfaceModuleSync.mockReturnValue({ + resolveGoogleChromeExecutableForPlatform, + readBrowserVersion, + parseBrowserMajorVersion, + }); +} + +export function expectBrowserHostInspectionDelegation(params: { + executable: ChromeExecutableFixture; + hostInspection: typeof import("./browser-host-inspection.js"); + loadBundledPluginPublicSurfaceModuleSync: FacadeLoaderMock; +}) { + expect(params.hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toEqual( + params.executable, + ); + expect(params.hostInspection.readBrowserVersion(params.executable.path)).toBe(BROWSER_VERSION); + expect(params.hostInspection.parseBrowserMajorVersion(BROWSER_VERSION)).toBe(144); + expect(params.loadBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledWith( + BROWSER_HOST_INSPECTION_ARTIFACT, + ); +} + +export async function expectBrowserHostInspectionFacadeUnavailable( + loadBundledPluginPublicSurfaceModuleSync: FacadeLoaderMock, +) { + loadBundledPluginPublicSurfaceModuleSync.mockImplementation(() => { + throw new Error("missing browser host inspection facade"); + }); + + const hostInspection = await import("./browser-host-inspection.js"); + + expect(() => hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toThrow( + "missing browser host inspection facade", + ); +} diff --git a/src/plugin-sdk/browser-facades.test.ts b/src/plugin-sdk/browser-facades.test.ts index faa834420ad..6df2119da0f 100644 --- a/src/plugin-sdk/browser-facades.test.ts +++ b/src/plugin-sdk/browser-facades.test.ts @@ -1,4 +1,9 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + expectBrowserHostInspectionDelegation, + expectBrowserHostInspectionFacadeUnavailable, + mockBrowserHostInspectionFacade, +} from "./browser-facade-test-helpers.js"; const loadBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn()); @@ -105,36 +110,18 @@ describe("plugin-sdk browser facades", () => { kind: "chrome", path: "/usr/bin/google-chrome", }; - - const resolveGoogleChromeExecutableForPlatform = vi.fn().mockReturnValue(executable); - const readBrowserVersion = vi.fn().mockReturnValue("Google Chrome 144.0.7534.0"); - const parseBrowserMajorVersion = vi.fn().mockReturnValue(144); - loadBundledPluginPublicSurfaceModuleSync.mockReturnValue({ - resolveGoogleChromeExecutableForPlatform, - readBrowserVersion, - parseBrowserMajorVersion, - }); + mockBrowserHostInspectionFacade(loadBundledPluginPublicSurfaceModuleSync, executable); const hostInspection = await import("./browser-host-inspection.js"); - expect(hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toEqual(executable); - expect(hostInspection.readBrowserVersion(executable.path)).toBe("Google Chrome 144.0.7534.0"); - expect(hostInspection.parseBrowserMajorVersion("Google Chrome 144.0.7534.0")).toBe(144); - expect(loadBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledWith({ - dirName: "browser", - artifactBasename: "browser-host-inspection.js", + expectBrowserHostInspectionDelegation({ + executable, + hostInspection, + loadBundledPluginPublicSurfaceModuleSync, }); }); it("hard-fails when browser host inspection facade is unavailable", async () => { - loadBundledPluginPublicSurfaceModuleSync.mockImplementation(() => { - throw new Error("missing browser host inspection facade"); - }); - - const hostInspection = await import("./browser-host-inspection.js"); - - expect(() => hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toThrow( - "missing browser host inspection facade", - ); + await expectBrowserHostInspectionFacadeUnavailable(loadBundledPluginPublicSurfaceModuleSync); }); }); diff --git a/src/plugin-sdk/browser-host-inspection.test.ts b/src/plugin-sdk/browser-host-inspection.test.ts index 114829c93e8..2cd4490af36 100644 --- a/src/plugin-sdk/browser-host-inspection.test.ts +++ b/src/plugin-sdk/browser-host-inspection.test.ts @@ -1,4 +1,9 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeEach, describe, it, vi } from "vitest"; +import { + expectBrowserHostInspectionDelegation, + expectBrowserHostInspectionFacadeUnavailable, + mockBrowserHostInspectionFacade, +} from "./browser-facade-test-helpers.js"; const loadBundledPluginPublicSurfaceModuleSync = vi.hoisted(() => vi.fn()); @@ -14,45 +19,22 @@ describe("browser host inspection", () => { }); it("delegates browser host inspection helpers through the browser facade", async () => { - const resolveGoogleChromeExecutableForPlatform = vi.fn().mockReturnValue({ + const executable: import("./browser-host-inspection.js").BrowserExecutable = { kind: "canary", path: "/usr/bin/google-chrome-beta", - }); - const readBrowserVersion = vi.fn().mockReturnValue("Google Chrome 144.0.7534.0"); - const parseBrowserMajorVersion = vi.fn().mockReturnValue(144); - - loadBundledPluginPublicSurfaceModuleSync.mockReturnValue({ - resolveGoogleChromeExecutableForPlatform, - readBrowserVersion, - parseBrowserMajorVersion, - }); + }; + mockBrowserHostInspectionFacade(loadBundledPluginPublicSurfaceModuleSync, executable); const hostInspection = await import("./browser-host-inspection.js"); - expect(hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toEqual({ - kind: "canary", - path: "/usr/bin/google-chrome-beta", - }); - expect(hostInspection.readBrowserVersion("/usr/bin/google-chrome-beta")).toBe( - "Google Chrome 144.0.7534.0", - ); - expect(hostInspection.parseBrowserMajorVersion("Google Chrome 144.0.7534.0")).toBe(144); - - expect(loadBundledPluginPublicSurfaceModuleSync).toHaveBeenCalledWith({ - dirName: "browser", - artifactBasename: "browser-host-inspection.js", + expectBrowserHostInspectionDelegation({ + executable, + hostInspection, + loadBundledPluginPublicSurfaceModuleSync, }); }); it("hard-fails when browser host inspection facade is unavailable", async () => { - loadBundledPluginPublicSurfaceModuleSync.mockImplementation(() => { - throw new Error("missing browser host inspection facade"); - }); - - const hostInspection = await import("./browser-host-inspection.js"); - - expect(() => hostInspection.resolveGoogleChromeExecutableForPlatform("linux")).toThrow( - "missing browser host inspection facade", - ); + await expectBrowserHostInspectionFacadeUnavailable(loadBundledPluginPublicSurfaceModuleSync); }); });