test: speed up browser control suites

This commit is contained in:
Peter Steinberger
2026-03-24 23:43:07 +00:00
parent 63b0036248
commit c9f4dd3c1b
2 changed files with 13 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("node:child_process", () => ({
execFileSync: vi.fn(),
@@ -31,6 +31,9 @@ async function loadResolveBrowserExecutableForPlatform() {
describe("browser default executable detection", () => {
const launchServicesPlist = "com.apple.launchservices.secure.plist";
const chromeExecutablePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
let resolveBrowserExecutableForPlatform: Awaited<
ReturnType<typeof loadResolveBrowserExecutableForPlatform>
>;
function mockMacDefaultBrowser(bundleId: string, appPath = ""): void {
vi.mocked(execFileSync).mockImplementation((cmd, args) => {
@@ -58,8 +61,11 @@ describe("browser default executable detection", () => {
});
}
beforeAll(async () => {
resolveBrowserExecutableForPlatform = await loadResolveBrowserExecutableForPlatform();
});
beforeEach(() => {
vi.resetModules();
vi.clearAllMocks();
vi.mocked(os.homedir).mockReturnValue("/Users/test");
});
@@ -67,7 +73,6 @@ describe("browser default executable detection", () => {
it("prefers default Chromium browser on macOS", async () => {
mockMacDefaultBrowser("com.google.Chrome", "/Applications/Google Chrome.app");
mockChromeExecutableExists();
const resolveBrowserExecutableForPlatform = await loadResolveBrowserExecutableForPlatform();
const exe = resolveBrowserExecutableForPlatform(
{} as Parameters<typeof resolveBrowserExecutableForPlatform>[0],
@@ -81,7 +86,6 @@ describe("browser default executable detection", () => {
it("falls back when default browser is non-Chromium on macOS", async () => {
mockMacDefaultBrowser("com.apple.Safari");
mockChromeExecutableExists();
const resolveBrowserExecutableForPlatform = await loadResolveBrowserExecutableForPlatform();
const exe = resolveBrowserExecutableForPlatform(
{} as Parameters<typeof resolveBrowserExecutableForPlatform>[0],

View File

@@ -1,5 +1,5 @@
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { isAuthorizedBrowserRequest } from "./http-auth.js";
import { getBrowserTestFetch, type BrowserTestFetch } from "./test-fetch.js";
@@ -8,9 +8,11 @@ let port = 0;
let realFetch: BrowserTestFetch;
describe("browser control HTTP auth", () => {
beforeEach(async () => {
vi.resetModules();
beforeAll(() => {
realFetch = getBrowserTestFetch();
});
beforeEach(async () => {
server = createServer((req: IncomingMessage, res: ServerResponse) => {
if (!isAuthorizedBrowserRequest(req, { token: "browser-control-secret" })) {
res.statusCode = 401;