|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// Track calls to createOpenAICompatible across module resets |
| 4 | +let createOpenAICompatibleCalls: unknown[][] = []; |
| 5 | +let createAnthropicCalls: unknown[][] = []; |
| 6 | + |
| 7 | +// Mock @voltagent/internal to avoid build dependency |
| 8 | +vi.mock("@voltagent/internal", () => ({ |
| 9 | + safeStringify: (value: unknown) => JSON.stringify(value), |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock @ai-sdk/openai-compatible with a tracking wrapper |
| 13 | +vi.mock("@ai-sdk/openai-compatible", () => ({ |
| 14 | + createOpenAICompatible: (...args: unknown[]) => { |
| 15 | + createOpenAICompatibleCalls.push(args); |
| 16 | + const mockModel = { |
| 17 | + modelId: "mock-model", |
| 18 | + specificationVersion: "v1", |
| 19 | + provider: "minimax", |
| 20 | + }; |
| 21 | + return { |
| 22 | + languageModel: () => mockModel, |
| 23 | + chatModel: () => mockModel, |
| 24 | + }; |
| 25 | + }, |
| 26 | +})); |
| 27 | + |
| 28 | +// Mock @ai-sdk/anthropic to track if it's called for MiniMax |
| 29 | +vi.mock("@ai-sdk/anthropic", () => ({ |
| 30 | + createAnthropic: (...args: unknown[]) => { |
| 31 | + createAnthropicCalls.push(args); |
| 32 | + return { |
| 33 | + languageModel: () => ({ modelId: "anthropic-model" }), |
| 34 | + chatModel: () => ({ modelId: "anthropic-model" }), |
| 35 | + }; |
| 36 | + }, |
| 37 | + anthropic: { |
| 38 | + languageModel: () => ({ modelId: "anthropic-model" }), |
| 39 | + chatModel: () => ({ modelId: "anthropic-model" }), |
| 40 | + }, |
| 41 | +})); |
| 42 | + |
| 43 | +describe("MiniMax provider registry", () => { |
| 44 | + const originalEnv = { ...process.env }; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + createOpenAICompatibleCalls = []; |
| 48 | + createAnthropicCalls = []; |
| 49 | + (globalThis as Record<string, unknown>).___voltagent_model_provider_registry = undefined; |
| 50 | + process.env = { ...originalEnv }; |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + process.env = originalEnv; |
| 55 | + (globalThis as Record<string, unknown>).___voltagent_model_provider_registry = undefined; |
| 56 | + }); |
| 57 | + |
| 58 | + it("should list minimax as a registered provider", async () => { |
| 59 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 60 | + const registry = ModelProviderRegistry.getInstance(); |
| 61 | + const providers = registry.listProviders(); |
| 62 | + expect(providers).toContain("minimax"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should list minimax-cn as a registered provider", async () => { |
| 66 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 67 | + const registry = ModelProviderRegistry.getInstance(); |
| 68 | + const providers = registry.listProviders(); |
| 69 | + expect(providers).toContain("minimax-cn"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should load minimax provider via @ai-sdk/openai-compatible", async () => { |
| 73 | + process.env.MINIMAX_API_KEY = "test-key-minimax"; |
| 74 | + |
| 75 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 76 | + const registry = ModelProviderRegistry.getInstance(); |
| 77 | + const model = await registry.resolveLanguageModel("minimax/MiniMax-M2.7"); |
| 78 | + |
| 79 | + expect(model).toBeDefined(); |
| 80 | + expect(createOpenAICompatibleCalls.length).toBeGreaterThan(0); |
| 81 | + |
| 82 | + const lastCall = createOpenAICompatibleCalls[createOpenAICompatibleCalls.length - 1]; |
| 83 | + const config = lastCall[0] as Record<string, unknown>; |
| 84 | + expect(config.name).toBe("minimax"); |
| 85 | + expect(config.baseURL).toBe("https://api.minimax.io/v1"); |
| 86 | + expect(config.apiKey).toBe("test-key-minimax"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should load minimax-cn provider with China base URL", async () => { |
| 90 | + process.env.MINIMAX_API_KEY = "test-key-minimax-cn"; |
| 91 | + |
| 92 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 93 | + const registry = ModelProviderRegistry.getInstance(); |
| 94 | + const model = await registry.resolveLanguageModel("minimax-cn/MiniMax-M2.7"); |
| 95 | + |
| 96 | + expect(model).toBeDefined(); |
| 97 | + |
| 98 | + // Find the call for minimax-cn |
| 99 | + const cnCall = createOpenAICompatibleCalls.find((call) => { |
| 100 | + const config = call[0] as Record<string, unknown>; |
| 101 | + return config.name === "minimax-cn"; |
| 102 | + }); |
| 103 | + expect(cnCall).toBeDefined(); |
| 104 | + const config = cnCall![0] as Record<string, unknown>; |
| 105 | + expect(config.baseURL).toBe("https://api.minimaxi.com/v1"); |
| 106 | + expect(config.apiKey).toBe("test-key-minimax-cn"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should support MINIMAX_BASE_URL override", async () => { |
| 110 | + process.env.MINIMAX_API_KEY = "test-key"; |
| 111 | + process.env.MINIMAX_BASE_URL = "https://custom.minimax.io/v1"; |
| 112 | + |
| 113 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 114 | + const registry = ModelProviderRegistry.getInstance(); |
| 115 | + await registry.resolveLanguageModel("minimax/MiniMax-M2.7"); |
| 116 | + |
| 117 | + const minimaxCall = createOpenAICompatibleCalls.find((call) => { |
| 118 | + const config = call[0] as Record<string, unknown>; |
| 119 | + return config.name === "minimax"; |
| 120 | + }); |
| 121 | + expect(minimaxCall).toBeDefined(); |
| 122 | + const config = minimaxCall![0] as Record<string, unknown>; |
| 123 | + expect(config.baseURL).toBe("https://custom.minimax.io/v1"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should throw if MINIMAX_API_KEY is not set", async () => { |
| 127 | + delete process.env.MINIMAX_API_KEY; |
| 128 | + |
| 129 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 130 | + const registry = ModelProviderRegistry.getInstance(); |
| 131 | + |
| 132 | + await expect(registry.resolveLanguageModel("minimax/MiniMax-M2.7")).rejects.toThrow( |
| 133 | + /MINIMAX_API_KEY/, |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should resolve multiple MiniMax model variants", async () => { |
| 138 | + process.env.MINIMAX_API_KEY = "test-key"; |
| 139 | + |
| 140 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 141 | + const registry = ModelProviderRegistry.getInstance(); |
| 142 | + |
| 143 | + const modelIds = [ |
| 144 | + "MiniMax-M2.7", |
| 145 | + "MiniMax-M2.7-highspeed", |
| 146 | + "MiniMax-M2.5", |
| 147 | + "MiniMax-M2.5-highspeed", |
| 148 | + ]; |
| 149 | + |
| 150 | + for (const modelId of modelIds) { |
| 151 | + const model = await registry.resolveLanguageModel(`minimax/${modelId}`); |
| 152 | + expect(model).toBeDefined(); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + it("should not use @ai-sdk/anthropic adapter for minimax", async () => { |
| 157 | + process.env.MINIMAX_API_KEY = "test-key"; |
| 158 | + |
| 159 | + const anthropicCallsBefore = createAnthropicCalls.length; |
| 160 | + |
| 161 | + const { ModelProviderRegistry } = await import("./model-provider-registry"); |
| 162 | + const registry = ModelProviderRegistry.getInstance(); |
| 163 | + await registry.resolveLanguageModel("minimax/MiniMax-M2.7"); |
| 164 | + |
| 165 | + // Anthropic adapter should NOT have been called for MiniMax |
| 166 | + expect(createAnthropicCalls.length).toBe(anthropicCallsBefore); |
| 167 | + // OpenAI-compatible adapter SHOULD have been called |
| 168 | + expect(createOpenAICompatibleCalls.length).toBeGreaterThan(0); |
| 169 | + }); |
| 170 | +}); |
0 commit comments