mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 04:00:21 +00:00
perf(test): remove module reloads in browser and embedding suites
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { BrowserServerState } from "./server-context.js";
|
||||
import * as cdpModule from "./cdp.js";
|
||||
import * as pwAiModule from "./pw-ai-module.js";
|
||||
import { createBrowserRouteContext } from "./server-context.js";
|
||||
|
||||
vi.mock("./chrome.js", () => ({
|
||||
isChromeCdpReady: vi.fn(async () => true),
|
||||
@@ -11,6 +14,13 @@ vi.mock("./chrome.js", () => ({
|
||||
stopOpenClawChrome: vi.fn(async () => {}),
|
||||
}));
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
function makeState(
|
||||
profile: "remote" | "openclaw",
|
||||
): BrowserServerState & { profiles: Map<string, { lastTargetId?: string | null }> } {
|
||||
@@ -46,7 +56,6 @@ function makeState(
|
||||
|
||||
describe("browser server-context remote profile tab operations", () => {
|
||||
it("uses Playwright tab operations when available", async () => {
|
||||
vi.resetModules();
|
||||
const listPagesViaPlaywright = vi.fn(async () => [
|
||||
{ targetId: "T1", title: "Tab 1", url: "https://a.example", type: "page" },
|
||||
]);
|
||||
@@ -58,11 +67,11 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
}));
|
||||
const closePageByTargetIdViaPlaywright = vi.fn(async () => {});
|
||||
|
||||
vi.doMock("./pw-ai.js", () => ({
|
||||
vi.spyOn(pwAiModule, "getPwAiModule").mockResolvedValue({
|
||||
listPagesViaPlaywright,
|
||||
createPageViaPlaywright,
|
||||
closePageByTargetIdViaPlaywright,
|
||||
}));
|
||||
} as Awaited<ReturnType<typeof pwAiModule.getPwAiModule>>);
|
||||
|
||||
const fetchMock = vi.fn(async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
@@ -70,7 +79,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("remote");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const remote = ctx.forProfile("remote");
|
||||
@@ -91,7 +99,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
});
|
||||
|
||||
it("prefers lastTargetId for remote profiles when targetId is omitted", async () => {
|
||||
vi.resetModules();
|
||||
const responses = [
|
||||
// ensureTabAvailable() calls listTabs twice
|
||||
[
|
||||
@@ -121,7 +128,7 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
return next;
|
||||
});
|
||||
|
||||
vi.doMock("./pw-ai.js", () => ({
|
||||
vi.spyOn(pwAiModule, "getPwAiModule").mockResolvedValue({
|
||||
listPagesViaPlaywright,
|
||||
createPageViaPlaywright: vi.fn(async () => {
|
||||
throw new Error("unexpected create");
|
||||
@@ -129,7 +136,7 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
closePageByTargetIdViaPlaywright: vi.fn(async () => {
|
||||
throw new Error("unexpected close");
|
||||
}),
|
||||
}));
|
||||
} as Awaited<ReturnType<typeof pwAiModule.getPwAiModule>>);
|
||||
|
||||
const fetchMock = vi.fn(async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
@@ -137,7 +144,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("remote");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const remote = ctx.forProfile("remote");
|
||||
@@ -149,16 +155,15 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
});
|
||||
|
||||
it("uses Playwright focus for remote profiles when available", async () => {
|
||||
vi.resetModules();
|
||||
const listPagesViaPlaywright = vi.fn(async () => [
|
||||
{ targetId: "T1", title: "Tab 1", url: "https://a.example", type: "page" },
|
||||
]);
|
||||
const focusPageByTargetIdViaPlaywright = vi.fn(async () => {});
|
||||
|
||||
vi.doMock("./pw-ai.js", () => ({
|
||||
vi.spyOn(pwAiModule, "getPwAiModule").mockResolvedValue({
|
||||
listPagesViaPlaywright,
|
||||
focusPageByTargetIdViaPlaywright,
|
||||
}));
|
||||
} as Awaited<ReturnType<typeof pwAiModule.getPwAiModule>>);
|
||||
|
||||
const fetchMock = vi.fn(async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
@@ -166,7 +171,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("remote");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const remote = ctx.forProfile("remote");
|
||||
@@ -181,12 +185,11 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
});
|
||||
|
||||
it("does not swallow Playwright runtime errors for remote profiles", async () => {
|
||||
vi.resetModules();
|
||||
vi.doMock("./pw-ai.js", () => ({
|
||||
vi.spyOn(pwAiModule, "getPwAiModule").mockResolvedValue({
|
||||
listPagesViaPlaywright: vi.fn(async () => {
|
||||
throw new Error("boom");
|
||||
}),
|
||||
}));
|
||||
} as Awaited<ReturnType<typeof pwAiModule.getPwAiModule>>);
|
||||
|
||||
const fetchMock = vi.fn(async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
@@ -194,7 +197,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("remote");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const remote = ctx.forProfile("remote");
|
||||
@@ -204,12 +206,7 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
});
|
||||
|
||||
it("falls back to /json/list when Playwright is not available", async () => {
|
||||
vi.resetModules();
|
||||
vi.doMock("./pw-ai.js", () => ({
|
||||
listPagesViaPlaywright: undefined,
|
||||
createPageViaPlaywright: undefined,
|
||||
closePageByTargetIdViaPlaywright: undefined,
|
||||
}));
|
||||
vi.spyOn(pwAiModule, "getPwAiModule").mockResolvedValue(null);
|
||||
|
||||
const fetchMock = vi.fn(async (url: unknown) => {
|
||||
const u = String(url);
|
||||
@@ -232,7 +229,6 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("remote");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const remote = ctx.forProfile("remote");
|
||||
@@ -245,15 +241,7 @@ describe("browser server-context remote profile tab operations", () => {
|
||||
|
||||
describe("browser server-context tab selection state", () => {
|
||||
it("updates lastTargetId when openTab is created via CDP", async () => {
|
||||
vi.resetModules();
|
||||
vi.doUnmock("./pw-ai.js");
|
||||
vi.doMock("./cdp.js", async () => {
|
||||
const actual = await vi.importActual<typeof import("./cdp.js")>("./cdp.js");
|
||||
return {
|
||||
...actual,
|
||||
createTargetViaCdp: vi.fn(async () => ({ targetId: "CREATED" })),
|
||||
};
|
||||
});
|
||||
vi.spyOn(cdpModule, "createTargetViaCdp").mockResolvedValue({ targetId: "CREATED" });
|
||||
|
||||
const fetchMock = vi.fn(async (url: unknown) => {
|
||||
const u = String(url);
|
||||
@@ -276,7 +264,6 @@ describe("browser server-context tab selection state", () => {
|
||||
|
||||
global.fetch = fetchMock;
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const state = makeState("openclaw");
|
||||
const ctx = createBrowserRouteContext({ getState: () => state });
|
||||
const openclaw = ctx.forProfile("openclaw");
|
||||
|
||||
Reference in New Issue
Block a user