|
| 1 | +// @vitest-environment jsdom |
| 2 | +import React from "react"; |
| 3 | +import ReactDOM from "react-dom"; |
| 4 | +import { act } from "react-dom/test-utils"; |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import McpCard from "../McpCard"; |
| 7 | +import McpDetailModal from "../McpDetailModal"; |
| 8 | +import type { McpDetail, McpListItem } from "../../types/mcp"; |
| 9 | + |
| 10 | +const fetchMcpDetail = vi.fn(); |
| 11 | + |
| 12 | +vi.mock("../../api/mcpService", () => ({ |
| 13 | + deleteMcp: vi.fn(), |
| 14 | + fetchMcpDetail: (...args: unknown[]) => fetchMcpDetail(...args), |
| 15 | +})); |
| 16 | +vi.mock("../../api/quickStartTemplates", () => ({ |
| 17 | + buildQuickStartTabs: () => [], |
| 18 | + TOKEN_PLACEHOLDER_RE: /$^/g, |
| 19 | +})); |
| 20 | +vi.mock("../../utils/icon", () => ({ IconGlyph: () => null })); |
| 21 | +vi.mock("@douyinfe/semi-ui", () => ({ |
| 22 | + Spin: () => null, |
| 23 | + Toast: { success: vi.fn(), error: vi.fn() }, |
| 24 | + Tooltip: ({ children }: { children: React.ReactNode }) => children, |
| 25 | +})); |
| 26 | +vi.mock("@octo/base", () => ({ |
| 27 | + t: (key: string) => (key === "mcp.card.officialPublisher" ? "官方发布" : key), |
| 28 | + WKButton: ({ children }: { children: React.ReactNode }) => |
| 29 | + React.createElement("button", null, children), |
| 30 | + WKModal: ({ |
| 31 | + children, |
| 32 | + header, |
| 33 | + }: { |
| 34 | + children: React.ReactNode; |
| 35 | + header?: React.ReactNode; |
| 36 | + }) => React.createElement("div", null, header, children), |
| 37 | + wkConfirm: vi.fn(), |
| 38 | +})); |
| 39 | + |
| 40 | +let container: HTMLDivElement | null = null; |
| 41 | + |
| 42 | +afterEach(() => { |
| 43 | + if (container) { |
| 44 | + ReactDOM.unmountComponentAtNode(container); |
| 45 | + container.remove(); |
| 46 | + container = null; |
| 47 | + } |
| 48 | + vi.clearAllMocks(); |
| 49 | +}); |
| 50 | + |
| 51 | +function render(element: React.ReactElement) { |
| 52 | + container = document.createElement("div"); |
| 53 | + document.body.appendChild(container); |
| 54 | + act(() => { |
| 55 | + ReactDOM.render(element, container); |
| 56 | + }); |
| 57 | + return container; |
| 58 | +} |
| 59 | + |
| 60 | +const baseItem: McpListItem = { |
| 61 | + id: "mcp-1", |
| 62 | + name: "Official MCP", |
| 63 | + slogan: "Test MCP", |
| 64 | + category: "dev", |
| 65 | + tags: [], |
| 66 | + toolCount: 1, |
| 67 | + icon: "", |
| 68 | + visibility: "system", |
| 69 | + source: "system", |
| 70 | + creatorName: "Internal Admin", |
| 71 | + matchReasons: ["creator:Internal Admin", "tool:search"], |
| 72 | +}; |
| 73 | + |
| 74 | +describe("official MCP publisher", () => { |
| 75 | + it("shows official publisher on cards without leaking creator identity", () => { |
| 76 | + const root = render(<McpCard item={baseItem} onClick={vi.fn()} />); |
| 77 | + |
| 78 | + expect(root.querySelector(".wk-mcp-card--official")).not.toBeNull(); |
| 79 | + expect(root.textContent).toContain("官方发布"); |
| 80 | + expect(root.textContent).not.toContain("Internal Admin"); |
| 81 | + expect(root.textContent).toContain("search"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("keeps normal publisher rendering for non-system MCPs", () => { |
| 85 | + const root = render( |
| 86 | + <McpCard |
| 87 | + item={{ ...baseItem, visibility: "public", source: "system" }} |
| 88 | + onClick={vi.fn()} |
| 89 | + /> |
| 90 | + ); |
| 91 | + |
| 92 | + expect(root.querySelector(".wk-mcp-card--official")).toBeNull(); |
| 93 | + expect(root.textContent).toContain("Internal Admin"); |
| 94 | + expect(root.textContent).not.toContain("官方发布"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("keeps card keyboard activation for official MCPs", () => { |
| 98 | + const onClick = vi.fn(); |
| 99 | + const root = render(<McpCard item={baseItem} onClick={onClick} />); |
| 100 | + const card = root.querySelector(".wk-mcp-card") as HTMLElement; |
| 101 | + |
| 102 | + act(() => { |
| 103 | + card.dispatchEvent( |
| 104 | + new KeyboardEvent("keydown", { key: "Enter", bubbles: true }) |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + expect(onClick).toHaveBeenCalledWith(baseItem); |
| 109 | + }); |
| 110 | + |
| 111 | + it("shows the same official publisher in details", async () => { |
| 112 | + const detail: McpDetail = { |
| 113 | + ...baseItem, |
| 114 | + quickStart: { transport: "streamable-http", serverName: "Official MCP" }, |
| 115 | + tools: [], |
| 116 | + usageExamples: [], |
| 117 | + faqs: [], |
| 118 | + notes: [], |
| 119 | + }; |
| 120 | + fetchMcpDetail.mockResolvedValue(detail); |
| 121 | + |
| 122 | + let root!: HTMLElement; |
| 123 | + await act(async () => { |
| 124 | + root = render(<McpDetailModal mcpId="mcp-1" onClose={vi.fn()} />); |
| 125 | + await Promise.resolve(); |
| 126 | + await Promise.resolve(); |
| 127 | + }); |
| 128 | + |
| 129 | + expect(root.textContent).toContain("官方发布"); |
| 130 | + expect(root.textContent).not.toContain("Internal Admin"); |
| 131 | + }); |
| 132 | +}); |
0 commit comments