mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:08:25 +00:00
perf(test): speed up browser test suites
This commit is contained in:
@@ -1,91 +1,46 @@
|
||||
import { createServer, type AddressInfo } from "node:net";
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import { fetch as realFetch } from "undici";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { isAuthorizedBrowserRequest } from "./http-auth.js";
|
||||
|
||||
let testPort = 0;
|
||||
let prevGatewayPort: string | undefined;
|
||||
|
||||
vi.mock("../config/config.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||
return {
|
||||
...actual,
|
||||
loadConfig: () => ({
|
||||
gateway: {
|
||||
auth: {
|
||||
token: "browser-control-secret",
|
||||
},
|
||||
},
|
||||
browser: {
|
||||
enabled: true,
|
||||
defaultProfile: "openclaw",
|
||||
profiles: {
|
||||
openclaw: { cdpPort: testPort + 1, color: "#FF4500" },
|
||||
},
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("./routes/index.js", () => ({
|
||||
registerBrowserRoutes(app: {
|
||||
get: (
|
||||
path: string,
|
||||
handler: (req: unknown, res: { json: (body: unknown) => void }) => void,
|
||||
) => void;
|
||||
}) {
|
||||
app.get("/", (_req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("./server-context.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("./server-context.js")>();
|
||||
return {
|
||||
...actual,
|
||||
createBrowserRouteContext: vi.fn(() => ({
|
||||
forProfile: vi.fn(() => ({
|
||||
stopRunningBrowser: vi.fn(async () => {}),
|
||||
})),
|
||||
})),
|
||||
};
|
||||
});
|
||||
let server: ReturnType<typeof createServer> | null = null;
|
||||
let port = 0;
|
||||
|
||||
describe("browser control HTTP auth", () => {
|
||||
beforeEach(async () => {
|
||||
prevGatewayPort = process.env.OPENCLAW_GATEWAY_PORT;
|
||||
|
||||
const probe = createServer();
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
probe.once("error", reject);
|
||||
probe.listen(0, "127.0.0.1", () => resolve());
|
||||
server = createServer((req: IncomingMessage, res: ServerResponse) => {
|
||||
if (!isAuthorizedBrowserRequest(req, { token: "browser-control-secret" })) {
|
||||
res.statusCode = 401;
|
||||
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
||||
res.end("Unauthorized");
|
||||
return;
|
||||
}
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Content-Type", "application/json; charset=utf-8");
|
||||
res.end(JSON.stringify({ ok: true }));
|
||||
});
|
||||
const addr = probe.address() as AddressInfo;
|
||||
testPort = addr.port;
|
||||
await new Promise<void>((resolve) => probe.close(() => resolve()));
|
||||
|
||||
process.env.OPENCLAW_GATEWAY_PORT = String(testPort - 2);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server?.once("error", reject);
|
||||
server?.listen(0, "127.0.0.1", () => resolve());
|
||||
});
|
||||
const addr = server.address();
|
||||
if (!addr || typeof addr === "string") {
|
||||
throw new Error("server address missing");
|
||||
}
|
||||
port = addr.port;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
if (prevGatewayPort === undefined) {
|
||||
delete process.env.OPENCLAW_GATEWAY_PORT;
|
||||
} else {
|
||||
process.env.OPENCLAW_GATEWAY_PORT = prevGatewayPort;
|
||||
const current = server;
|
||||
server = null;
|
||||
if (!current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { stopBrowserControlServer } = await import("./server.js");
|
||||
await stopBrowserControlServer();
|
||||
await new Promise<void>((resolve) => current.close(() => resolve()));
|
||||
});
|
||||
|
||||
it("requires bearer auth for standalone browser HTTP routes", async () => {
|
||||
const { startBrowserControlServerFromConfig } = await import("./server.js");
|
||||
const started = await startBrowserControlServerFromConfig();
|
||||
expect(started?.port).toBe(testPort);
|
||||
|
||||
const base = `http://127.0.0.1:${testPort}`;
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
|
||||
const missingAuth = await realFetch(`${base}/`);
|
||||
expect(missingAuth.status).toBe(401);
|
||||
|
||||
Reference in New Issue
Block a user