test: tighten browser tab selection assertions

This commit is contained in:
Peter Steinberger
2026-05-11 01:21:23 +01:00
parent 8dc221121c
commit 9e8ea39284

View File

@@ -6,6 +6,7 @@ vi.hoisted(() => {
});
import "./server-context.chrome-test-harness.js";
import { CDP_JSON_NEW_TIMEOUT_MS } from "./cdp-timeouts.js";
import * as cdpHelpersModule from "./cdp.helpers.js";
import * as cdpModule from "./cdp.js";
import { InvalidBrowserNavigationUrlError } from "./navigation-guard.js";
@@ -36,13 +37,14 @@ function seedRunningProfileState(
async function expectOldManagedTabClose(fetchMock: ReturnType<typeof vi.fn>): Promise<void> {
await vi.waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith(
expect.stringContaining("/json/close/OLD1"),
expect.any(Object),
);
expect(fetchCallUrls(fetchMock).some((url) => url.includes("/json/close/OLD1"))).toBe(true);
});
}
function fetchCallUrls(fetchMock: ReturnType<typeof vi.fn>): string[] {
return fetchMock.mock.calls.map(([url]) => String(url));
}
function createOldTabCleanupFetchMock(
existingTabs: ReturnType<typeof makeManagedTabsWithNew>,
params?: { rejectNewTabClose?: boolean },
@@ -192,10 +194,7 @@ describe("browser server-context tab selection state", () => {
const opened = await openManagedTabWithRunningProfile({ fetchMock });
expect(opened.targetId).toBe("NEW");
await expectOldManagedTabClose(fetchMock);
expect(fetchMock).not.toHaveBeenCalledWith(
expect.stringContaining("/json/close/NEW"),
expect.anything(),
);
expect(fetchCallUrls(fetchMock).some((url) => url.includes("/json/close/NEW"))).toBe(false);
});
it("does not fail tab open when managed-tab cleanup list fails", async () => {
@@ -253,10 +252,7 @@ describe("browser server-context tab selection state", () => {
const opened = await openclaw.openTab("http://127.0.0.1:3009");
expect(opened.targetId).toBe("NEW");
expect(fetchMock).not.toHaveBeenCalledWith(
expect.stringContaining("/json/close/"),
expect.anything(),
);
expect(fetchCallUrls(fetchMock).some((url) => url.includes("/json/close/"))).toBe(false);
});
it("does not block openTab on slow best-effort cleanup closes", async () => {
@@ -321,19 +317,18 @@ describe("browser server-context tab selection state", () => {
const opened = await openclaw.openTab("https://example.com");
expect(opened.targetId).toBe("NEW");
expect(fetchJson).toHaveBeenNthCalledWith(
1,
expect.stringContaining("/json/new"),
expect.any(Number),
const jsonNewEndpoint = "http://127.0.0.1:18800/json/new?https%3A%2F%2Fexample.com";
expect(fetchJson.mock.calls[0]).toEqual([
jsonNewEndpoint,
CDP_JSON_NEW_TIMEOUT_MS,
{ method: "PUT" },
undefined,
);
expect(fetchJson).toHaveBeenNthCalledWith(
2,
expect.stringContaining("/json/new"),
expect.any(Number),
]);
expect(fetchJson.mock.calls[1]).toEqual([
jsonNewEndpoint,
CDP_JSON_NEW_TIMEOUT_MS,
undefined,
undefined,
);
]);
});
});