Skip to content

Open-source, local-first AI agent for real work.

test(mcp): MCP 引用相关测试改为断言可观察结果

bent2685fda848e702c2
diff --git a/apps/desktop/src/main/mcp/mcp-tools-service.test.ts b/apps/desktop/src/main/mcp/mcp-tools-service.test.ts
index a5fe3abc5..e182364ad 100644
--- a/apps/desktop/src/main/mcp/mcp-tools-service.test.ts
+++ b/apps/desktop/src/main/mcp/mcp-tools-service.test.ts
@@ -1,4 +1,4 @@
-import type { McpClientHandle } from "@vetta/runtime-mcp";
+import type { McpClientHandle, McpServerConfig } from "@vetta/runtime-mcp";
import { describe, expect, it, vi } from "vitest";
import { McpToolsService } from "./mcp-tools-service.js";
@@ -64,21 +64,22 @@ describe("McpToolsService", () => {
const loadServer = vi.fn(async (_name: string, cwd: string | undefined) =>
cwd ? ({ command: `${cwd}/mcp` } as const) : undefined,
);
- const createClient = vi.fn(
- () =>
- ({
- initialize: async () => ({ capabilities: { tools: {} } }),
- listTools: async () => ({ tools: [{ name: "only" }] }),
- close: async () => undefined,
- }) as unknown as McpClientHandle,
- );
+ // 每个项目的服务只暴露一个以自身启动命令命名的工具,结果能看出连的是哪份配置。
+ const createClient = (_name: string, config: McpServerConfig) =>
+ ({
+ initialize: async () => ({ capabilities: { tools: {} } }),
+ listTools: async () => ({
+ tools: [{ name: `tool from ${"command" in config ? config.command : config.url}` }],
+ }),
+ close: async () => undefined,
+ }) as unknown as McpClientHandle;
const service = new McpToolsService({ loadServer, createClient });
const first = service.list("docs", "/a");
- expect(service.list("docs", "/b")).not.toBe(first);
- await expect(first).resolves.toEqual([{ name: "only", description: undefined }]);
+ const second = service.list("docs", "/b");
+ expect(second).not.toBe(first);
+ await expect(first).resolves.toEqual([{ name: "tool from /a/mcp", description: undefined }]);
+ await expect(second).resolves.toEqual([{ name: "tool from /b/mcp", description: undefined }]);
await expect(service.list("docs")).rejects.toThrow("not found");
- expect(createClient).toHaveBeenCalledWith("docs", { command: "/a/mcp" });
- expect(createClient).toHaveBeenCalledWith("docs", { command: "/b/mcp" });
});
it("rejects looping pagination and releases the client", async () => {
const { service, listTools, close } = fixture();
diff --git a/apps/desktop/src/renderer/domains/conversation/components/command-panel/CommandPanel.mcp.test.tsx b/apps/desktop/src/renderer/domains/conversation/components/command-panel/CommandPanel.mcp.test.tsx
index 8b18069bb..dab492bce 100644
--- a/apps/desktop/src/renderer/domains/conversation/components/command-panel/CommandPanel.mcp.test.tsx
+++ b/apps/desktop/src/renderer/domains/conversation/components/command-panel/CommandPanel.mcp.test.tsx
@@ -50,12 +50,18 @@ const toolsByServer: Record<string, { name: string; description?: string }[]> =
single: [{ name: "only_tool" }],
};
+/** 记录面板经 IPC 发出的 MCP 查询,用来断言读了什么、读了几次。 */
function installVetta(flag: boolean) {
- const listTools = vi.fn(async (server: string) => {
+ const requests: string[] = [];
+ const listTools = async (server: string, scope?: string) => {
+ requests.push(`tools ${server} @ ${scope}`);
if (server === "docs") throw new Error("Error invoking remote method 'vetta:mcp:list-tools': Error: spawn ENOENT");
return toolsByServer[server] ?? [];
- });
- const listReferenceable = vi.fn(async () => servers);
+ };
+ const listReferenceable = async (scope?: string) => {
+ requests.push(`servers @ ${scope}`);
+ return servers;
+ };
Object.defineProperty(window, "vetta", {
configurable: true,
value: {
@@ -63,7 +69,7 @@ function installVetta(flag: boolean) {
mcp: { listReferenceable, listTools },
},
});
- return { listTools, listReferenceable };
+ return { requests };
}
function press(key: string): void {
@@ -72,21 +78,28 @@ function press(key: string): void {
});
}
+type InsertedReference = { name: string; label: string; tool?: string };
+
function renderPanel(filter = "/") {
- const onSelectConnector = vi.fn();
- const onClose = vi.fn();
+ /** 面板交给输入框插入的引用,按顺序记下。 */
+ const inserted: InsertedReference[] = [];
+ const panel = { closeCount: 0 };
const props = {
open: true,
- onClose,
+ onClose: () => {
+ panel.closeCount += 1;
+ },
onSelect: vi.fn(),
- onSelectConnector,
+ onSelectConnector: (reference: InsertedReference) => {
+ inserted.push(reference);
+ },
filter,
cwd,
allowCompaction: false,
};
const view = render(<CommandPanel {...props} />);
const rerender = (next: Partial<typeof props>) => view.rerender(<CommandPanel {...props} {...next} />);
- return { ...view, rerender, onSelectConnector, onClose };
+ return { ...view, rerender, inserted, panel };
}
function row(name: string): HTMLElement {
@@ -104,21 +117,21 @@ beforeEach(() => {
describe("斜杠面板引用 MCP", () => {
it("设置关闭时面板保持原样:只有连接器宫格与 skills,不读 MCP 列表", async () => {
- const { listReferenceable } = installVetta(false);
+ const { requests } = installVetta(false);
renderPanel();
expect(await screen.findByText("Slack 宫格")).toBeTruthy();
expect(screen.getByText("review")).toBeTruthy();
await waitFor(() => expect(screen.queryByText("github")).toBeNull());
- expect(listReferenceable).not.toHaveBeenCalled();
+ expect(requests).toEqual([]);
});
it("打开后 MCP 列表取代宫格,排在 skills 前,项目里的服务带标签,禁用的不列", async () => {
- const { listReferenceable } = installVetta(true);
+ const { requests } = installVetta(true);
renderPanel();
expect(await screen.findByText("github")).toBeTruthy();
- expect(listReferenceable).toHaveBeenCalledWith(cwd);
+ expect(requests).toEqual([`servers @ ${cwd}`]);
expect(screen.queryByText("Slack 宫格")).toBeNull();
expect(screen.queryByTitle("off")).toBeNull();
expect(row("docs").textContent).toContain("Project");
@@ -130,34 +143,34 @@ describe("斜杠面板引用 MCP", () => {
});
it("Enter 下钻:第一行引用整个服务,工具行引用 服务/工具,工具列表只取一次", async () => {
- const { listTools } = installVetta(true);
- const { onSelectConnector, onClose } = renderPanel();
+ const { requests } = installVetta(true);
+ const { inserted, panel } = renderPanel();
await screen.findByText("github");
press("Enter");
expect(await screen.findByText("list_issues")).toBeTruthy();
expect(screen.getByText("Esc to go back")).toBeTruthy();
expect(screen.queryByText("review")).toBeNull();
- expect(listTools).toHaveBeenCalledWith("github", cwd);
press("Enter");
- expect(onSelectConnector).toHaveBeenLastCalledWith({ name: "github", label: "github" });
-
press("ArrowDown");
press("Enter");
- expect(onSelectConnector).toHaveBeenLastCalledWith({ name: "github", label: "github", tool: "list_issues" });
+ expect(inserted).toEqual([
+ { name: "github", label: "github" },
+ { name: "github", label: "github", tool: "list_issues" },
+ ]);
press("Escape");
- expect(onClose).not.toHaveBeenCalled();
+ expect(panel.closeCount).toBe(0);
expect(await screen.findByText("review")).toBeTruthy();
press("Tab");
expect(await screen.findByText("create_pr")).toBeTruthy();
- expect(listTools).toHaveBeenCalledTimes(1);
+ expect(requests).toEqual([`servers @ ${cwd}`, `tools github @ ${cwd}`]);
press("Escape");
press("Escape");
- expect(onClose).toHaveBeenCalledOnce();
+ expect(panel.closeCount).toBe(1);
});
it("下钻后继续输入的字只过滤工具;关掉面板再打开回到顶层", async () => {
@@ -187,25 +200,25 @@ describe("斜杠面板引用 MCP", () => {
it("取工具失败时显示原因,服务本身仍可引用", async () => {
installVetta(true);
- const { onSelectConnector } = renderPanel();
+ const { inserted } = renderPanel();
fireEvent.click(await screen.findByTitle("docs"));
expect((await screen.findByRole("alert")).textContent).toContain("spawn ENOENT");
expect(screen.getByRole("alert").textContent).not.toContain("invoking remote method");
fireEvent.click(row("docs"));
- expect(onSelectConnector).toHaveBeenCalledWith({ name: "docs", label: "docs" });
+ expect(inserted).toEqual([{ name: "docs", label: "docs" }]);
});
it("已知只有一个工具的服务不再显示箭头,选中直接引用整个服务", async () => {
installVetta(true);
- const { onSelectConnector } = renderPanel();
+ const { inserted } = renderPanel();
fireEvent.click(await screen.findByTitle("single"));
expect(await screen.findByText("only_tool")).toBeTruthy();
press("Escape");
await waitFor(() => expect(hasChevron(row("single"))).toBe(false));
fireEvent.click(row("single"));
- expect(onSelectConnector).toHaveBeenCalledWith({ name: "single", label: "single" });
+ expect(inserted).toEqual([{ name: "single", label: "single" }]);
expect(screen.queryByText("only_tool")).toBeNull();
});
});
diff --git a/apps/desktop/src/renderer/domains/settings/components/useAgentSettingsModel.test.tsx b/apps/desktop/src/renderer/domains/settings/components/useAgentSettingsModel.test.tsx
index 43bc09601..d3e422f9d 100644
--- a/apps/desktop/src/renderer/domains/settings/components/useAgentSettingsModel.test.tsx
+++ b/apps/desktop/src/renderer/domains/settings/components/useAgentSettingsModel.test.tsx
@@ -7,8 +7,12 @@ import { useAgentSettingsModel } from "./useAgentSettingsModel";
initI18n();
+/** 假配置存储:set 合并进 saved,断言最终落盘的内容。 */
function installVetta(config: Record<string, unknown>) {
- const set = vi.fn(async () => undefined);
+ const saved: Record<string, unknown> = {};
+ const set = async (patch: Record<string, unknown>) => {
+ Object.assign(saved, patch);
+ };
Object.defineProperty(window, "vetta", {
configurable: true,
value: {
@@ -20,12 +24,12 @@ function installVetta(config: Record<string, unknown>) {
appMonitor: { recordEvent: vi.fn() },
},
});
- return { set };
+ return { saved };
}
describe("在输入框中引用 MCP", () => {
it("缺省关闭,打开后写入顶层 inputMcpReferences 而不是 experimental", async () => {
- const { set } = installVetta({});
+ const { saved } = installVetta({});
const { result } = renderHook(() => useAgentSettingsModel());
await waitFor(() => expect(result.current.labels.inputMcpReferences).toBeTruthy());
expect(result.current.inputMcpReferencesEnabled).toBe(false);
@@ -33,7 +37,7 @@ describe("在输入框中引用 MCP", () => {
act(() => result.current.actions.toggleInputMcpReferences(true));
expect(result.current.inputMcpReferencesEnabled).toBe(true);
- expect(set).toHaveBeenCalledWith({ inputMcpReferences: true });
+ expect(saved).toEqual({ inputMcpReferences: true });
});
it("读出已保存的开启状态", async () => {