From 3f985ad04ca3186b6ed874fd46de55e08b1b099a Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Thu, 8 Oct 2026 12:39:30 +0800 Subject: [PATCH] fix(chat): distinguish empty MCP selection from inheritance --- web/hooks/useComposerResources.ts | 5 ++- web/tests/composer-empty-mcp-scope.spec.tsx | 47 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 web/tests/composer-empty-mcp-scope.spec.tsx diff --git a/web/hooks/useComposerResources.ts b/web/hooks/useComposerResources.ts index bee262f1a..af309e769 100644 --- a/web/hooks/useComposerResources.ts +++ b/web/hooks/useComposerResources.ts @@ -40,14 +40,15 @@ export function useComposerResources( // Depend on the contents rather than the array identity: `workspaces` is // refetched on every session change, and re-running on a new array of the // same rows would refetch the catalog with it. - const allowedKey = allowedMcp ? allowedMcp.join("\u0000") : ""; + const allowedKey = JSON.stringify(allowedMcp); useEffect(() => { let alive = true; void getWorkspaceResources(workspaceId ?? "") .then((remote) => { if (!alive) return; - const allowed = allowedKey ? new Set(allowedKey.split("\u0000")) : null; + const selected = JSON.parse(allowedKey) as string[] | null; + const allowed = selected === null ? null : new Set(selected); setCatalog({ skills: remote.skills ?? [], mcp: (remote.mcp ?? []).filter( diff --git a/web/tests/composer-empty-mcp-scope.spec.tsx b/web/tests/composer-empty-mcp-scope.spec.tsx new file mode 100644 index 000000000..72e404267 --- /dev/null +++ b/web/tests/composer-empty-mcp-scope.spec.tsx @@ -0,0 +1,47 @@ +import { renderHook, waitFor } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { useComposerResources } from "@/hooks/useComposerResources"; +import type { ChatWorkspaceRegistration } from "@/lib/workspaces-api"; + +const api = vi.hoisted(() => ({ fetch: vi.fn() })); +vi.mock("@/lib/api", () => ({ apiFetch: api.fetch, apiUrl: (url: string) => url })); + +function workspace(mcp: string[] | null): ChatWorkspaceRegistration { + return { + workspace_id: "workspace-a", kind: "workspace", follows_root: true, + display_name: "Workspace A", path: "/workspace-a", archived: false, + created_at: "2026-10-05T00:00:00Z", status: "ready", error: "", + resources: { skills: null, mcp, knowledge_bases: null }, + }; +} + +function catalog() { + api.fetch.mockImplementation(() => Promise.resolve(new Response(JSON.stringify({ + skills: [{ id: "skill-a", name: "Skill A" }], + mcp: [{ id: "server-a", name: "Server A" }, { id: "server-b", name: "Server B" }], + knowledge_bases: [], + })))); +} + +describe("composer MCP workspace selection", () => { + it.each([ + [null, ["server-a", "server-b"]], + [["server-a"], ["server-a"]], + [[], []], + ] as const)("respects configured MCP selection %j", async (mcp, expected) => { + catalog(); + const { result } = renderHook(() => useComposerResources("workspace-a", [workspace(mcp ? [...mcp] : null)])); + await waitFor(() => expect(result.current.skills).toHaveLength(1)); + expect(result.current.mcp.map(option => option.id)).toEqual(expected); + }); + + it("refreshes when an inherited selection becomes explicitly empty", async () => { + catalog(); + const { result, rerender } = renderHook(({ mcp }: { mcp: string[] | null }) => + useComposerResources("workspace-a", [workspace(mcp)]), + { initialProps: { mcp: null } as { mcp: string[] | null } }); + await waitFor(() => expect(result.current.mcp).toHaveLength(2)); + rerender({ mcp: [] }); + await waitFor(() => expect(result.current.mcp).toHaveLength(0)); + }); +});