|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mockFetch = vi.hoisted(() => vi.fn()); |
| 4 | + |
| 5 | +vi.mock("../../utils/logger.js", () => ({ |
| 6 | + logger: { |
| 7 | + scope: () => ({ |
| 8 | + info: vi.fn(), |
| 9 | + error: vi.fn(), |
| 10 | + warn: vi.fn(), |
| 11 | + debug: vi.fn(), |
| 12 | + }), |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("@posthog/agent/posthog-api", () => ({ |
| 17 | + getLlmGatewayUrl: vi.fn(() => "https://gateway.example.com"), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.stubGlobal("fetch", mockFetch); |
| 21 | + |
| 22 | +import { AgentAuthAdapter } from "./auth-adapter"; |
| 23 | + |
| 24 | +const baseCredentials = { |
| 25 | + apiHost: "https://app.posthog.com", |
| 26 | + projectId: 1, |
| 27 | +}; |
| 28 | + |
| 29 | +function createDependencies() { |
| 30 | + return { |
| 31 | + authService: { |
| 32 | + getValidAccessToken: vi.fn().mockResolvedValue({ |
| 33 | + accessToken: "test-access-token", |
| 34 | + apiHost: "https://app.posthog.com", |
| 35 | + }), |
| 36 | + refreshAccessToken: vi.fn().mockResolvedValue({ |
| 37 | + accessToken: "fresh-access-token", |
| 38 | + apiHost: "https://app.posthog.com", |
| 39 | + }), |
| 40 | + authenticatedFetch: vi |
| 41 | + .fn() |
| 42 | + .mockImplementation( |
| 43 | + async ( |
| 44 | + fetchImpl: typeof fetch, |
| 45 | + input: string | Request, |
| 46 | + init?: RequestInit, |
| 47 | + ) => fetchImpl(input, init), |
| 48 | + ), |
| 49 | + }, |
| 50 | + authProxy: { |
| 51 | + start: vi.fn().mockResolvedValue("http://127.0.0.1:9999"), |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +describe("AgentAuthAdapter", () => { |
| 57 | + let adapter: AgentAuthAdapter; |
| 58 | + let deps: ReturnType<typeof createDependencies>; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + vi.clearAllMocks(); |
| 62 | + mockFetch.mockResolvedValue({ |
| 63 | + ok: true, |
| 64 | + json: () => Promise.resolve({ results: [] }), |
| 65 | + }); |
| 66 | + |
| 67 | + deps = createDependencies(); |
| 68 | + adapter = new AgentAuthAdapter( |
| 69 | + deps.authService as never, |
| 70 | + deps.authProxy as never, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(() => { |
| 75 | + vi.restoreAllMocks(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("builds the default PostHog MCP server", async () => { |
| 79 | + const servers = await adapter.buildMcpServers(baseCredentials); |
| 80 | + |
| 81 | + expect(servers).toEqual( |
| 82 | + expect.arrayContaining([ |
| 83 | + expect.objectContaining({ |
| 84 | + name: "posthog", |
| 85 | + type: "http", |
| 86 | + url: "https://mcp.posthog.com/mcp", |
| 87 | + headers: expect.arrayContaining([ |
| 88 | + { |
| 89 | + name: "Authorization", |
| 90 | + value: "Bearer test-access-token", |
| 91 | + }, |
| 92 | + ]), |
| 93 | + }), |
| 94 | + ]), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("includes enabled user-installed MCP servers from backend", async () => { |
| 99 | + mockFetch.mockResolvedValue({ |
| 100 | + ok: true, |
| 101 | + json: () => |
| 102 | + Promise.resolve({ |
| 103 | + results: [ |
| 104 | + { |
| 105 | + id: "inst-1", |
| 106 | + url: "https://custom-mcp.example.com", |
| 107 | + proxy_url: "https://proxy.posthog.com/inst-1/", |
| 108 | + name: "custom-server", |
| 109 | + display_name: "Custom Server", |
| 110 | + auth_type: "none", |
| 111 | + is_enabled: true, |
| 112 | + pending_oauth: false, |
| 113 | + needs_reauth: false, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }), |
| 117 | + }); |
| 118 | + |
| 119 | + const servers = await adapter.buildMcpServers(baseCredentials); |
| 120 | + |
| 121 | + expect(servers).toEqual( |
| 122 | + expect.arrayContaining([ |
| 123 | + expect.objectContaining({ |
| 124 | + name: "custom-server", |
| 125 | + url: "https://custom-mcp.example.com", |
| 126 | + headers: [], |
| 127 | + }), |
| 128 | + ]), |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it("routes authenticated installed MCP servers through the proxy URL", async () => { |
| 133 | + mockFetch.mockResolvedValue({ |
| 134 | + ok: true, |
| 135 | + json: () => |
| 136 | + Promise.resolve({ |
| 137 | + results: [ |
| 138 | + { |
| 139 | + id: "inst-2", |
| 140 | + url: "https://remote-mcp.example.com", |
| 141 | + proxy_url: "https://proxy.posthog.com/inst-2/", |
| 142 | + name: "secure-server", |
| 143 | + display_name: "Secure Server", |
| 144 | + auth_type: "oauth", |
| 145 | + is_enabled: true, |
| 146 | + pending_oauth: false, |
| 147 | + needs_reauth: false, |
| 148 | + }, |
| 149 | + ], |
| 150 | + }), |
| 151 | + }); |
| 152 | + |
| 153 | + const servers = await adapter.buildMcpServers(baseCredentials); |
| 154 | + |
| 155 | + expect(servers).toEqual( |
| 156 | + expect.arrayContaining([ |
| 157 | + expect.objectContaining({ |
| 158 | + name: "secure-server", |
| 159 | + url: "https://proxy.posthog.com/inst-2/", |
| 160 | + headers: [ |
| 161 | + { name: "Authorization", value: "Bearer test-access-token" }, |
| 162 | + ], |
| 163 | + }), |
| 164 | + ]), |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it("configures environment using the gateway proxy and current token", async () => { |
| 169 | + await adapter.configureProcessEnv({ |
| 170 | + credentials: baseCredentials, |
| 171 | + mockNodeDir: "/mock/node", |
| 172 | + proxyUrl: "http://127.0.0.1:9999", |
| 173 | + claudeCliPath: "/mock/claude-cli.js", |
| 174 | + }); |
| 175 | + |
| 176 | + expect(process.env.POSTHOG_API_KEY).toBe("test-access-token"); |
| 177 | + expect(process.env.POSTHOG_AUTH_HEADER).toBe("Bearer test-access-token"); |
| 178 | + expect(process.env.LLM_GATEWAY_URL).toBe("http://127.0.0.1:9999"); |
| 179 | + expect(process.env.CLAUDE_CODE_EXECUTABLE).toBe("/mock/claude-cli.js"); |
| 180 | + expect(process.env.POSTHOG_PROJECT_ID).toBe("1"); |
| 181 | + }); |
| 182 | +}); |
0 commit comments