test: share browser facade fixtures

This commit is contained in:
Peter Steinberger
2026-04-20 21:42:48 +01:00
parent ba331014be
commit d8745d928d
3 changed files with 84 additions and 56 deletions

View File

@@ -0,0 +1,59 @@
import { expect, vi } from "vitest";
type FacadeLoaderMock = ReturnType<typeof vi.fn>;
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",
);
}

View File

@@ -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);
});
});

View File

@@ -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);
});
});