|
| 1 | +// Mocks must come first, before imports |
| 2 | +vi.mock("axios") |
| 3 | + |
| 4 | +import type { Mock } from "vitest" |
| 5 | +import type { ModelInfo } from "@roo-code/types" |
| 6 | +import axios from "axios" |
| 7 | +import { getChutesModels } from "../chutes" |
| 8 | +import { chutesModels } from "@roo-code/types" |
| 9 | + |
| 10 | +const mockedAxios = axios as typeof axios & { |
| 11 | + get: Mock |
| 12 | +} |
| 13 | + |
| 14 | +describe("getChutesModels", () => { |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks() |
| 17 | + }) |
| 18 | + |
| 19 | + it("should fetch and parse models successfully", async () => { |
| 20 | + const mockResponse = { |
| 21 | + data: { |
| 22 | + data: [ |
| 23 | + { |
| 24 | + id: "test/new-model", |
| 25 | + object: "model", |
| 26 | + owned_by: "test", |
| 27 | + created: 1234567890, |
| 28 | + context_length: 128000, |
| 29 | + max_model_len: 8192, |
| 30 | + input_modalities: ["text"], |
| 31 | + }, |
| 32 | + ], |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 37 | + |
| 38 | + const models = await getChutesModels("test-api-key") |
| 39 | + |
| 40 | + expect(mockedAxios.get).toHaveBeenCalledWith( |
| 41 | + "https://llm.chutes.ai/v1/models", |
| 42 | + expect.objectContaining({ |
| 43 | + headers: expect.objectContaining({ |
| 44 | + Authorization: "Bearer test-api-key", |
| 45 | + }), |
| 46 | + }), |
| 47 | + ) |
| 48 | + |
| 49 | + expect(models["test/new-model"]).toEqual({ |
| 50 | + maxTokens: 8192, |
| 51 | + contextWindow: 128000, |
| 52 | + supportsImages: false, |
| 53 | + supportsPromptCache: false, |
| 54 | + supportsNativeTools: false, |
| 55 | + inputPrice: 0, |
| 56 | + outputPrice: 0, |
| 57 | + description: "Chutes AI model: test/new-model", |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + it("should override hardcoded models with dynamic API data", async () => { |
| 62 | + // Find any hardcoded model |
| 63 | + const [modelId] = Object.entries(chutesModels)[0] |
| 64 | + |
| 65 | + const mockResponse = { |
| 66 | + data: { |
| 67 | + data: [ |
| 68 | + { |
| 69 | + id: modelId, |
| 70 | + object: "model", |
| 71 | + owned_by: "test", |
| 72 | + created: 1234567890, |
| 73 | + context_length: 200000, // Different from hardcoded |
| 74 | + max_model_len: 10000, // Different from hardcoded |
| 75 | + input_modalities: ["text", "image"], |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 82 | + |
| 83 | + const models = await getChutesModels("test-api-key") |
| 84 | + |
| 85 | + // Dynamic values should override hardcoded |
| 86 | + expect(models[modelId]).toBeDefined() |
| 87 | + expect(models[modelId].contextWindow).toBe(200000) |
| 88 | + expect(models[modelId].maxTokens).toBe(10000) |
| 89 | + expect(models[modelId].supportsImages).toBe(true) |
| 90 | + }) |
| 91 | + |
| 92 | + it("should return hardcoded models when API returns empty", async () => { |
| 93 | + const mockResponse = { |
| 94 | + data: { |
| 95 | + data: [], |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 100 | + |
| 101 | + const models = await getChutesModels("test-api-key") |
| 102 | + |
| 103 | + // Should still have hardcoded models |
| 104 | + expect(Object.keys(models).length).toBeGreaterThan(0) |
| 105 | + expect(models).toEqual(expect.objectContaining(chutesModels)) |
| 106 | + }) |
| 107 | + |
| 108 | + it("should return hardcoded models on API error", async () => { |
| 109 | + mockedAxios.get.mockRejectedValue(new Error("Network error")) |
| 110 | + |
| 111 | + const models = await getChutesModels("test-api-key") |
| 112 | + |
| 113 | + // Should still have hardcoded models |
| 114 | + expect(Object.keys(models).length).toBeGreaterThan(0) |
| 115 | + expect(models).toEqual(chutesModels) |
| 116 | + }) |
| 117 | + |
| 118 | + it("should work without API key", async () => { |
| 119 | + const mockResponse = { |
| 120 | + data: { |
| 121 | + data: [], |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 126 | + |
| 127 | + const models = await getChutesModels() |
| 128 | + |
| 129 | + expect(mockedAxios.get).toHaveBeenCalledWith( |
| 130 | + "https://llm.chutes.ai/v1/models", |
| 131 | + expect.objectContaining({ |
| 132 | + headers: expect.not.objectContaining({ |
| 133 | + Authorization: expect.anything(), |
| 134 | + }), |
| 135 | + }), |
| 136 | + ) |
| 137 | + |
| 138 | + expect(Object.keys(models).length).toBeGreaterThan(0) |
| 139 | + }) |
| 140 | + |
| 141 | + it("should detect image support from input_modalities", async () => { |
| 142 | + const mockResponse = { |
| 143 | + data: { |
| 144 | + data: [ |
| 145 | + { |
| 146 | + id: "test/image-model", |
| 147 | + object: "model", |
| 148 | + owned_by: "test", |
| 149 | + created: 1234567890, |
| 150 | + context_length: 128000, |
| 151 | + max_model_len: 8192, |
| 152 | + input_modalities: ["text", "image"], |
| 153 | + }, |
| 154 | + ], |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 159 | + |
| 160 | + const models = await getChutesModels("test-api-key") |
| 161 | + |
| 162 | + expect(models["test/image-model"].supportsImages).toBe(true) |
| 163 | + }) |
| 164 | + |
| 165 | + it("should detect native tool support from supported_features", async () => { |
| 166 | + const mockResponse = { |
| 167 | + data: { |
| 168 | + data: [ |
| 169 | + { |
| 170 | + id: "test/tools-model", |
| 171 | + object: "model", |
| 172 | + owned_by: "test", |
| 173 | + created: 1234567890, |
| 174 | + context_length: 128000, |
| 175 | + max_model_len: 8192, |
| 176 | + input_modalities: ["text"], |
| 177 | + supported_features: ["json_mode", "tools", "reasoning"], |
| 178 | + }, |
| 179 | + ], |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 184 | + |
| 185 | + const models = await getChutesModels("test-api-key") |
| 186 | + |
| 187 | + expect(models["test/tools-model"].supportsNativeTools).toBe(true) |
| 188 | + }) |
| 189 | + |
| 190 | + it("should not enable native tool support when tools is not in supported_features", async () => { |
| 191 | + const mockResponse = { |
| 192 | + data: { |
| 193 | + data: [ |
| 194 | + { |
| 195 | + id: "test/no-tools-model", |
| 196 | + object: "model", |
| 197 | + owned_by: "test", |
| 198 | + created: 1234567890, |
| 199 | + context_length: 128000, |
| 200 | + max_model_len: 8192, |
| 201 | + input_modalities: ["text"], |
| 202 | + supported_features: ["json_mode", "reasoning"], |
| 203 | + }, |
| 204 | + ], |
| 205 | + }, |
| 206 | + } |
| 207 | + |
| 208 | + mockedAxios.get.mockResolvedValue(mockResponse) |
| 209 | + |
| 210 | + const models = await getChutesModels("test-api-key") |
| 211 | + |
| 212 | + expect(models["test/no-tools-model"].supportsNativeTools).toBe(false) |
| 213 | + expect(models["test/no-tools-model"].defaultToolProtocol).toBeUndefined() |
| 214 | + }) |
| 215 | +}) |
0 commit comments