|
| 1 | +import { jest } from "@jest/globals"; |
| 2 | +import { ApiClient } from "../../src/common/atlas/apiClient.js"; |
| 3 | +import { CommonProperties, TelemetryEvent, TelemetryResult } from "../../src/telemetry/types.js"; |
| 4 | + |
| 5 | +describe("ApiClient", () => { |
| 6 | + let apiClient: ApiClient; |
| 7 | + |
| 8 | + const mockEvents: TelemetryEvent<CommonProperties>[] = [ |
| 9 | + { |
| 10 | + timestamp: new Date().toISOString(), |
| 11 | + source: "mdbmcp", |
| 12 | + properties: { |
| 13 | + mcp_client_version: "1.0.0", |
| 14 | + mcp_client_name: "test-client", |
| 15 | + mcp_server_version: "1.0.0", |
| 16 | + mcp_server_name: "test-server", |
| 17 | + platform: "test-platform", |
| 18 | + arch: "test-arch", |
| 19 | + os_type: "test-os", |
| 20 | + component: "test-component", |
| 21 | + duration_ms: 100, |
| 22 | + result: "success" as TelemetryResult, |
| 23 | + category: "test-category", |
| 24 | + }, |
| 25 | + }, |
| 26 | + ]; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + apiClient = new ApiClient({ |
| 30 | + baseUrl: "https://api.test.com", |
| 31 | + credentials: { |
| 32 | + clientId: "test-client-id", |
| 33 | + clientSecret: "test-client-secret", |
| 34 | + }, |
| 35 | + userAgent: "test-user-agent", |
| 36 | + }); |
| 37 | + |
| 38 | + // @ts-expect-error accessing private property for testing |
| 39 | + apiClient.getAccessToken = jest.fn().mockResolvedValue("mockToken"); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + jest.clearAllMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("constructor", () => { |
| 47 | + it("should create a client with the correct configuration", () => { |
| 48 | + expect(apiClient).toBeDefined(); |
| 49 | + expect(apiClient.hasCredentials()).toBeDefined(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("listProjects", () => { |
| 54 | + it("should return a list of projects", async () => { |
| 55 | + const mockProjects = { |
| 56 | + results: [ |
| 57 | + { id: "1", name: "Project 1" }, |
| 58 | + { id: "2", name: "Project 2" }, |
| 59 | + ], |
| 60 | + totalCount: 2, |
| 61 | + }; |
| 62 | + |
| 63 | + const mockGet = jest.fn().mockImplementation(() => ({ |
| 64 | + data: mockProjects, |
| 65 | + error: null, |
| 66 | + response: new Response(), |
| 67 | + })); |
| 68 | + |
| 69 | + // @ts-expect-error accessing private property for testing |
| 70 | + apiClient.client.GET = mockGet; |
| 71 | + |
| 72 | + const result = await apiClient.listProjects(); |
| 73 | + |
| 74 | + expect(mockGet).toHaveBeenCalledWith("/api/atlas/v2/groups", undefined); |
| 75 | + expect(result).toEqual(mockProjects); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should throw an error when the API call fails", async () => { |
| 79 | + const mockError = { |
| 80 | + reason: "Test error", |
| 81 | + detail: "Something went wrong", |
| 82 | + }; |
| 83 | + |
| 84 | + const mockGet = jest.fn().mockImplementation(() => ({ |
| 85 | + data: null, |
| 86 | + error: mockError, |
| 87 | + response: new Response(), |
| 88 | + })); |
| 89 | + |
| 90 | + // @ts-expect-error accessing private property for testing |
| 91 | + apiClient.client.GET = mockGet; |
| 92 | + |
| 93 | + await expect(apiClient.listProjects()).rejects.toThrow(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("sendEvents", () => { |
| 98 | + it("should send events to authenticated endpoint when token is available", async () => { |
| 99 | + const mockFetch = jest.spyOn(global, "fetch"); |
| 100 | + mockFetch.mockResolvedValueOnce(new Response(null, { status: 200 })); |
| 101 | + |
| 102 | + await apiClient.sendEvents(mockEvents); |
| 103 | + |
| 104 | + const url = new URL("api/private/v1.0/telemetry/events", "https://api.test.com"); |
| 105 | + expect(mockFetch).toHaveBeenCalledWith(url, { |
| 106 | + method: "POST", |
| 107 | + headers: { |
| 108 | + "Content-Type": "application/json", |
| 109 | + Authorization: "Bearer mockToken", |
| 110 | + Accept: "application/json", |
| 111 | + "User-Agent": "test-user-agent", |
| 112 | + }, |
| 113 | + body: JSON.stringify(mockEvents), |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should fall back to unauthenticated endpoint when token is not available", async () => { |
| 118 | + const mockFetch = jest.spyOn(global, "fetch"); |
| 119 | + mockFetch.mockResolvedValueOnce(new Response(null, { status: 200 })); |
| 120 | + |
| 121 | + // @ts-expect-error accessing private property for testing |
| 122 | + apiClient.getAccessToken = jest.fn().mockResolvedValue(undefined); |
| 123 | + |
| 124 | + await apiClient.sendEvents(mockEvents); |
| 125 | + |
| 126 | + const url = new URL("api/private/unauth/telemetry/events", "https://api.test.com"); |
| 127 | + expect(mockFetch).toHaveBeenCalledWith(url, { |
| 128 | + method: "POST", |
| 129 | + headers: { |
| 130 | + "Content-Type": "application/json", |
| 131 | + Accept: "application/json", |
| 132 | + "User-Agent": "test-user-agent", |
| 133 | + }, |
| 134 | + body: JSON.stringify(mockEvents), |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should fall back to unauthenticated endpoint on 401 error", async () => { |
| 139 | + const mockFetch = jest.spyOn(global, "fetch"); |
| 140 | + mockFetch |
| 141 | + .mockResolvedValueOnce(new Response(null, { status: 401 })) |
| 142 | + .mockResolvedValueOnce(new Response(null, { status: 200 })); |
| 143 | + |
| 144 | + await apiClient.sendEvents(mockEvents); |
| 145 | + |
| 146 | + const url = new URL("api/private/unauth/telemetry/events", "https://api.test.com"); |
| 147 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 148 | + expect(mockFetch).toHaveBeenLastCalledWith(url, { |
| 149 | + method: "POST", |
| 150 | + headers: { |
| 151 | + "Content-Type": "application/json", |
| 152 | + Accept: "application/json", |
| 153 | + "User-Agent": "test-user-agent", |
| 154 | + }, |
| 155 | + body: JSON.stringify(mockEvents), |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should throw error when both authenticated and unauthenticated requests fail", async () => { |
| 160 | + const mockFetch = jest.spyOn(global, "fetch"); |
| 161 | + mockFetch |
| 162 | + .mockResolvedValueOnce(new Response(null, { status: 401 })) |
| 163 | + .mockResolvedValueOnce(new Response(null, { status: 500 })); |
| 164 | + |
| 165 | + const mockToken = "test-token"; |
| 166 | + // @ts-expect-error accessing private property for testing |
| 167 | + apiClient.getAccessToken = jest.fn().mockResolvedValue(mockToken); |
| 168 | + |
| 169 | + await expect(apiClient.sendEvents(mockEvents)).rejects.toThrow(); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments