From 0b0c43d2e4fac50aa6cdbf955f237d6468a2a3a3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 23:41:21 +0100 Subject: [PATCH] test: guard ui sidebar mock calls --- ui/src/ui/chat/grouped-render.test.ts | 18 ++++++++++++++++-- ui/src/ui/chat/tool-cards.test.ts | 25 +++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/ui/src/ui/chat/grouped-render.test.ts b/ui/src/ui/chat/grouped-render.test.ts index 65693f717c6..dec616a93d7 100644 --- a/ui/src/ui/chat/grouped-render.test.ts +++ b/ui/src/ui/chat/grouped-render.test.ts @@ -29,6 +29,21 @@ vi.mock("../icons.ts", () => ({ icons: {}, })); +function requireFirstMockArg( + mock: ReturnType, + label: string, +): Record { + const [call] = mock.mock.calls; + if (!call) { + throw new Error(`expected ${label} call`); + } + const [arg] = call; + if (!arg || typeof arg !== "object" || Array.isArray(arg)) { + throw new Error(`expected ${label} payload`); + } + return arg as Record; +} + vi.mock("../views/agents-utils.ts", () => { const isRenderableControlUiAvatarUrl = (value: string) => /^data:image\//i.test(value) || (value.startsWith("/") && !value.startsWith("//")); @@ -1768,7 +1783,6 @@ describe("grouped chat rendering", () => { expect(container.querySelector(".chat-tool-card__preview-frame")).toBeNull(); expect(onOpenSidebar).toHaveBeenCalledTimes(1); - const sidebarPayload = onOpenSidebar.mock.calls[0]?.[0] as { kind?: string } | undefined; - expect(sidebarPayload?.kind).toBe("markdown"); + expect(requireFirstMockArg(onOpenSidebar, "sidebar open").kind).toBe("markdown"); }); }); diff --git a/ui/src/ui/chat/tool-cards.test.ts b/ui/src/ui/chat/tool-cards.test.ts index 334bd63896c..7f2154c5795 100644 --- a/ui/src/ui/chat/tool-cards.test.ts +++ b/ui/src/ui/chat/tool-cards.test.ts @@ -20,6 +20,21 @@ vi.mock("../tool-display.ts", () => ({ }), })); +function requireFirstMockArg( + mock: ReturnType, + label: string, +): Record { + const [call] = mock.mock.calls; + if (!call) { + throw new Error(`expected ${label} call`); + } + const [arg] = call; + if (!arg || typeof arg !== "object" || Array.isArray(arg)) { + throw new Error(`expected ${label} payload`); + } + return arg as Record; +} + describe("tool-cards", () => { it("renders expanded cards with inline input and output sections", () => { const container = document.createElement("div"); @@ -178,11 +193,9 @@ describe("tool-cards", () => { expect(sidebarButton?.classList.contains("chat-tool-card__action-btn")).toBe(true); sidebarButton!.click(); - const sidebar = onOpenSidebar.mock.calls[0]?.[0] as - | { kind?: string; docId?: string; entryUrl?: string } - | undefined; - expect(sidebar?.kind).toBe("canvas"); - expect(sidebar?.docId).toBe("cv_sidebar"); - expect(sidebar?.entryUrl).toBe("/__openclaw__/canvas/documents/cv_sidebar/index.html"); + const sidebar = requireFirstMockArg(onOpenSidebar, "sidebar open"); + expect(sidebar.kind).toBe("canvas"); + expect(sidebar.docId).toBe("cv_sidebar"); + expect(sidebar.entryUrl).toBe("/__openclaw__/canvas/documents/cv_sidebar/index.html"); }); });