perf(browser): trim invoke-browser test imports

This commit is contained in:
Vincent Koc
2026-04-03 20:12:40 +09:00
parent 045d590542
commit 5021b12ac1

View File

@@ -26,14 +26,88 @@ const browserConfigMocks = vi.hoisted(() => ({
})),
}));
vi.mock("../core-api.js", async () => ({
...(await vi.importActual<object>("../core-api.js")),
vi.mock("../core-api.js", () => ({
createBrowserControlContext: controlServiceMocks.createBrowserControlContext,
createBrowserRouteDispatcher: dispatcherMocks.createBrowserRouteDispatcher,
detectMime: vi.fn(async () => "image/png"),
isPersistentBrowserProfileMutation: vi.fn((method: string, path: string) => {
if (method === "POST" && (path === "/profiles/create" || path === "/reset-profile")) {
return true;
}
return method === "DELETE" && /^\/profiles\/[^/]+$/.test(path);
}),
loadConfig: configMocks.loadConfig,
normalizeBrowserRequestPath: vi.fn((path: string) => path),
redactCdpUrl: vi.fn((url: string) => {
try {
const parsed = new URL(url);
parsed.username = "";
parsed.password = "";
const normalized = parsed.toString().replace(/\/$/, "");
const token = parsed.searchParams.get("token");
if (!token || token.length <= 8) {
return normalized;
}
return normalized.replace(token, `${token.slice(0, 6)}${token.slice(-4)}`);
} catch {
return url;
}
}),
resolveBrowserConfig: browserConfigMocks.resolveBrowserConfig,
resolveRequestedBrowserProfile: vi.fn(
({
query,
body,
profile,
}: {
query?: Record<string, unknown>;
body?: unknown;
profile?: string;
}) => {
if (query && typeof query.profile === "string" && query.profile.trim()) {
return query.profile.trim();
}
const bodyProfile =
body && typeof body === "object" ? (body as { profile?: unknown }).profile : undefined;
if (typeof bodyProfile === "string" && bodyProfile.trim()) {
return bodyProfile.trim();
}
return typeof profile === "string" && profile.trim() ? profile.trim() : undefined;
},
),
startBrowserControlServiceFromConfig: controlServiceMocks.startBrowserControlServiceFromConfig,
withTimeout: vi.fn(
async (
run: (signal: AbortSignal | undefined) => Promise<unknown>,
timeoutMs?: number,
label?: string,
) => {
const resolved =
typeof timeoutMs === "number" && Number.isFinite(timeoutMs)
? Math.max(1, Math.floor(timeoutMs))
: undefined;
if (!resolved) {
return await run(undefined);
}
const abortCtrl = new AbortController();
const timeoutError = new Error(`${label ?? "request"} timed out`);
const timer = setTimeout(() => abortCtrl.abort(timeoutError), resolved);
try {
return await Promise.race([
run(abortCtrl.signal),
new Promise<never>((_, reject) => {
abortCtrl.signal.addEventListener(
"abort",
() => reject(abortCtrl.signal.reason ?? timeoutError),
{ once: true },
);
}),
]);
} finally {
clearTimeout(timer);
}
},
),
}));
let resetBrowserProxyCommandStateForTests: typeof import("./invoke-browser.js").resetBrowserProxyCommandStateForTests;