test: add comprehensive MCP UI test coverage with MSW infrastructure
## MSW Infrastructure Enhancement - Add 5 MCP API handlers to tests/msw/handlers.ts: - get_mcp_config: Fetch MCP configuration for app type - import_mcp_from_claude/codex: Mock import operations (returns count: 1) - set_mcp_enabled: Toggle MCP server enabled state - upsert_mcp_server_in_config: Create/update MCP server - delete_mcp_server_in_config: Remove MCP server - Add MCP state management to tests/msw/state.ts: - McpConfigState type with per-app server storage - Default test data (stdio server for Claude, http server for Codex) - CRUD functions: getMcpConfig, setMcpServerEnabled, upsertMcpServer, deleteMcpServer - Immutable state operations with deep cloning ## McpFormModal Component Tests (4 tests) - Test preset application: Verify ID and config JSON auto-fill from preset selection - Test conflict detection: Async validation shows warning when syncing to conflicting ID - Test field sanitization: Verify trim whitespace, split tags, clean URLs before save - Test validation errors: Block submit and show toast error for invalid stdio config (missing command) ## McpPanel Integration Tests (3 tests) - Test toggle enabled state: Click toggle button triggers useMcpActions.toggleEnabled with correct params - Test create server flow: Open form → submit → saveServer called with syncOtherSide option - Test delete server flow: Click delete → confirm dialog → deleteServer called with ID ## Test Utilities - Add createTestQueryClient helper with retry: false for faster test execution ## Test Coverage - Test files: 15 → 17 (+2) - Total tests: 105 → 112 (+6.7%) - All 112 tests passing - Execution time: 3.15s
This commit is contained in:
231
tests/integration/McpPanel.test.tsx
Normal file
231
tests/integration/McpPanel.test.tsx
Normal file
@@ -0,0 +1,231 @@
|
||||
import React from "react";
|
||||
import { QueryClientProvider } from "@tanstack/react-query";
|
||||
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import McpPanel from "@/components/mcp/McpPanel";
|
||||
import type { McpServer } from "@/types";
|
||||
import { createTestQueryClient } from "../utils/testQueryClient";
|
||||
|
||||
const toastSuccessMock = vi.hoisted(() => vi.fn());
|
||||
const toastErrorMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("sonner", () => ({
|
||||
toast: {
|
||||
success: (...args: unknown[]) => toastSuccessMock(...args),
|
||||
error: (...args: unknown[]) => toastErrorMock(...args),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, params?: Record<string, unknown>) =>
|
||||
params ? `${key}:${JSON.stringify(params)}` : key,
|
||||
}),
|
||||
}));
|
||||
|
||||
const importFromClaudeMock = vi.hoisted(() => vi.fn().mockResolvedValue(1));
|
||||
const importFromCodexMock = vi.hoisted(() => vi.fn().mockResolvedValue(1));
|
||||
|
||||
const toggleEnabledMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
||||
const saveServerMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
||||
const deleteServerMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
||||
const reloadMock = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
|
||||
|
||||
const baseServers: Record<string, McpServer> = {
|
||||
sample: {
|
||||
id: "sample",
|
||||
name: "Sample Claude Server",
|
||||
enabled: true,
|
||||
server: {
|
||||
type: "stdio",
|
||||
command: "claude-server",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
vi.mock("@/lib/api", async () => {
|
||||
const actual = await vi.importActual<typeof import("@/lib/api")>("@/lib/api");
|
||||
return {
|
||||
...actual,
|
||||
mcpApi: {
|
||||
...actual.mcpApi,
|
||||
importFromClaude: (...args: unknown[]) =>
|
||||
importFromClaudeMock(...args),
|
||||
importFromCodex: (...args: unknown[]) => importFromCodexMock(...args),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("@/components/mcp/McpListItem", () => ({
|
||||
default: ({ id, server, onToggle, onEdit, onDelete }: any) => (
|
||||
<div data-testid={`mcp-item-${id}`}>
|
||||
<span>{server.name || id}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onToggle(id, !server.enabled)}
|
||||
data-testid={`toggle-${id}`}
|
||||
>
|
||||
toggle
|
||||
</button>
|
||||
<button type="button" onClick={() => onEdit(id)} data-testid={`edit-${id}`}>
|
||||
edit
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onDelete(id)}
|
||||
data-testid={`delete-${id}`}
|
||||
>
|
||||
delete
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("@/components/mcp/McpFormModal", () => ({
|
||||
default: ({ onSave, onClose }: any) => (
|
||||
<div data-testid="mcp-form">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
onSave(
|
||||
"new-server",
|
||||
{
|
||||
id: "new-server",
|
||||
name: "New Server",
|
||||
enabled: true,
|
||||
server: { type: "stdio", command: "new.cmd" },
|
||||
},
|
||||
{ syncOtherSide: true },
|
||||
)
|
||||
}
|
||||
>
|
||||
submit-form
|
||||
</button>
|
||||
<button type="button" onClick={onClose}>
|
||||
close-form
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("@/components/ui/button", () => ({
|
||||
Button: ({ children, onClick, ...rest }: any) => (
|
||||
<button type="button" onClick={onClick} {...rest}>
|
||||
{children}
|
||||
</button>
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock("@/components/ui/dialog", () => ({
|
||||
Dialog: ({ open, children }: any) => (open ? <div>{children}</div> : null),
|
||||
DialogContent: ({ children }: any) => <div>{children}</div>,
|
||||
DialogHeader: ({ children }: any) => <div>{children}</div>,
|
||||
DialogTitle: ({ children }: any) => <div>{children}</div>,
|
||||
DialogFooter: ({ children }: any) => <div>{children}</div>,
|
||||
}));
|
||||
|
||||
vi.mock("@/components/ConfirmDialog", () => ({
|
||||
ConfirmDialog: ({ isOpen, onConfirm }: any) =>
|
||||
isOpen ? (
|
||||
<div data-testid="confirm-dialog">
|
||||
<button type="button" onClick={onConfirm}>
|
||||
confirm-delete
|
||||
</button>
|
||||
</div>
|
||||
) : null,
|
||||
}));
|
||||
|
||||
const renderPanel = (props?: Partial<React.ComponentProps<typeof McpPanel>>) => {
|
||||
const client = createTestQueryClient();
|
||||
return render(
|
||||
<QueryClientProvider client={client}>
|
||||
<McpPanel open onOpenChange={() => {}} appType="claude" {...props} />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
};
|
||||
|
||||
const useMcpActionsMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock("@/hooks/useMcpActions", () => ({
|
||||
useMcpActions: (...args: unknown[]) => useMcpActionsMock(...args),
|
||||
}));
|
||||
|
||||
describe("McpPanel integration", () => {
|
||||
beforeEach(() => {
|
||||
toastSuccessMock.mockReset();
|
||||
toastErrorMock.mockReset();
|
||||
importFromClaudeMock.mockClear();
|
||||
importFromClaudeMock.mockResolvedValue(1);
|
||||
importFromCodexMock.mockClear();
|
||||
importFromCodexMock.mockResolvedValue(1);
|
||||
|
||||
toggleEnabledMock.mockClear();
|
||||
saveServerMock.mockClear();
|
||||
deleteServerMock.mockClear();
|
||||
reloadMock.mockClear();
|
||||
|
||||
useMcpActionsMock.mockReturnValue({
|
||||
servers: baseServers,
|
||||
loading: false,
|
||||
reload: reloadMock,
|
||||
toggleEnabled: toggleEnabledMock,
|
||||
saveServer: saveServerMock,
|
||||
deleteServer: deleteServerMock,
|
||||
});
|
||||
});
|
||||
|
||||
it("加载并切换 MCP 启用状态", async () => {
|
||||
renderPanel();
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("mcp-item-sample")).toBeInTheDocument(),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByTestId("toggle-sample"));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(toggleEnabledMock).toHaveBeenCalledWith("sample", false),
|
||||
);
|
||||
});
|
||||
|
||||
it("新增 MCP 并触发保存与同步选项", async () => {
|
||||
renderPanel();
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
screen.getByText((content) => content.startsWith("mcp.serverCount")),
|
||||
).toBeInTheDocument(),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByText("mcp.add"));
|
||||
await waitFor(() => expect(screen.getByTestId("mcp-form")).toBeInTheDocument());
|
||||
|
||||
fireEvent.click(screen.getByText("submit-form"));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.queryByTestId("mcp-form")).not.toBeInTheDocument(),
|
||||
);
|
||||
await waitFor(() =>
|
||||
expect(saveServerMock).toHaveBeenCalledWith(
|
||||
"new-server",
|
||||
expect.objectContaining({ id: "new-server" }),
|
||||
{ syncOtherSide: true },
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("删除 MCP 并发送确认请求", async () => {
|
||||
renderPanel();
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("mcp-item-sample")).toBeInTheDocument(),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByTestId("delete-sample"));
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId("confirm-dialog")).toBeInTheDocument(),
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByText("confirm-delete"));
|
||||
|
||||
await waitFor(() => expect(deleteServerMock).toHaveBeenCalledWith("sample"));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user