fix(browser): remote profile tab ops follow-up (#1060) (thanks @mukhtharcm)

Landed via follow-up to #1057.

Gate: pnpm lint && pnpm build && pnpm test
This commit is contained in:
Peter Steinberger
2026-01-17 01:28:22 +00:00
committed by GitHub
parent e16ce1a0a1
commit a76cbc43bb
8 changed files with 232 additions and 48 deletions

View File

@@ -93,6 +93,94 @@ describe("browser server-context remote profile tab operations", () => {
expect(fetchMock).not.toHaveBeenCalled();
});
it("prefers lastTargetId for remote profiles when targetId is omitted", async () => {
vi.resetModules();
const responses = [
// ensureTabAvailable() calls listTabs twice
[
{ targetId: "A", title: "A", url: "https://a.example", type: "page" },
{ targetId: "B", title: "B", url: "https://b.example", type: "page" },
],
[
{ targetId: "A", title: "A", url: "https://a.example", type: "page" },
{ targetId: "B", title: "B", url: "https://b.example", type: "page" },
],
// second ensureTabAvailable() calls listTabs twice, order flips
[
{ targetId: "B", title: "B", url: "https://b.example", type: "page" },
{ targetId: "A", title: "A", url: "https://a.example", type: "page" },
],
[
{ targetId: "B", title: "B", url: "https://b.example", type: "page" },
{ targetId: "A", title: "A", url: "https://a.example", type: "page" },
],
];
const listPagesViaPlaywright = vi.fn(async () => {
const next = responses.shift();
if (!next) throw new Error("no more responses");
return next;
});
vi.doMock("./pw-ai.js", () => ({
listPagesViaPlaywright,
createPageViaPlaywright: vi.fn(async () => {
throw new Error("unexpected create");
}),
closePageByTargetIdViaPlaywright: vi.fn(async () => {
throw new Error("unexpected close");
}),
}));
const fetchMock = vi.fn(async () => {
throw new Error("unexpected fetch");
});
// @ts-expect-error test override
global.fetch = fetchMock;
const { createBrowserRouteContext } = await import("./server-context.js");
const state = makeState("remote");
const ctx = createBrowserRouteContext({ getState: () => state });
const remote = ctx.forProfile("remote");
const first = await remote.ensureTabAvailable();
expect(first.targetId).toBe("A");
const second = await remote.ensureTabAvailable();
expect(second.targetId).toBe("A");
});
it("uses Playwright focus for remote profiles when available", async () => {
vi.resetModules();
const listPagesViaPlaywright = vi.fn(async () => [
{ targetId: "T1", title: "Tab 1", url: "https://a.example", type: "page" },
]);
const focusPageByTargetIdViaPlaywright = vi.fn(async () => {});
vi.doMock("./pw-ai.js", () => ({
listPagesViaPlaywright,
focusPageByTargetIdViaPlaywright,
}));
const fetchMock = vi.fn(async () => {
throw new Error("unexpected fetch");
});
// @ts-expect-error test override
global.fetch = fetchMock;
const { createBrowserRouteContext } = await import("./server-context.js");
const state = makeState("remote");
const ctx = createBrowserRouteContext({ getState: () => state });
const remote = ctx.forProfile("remote");
await remote.focusTab("T1");
expect(focusPageByTargetIdViaPlaywright).toHaveBeenCalledWith({
cdpUrl: "https://browserless.example/chrome?token=abc",
targetId: "T1",
});
expect(fetchMock).not.toHaveBeenCalled();
expect(state.profiles.get("remote")?.lastTargetId).toBe("T1");
});
it("does not swallow Playwright runtime errors for remote profiles", async () => {
vi.resetModules();
vi.doMock("./pw-ai.js", () => ({