|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { withFetchPreconnect } from "../test-utils/fetch-mock.js"; |
| 3 | +import * as cdpModule from "./cdp.js"; |
| 4 | +import { createBrowserRouteContext } from "./server-context.js"; |
| 5 | +import { makeState, originalFetch } from "./server-context.remote-tab-ops.harness.js"; |
| 6 | + |
| 7 | +afterEach(() => { |
| 8 | + globalThis.fetch = originalFetch; |
| 9 | + vi.restoreAllMocks(); |
| 10 | +}); |
| 11 | + |
| 12 | +describe("browser server-context loopback direct WebSocket profiles", () => { |
| 13 | + it("uses an HTTP /json/list base when opening tabs", async () => { |
| 14 | + const createTargetViaCdp = vi |
| 15 | + .spyOn(cdpModule, "createTargetViaCdp") |
| 16 | + .mockResolvedValue({ targetId: "CREATED" }); |
| 17 | + |
| 18 | + const fetchMock = vi.fn(async (url: unknown) => { |
| 19 | + const u = String(url); |
| 20 | + expect(u).toBe("http://127.0.0.1:18800/json/list?token=abc"); |
| 21 | + return { |
| 22 | + ok: true, |
| 23 | + json: async () => [ |
| 24 | + { |
| 25 | + id: "CREATED", |
| 26 | + title: "New Tab", |
| 27 | + url: "http://127.0.0.1:8080", |
| 28 | + webSocketDebuggerUrl: "ws://127.0.0.1/devtools/page/CREATED", |
| 29 | + type: "page", |
| 30 | + }, |
| 31 | + ], |
| 32 | + } as unknown as Response; |
| 33 | + }); |
| 34 | + |
| 35 | + global.fetch = withFetchPreconnect(fetchMock); |
| 36 | + const state = makeState("openclaw"); |
| 37 | + state.resolved.profiles.openclaw = { |
| 38 | + cdpUrl: "ws://127.0.0.1:18800/devtools/browser/SESSION?token=abc", |
| 39 | + color: "#FF4500", |
| 40 | + }; |
| 41 | + const ctx = createBrowserRouteContext({ getState: () => state }); |
| 42 | + const openclaw = ctx.forProfile("openclaw"); |
| 43 | + |
| 44 | + const opened = await openclaw.openTab("http://127.0.0.1:8080"); |
| 45 | + expect(opened.targetId).toBe("CREATED"); |
| 46 | + expect(createTargetViaCdp).toHaveBeenCalledWith({ |
| 47 | + cdpUrl: "ws://127.0.0.1:18800/devtools/browser/SESSION?token=abc", |
| 48 | + url: "http://127.0.0.1:8080", |
| 49 | + ssrfPolicy: { allowPrivateNetwork: true }, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it("uses an HTTP /json base for focus and close", async () => { |
| 54 | + const fetchMock = vi.fn(async (url: unknown) => { |
| 55 | + const u = String(url); |
| 56 | + if (u === "http://127.0.0.1:18800/json/list?token=abc") { |
| 57 | + return { |
| 58 | + ok: true, |
| 59 | + json: async () => [ |
| 60 | + { |
| 61 | + id: "T1", |
| 62 | + title: "Tab 1", |
| 63 | + url: "https://example.com", |
| 64 | + webSocketDebuggerUrl: "ws://127.0.0.1/devtools/page/T1", |
| 65 | + type: "page", |
| 66 | + }, |
| 67 | + ], |
| 68 | + } as unknown as Response; |
| 69 | + } |
| 70 | + if (u === "http://127.0.0.1:18800/json/activate/T1?token=abc") { |
| 71 | + return { ok: true, json: async () => ({}) } as unknown as Response; |
| 72 | + } |
| 73 | + if (u === "http://127.0.0.1:18800/json/close/T1?token=abc") { |
| 74 | + return { ok: true, json: async () => ({}) } as unknown as Response; |
| 75 | + } |
| 76 | + throw new Error(`unexpected fetch: ${u}`); |
| 77 | + }); |
| 78 | + |
| 79 | + global.fetch = withFetchPreconnect(fetchMock); |
| 80 | + const state = makeState("openclaw"); |
| 81 | + state.resolved.profiles.openclaw = { |
| 82 | + cdpUrl: "ws://127.0.0.1:18800/devtools/browser/SESSION?token=abc", |
| 83 | + color: "#FF4500", |
| 84 | + }; |
| 85 | + const ctx = createBrowserRouteContext({ getState: () => state }); |
| 86 | + const openclaw = ctx.forProfile("openclaw"); |
| 87 | + |
| 88 | + await openclaw.focusTab("T1"); |
| 89 | + await openclaw.closeTab("T1"); |
| 90 | + |
| 91 | + expect(fetchMock).toHaveBeenCalledWith( |
| 92 | + "http://127.0.0.1:18800/json/activate/T1?token=abc", |
| 93 | + expect.any(Object), |
| 94 | + ); |
| 95 | + expect(fetchMock).toHaveBeenCalledWith( |
| 96 | + "http://127.0.0.1:18800/json/close/T1?token=abc", |
| 97 | + expect.any(Object), |
| 98 | + ); |
| 99 | + }); |
| 100 | +}); |
0 commit comments