test: tighten browser server assertions

This commit is contained in:
Peter Steinberger
2026-05-11 14:45:43 +01:00
parent 09fcd19814
commit 248207dace
4 changed files with 102 additions and 108 deletions

View File

@@ -142,11 +142,8 @@ describe("cdp.helpers internal", () => {
await fetchCdpChecked("http://93.184.216.34:9222/json/version", 250, undefined, {
allowPrivateNetwork: true,
});
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
policy: expect.objectContaining({ allowPrivateNetwork: true }),
}),
);
const [request] = fetchWithSsrFGuardMock.mock.calls[0] ?? [];
expect(request?.policy?.allowPrivateNetwork).toBe(true);
});
it("falls back to a permissive private-network policy when none is supplied on a non-loopback host", async () => {
@@ -157,11 +154,8 @@ describe("cdp.helpers internal", () => {
release,
});
await fetchCdpChecked("http://93.184.216.34:9222/json/version", 250);
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
policy: { allowPrivateNetwork: true },
}),
);
const [request] = fetchWithSsrFGuardMock.mock.calls[0] ?? [];
expect(request?.policy).toEqual({ allowPrivateNetwork: true });
});
});

View File

@@ -105,15 +105,12 @@ describe("cdp helpers", () => {
}),
).resolves.toBeUndefined();
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "http://127.0.0.1:9222/json/version",
policy: {
dangerouslyAllowPrivateNetwork: false,
allowedHostnames: ["127.0.0.1"],
},
}),
);
const [request] = fetchWithSsrFGuardMock.mock.calls[0] ?? [];
expect(request?.url).toBe("http://127.0.0.1:9222/json/version");
expect(request?.policy).toEqual({
dangerouslyAllowPrivateNetwork: false,
allowedHostnames: ["127.0.0.1"],
});
expect(release).toHaveBeenCalledTimes(1);
});
@@ -134,16 +131,13 @@ describe("cdp helpers", () => {
}),
).resolves.toBeUndefined();
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
expect.objectContaining({
url: "http://127.0.0.1:9222/json/version",
policy: {
dangerouslyAllowPrivateNetwork: false,
hostnameAllowlist: ["*.corp.example"],
allowedHostnames: ["127.0.0.1"],
},
}),
);
const [request] = fetchWithSsrFGuardMock.mock.calls[0] ?? [];
expect(request?.url).toBe("http://127.0.0.1:9222/json/version");
expect(request?.policy).toEqual({
dangerouslyAllowPrivateNetwork: false,
hostnameAllowlist: ["*.corp.example"],
allowedHostnames: ["127.0.0.1"],
});
expect(release).toHaveBeenCalledTimes(1);
});
});

View File

