perf(test): speed up browser test suites

This commit is contained in:
Peter Steinberger
2026-02-14 14:25:35 +00:00
parent 57f40a5da6
commit 493f6f458b
6 changed files with 242 additions and 248 deletions

View File

@@ -8,6 +8,12 @@ import {
} from "./control-service.js";
import { createBrowserRouteDispatcher } from "./routes/dispatcher.js";
type LoopbackBrowserAuthDeps = {
loadConfig: typeof loadConfig;
resolveBrowserControlAuth: typeof resolveBrowserControlAuth;
getBridgeAuthForPort: typeof getBridgeAuthForPort;
};
function isAbsoluteHttp(url: string): boolean {
return /^https?:\/\//i.test(url.trim());
}
@@ -21,9 +27,10 @@ function isLoopbackHttpUrl(url: string): boolean {
}
}
function withLoopbackBrowserAuth(
function withLoopbackBrowserAuthImpl(
url: string,
init: (RequestInit & { timeoutMs?: number }) | undefined,
deps: LoopbackBrowserAuthDeps,
): RequestInit & { timeoutMs?: number } {
const headers = new Headers(init?.headers ?? {});
if (headers.has("authorization") || headers.has("x-openclaw-password")) {
@@ -34,8 +41,8 @@ function withLoopbackBrowserAuth(
}
try {
const cfg = loadConfig();
const auth = resolveBrowserControlAuth(cfg);
const cfg = deps.loadConfig();
const auth = deps.resolveBrowserControlAuth(cfg);
if (auth.token) {
headers.set("Authorization", `Bearer ${auth.token}`);
return { ...init, headers };
@@ -58,7 +65,7 @@ function withLoopbackBrowserAuth(
: parsed.protocol === "https:"
? 443
: 80;
const bridgeAuth = getBridgeAuthForPort(port);
const bridgeAuth = deps.getBridgeAuthForPort(port);
if (bridgeAuth?.token) {
headers.set("Authorization", `Bearer ${bridgeAuth.token}`);
} else if (bridgeAuth?.password) {
@@ -71,6 +78,17 @@ function withLoopbackBrowserAuth(
return { ...init, headers };
}
function withLoopbackBrowserAuth(
url: string,
init: (RequestInit & { timeoutMs?: number }) | undefined,
): RequestInit & { timeoutMs?: number } {
return withLoopbackBrowserAuthImpl(url, init, {
loadConfig,
resolveBrowserControlAuth,
getBridgeAuthForPort,
});
}
function enhanceBrowserFetchError(url: string, err: unknown, timeoutMs: number): Error {
const hint = isAbsoluteHttp(url)
? "If this is a sandboxed session, ensure the sandbox browser is running and try again."
@@ -215,3 +233,7 @@ export async function fetchBrowserJson<T>(
throw enhanceBrowserFetchError(url, err, timeoutMs);
}
}
export const __test = {
withLoopbackBrowserAuth: withLoopbackBrowserAuthImpl,
};