mirror of https://github.com/openclaw/openclaw.git
103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { createBrowserRouteContext } from "./server-context.js";
|
|
import type { BrowserServerState } from "./server-context.js";
|
|
|
|
vi.mock("./chrome-mcp.js", () => ({
|
|
closeChromeMcpSession: vi.fn(async () => true),
|
|
ensureChromeMcpAvailable: vi.fn(async () => {}),
|
|
focusChromeMcpTab: vi.fn(async () => {}),
|
|
listChromeMcpTabs: vi.fn(async () => [
|
|
{ targetId: "7", title: "", url: "https://example.com", type: "page" },
|
|
]),
|
|
openChromeMcpTab: vi.fn(async () => ({
|
|
targetId: "8",
|
|
title: "",
|
|
url: "https://openclaw.ai",
|
|
type: "page",
|
|
})),
|
|
closeChromeMcpTab: vi.fn(async () => {}),
|
|
getChromeMcpPid: vi.fn(() => 4321),
|
|
}));
|
|
|
|
import * as chromeMcp from "./chrome-mcp.js";
|
|
|
|
function makeState(): BrowserServerState {
|
|
return {
|
|
server: null,
|
|
port: 0,
|
|
resolved: {
|
|
enabled: true,
|
|
evaluateEnabled: true,
|
|
controlPort: 18791,
|
|
cdpPortRangeStart: 18800,
|
|
cdpPortRangeEnd: 18899,
|
|
cdpProtocol: "http",
|
|
cdpHost: "127.0.0.1",
|
|
cdpIsLoopback: true,
|
|
remoteCdpTimeoutMs: 1500,
|
|
remoteCdpHandshakeTimeoutMs: 3000,
|
|
color: "#FF4500",
|
|
headless: false,
|
|
noSandbox: false,
|
|
attachOnly: false,
|
|
defaultProfile: "chrome-live",
|
|
profiles: {
|
|
"chrome-live": {
|
|
cdpPort: 18801,
|
|
color: "#0066CC",
|
|
driver: "existing-session",
|
|
attachOnly: true,
|
|
},
|
|
},
|
|
extraArgs: [],
|
|
ssrfPolicy: { dangerouslyAllowPrivateNetwork: true },
|
|
},
|
|
profiles: new Map(),
|
|
};
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("browser server-context existing-session profile", () => {
|
|
it("routes tab operations through the Chrome MCP backend", async () => {
|
|
const state = makeState();
|
|
const ctx = createBrowserRouteContext({ getState: () => state });
|
|
const live = ctx.forProfile("chrome-live");
|
|
|
|
vi.mocked(chromeMcp.listChromeMcpTabs)
|
|
.mockResolvedValueOnce([
|
|
{ targetId: "7", title: "", url: "https://example.com", type: "page" },
|
|
])
|
|
.mockResolvedValueOnce([
|
|
{ targetId: "8", title: "", url: "https://openclaw.ai", type: "page" },
|
|
])
|
|
.mockResolvedValueOnce([
|
|
{ targetId: "8", title: "", url: "https://openclaw.ai", type: "page" },
|
|
])
|
|
.mockResolvedValueOnce([
|
|
{ targetId: "7", title: "", url: "https://example.com", type: "page" },
|
|
]);
|
|
|
|
await live.ensureBrowserAvailable();
|
|
const tabs = await live.listTabs();
|
|
expect(tabs.map((tab) => tab.targetId)).toEqual(["7"]);
|
|
|
|
const opened = await live.openTab("https://openclaw.ai");
|
|
expect(opened.targetId).toBe("8");
|
|
|
|
const selected = await live.ensureTabAvailable();
|
|
expect(selected.targetId).toBe("8");
|
|
|
|
await live.focusTab("7");
|
|
await live.stopRunningBrowser();
|
|
|
|
expect(chromeMcp.ensureChromeMcpAvailable).toHaveBeenCalledWith("chrome-live");
|
|
expect(chromeMcp.listChromeMcpTabs).toHaveBeenCalledWith("chrome-live");
|
|
expect(chromeMcp.openChromeMcpTab).toHaveBeenCalledWith("chrome-live", "https://openclaw.ai");
|
|
expect(chromeMcp.focusChromeMcpTab).toHaveBeenCalledWith("chrome-live", "7");
|
|
expect(chromeMcp.closeChromeMcpSession).toHaveBeenCalledWith("chrome-live");
|
|
});
|
|
});
|