fix(browser): keep dispatcher context with no-retry hints

Landed from #39090 by @NewdlDewdl.

Co-authored-by: NewdlDewdl <rohin.agrawal@gmail.com>
This commit is contained in:
Peter Steinberger
2026-03-07 20:45:06 +00:00
parent f17f2f918c
commit 9b4a114eb6
3 changed files with 79 additions and 12 deletions

View File

@@ -8,6 +8,8 @@ const mocks = vi.hoisted(() => ({
},
},
})),
startBrowserControlServiceFromConfig: vi.fn(async () => ({ ok: true })),
dispatch: vi.fn(async () => ({ status: 200, body: { ok: true } })),
}));
vi.mock("../config/config.js", async (importOriginal) => {
@@ -20,12 +22,12 @@ vi.mock("../config/config.js", async (importOriginal) => {
vi.mock("./control-service.js", () => ({
createBrowserControlContext: vi.fn(() => ({})),
startBrowserControlServiceFromConfig: vi.fn(async () => ({ ok: true })),
startBrowserControlServiceFromConfig: mocks.startBrowserControlServiceFromConfig,
}));
vi.mock("./routes/dispatcher.js", () => ({
createBrowserRouteDispatcher: vi.fn(() => ({
dispatch: vi.fn(async () => ({ status: 200, body: { ok: true } })),
dispatch: mocks.dispatch,
})),
}));
@@ -54,6 +56,8 @@ describe("fetchBrowserJson loopback auth", () => {
},
},
});
mocks.startBrowserControlServiceFromConfig.mockReset().mockResolvedValue({ ok: true });
mocks.dispatch.mockReset().mockResolvedValue({ status: 200, body: { ok: true } });
});
afterEach(() => {
@@ -114,4 +118,32 @@ describe("fetchBrowserJson loopback auth", () => {
const headers = new Headers(init?.headers);
expect(headers.get("authorization")).toBe("Bearer loopback-token");
});
it("preserves dispatcher error context while keeping no-retry hint", async () => {
mocks.dispatch.mockRejectedValueOnce(new Error("Chrome CDP handshake timeout"));
const thrown = await fetchBrowserJson<{ ok: boolean }>("/tabs").catch((err) => err as Error);
expect(thrown).toBeInstanceOf(Error);
expect(thrown.message).toContain("Chrome CDP handshake timeout");
expect(thrown.message).toContain("Do NOT retry the browser tool");
expect(thrown.message).not.toContain("Can't reach the OpenClaw browser control service");
});
it("keeps absolute URL failures wrapped as reachability errors", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("socket hang up");
}),
);
const thrown = await fetchBrowserJson<{ ok: boolean }>("http://example.com/").catch(
(err) => err as Error,
);
expect(thrown).toBeInstanceOf(Error);
expect(thrown.message).toContain("Can't reach the OpenClaw browser control service");
expect(thrown.message).toContain("Do NOT retry the browser tool");
});
});