|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createTestPluginApi } from "../../test/helpers/extensions/plugin-api.js"; |
| 3 | +import type { OpenClawPluginApi } from "./runtime-api.js"; |
| 4 | + |
| 5 | +const runtimeApiMocks = vi.hoisted(() => ({ |
| 6 | + createBrowserPluginService: vi.fn(() => ({ id: "browser-control", start: vi.fn() })), |
| 7 | + createBrowserTool: vi.fn(() => ({ |
| 8 | + name: "browser", |
| 9 | + description: "browser", |
| 10 | + parameters: { type: "object", properties: {} }, |
| 11 | + execute: vi.fn(), |
| 12 | + })), |
| 13 | + handleBrowserGatewayRequest: vi.fn(), |
| 14 | + registerBrowserCli: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("./runtime-api.js", async (importOriginal) => { |
| 18 | + const actual = await importOriginal<typeof import("./runtime-api.js")>(); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + createBrowserPluginService: runtimeApiMocks.createBrowserPluginService, |
| 22 | + createBrowserTool: runtimeApiMocks.createBrowserTool, |
| 23 | + handleBrowserGatewayRequest: runtimeApiMocks.handleBrowserGatewayRequest, |
| 24 | + registerBrowserCli: runtimeApiMocks.registerBrowserCli, |
| 25 | + }; |
| 26 | +}); |
| 27 | + |
| 28 | +import browserPlugin from "./index.js"; |
| 29 | + |
| 30 | +function createApi() { |
| 31 | + const registerCli = vi.fn(); |
| 32 | + const registerGatewayMethod = vi.fn(); |
| 33 | + const registerService = vi.fn(); |
| 34 | + const registerTool = vi.fn(); |
| 35 | + const api = createTestPluginApi({ |
| 36 | + id: "browser", |
| 37 | + name: "Browser", |
| 38 | + source: "test", |
| 39 | + config: {}, |
| 40 | + runtime: {} as OpenClawPluginApi["runtime"], |
| 41 | + registerCli, |
| 42 | + registerGatewayMethod, |
| 43 | + registerService, |
| 44 | + registerTool, |
| 45 | + }) as OpenClawPluginApi; |
| 46 | + return { api, registerCli, registerGatewayMethod, registerService, registerTool }; |
| 47 | +} |
| 48 | + |
| 49 | +describe("browser plugin", () => { |
| 50 | + it("registers browser tool, cli, gateway method, and service ownership", () => { |
| 51 | + const { api, registerCli, registerGatewayMethod, registerService, registerTool } = createApi(); |
| 52 | + browserPlugin.register(api); |
| 53 | + |
| 54 | + expect(registerTool).toHaveBeenCalledTimes(1); |
| 55 | + expect(registerCli).toHaveBeenCalledWith(expect.any(Function), { commands: ["browser"] }); |
| 56 | + expect(registerGatewayMethod).toHaveBeenCalledWith( |
| 57 | + "browser.request", |
| 58 | + runtimeApiMocks.handleBrowserGatewayRequest, |
| 59 | + { scope: "operator.write" }, |
| 60 | + ); |
| 61 | + expect(runtimeApiMocks.createBrowserPluginService).toHaveBeenCalledTimes(1); |
| 62 | + expect(registerService).toHaveBeenCalledWith( |
| 63 | + runtimeApiMocks.createBrowserPluginService.mock.results[0]?.value, |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("forwards per-session browser options into the tool factory", () => { |
| 68 | + const { api, registerTool } = createApi(); |
| 69 | + browserPlugin.register(api); |
| 70 | + |
| 71 | + const tool = registerTool.mock.calls[0]?.[0]; |
| 72 | + if (typeof tool !== "function") { |
| 73 | + throw new Error("expected browser plugin to register a tool factory"); |
| 74 | + } |
| 75 | + |
| 76 | + tool({ |
| 77 | + sessionKey: "agent:main:webchat:direct:123", |
| 78 | + browser: { |
| 79 | + sandboxBridgeUrl: "http://127.0.0.1:9999", |
| 80 | + allowHostControl: true, |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + expect(runtimeApiMocks.createBrowserTool).toHaveBeenCalledWith({ |
| 85 | + sandboxBridgeUrl: "http://127.0.0.1:9999", |
| 86 | + allowHostControl: true, |
| 87 | + agentSessionKey: "agent:main:webchat:direct:123", |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments