perf(test): cut setup/import overhead in hot suites

This commit is contained in:
Peter Steinberger
2026-02-13 21:23:44 +00:00
parent 93dd51bce0
commit caebe70e9a
16 changed files with 428 additions and 426 deletions

View File

@@ -28,10 +28,7 @@ const sessionMocks = vi.hoisted(() => ({
}));
vi.mock("./pw-session.js", () => sessionMocks);
async function importModule() {
return await import("./pw-tools-core.js");
}
const mod = await import("./pw-tools-core.js");
describe("pw-tools-core", () => {
beforeEach(() => {
@@ -53,7 +50,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
const mod = await importModule();
await mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -70,7 +66,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
const mod = await importModule();
await expect(
mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -86,7 +81,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
const mod = await importModule();
await expect(
mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -102,7 +96,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { click };
currentPage = {};
const mod = await importModule();
await expect(
mod.clickViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -118,7 +111,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { click };
currentPage = {};
const mod = await importModule();
await expect(
mod.clickViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -136,7 +128,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { click };
currentPage = {};
const mod = await importModule();
await expect(
mod.clickViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",

View File

@@ -28,10 +28,7 @@ const sessionMocks = vi.hoisted(() => ({
}));
vi.mock("./pw-session.js", () => sessionMocks);
async function importModule() {
return await import("./pw-tools-core.js");
}
const mod = await import("./pw-tools-core.js");
describe("pw-tools-core", () => {
beforeEach(() => {
@@ -75,7 +72,6 @@ describe("pw-tools-core", () => {
keyboard: { press: vi.fn(async () => {}) },
};
const mod = await importModule();
await mod.armFileUploadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
paths: ["/tmp/1"],
@@ -101,7 +97,6 @@ describe("pw-tools-core", () => {
waitForEvent,
};
const mod = await importModule();
await mod.armDialogViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
accept: true,
@@ -145,7 +140,6 @@ describe("pw-tools-core", () => {
getByText: vi.fn(() => ({ first: () => ({ waitFor: vi.fn() }) })),
};
const mod = await importModule();
await mod.waitForViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
selector: "#main",

View File

@@ -28,10 +28,7 @@ const sessionMocks = vi.hoisted(() => ({
}));
vi.mock("./pw-session.js", () => sessionMocks);
async function importModule() {
return await import("./pw-tools-core.js");
}
const mod = await import("./pw-tools-core.js");
describe("pw-tools-core", () => {
beforeEach(() => {
@@ -57,7 +54,6 @@ describe("pw-tools-core", () => {
screenshot: vi.fn(async () => Buffer.from("P")),
};
const mod = await importModule();
const res = await mod.takeScreenshotViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -78,7 +74,6 @@ describe("pw-tools-core", () => {
screenshot: vi.fn(async () => Buffer.from("P")),
};
const mod = await importModule();
const res = await mod.takeScreenshotViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -99,8 +94,6 @@ describe("pw-tools-core", () => {
screenshot: vi.fn(async () => Buffer.from("P")),
};
const mod = await importModule();
await expect(
mod.takeScreenshotViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -127,7 +120,6 @@ describe("pw-tools-core", () => {
keyboard: { press: vi.fn(async () => {}) },
};
const mod = await importModule();
await mod.armFileUploadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -151,7 +143,6 @@ describe("pw-tools-core", () => {
keyboard: { press },
};
const mod = await importModule();
await mod.armFileUploadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
paths: [],

View File

@@ -33,10 +33,7 @@ const tmpDirMocks = vi.hoisted(() => ({
resolvePreferredOpenClawTmpDir: vi.fn(() => "/tmp/openclaw"),
}));
vi.mock("../infra/tmp-openclaw-dir.js", () => tmpDirMocks);
async function importModule() {
return await import("./pw-tools-core.js");
}
const mod = await import("./pw-tools-core.js");
describe("pw-tools-core", () => {
beforeEach(() => {
@@ -75,7 +72,6 @@ describe("pw-tools-core", () => {
currentPage = { on, off };
const mod = await importModule();
const targetPath = path.resolve("/tmp/file.bin");
const p = mod.waitForDownloadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -113,7 +109,6 @@ describe("pw-tools-core", () => {
currentPage = { on, off };
const mod = await importModule();
const targetPath = path.resolve("/tmp/report.pdf");
const p = mod.downloadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
@@ -152,7 +147,6 @@ describe("pw-tools-core", () => {
tmpDirMocks.resolvePreferredOpenClawTmpDir.mockReturnValue("/tmp/openclaw-preferred");
currentPage = { on, off };
const mod = await importModule();
const p = mod.waitForDownloadViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -194,7 +188,6 @@ describe("pw-tools-core", () => {
text: async () => '{"ok":true,"value":123}',
};
const mod = await importModule();
const p = mod.responseBodyViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -218,7 +211,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { scrollIntoViewIfNeeded };
currentPage = {};
const mod = await importModule();
await mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",
targetId: "T1",
@@ -232,7 +224,6 @@ describe("pw-tools-core", () => {
currentRefLocator = { scrollIntoViewIfNeeded: vi.fn(async () => {}) };
currentPage = {};
const mod = await importModule();
await expect(
mod.scrollIntoViewViaPlaywright({
cdpUrl: "http://127.0.0.1:18792",

View File

@@ -154,6 +154,9 @@ vi.mock("./screenshot.js", () => ({
})),
}));
const { startBrowserControlServerFromConfig, stopBrowserControlServer } =
await import("./server.js");
async function getFreePort(): Promise<number> {
while (true) {
const port = await new Promise<number>((resolve, reject) => {
@@ -271,12 +274,10 @@ describe("browser control server", () => {
} else {
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
}
const { stopBrowserControlServer } = await import("./server.js");
await stopBrowserControlServer();
});
const startServerAndBase = async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
await realFetch(`${base}/start`, { method: "POST" }).then((r) => r.json());

View File

@@ -63,6 +63,9 @@ vi.mock("./server-context.js", async (importOriginal) => {
};
});
const { startBrowserControlServerFromConfig, stopBrowserControlServer } =
await import("./server.js");
async function getFreePort(): Promise<number> {
const probe = createServer();
await new Promise<void>((resolve, reject) => {
@@ -95,12 +98,10 @@ describe("browser control evaluate gating", () => {
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
}
const { stopBrowserControlServer } = await import("./server.js");
await stopBrowserControlServer();
});
it("blocks act:evaluate but still allows cookies/storage reads", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;

View File

@@ -153,6 +153,9 @@ vi.mock("./screenshot.js", () => ({
})),
}));
const { startBrowserControlServerFromConfig, stopBrowserControlServer } =
await import("./server.js");
async function getFreePort(): Promise<number> {
while (true) {
const port = await new Promise<number>((resolve, reject) => {
@@ -270,12 +273,10 @@ describe("browser control server", () => {
} else {
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
}
const { stopBrowserControlServer } = await import("./server.js");
await stopBrowserControlServer();
});
it("POST /tabs/open?profile=unknown returns 404", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -307,9 +308,6 @@ describe("profile CRUD endpoints", () => {
prevGatewayPort = process.env.OPENCLAW_GATEWAY_PORT;
process.env.OPENCLAW_GATEWAY_PORT = String(testPort - 2);
prevGatewayPort = process.env.OPENCLAW_GATEWAY_PORT;
process.env.OPENCLAW_GATEWAY_PORT = String(testPort - 2);
vi.stubGlobal(
"fetch",
vi.fn(async (url: string) => {
@@ -330,12 +328,10 @@ describe("profile CRUD endpoints", () => {
} else {
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
}
const { stopBrowserControlServer } = await import("./server.js");
await stopBrowserControlServer();
});
it("POST /profiles/create returns 400 for missing name", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -350,7 +346,6 @@ describe("profile CRUD endpoints", () => {
});
it("POST /profiles/create returns 400 for invalid name format", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -365,7 +360,6 @@ describe("profile CRUD endpoints", () => {
});
it("POST /profiles/create returns 409 for duplicate name", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -381,7 +375,6 @@ describe("profile CRUD endpoints", () => {
});
it("POST /profiles/create accepts cdpUrl for remote profiles", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -402,7 +395,6 @@ describe("profile CRUD endpoints", () => {
});
it("POST /profiles/create returns 400 for invalid cdpUrl", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -417,7 +409,6 @@ describe("profile CRUD endpoints", () => {
});
it("DELETE /profiles/:name returns 404 for non-existent profile", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -430,7 +421,6 @@ describe("profile CRUD endpoints", () => {
});
it("DELETE /profiles/:name returns 400 for default profile deletion", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;
@@ -444,7 +434,6 @@ describe("profile CRUD endpoints", () => {
});
it("DELETE /profiles/:name returns 400 for invalid name format", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js");
await startBrowserControlServerFromConfig();
const base = `http://127.0.0.1:${testPort}`;