test: share browser security mock

This commit is contained in:
Peter Steinberger
2026-04-20 20:36:09 +01:00
parent 44082acef5
commit 84d8cb0826
6 changed files with 21 additions and 75 deletions

View File

@@ -0,0 +1,16 @@
import { vi } from "vitest";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});

View File

@@ -3,27 +3,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { type WebSocket, WebSocketServer } from "ws";
import { SsrFBlockedError } from "../infra/net/ssrf.js";
import { rawDataToString } from "../infra/ws.js";
import "./browser-security-runtime.test-helper.js";
import { isDirectCdpWebSocketEndpoint, isWebSocketUrl } from "./cdp.helpers.js";
import { createTargetViaCdp, evaluateJavaScript, normalizeCdpWsUrl, snapshotAria } from "./cdp.js";
import { parseHttpUrl } from "./config.js";
import { BrowserCdpEndpointBlockedError } from "./errors.js";
import { InvalidBrowserNavigationUrlError } from "./navigation-guard.js";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});
describe("cdp", () => {
let httpServer: ReturnType<typeof createServer> | null = null;
let wsServer: WebSocketServer | null = null;

View File

@@ -1,21 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import "./browser-security-runtime.test-helper.js";
import type { BrowserDispatchResponse } from "./routes/dispatcher.js";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});
vi.mock("openclaw/plugin-sdk/ssrf-runtime", async () => {
const actual = await vi.importActual<typeof import("openclaw/plugin-sdk/ssrf-runtime")>(
"openclaw/plugin-sdk/ssrf-runtime",

View File

@@ -1,6 +1,7 @@
import { chromium } from "playwright-core";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SsrFBlockedError } from "../infra/net/ssrf.js";
import "./browser-security-runtime.test-helper.js";
import * as chromeModule from "./chrome.js";
import { BrowserTabNotFoundError } from "./errors.js";
import { InvalidBrowserNavigationUrlError } from "./navigation-guard.js";
@@ -15,21 +16,6 @@ import {
listPagesViaPlaywright,
} from "./pw-session.js";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});
const connectOverCdpSpy = vi.spyOn(chromium, "connectOverCDP");
const getChromeWebSocketUrlSpy = vi.spyOn(chromeModule, "getChromeWebSocketUrl");

View File

@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SsrFBlockedError } from "../infra/net/ssrf.js";
import "./browser-security-runtime.test-helper.js";
import { InvalidBrowserNavigationUrlError } from "./navigation-guard.js";
import {
getPwToolsCoreSessionMocks,
@@ -7,21 +8,6 @@ import {
setPwToolsCoreCurrentPage,
} from "./pw-tools-core.test-harness.js";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});
installPwToolsCoreTestHooks();
const mod = await import("./pw-tools-core.snapshot.js");

View File

@@ -1,22 +1,8 @@
import fs from "node:fs";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import "./browser-security-runtime.test-helper.js";
import type { BrowserServerState } from "./server-context.js";
vi.mock("openclaw/plugin-sdk/browser-security-runtime", async () => {
const actual = await vi.importActual<
typeof import("openclaw/plugin-sdk/browser-security-runtime")
>("openclaw/plugin-sdk/browser-security-runtime");
const lookupFn = async (_hostname: string, options?: { all?: boolean }) => {
const result = { address: "93.184.216.34", family: 4 };
return options?.all === true ? [result] : result;
};
return {
...actual,
resolvePinnedHostnameWithPolicy: (hostname: string, params: object = {}) =>
actual.resolvePinnedHostnameWithPolicy(hostname, { ...params, lookupFn: lookupFn as never }),
};
});
vi.mock("./chrome-mcp.js", () => ({
closeChromeMcpSession: vi.fn(async () => true),
ensureChromeMcpAvailable: vi.fn(async () => {}),