|
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | +import { |
| 3 | + handleListAllocationsRequest, |
| 4 | + handleGetAllocationRequest, |
| 5 | +} from "../allocations.js"; |
| 6 | +import { |
| 7 | + createErrorResponse, |
| 8 | + createSuccessResponse, |
| 9 | + formatZodError, |
| 10 | + handleGeneralError, |
| 11 | + makeDoitRequest, |
| 12 | +} from "../../utils/util.js"; |
| 13 | + |
| 14 | +// Mock the utility functions |
| 15 | +vi.mock("../../utils/util.js", () => ({ |
| 16 | + createErrorResponse: vi.fn((msg) => ({ |
| 17 | + content: [{ type: "text", text: msg }], |
| 18 | + })), |
| 19 | + createSuccessResponse: vi.fn((text) => ({ |
| 20 | + content: [{ type: "text", text }], |
| 21 | + })), |
| 22 | + formatZodError: vi.fn((error) => `Formatted Zod Error: ${error.message}`), |
| 23 | + handleGeneralError: vi.fn((error, context) => ({ |
| 24 | + content: [{ type: "text", text: `General Error: ${context}` }], |
| 25 | + })), |
| 26 | + makeDoitRequest: vi.fn(), |
| 27 | + DOIT_API_BASE: "https://api.doit.com", |
| 28 | +})); |
| 29 | + |
| 30 | +describe("allocations", () => { |
| 31 | + const mockToken = "fake-token"; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("handleListAllocationsRequest", () => { |
| 38 | + it("should call makeDoitRequest with correct parameters and return success response", async () => { |
| 39 | + const mockArgs = { pageToken: "next-page" }; |
| 40 | + const mockApiResponse = { |
| 41 | + pageToken: "another-page", |
| 42 | + allocations: [ |
| 43 | + { |
| 44 | + id: "allocation-1", |
| 45 | + name: "Test Allocation", |
| 46 | + |
| 47 | + type: "standard", |
| 48 | + allocationType: "single", |
| 49 | + createTime: 1678886400000, |
| 50 | + updateTime: 1678972800000, |
| 51 | + urlUI: "https://console.doit.com/allocations/allocation-1", |
| 52 | + }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 56 | + |
| 57 | + const response = await handleListAllocationsRequest(mockArgs, mockToken); |
| 58 | + |
| 59 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 60 | + "https://api.doit.com/analytics/v1/allocations?pageToken=next-page", |
| 61 | + mockToken, |
| 62 | + { |
| 63 | + method: "GET", |
| 64 | + } |
| 65 | + ); |
| 66 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 67 | + expect.stringContaining("allocation-1") |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should handle request without pageToken", async () => { |
| 72 | + const mockArgs = {}; |
| 73 | + const mockApiResponse = { |
| 74 | + allocations: [ |
| 75 | + { |
| 76 | + id: "allocation-1", |
| 77 | + name: "Test Allocation", |
| 78 | + |
| 79 | + type: "standard", |
| 80 | + allocationType: "single", |
| 81 | + createTime: 1678886400000, |
| 82 | + updateTime: 1678972800000, |
| 83 | + urlUI: "https://console.doit.com/allocations/allocation-1", |
| 84 | + }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 88 | + |
| 89 | + const response = await handleListAllocationsRequest(mockArgs, mockToken); |
| 90 | + |
| 91 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 92 | + "https://api.doit.com/analytics/v1/allocations", |
| 93 | + mockToken, |
| 94 | + { |
| 95 | + method: "GET", |
| 96 | + } |
| 97 | + ); |
| 98 | + expect(createSuccessResponse).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should handle no allocations found", async () => { |
| 102 | + const mockArgs = {}; |
| 103 | + const mockApiResponse = { |
| 104 | + allocations: [], |
| 105 | + }; |
| 106 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 107 | + |
| 108 | + const response = await handleListAllocationsRequest(mockArgs, mockToken); |
| 109 | + |
| 110 | + expect(createErrorResponse).toHaveBeenCalledWith("No allocations found"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should handle API request failure", async () => { |
| 114 | + const mockArgs = {}; |
| 115 | + (makeDoitRequest as vi.Mock).mockResolvedValue(null); |
| 116 | + |
| 117 | + const response = await handleListAllocationsRequest(mockArgs, mockToken); |
| 118 | + |
| 119 | + expect(createErrorResponse).toHaveBeenCalledWith( |
| 120 | + "Failed to retrieve allocations data" |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should handle makeDoitRequest throwing an error", async () => { |
| 125 | + const mockArgs = {}; |
| 126 | + (makeDoitRequest as vi.Mock).mockRejectedValue( |
| 127 | + new Error("Network error") |
| 128 | + ); |
| 129 | + |
| 130 | + const response = await handleListAllocationsRequest(mockArgs, mockToken); |
| 131 | + |
| 132 | + expect(handleGeneralError).toHaveBeenCalledWith( |
| 133 | + expect.any(Error), |
| 134 | + "making DoiT API request" |
| 135 | + ); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe("handleGetAllocationRequest", () => { |
| 140 | + it("should call makeDoitRequest with correct parameters and return success response", async () => { |
| 141 | + const mockArgs = { id: "allocation-123" }; |
| 142 | + const mockApiResponse = { |
| 143 | + id: "allocation-123", |
| 144 | + name: "Test Allocation", |
| 145 | + description: "A test allocation", |
| 146 | + type: "standard", |
| 147 | + allocationType: "single", |
| 148 | + createTime: 1678886400000, |
| 149 | + updateTime: 1678972800000, |
| 150 | + anomalyDetection: true, |
| 151 | + rule: { |
| 152 | + components: [ |
| 153 | + { |
| 154 | + key: "project_id", |
| 155 | + type: "fixed", |
| 156 | + values: ["project-1", "project-2"], |
| 157 | + inverse_selection: false, |
| 158 | + include_null: false, |
| 159 | + mode: "is", |
| 160 | + }, |
| 161 | + ], |
| 162 | + formula: "A AND B", |
| 163 | + }, |
| 164 | + }; |
| 165 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 166 | + |
| 167 | + const response = await handleGetAllocationRequest(mockArgs, mockToken); |
| 168 | + |
| 169 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 170 | + "https://api.doit.com/analytics/v1/allocations/allocation-123", |
| 171 | + mockToken, |
| 172 | + { |
| 173 | + method: "GET", |
| 174 | + } |
| 175 | + ); |
| 176 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 177 | + expect.stringContaining("allocation-123") |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it("should handle missing allocation ID", async () => { |
| 182 | + const mockArgs = {}; |
| 183 | + |
| 184 | + const response = await handleGetAllocationRequest(mockArgs, mockToken); |
| 185 | + |
| 186 | + expect(createErrorResponse).toHaveBeenCalledWith( |
| 187 | + expect.stringContaining("Formatted Zod Error") |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it("should handle API request failure", async () => { |
| 192 | + const mockArgs = { id: "allocation-123" }; |
| 193 | + (makeDoitRequest as vi.Mock).mockResolvedValue(null); |
| 194 | + |
| 195 | + const response = await handleGetAllocationRequest(mockArgs, mockToken); |
| 196 | + |
| 197 | + expect(createErrorResponse).toHaveBeenCalledWith( |
| 198 | + "Failed to retrieve allocation data" |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it("should handle makeDoitRequest throwing an error", async () => { |
| 203 | + const mockArgs = { id: "allocation-123" }; |
| 204 | + (makeDoitRequest as vi.Mock).mockRejectedValue( |
| 205 | + new Error("Network error") |
| 206 | + ); |
| 207 | + |
| 208 | + const response = await handleGetAllocationRequest(mockArgs, mockToken); |
| 209 | + |
| 210 | + expect(handleGeneralError).toHaveBeenCalledWith( |
| 211 | + expect.any(Error), |
| 212 | + "making DoiT API request" |
| 213 | + ); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should handle Zod validation errors", async () => { |
| 217 | + const mockArgs = { id: 123 }; // Invalid type (should be string) |
| 218 | + |
| 219 | + const response = await handleGetAllocationRequest(mockArgs, mockToken); |
| 220 | + |
| 221 | + expect(response).toEqual({ |
| 222 | + content: [ |
| 223 | + { |
| 224 | + type: "text", |
| 225 | + text: expect.stringContaining("Formatted Zod Error"), |
| 226 | + }, |
| 227 | + ], |
| 228 | + }); |
| 229 | + }); |
| 230 | + }); |
| 231 | +}); |
0 commit comments