|
| 1 | +import type { McpRecommendedServer } from "@posthog/api-client/posthog-client"; |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { act, renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mockClient = vi.hoisted(() => ({ |
| 8 | + getMcpServerInstallations: vi.fn(), |
| 9 | + getMcpServers: vi.fn(), |
| 10 | + installMcpTemplate: vi.fn(), |
| 11 | + installCustomMcpServer: vi.fn(), |
| 12 | + uninstallMcpServer: vi.fn(), |
| 13 | + updateMcpServerInstallation: vi.fn(), |
| 14 | + authorizeMcpInstallation: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +const mockTrpcClient = vi.hoisted(() => ({ |
| 18 | + mcpCallback: { |
| 19 | + getCallbackUrl: { query: vi.fn() }, |
| 20 | + openAndWaitForCallback: { mutate: vi.fn() }, |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +const mockTrpc = vi.hoisted(() => ({ |
| 25 | + mcpCallback: { |
| 26 | + onOAuthComplete: { |
| 27 | + subscriptionOptions: vi.fn(() => ({})), |
| 28 | + }, |
| 29 | + }, |
| 30 | +})); |
| 31 | + |
| 32 | +vi.mock("@posthog/ui/features/auth/authClient", () => ({ |
| 33 | + useOptionalAuthenticatedClient: () => mockClient, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("@posthog/host-router/react", () => ({ |
| 37 | + useHostTRPC: () => mockTrpc, |
| 38 | + useHostTRPCClient: () => mockTrpcClient, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("@trpc/tanstack-react-query", () => ({ |
| 42 | + useSubscription: vi.fn(), |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock("@posthog/ui/primitives/toast", () => ({ |
| 46 | + toast: { |
| 47 | + error: vi.fn(), |
| 48 | + success: vi.fn(), |
| 49 | + }, |
| 50 | +})); |
| 51 | + |
| 52 | +import { useMcpServers } from "./useMcpServers"; |
| 53 | + |
| 54 | +function wrapper({ children }: { children: ReactNode }) { |
| 55 | + const queryClient = new QueryClient({ |
| 56 | + defaultOptions: { |
| 57 | + queries: { retry: false }, |
| 58 | + mutations: { retry: false }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + return ( |
| 62 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +const template = { |
| 67 | + id: "granola", |
| 68 | + name: "Granola", |
| 69 | + auth_type: "oauth", |
| 70 | +} as McpRecommendedServer; |
| 71 | + |
| 72 | +describe("useMcpServers", () => { |
| 73 | + beforeEach(() => { |
| 74 | + vi.clearAllMocks(); |
| 75 | + mockClient.getMcpServerInstallations.mockResolvedValue([]); |
| 76 | + mockClient.getMcpServers.mockResolvedValue([]); |
| 77 | + mockTrpcClient.mcpCallback.getCallbackUrl.query.mockResolvedValue({ |
| 78 | + callbackUrl: "posthog-code://mcp-oauth-complete", |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("reverts template connect loading state after a failed install", async () => { |
| 83 | + let rejectInstall!: (error: Error) => void; |
| 84 | + mockClient.installMcpTemplate.mockReturnValue( |
| 85 | + new Promise((_resolve, reject) => { |
| 86 | + rejectInstall = reject; |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + const { result } = renderHook(() => useMcpServers(), { wrapper }); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + result.current.installTemplate(template); |
| 94 | + }); |
| 95 | + |
| 96 | + await waitFor(() => expect(result.current.installingId).toBe("granola")); |
| 97 | + await waitFor(() => |
| 98 | + expect(mockClient.installMcpTemplate).toHaveBeenCalledWith({ |
| 99 | + template_id: "granola", |
| 100 | + install_source: "posthog-code", |
| 101 | + posthog_code_callback_url: "posthog-code://mcp-oauth-complete", |
| 102 | + api_key: undefined, |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + await act(async () => { |
| 107 | + rejectInstall(new Error("Connection failed")); |
| 108 | + }); |
| 109 | + |
| 110 | + await waitFor(() => expect(result.current.installingId).toBeNull()); |
| 111 | + }); |
| 112 | +}); |
0 commit comments