|
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | +import { handleListAssetsRequest, formatAsset } from "../assets.js"; |
| 3 | +import { |
| 4 | + createErrorResponse, |
| 5 | + createSuccessResponse, |
| 6 | + formatZodError, |
| 7 | + handleGeneralError, |
| 8 | + makeDoitRequest, |
| 9 | +} from "../../utils/util.js"; |
| 10 | + |
| 11 | +// Mock the utility functions |
| 12 | +vi.mock("../../utils/util.js", () => ({ |
| 13 | + createErrorResponse: vi.fn((msg) => ({ |
| 14 | + content: [{ type: "text", text: msg }], |
| 15 | + })), |
| 16 | + createSuccessResponse: vi.fn((text) => ({ |
| 17 | + content: [{ type: "text", text }], |
| 18 | + })), |
| 19 | + formatZodError: vi.fn((error) => `Formatted Zod Error: ${error.message}`), |
| 20 | + handleGeneralError: vi.fn((error, context) => ({ |
| 21 | + content: [{ type: "text", text: `General Error: ${context}` }], |
| 22 | + })), |
| 23 | + makeDoitRequest: vi.fn(), |
| 24 | + DOIT_API_BASE: "https://api.doit.com", |
| 25 | +})); |
| 26 | + |
| 27 | +describe("assets", () => { |
| 28 | + describe("formatAsset", () => { |
| 29 | + it("should format an asset object correctly", () => { |
| 30 | + const mockAsset = { |
| 31 | + createTime: 1640995200, |
| 32 | + id: "asset-123", |
| 33 | + name: "Test Asset", |
| 34 | + quantity: 5, |
| 35 | + type: "billing_account", |
| 36 | + url: "https://console.cloud.google.com/billing/123", |
| 37 | + }; |
| 38 | + |
| 39 | + const expected = `ID: asset-123 |
| 40 | +Name: Test Asset |
| 41 | +Type: billing_account |
| 42 | +Quantity: 5 |
| 43 | +URL: https://console.cloud.google.com/billing/123 |
| 44 | +Created: 2022-01-01T00:00:00.000Z |
| 45 | +-----------`; |
| 46 | + |
| 47 | + expect(formatAsset(mockAsset)).toBe(expected); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe("handleListAssetsRequest", () => { |
| 52 | + const mockToken = "fake-token"; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should call makeDoitRequest with correct parameters and return success response", async () => { |
| 59 | + const mockArgs = { pageToken: "next-page" }; |
| 60 | + const mockApiResponse = { |
| 61 | + assets: [ |
| 62 | + { |
| 63 | + createTime: 1640995200, |
| 64 | + id: "asset-123", |
| 65 | + name: "Test Asset", |
| 66 | + quantity: 5, |
| 67 | + type: "billing_account", |
| 68 | + url: "https://console.cloud.google.com/billing/123", |
| 69 | + }, |
| 70 | + ], |
| 71 | + pageToken: "another-page", |
| 72 | + rowCount: 1, |
| 73 | + }; |
| 74 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 75 | + |
| 76 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 77 | + |
| 78 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 79 | + "https://api.doit.com/billing/v1/assets?pageToken=next-page", |
| 80 | + mockToken, |
| 81 | + { method: "GET", customerContext: undefined } |
| 82 | + ); |
| 83 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 84 | + expect.stringContaining("Found 1 assets:") |
| 85 | + ); |
| 86 | + expect(response).toEqual({ |
| 87 | + content: [ |
| 88 | + { type: "text", text: expect.stringContaining("Found 1 assets:") }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should handle no assets found", async () => { |
| 94 | + const mockArgs = {}; |
| 95 | + const mockApiResponse = { |
| 96 | + assets: [], |
| 97 | + pageToken: "", |
| 98 | + rowCount: 0, |
| 99 | + }; |
| 100 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 101 | + |
| 102 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 103 | + |
| 104 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 105 | + "https://api.doit.com/billing/v1/assets", |
| 106 | + mockToken, |
| 107 | + { method: "GET", customerContext: undefined } |
| 108 | + ); |
| 109 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 110 | + "No assets found for this customer context." |
| 111 | + ); |
| 112 | + expect(response).toEqual({ |
| 113 | + content: [ |
| 114 | + { |
| 115 | + type: "text", |
| 116 | + text: "No assets found for this customer context.", |
| 117 | + }, |
| 118 | + ], |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should handle API request failure", async () => { |
| 123 | + const mockArgs = {}; |
| 124 | + (makeDoitRequest as vi.Mock).mockResolvedValue(null); |
| 125 | + |
| 126 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 127 | + |
| 128 | + expect(makeDoitRequest).toHaveBeenCalledWith( |
| 129 | + "https://api.doit.com/billing/v1/assets", |
| 130 | + mockToken, |
| 131 | + { method: "GET", customerContext: undefined } |
| 132 | + ); |
| 133 | + expect(createErrorResponse).toHaveBeenCalledWith( |
| 134 | + "Failed to retrieve assets data" |
| 135 | + ); |
| 136 | + expect(response).toEqual({ |
| 137 | + content: [{ type: "text", text: "Failed to retrieve assets data" }], |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should handle ZodError for invalid arguments", async () => { |
| 142 | + const mockArgs = { pageToken: 123 }; // Invalid pageToken type |
| 143 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 144 | + |
| 145 | + expect(formatZodError).toHaveBeenCalled(); |
| 146 | + expect(createErrorResponse).toHaveBeenCalled(); |
| 147 | + expect(response).toEqual({ |
| 148 | + content: [ |
| 149 | + { |
| 150 | + type: "text", |
| 151 | + text: expect.stringContaining("Formatted Zod Error:"), |
| 152 | + }, |
| 153 | + ], |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it("should handle general errors", async () => { |
| 158 | + const mockArgs = {}; |
| 159 | + (makeDoitRequest as vi.Mock).mockRejectedValue( |
| 160 | + new Error("Network error") |
| 161 | + ); |
| 162 | + |
| 163 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 164 | + |
| 165 | + expect(handleGeneralError).toHaveBeenCalledWith( |
| 166 | + expect.any(Error), |
| 167 | + "making DoiT API request" |
| 168 | + ); |
| 169 | + expect(response).toEqual({ |
| 170 | + content: [ |
| 171 | + { type: "text", text: "General Error: making DoiT API request" }, |
| 172 | + ], |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should include page token in response when provided", async () => { |
| 177 | + const mockArgs = {}; |
| 178 | + const mockApiResponse = { |
| 179 | + assets: [ |
| 180 | + { |
| 181 | + createTime: 1640995200, |
| 182 | + id: "asset-123", |
| 183 | + name: "Test Asset", |
| 184 | + quantity: 5, |
| 185 | + type: "billing_account", |
| 186 | + url: "https://console.cloud.google.com/billing/123", |
| 187 | + }, |
| 188 | + ], |
| 189 | + pageToken: "next-page-token", |
| 190 | + rowCount: 1, |
| 191 | + }; |
| 192 | + (makeDoitRequest as vi.Mock).mockResolvedValue(mockApiResponse); |
| 193 | + |
| 194 | + const response = await handleListAssetsRequest(mockArgs, mockToken); |
| 195 | + |
| 196 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 197 | + expect.stringContaining("Page token: next-page-token") |
| 198 | + ); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments