|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import OpenAI from "openai" |
| 3 | + |
| 4 | +import { OpenAiCompatibleHandler } from "../openai-compatible" |
| 5 | + |
| 6 | +vi.mock("openai") |
| 7 | + |
| 8 | +describe("OpenAiCompatibleHandler", () => { |
| 9 | + let handler: OpenAiCompatibleHandler |
| 10 | + let mockCreate: any |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + vi.clearAllMocks() |
| 14 | + mockCreate = vi.fn() |
| 15 | + ;(OpenAI as any).mockImplementation(() => ({ |
| 16 | + chat: { |
| 17 | + completions: { |
| 18 | + create: mockCreate, |
| 19 | + }, |
| 20 | + }, |
| 21 | + })) |
| 22 | + }) |
| 23 | + |
| 24 | + describe("initialization", () => { |
| 25 | + it("should create handler with valid configuration", () => { |
| 26 | + handler = new OpenAiCompatibleHandler({ |
| 27 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 28 | + openAiCompatibleApiKey: "test-api-key", |
| 29 | + apiModelId: "minimaxai/minimax-m2", |
| 30 | + } as any) |
| 31 | + |
| 32 | + expect(handler).toBeInstanceOf(OpenAiCompatibleHandler) |
| 33 | + expect(OpenAI).toHaveBeenCalledWith( |
| 34 | + expect.objectContaining({ |
| 35 | + baseURL: "https://integrate.api.nvidia.com/v1", |
| 36 | + apiKey: "test-api-key", |
| 37 | + }), |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + it("should throw error when base URL is missing", () => { |
| 42 | + expect( |
| 43 | + () => |
| 44 | + new OpenAiCompatibleHandler({ |
| 45 | + openAiCompatibleApiKey: "test-api-key", |
| 46 | + } as any), |
| 47 | + ).toThrow("OpenAI-compatible base URL is required") |
| 48 | + }) |
| 49 | + |
| 50 | + it("should throw error when API key is missing", () => { |
| 51 | + expect( |
| 52 | + () => |
| 53 | + new OpenAiCompatibleHandler({ |
| 54 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 55 | + } as any), |
| 56 | + ).toThrow("OpenAI-compatible API key is required") |
| 57 | + }) |
| 58 | + |
| 59 | + it("should use fallback properties if openAiCompatible ones are not present", () => { |
| 60 | + handler = new OpenAiCompatibleHandler({ |
| 61 | + openAiBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 62 | + openAiApiKey: "test-api-key", |
| 63 | + apiModelId: "minimaxai/minimax-m2", |
| 64 | + } as any) |
| 65 | + |
| 66 | + expect(handler).toBeInstanceOf(OpenAiCompatibleHandler) |
| 67 | + expect(OpenAI).toHaveBeenCalledWith( |
| 68 | + expect.objectContaining({ |
| 69 | + baseURL: "https://integrate.api.nvidia.com/v1", |
| 70 | + apiKey: "test-api-key", |
| 71 | + }), |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it("should use default model when apiModelId is not provided", () => { |
| 76 | + handler = new OpenAiCompatibleHandler({ |
| 77 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 78 | + openAiCompatibleApiKey: "test-api-key", |
| 79 | + } as any) |
| 80 | + |
| 81 | + const model = handler.getModel() |
| 82 | + expect(model.id).toBe("default") |
| 83 | + }) |
| 84 | + |
| 85 | + it("should support NVIDIA API with MiniMax model", () => { |
| 86 | + handler = new OpenAiCompatibleHandler({ |
| 87 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 88 | + openAiCompatibleApiKey: "nvapi-test-key", |
| 89 | + apiModelId: "minimaxai/minimax-m2", |
| 90 | + } as any) |
| 91 | + |
| 92 | + const model = handler.getModel() |
| 93 | + expect(model.id).toBe("minimaxai/minimax-m2") |
| 94 | + expect(model.info.maxTokens).toBe(128000) |
| 95 | + expect(model.info.contextWindow).toBe(128000) |
| 96 | + }) |
| 97 | + |
| 98 | + it("should support any custom OpenAI-compatible endpoint", () => { |
| 99 | + handler = new OpenAiCompatibleHandler({ |
| 100 | + openAiCompatibleBaseUrl: "https://custom.api.example.com/v1", |
| 101 | + openAiCompatibleApiKey: "custom-api-key", |
| 102 | + apiModelId: "custom-model", |
| 103 | + } as any) |
| 104 | + |
| 105 | + const model = handler.getModel() |
| 106 | + expect(model.id).toBe("custom-model") |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + describe("getModel", () => { |
| 111 | + beforeEach(() => { |
| 112 | + handler = new OpenAiCompatibleHandler({ |
| 113 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 114 | + openAiCompatibleApiKey: "test-api-key", |
| 115 | + apiModelId: "minimaxai/minimax-m2", |
| 116 | + } as any) |
| 117 | + }) |
| 118 | + |
| 119 | + it("should return correct model info", () => { |
| 120 | + const model = handler.getModel() |
| 121 | + expect(model.id).toBe("minimaxai/minimax-m2") |
| 122 | + expect(model.info).toMatchObject({ |
| 123 | + maxTokens: 128000, |
| 124 | + contextWindow: 128000, |
| 125 | + supportsPromptCache: false, |
| 126 | + supportsImages: false, |
| 127 | + }) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe("createMessage", () => { |
| 132 | + beforeEach(() => { |
| 133 | + handler = new OpenAiCompatibleHandler({ |
| 134 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 135 | + openAiCompatibleApiKey: "test-api-key", |
| 136 | + apiModelId: "minimaxai/minimax-m2", |
| 137 | + } as any) |
| 138 | + }) |
| 139 | + |
| 140 | + it("should create streaming request with correct parameters", async () => { |
| 141 | + const mockStream = (async function* () { |
| 142 | + yield { |
| 143 | + choices: [ |
| 144 | + { |
| 145 | + delta: { content: "Test response" }, |
| 146 | + }, |
| 147 | + ], |
| 148 | + } |
| 149 | + yield { |
| 150 | + choices: [ |
| 151 | + { |
| 152 | + delta: {}, |
| 153 | + }, |
| 154 | + ], |
| 155 | + usage: { |
| 156 | + prompt_tokens: 10, |
| 157 | + completion_tokens: 5, |
| 158 | + }, |
| 159 | + } |
| 160 | + })() |
| 161 | + |
| 162 | + mockCreate.mockReturnValue(mockStream) |
| 163 | + |
| 164 | + const systemPrompt = "You are a helpful assistant." |
| 165 | + const messages = [ |
| 166 | + { |
| 167 | + role: "user" as const, |
| 168 | + content: "Hello, can you help me?", |
| 169 | + }, |
| 170 | + ] |
| 171 | + |
| 172 | + const stream = handler.createMessage(systemPrompt, messages) |
| 173 | + const chunks = [] |
| 174 | + |
| 175 | + for await (const chunk of stream) { |
| 176 | + chunks.push(chunk) |
| 177 | + } |
| 178 | + |
| 179 | + expect(mockCreate).toHaveBeenCalledWith( |
| 180 | + expect.objectContaining({ |
| 181 | + model: "minimaxai/minimax-m2", |
| 182 | + messages: [ |
| 183 | + { role: "system", content: systemPrompt }, |
| 184 | + { role: "user", content: "Hello, can you help me?" }, |
| 185 | + ], |
| 186 | + stream: true, |
| 187 | + stream_options: { include_usage: true }, |
| 188 | + temperature: 0.7, |
| 189 | + max_tokens: 25600, // 20% of context window (128000) |
| 190 | + }), |
| 191 | + undefined, |
| 192 | + ) |
| 193 | + |
| 194 | + expect(chunks).toContainEqual( |
| 195 | + expect.objectContaining({ |
| 196 | + type: "text", |
| 197 | + text: "Test response", |
| 198 | + }), |
| 199 | + ) |
| 200 | + |
| 201 | + expect(chunks).toContainEqual( |
| 202 | + expect.objectContaining({ |
| 203 | + type: "usage", |
| 204 | + inputTokens: 10, |
| 205 | + outputTokens: 5, |
| 206 | + }), |
| 207 | + ) |
| 208 | + }) |
| 209 | + }) |
| 210 | + |
| 211 | + describe("completePrompt", () => { |
| 212 | + beforeEach(() => { |
| 213 | + handler = new OpenAiCompatibleHandler({ |
| 214 | + openAiCompatibleBaseUrl: "https://integrate.api.nvidia.com/v1", |
| 215 | + openAiCompatibleApiKey: "test-api-key", |
| 216 | + apiModelId: "minimaxai/minimax-m2", |
| 217 | + } as any) |
| 218 | + }) |
| 219 | + |
| 220 | + it("should complete prompt correctly", async () => { |
| 221 | + const mockResponse = { |
| 222 | + choices: [ |
| 223 | + { |
| 224 | + message: { content: "Completed response" }, |
| 225 | + }, |
| 226 | + ], |
| 227 | + } |
| 228 | + |
| 229 | + mockCreate.mockResolvedValue(mockResponse) |
| 230 | + |
| 231 | + const result = await handler.completePrompt("Complete this: Hello") |
| 232 | + |
| 233 | + expect(mockCreate).toHaveBeenCalledWith( |
| 234 | + expect.objectContaining({ |
| 235 | + model: "minimaxai/minimax-m2", |
| 236 | + messages: [{ role: "user", content: "Complete this: Hello" }], |
| 237 | + }), |
| 238 | + ) |
| 239 | + |
| 240 | + expect(result).toBe("Completed response") |
| 241 | + }) |
| 242 | + |
| 243 | + it("should return empty string when no content", async () => { |
| 244 | + const mockResponse = { |
| 245 | + choices: [ |
| 246 | + { |
| 247 | + message: { content: null }, |
| 248 | + }, |
| 249 | + ], |
| 250 | + } |
| 251 | + |
| 252 | + mockCreate.mockResolvedValue(mockResponse) |
| 253 | + |
| 254 | + const result = await handler.completePrompt("Complete this") |
| 255 | + |
| 256 | + expect(result).toBe("") |
| 257 | + }) |
| 258 | + }) |
| 259 | +}) |
0 commit comments