@@ -29,6 +29,12 @@ vi.mock("./chrome-mcp.runtime.js", () => ({
const { createBrowserRouteContext } = await import("./server-context.js");
const chromeMcp = chromeMcpMock;
type ChromeLiveProfile = {
driver?: string;
name?: string;
userDataDir?: string;
};
function makeState(): BrowserServerState {
return {
server: null,
@@ -74,14 +80,6 @@ function makeState(): BrowserServerState {
};
}
function expectChromeLiveProfile() {
return expect.objectContaining({
name: "chrome-live",
driver: "existing-session",
userDataDir: "/tmp/brave-profile",
});
}
beforeEach(() => {
for (const key of [
"ALL_PROXY",
@@ -111,27 +109,32 @@ describe("browser server-context existing-session profile", () => {
vi.mocked(chromeMcp.listChromeMcpTabs).mockRejectedValueOnce(new Error("No page selected"));
const profiles = await ctx.listProfiles();
expect(profiles).toEqual([
expect.objectContaining({
name: "chrome-live",
transport: "chrome-mcp",
running: true,
tabCount: 0,
}),
]);
expect(profiles).toHaveLength(1);
expect(profiles[0]?.name).toBe("chrome-live");
expect(profiles[0]?.transport).toBe("chrome-mcp");
expect(profiles[0]?.running).toBe(true);
expect(profiles[0]?.tabCount).toBe(0);
expect(chromeMcp.ensureChromeMcpAvailable).toHaveBeenCalledWith(
"chrome-live",
expectChromeLiveProfile(),
{ ephemeral: true, timeoutMs: 300 },
);
expect(chromeMcp.listChromeMcpTabs).toHaveBeenCalledWith(
"chrome-live",
expectChromeLiveProfile(),
{
ephemeral: true,
},
);
const [, ensuredProfile, ensureOptions] =
(
vi.mocked(chromeMcp.ensureChromeMcpAvailable).mock.calls as unknown as Array<
[string, ChromeLiveProfile, { ephemeral?: boolean; timeoutMs?: number }]
>
)[0] ?? [];
expect(ensuredProfile?.name).toBe("chrome-live");
expect(ensuredProfile?.driver).toBe("existing-session");
expect(ensuredProfile?.userDataDir).toBe("/tmp/brave-profile");
expect(ensureOptions).toEqual({ ephemeral: true, timeoutMs: 300 });
const [, listedProfile, listOptions] =
(
vi.mocked(chromeMcp.listChromeMcpTabs).mock.calls as unknown as Array<
[string, ChromeLiveProfile, { ephemeral?: boolean }]
>
)[0] ?? [];
expect(listedProfile?.name).toBe("chrome-live");
expect(listedProfile?.driver).toBe("existing-session");
expect(listedProfile?.userDataDir).toBe("/tmp/brave-profile");
expect(listOptions).toEqual({ ephemeral: true });
});
it("keeps the next real attach on the normal sticky session path after an idle status probe", async () => {
@@ -142,13 +145,11 @@ describe("browser server-context existing-session profile", () => {
vi.mocked(chromeMcp.listChromeMcpTabs).mockRejectedValueOnce(new Error("No page selected"));
await expect(ctx.listProfiles()).resolves.toEqual([
expect.objectContaining({
name: "chrome-live",
running: true,
tabCount: 0,
}),
]);
const profiles = await ctx.listProfiles();
expect(profiles).toHaveLength(1);
expect(profiles[0]?.name).toBe("chrome-live");
expect(profiles[0]?.running).toBe(true);
expect(profiles[0]?.tabCount).toBe(0);
vi.mocked(chromeMcp.listChromeMcpTabs).mockClear();
@@ -156,20 +157,24 @@ describe("browser server-context existing-session profile", () => {
const tabs = await live.listTabs();
expect(tabs.map((tab) => tab.targetId)).toEqual(["7"]);
expect(chromeMcp.ensureChromeMcpAvailable).toHaveBeenLastCalledWith(
"chrome-live",
expectChromeLiveProfile(),
);
expect(chromeMcp.listChromeMcpTabs).toHaveBeenNthCalledWith(
1,
"chrome-live",
expectChromeLiveProfile(),
);
expect(chromeMcp.listChromeMcpTabs).toHaveBeenNthCalledWith(
2,
"chrome-live",
expectChromeLiveProfile(),
);
const ensureCalls = vi.mocked(chromeMcp.ensureChromeMcpAvailable).mock
.calls as unknown as Array<[string, ChromeLiveProfile]>;
const lastEnsureCall = ensureCalls.at(-1);
expect(lastEnsureCall?.[0]).toBe("chrome-live");
expect(lastEnsureCall?.[1]?.name).toBe("chrome-live");
expect(lastEnsureCall?.[1]?.driver).toBe("existing-session");
expect(lastEnsureCall?.[1]?.userDataDir).toBe("/tmp/brave-profile");
const listCalls = vi.mocked(chromeMcp.listChromeMcpTabs).mock.calls as unknown as Array<
[string, ChromeLiveProfile]
>;
expect(listCalls[0]?.[0]).toBe("chrome-live");
expect(listCalls[0]?.[1]?.name).toBe("chrome-live");
expect(listCalls[0]?.[1]?.driver).toBe("existing-session");
expect(listCalls[0]?.[1]?.userDataDir).toBe("/tmp/brave-profile");
expect(listCalls[1]?.[0]).toBe("chrome-live");
expect(listCalls[1]?.[1]?.name).toBe("chrome-live");
expect(listCalls[1]?.[1]?.driver).toBe("existing-session");
expect(listCalls[1]?.[1]?.userDataDir).toBe("/tmp/brave-profile");
});
it("routes tab operations through the Chrome MCP backend", async () => {
@@ -211,24 +216,31 @@ describe("browser server-context existing-session profile", () => {
await live.focusTab("7");
await live.stopRunningBrowser();
expect(chromeMcp.ensureChromeMcpAvailable).toHaveBeenCalledWith(
"chrome-live",
expectChromeLiveProfile(),
);
expect(chromeMcp.listChromeMcpTabs).toHaveBeenCalledWith(
"chrome-live",
expectChromeLiveProfile(),
);
expect(chromeMcp.openChromeMcpTab).toHaveBeenCalledWith(
"chrome-live",
"about:blank",
expectChromeLiveProfile(),
);
expect(chromeMcp.focusChromeMcpTab).toHaveBeenCalledWith(
"chrome-live",
"7",
expectChromeLiveProfile(),
);
const [ensureCall] = vi.mocked(chromeMcp.ensureChromeMcpAvailable).mock
.calls as unknown as Array<[string, ChromeLiveProfile]>;
expect(ensureCall?.[0]).toBe("chrome-live");
expect(ensureCall?.[1]?.name).toBe("chrome-live");
expect(ensureCall?.[1]?.driver).toBe("existing-session");
const [listCall] = vi.mocked(chromeMcp.listChromeMcpTabs).mock.calls as unknown as Array<
[string, ChromeLiveProfile]
>;
expect(listCall?.[0]).toBe("chrome-live");
expect(listCall?.[1]?.name).toBe("chrome-live");
expect(listCall?.[1]?.driver).toBe("existing-session");
const [openCall] = vi.mocked(chromeMcp.openChromeMcpTab).mock.calls as unknown as Array<
[string, string, ChromeLiveProfile]
>;
expect(openCall?.[0]).toBe("chrome-live");
expect(openCall?.[1]).toBe("about:blank");
expect(openCall?.[2]?.name).toBe("chrome-live");
expect(openCall?.[2]?.driver).toBe("existing-session");
const [focusCall] = vi.mocked(chromeMcp.focusChromeMcpTab).mock.calls as unknown as Array<
[string, string, ChromeLiveProfile]
>;
expect(focusCall?.[0]).toBe("chrome-live");
expect(focusCall?.[1]).toBe("7");
expect(focusCall?.[2]?.name).toBe("chrome-live");
expect(focusCall?.[2]?.driver).toBe("existing-session");
expect(chromeMcp.closeChromeMcpSession).toHaveBeenCalledWith("chrome-live");
});

View File

@@ -23,12 +23,9 @@ describe("browser server-context listProfiles", () => {
const profiles = await ctx.listProfiles();
expect(isChromeReachable).toHaveBeenCalledWith("http://127.0.0.1:18800", 200, undefined);
expect(profiles).toEqual([
expect.objectContaining({
name: "openclaw",
running: true,
}),
]);
expect(profiles).toHaveLength(1);
expect(profiles[0]?.name).toBe("openclaw");
expect(profiles[0]?.running).toBe(true);
});
it("uses remote-class probes for attachOnly loopback CDP profiles", async () => {
@@ -60,11 +57,8 @@ describe("browser server-context listProfiles", () => {
state.resolved.remoteCdpTimeoutMs,
undefined,
);
expect(profiles).toEqual([
expect.objectContaining({
name: "manual-cdp",
running: true,
}),
]);
expect(profiles).toHaveLength(1);
expect(profiles[0]?.name).toBe("manual-cdp");
expect(profiles[0]?.running).toBe(true);
});
});