|
| 1 | +import type { LocalMcpCloudClassification } from "@posthog/core/local-mcp/localMcpImport"; |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + flagEnabled: false, |
| 9 | + getCloudAvailability: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@posthog/di/react", () => ({ |
| 13 | + useServiceOptional: () => ({ |
| 14 | + getCloudAvailability: mocks.getCloudAvailability, |
| 15 | + }), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("../feature-flags/useFeatureFlag", () => ({ |
| 19 | + useFeatureFlag: () => mocks.flagEnabled, |
| 20 | +})); |
| 21 | + |
| 22 | +import { useLocalMcpCloudServers } from "./useLocalMcpCloudServers"; |
| 23 | + |
| 24 | +function wrapper({ children }: { children: ReactNode }) { |
| 25 | + const queryClient = new QueryClient({ |
| 26 | + defaultOptions: { queries: { retry: false } }, |
| 27 | + }); |
| 28 | + return ( |
| 29 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +const server: LocalMcpCloudClassification = { |
| 34 | + name: "internal-tools", |
| 35 | + availability: "requires_desktop", |
| 36 | + reason: "stdio_transport", |
| 37 | +}; |
| 38 | + |
| 39 | +describe("useLocalMcpCloudServers", () => { |
| 40 | + beforeEach(() => { |
| 41 | + mocks.flagEnabled = false; |
| 42 | + mocks.getCloudAvailability.mockReset(); |
| 43 | + mocks.getCloudAvailability.mockResolvedValue([server]); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does not inspect local MCP servers while the feature flag is disabled", () => { |
| 47 | + const { result } = renderHook(() => useLocalMcpCloudServers(true), { |
| 48 | + wrapper, |
| 49 | + }); |
| 50 | + |
| 51 | + expect(result.current).toEqual({ servers: [], isLoading: false }); |
| 52 | + expect(mocks.getCloudAvailability).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns local MCP servers while the feature flag is enabled", async () => { |
| 56 | + mocks.flagEnabled = true; |
| 57 | + |
| 58 | + const { result } = renderHook(() => useLocalMcpCloudServers(true), { |
| 59 | + wrapper, |
| 60 | + }); |
| 61 | + |
| 62 | + await waitFor(() => |
| 63 | + expect(result.current).toEqual({ servers: [server], isLoading: false }), |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("hides cached local MCP servers when the feature flag is disabled", async () => { |
| 68 | + mocks.flagEnabled = true; |
| 69 | + const { result, rerender } = renderHook( |
| 70 | + () => useLocalMcpCloudServers(true), |
| 71 | + { wrapper }, |
| 72 | + ); |
| 73 | + await waitFor(() => expect(result.current.servers).toEqual([server])); |
| 74 | + |
| 75 | + mocks.flagEnabled = false; |
| 76 | + rerender(); |
| 77 | + |
| 78 | + expect(result.current).toEqual({ servers: [], isLoading: false }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments