|
| 1 | +import axios from "axios" |
| 2 | +import { getLiteLLMModels } from "../litellm" |
| 3 | +import { COMPUTER_USE_MODELS } from "../../../../shared/api" |
| 4 | + |
| 5 | +// Mock axios |
| 6 | +jest.mock("axios") |
| 7 | +const mockedAxios = axios as jest.Mocked<typeof axios> |
| 8 | + |
| 9 | +const DUMMY_INVALID_KEY = "invalid-key-for-testing" |
| 10 | + |
| 11 | +describe("getLiteLLMModels", () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks() |
| 14 | + }) |
| 15 | + |
| 16 | + it("successfully fetches and formats LiteLLM models", async () => { |
| 17 | + const mockResponse = { |
| 18 | + data: { |
| 19 | + data: [ |
| 20 | + { |
| 21 | + model_name: "claude-3-5-sonnet", |
| 22 | + model_info: { |
| 23 | + max_tokens: 4096, |
| 24 | + max_input_tokens: 200000, |
| 25 | + supports_vision: true, |
| 26 | + supports_prompt_caching: false, |
| 27 | + input_cost_per_token: 0.000003, |
| 28 | + output_cost_per_token: 0.000015, |
| 29 | + }, |
| 30 | + litellm_params: { |
| 31 | + model: "anthropic/claude-3.5-sonnet", |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + model_name: "gpt-4-turbo", |
| 36 | + model_info: { |
| 37 | + max_tokens: 8192, |
| 38 | + max_input_tokens: 128000, |
| 39 | + supports_vision: false, |
| 40 | + supports_prompt_caching: false, |
| 41 | + input_cost_per_token: 0.00001, |
| 42 | + output_cost_per_token: 0.00003, |
| 43 | + }, |
| 44 | + litellm_params: { |
| 45 | + model: "openai/gpt-4-turbo", |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 53 | + |
| 54 | + const result = await getLiteLLMModels("test-api-key", "http://localhost:4000") |
| 55 | + |
| 56 | + expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/v1/model/info", { |
| 57 | + headers: { |
| 58 | + Authorization: "Bearer test-api-key", |
| 59 | + "Content-Type": "application/json", |
| 60 | + }, |
| 61 | + timeout: 5000, |
| 62 | + }) |
| 63 | + |
| 64 | + expect(result).toEqual({ |
| 65 | + "claude-3-5-sonnet": { |
| 66 | + maxTokens: 4096, |
| 67 | + contextWindow: 200000, |
| 68 | + supportsImages: true, |
| 69 | + supportsComputerUse: true, |
| 70 | + supportsPromptCache: false, |
| 71 | + inputPrice: 3, |
| 72 | + outputPrice: 15, |
| 73 | + description: "claude-3-5-sonnet via LiteLLM proxy", |
| 74 | + }, |
| 75 | + "gpt-4-turbo": { |
| 76 | + maxTokens: 8192, |
| 77 | + contextWindow: 128000, |
| 78 | + supportsImages: false, |
| 79 | + supportsComputerUse: false, |
| 80 | + supportsPromptCache: false, |
| 81 | + inputPrice: 10, |
| 82 | + outputPrice: 30, |
| 83 | + description: "gpt-4-turbo via LiteLLM proxy", |
| 84 | + }, |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + it("makes request without authorization header when no API key provided", async () => { |
| 89 | + const mockResponse = { |
| 90 | + data: { |
| 91 | + data: [], |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 96 | + |
| 97 | + await getLiteLLMModels("", "http://localhost:4000") |
| 98 | + |
| 99 | + expect(mockedAxios.get).toHaveBeenCalledWith("http://localhost:4000/v1/model/info", { |
| 100 | + headers: { |
| 101 | + "Content-Type": "application/json", |
| 102 | + }, |
| 103 | + timeout: 5000, |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it("handles computer use models correctly", async () => { |
| 108 | + const computerUseModel = Array.from(COMPUTER_USE_MODELS)[0] |
| 109 | + const mockResponse = { |
| 110 | + data: { |
| 111 | + data: [ |
| 112 | + { |
| 113 | + model_name: "test-computer-model", |
| 114 | + model_info: { |
| 115 | + max_tokens: 4096, |
| 116 | + max_input_tokens: 200000, |
| 117 | + supports_vision: true, |
| 118 | + }, |
| 119 | + litellm_params: { |
| 120 | + model: `anthropic/${computerUseModel}`, |
| 121 | + }, |
| 122 | + }, |
| 123 | + ], |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 128 | + |
| 129 | + const result = await getLiteLLMModels("test-api-key", "http://localhost:4000") |
| 130 | + |
| 131 | + expect(result["test-computer-model"]).toEqual({ |
| 132 | + maxTokens: 4096, |
| 133 | + contextWindow: 200000, |
| 134 | + supportsImages: true, |
| 135 | + supportsComputerUse: true, |
| 136 | + supportsPromptCache: false, |
| 137 | + inputPrice: undefined, |
| 138 | + outputPrice: undefined, |
| 139 | + description: "test-computer-model via LiteLLM proxy", |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it("throws error for unexpected response format", async () => { |
| 144 | + const mockResponse = { |
| 145 | + data: { |
| 146 | + // Missing 'data' field |
| 147 | + models: [], |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 152 | + |
| 153 | + await expect(getLiteLLMModels("test-api-key", "http://localhost:4000")).rejects.toThrow( |
| 154 | + "Failed to fetch LiteLLM models: Unexpected response format.", |
| 155 | + ) |
| 156 | + }) |
| 157 | + |
| 158 | + it("throws detailed error for HTTP error responses", async () => { |
| 159 | + const axiosError = { |
| 160 | + response: { |
| 161 | + status: 401, |
| 162 | + statusText: "Unauthorized", |
| 163 | + }, |
| 164 | + isAxiosError: true, |
| 165 | + } |
| 166 | + |
| 167 | + mockedAxios.isAxiosError.mockReturnValue(true) |
| 168 | + mockedAxios.get.mockRejectedValue(axiosError) |
| 169 | + |
| 170 | + await expect(getLiteLLMModels(DUMMY_INVALID_KEY, "http://localhost:4000")).rejects.toThrow( |
| 171 | + "Failed to fetch LiteLLM models: 401 Unauthorized. Check base URL and API key.", |
| 172 | + ) |
| 173 | + }) |
| 174 | + |
| 175 | + it("throws network error for request failures", async () => { |
| 176 | + const axiosError = { |
| 177 | + request: {}, |
| 178 | + isAxiosError: true, |
| 179 | + } |
| 180 | + |
| 181 | + mockedAxios.isAxiosError.mockReturnValue(true) |
| 182 | + mockedAxios.get.mockRejectedValue(axiosError) |
| 183 | + |
| 184 | + await expect(getLiteLLMModels("test-api-key", "http://invalid-url")).rejects.toThrow( |
| 185 | + "Failed to fetch LiteLLM models: No response from server. Check LiteLLM server status and base URL.", |
| 186 | + ) |
| 187 | + }) |
| 188 | + |
| 189 | + it("throws generic error for other failures", async () => { |
| 190 | + const genericError = new Error("Network timeout") |
| 191 | + |
| 192 | + mockedAxios.isAxiosError.mockReturnValue(false) |
| 193 | + mockedAxios.get.mockRejectedValue(genericError) |
| 194 | + |
| 195 | + await expect(getLiteLLMModels("test-api-key", "http://localhost:4000")).rejects.toThrow( |
| 196 | + "Failed to fetch LiteLLM models: Network timeout", |
| 197 | + ) |
| 198 | + }) |
| 199 | + |
| 200 | + it("handles timeout parameter correctly", async () => { |
| 201 | + const mockResponse = { data: { data: [] } } |
| 202 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 203 | + |
| 204 | + await getLiteLLMModels("test-api-key", "http://localhost:4000") |
| 205 | + |
| 206 | + expect(mockedAxios.get).toHaveBeenCalledWith( |
| 207 | + "http://localhost:4000/v1/model/info", |
| 208 | + expect.objectContaining({ |
| 209 | + timeout: 5000, |
| 210 | + }), |
| 211 | + ) |
| 212 | + }) |
| 213 | + |
| 214 | + it("returns empty object when data array is empty", async () => { |
| 215 | + const mockResponse = { |
| 216 | + data: { |
| 217 | + data: [], |
| 218 | + }, |
| 219 | + } |
| 220 | + |
| 221 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 222 | + |
| 223 | + const result = await getLiteLLMModels("test-api-key", "http://localhost:4000") |
| 224 | + |
| 225 | + expect(result).toEqual({}) |
| 226 | + }) |
| 227 | +}) |
0 commit comments