|
| 1 | +import { afterEach, expect, it, vi } from "vite-plus/test"; |
| 2 | +import { Client, chatPath, responsesPath, getModelProfilePreset } from "bailian-cli-core"; |
| 3 | +import { resolveApiKeyLoginConfigName } from "../../core/src/config/profile-presets.ts"; |
| 4 | +import { apiKeyValidationCandidates } from "../src/commands/auth/login-api-key.ts"; |
| 5 | +import loginCommand from "../src/commands/auth/login.ts"; |
| 6 | +import chatCommand from "../src/commands/text/chat.ts"; |
| 7 | + |
| 8 | +afterEach(() => vi.restoreAllMocks()); |
| 9 | + |
| 10 | +for (const [profile, origin] of [ |
| 11 | + ["minimax", "https://api.minimax.io"], |
| 12 | + ["minimax-cn", "https://api.minimax.cn"], |
| 13 | +]) { |
| 14 | + it(`${profile} authenticates only on its selected origin and preserves saved models`, async () => { |
| 15 | + vi.spyOn(process.stderr, "write").mockImplementation(() => true); |
| 16 | + const fetchMock = vi.spyOn(globalThis, "fetch").mockImplementation( |
| 17 | + async () => |
| 18 | + new Response(JSON.stringify({ object: "list", data: [] }), { |
| 19 | + status: 200, |
| 20 | + headers: { "Content-Type": "application/json" }, |
| 21 | + }), |
| 22 | + ); |
| 23 | + const persist = vi.fn(); |
| 24 | + for (const saved of [undefined, "MiniMax-M3", "MiniMax-M2.7", "custom-model"]) { |
| 25 | + const stored = { baseUrl: undefined, apiKeyCapabilities: undefined }; |
| 26 | + await loginCommand.run!({ |
| 27 | + flags: { apiKey: "sk-sp-test-minimax" }, |
| 28 | + settings: { configName: profile, defaultTextModel: saved, timeout: 5, language: "en-US" }, |
| 29 | + identity: { productName: "bailian", version: "0.0.0", userAgent: "test" }, |
| 30 | + authStore: { stored: () => stored, login: persist, path: "test-config" }, |
| 31 | + } as never); |
| 32 | + expect(persist).toHaveBeenLastCalledWith( |
| 33 | + expect.objectContaining({ |
| 34 | + base_url: origin, |
| 35 | + default_text_model: saved ?? "MiniMax-M3.1-Flash", |
| 36 | + api_key_capabilities: ["text.chat"], |
| 37 | + }), |
| 38 | + ); |
| 39 | + expect(persist.mock.lastCall![0].default_speech_model).toBeUndefined(); |
| 40 | + } |
| 41 | + expect(fetchMock.mock.calls.every(([url]) => url === `${origin}/v1/models`)).toBe(true); |
| 42 | + expect(apiKeyValidationCandidates("sk-sp-test", { storedBaseUrl: origin })).toEqual([ |
| 43 | + { baseUrl: origin, kind: "minimax", modelsUrl: `${origin}/v1/models` }, |
| 44 | + ]); |
| 45 | + expect(resolveApiKeyLoginConfigName("sk-sp-test", profile, false, origin)).toBe(profile); |
| 46 | + expect(getModelProfilePreset(profile)?.apiKeyCapabilities).toEqual(["text.chat"]); |
| 47 | + }); |
| 48 | + |
| 49 | + for (const api of ["chat", "responses"]) { |
| 50 | + for (const stream of [false, true]) { |
| 51 | + it(`${profile} ${api} stream=${stream} sends a real client request with Flash`, async () => { |
| 52 | + vi.spyOn(process.stdout, "write").mockImplementation(() => true); |
| 53 | + const response = |
| 54 | + api === "chat" |
| 55 | + ? { choices: [{ message: { content: "hello" } }] } |
| 56 | + : { |
| 57 | + status: "completed", |
| 58 | + output: [{ type: "message", content: [{ type: "output_text", text: "hello" }] }], |
| 59 | + }; |
| 60 | + const sse = |
| 61 | + api === "chat" |
| 62 | + ? 'data: {"choices":[{"delta":{"content":"hello"}}]}\n\ndata: [DONE]\n\n' |
| 63 | + : 'data: {"type":"response.output_text.delta","delta":"hello"}\n\ndata: {"type":"response.completed","response":{"status":"completed"}}\n\n'; |
| 64 | + const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue( |
| 65 | + new Response(stream ? sse : JSON.stringify(response), { |
| 66 | + status: 200, |
| 67 | + headers: { "Content-Type": stream ? "text/event-stream" : "application/json" }, |
| 68 | + }), |
| 69 | + ); |
| 70 | + const settings = { output: "json", timeout: 5, language: "en-US" }; |
| 71 | + const identity = { productName: "bailian", version: "0.0.0", userAgent: "test" }; |
| 72 | + const client = new Client({ |
| 73 | + identity, |
| 74 | + settings, |
| 75 | + baseUrl: origin, |
| 76 | + apiCred: { token: "test-minimax-key", baseUrl: origin }, |
| 77 | + } as never); |
| 78 | + await chatCommand.run!({ |
| 79 | + client, |
| 80 | + settings, |
| 81 | + flags: { api, stream, message: ["hello"], enableThinking: true }, |
| 82 | + } as never); |
| 83 | + const [url, request] = fetchMock.mock.calls[0]; |
| 84 | + expect(url).toBe(`${origin}/v1/${api === "chat" ? "chat/completions" : "responses"}`); |
| 85 | + const body = JSON.parse(request!.body as string); |
| 86 | + expect(body.model).toBe("MiniMax-M3.1-Flash"); |
| 87 | + expect(body.enable_thinking).toBeUndefined(); |
| 88 | + expect(api === "chat" ? body.thinking : body.reasoning).toEqual( |
| 89 | + api === "chat" ? { type: "adaptive" } : { effort: "max" }, |
| 90 | + ); |
| 91 | + expect(new Headers(request!.headers).get("Authorization")).toBe("Bearer test-minimax-key"); |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +it("keeps legacy/custom selections and non-MiniMax routing", async () => { |
| 98 | + vi.spyOn(process.stdout, "write").mockImplementation(() => true); |
| 99 | + for (const [origin, saved, explicit, expected] of [ |
| 100 | + ["https://api.minimax.io", "MiniMax-M2.7", undefined, "MiniMax-M2.7"], |
| 101 | + ["https://api.minimax.cn", "MiniMax-M3", "custom-model", "custom-model"], |
| 102 | + ["https://dashscope.aliyuncs.com", undefined, undefined, "qwen3.8-max"], |
| 103 | + ["https://gateway.example", "custom-model", undefined, "custom-model"], |
| 104 | + ]) { |
| 105 | + const requestJson = vi.fn().mockResolvedValue({ choices: [{ message: { content: "hello" } }] }); |
| 106 | + await chatCommand.run!({ |
| 107 | + client: { baseUrl: origin, requestJson }, |
| 108 | + settings: { output: "json", defaultTextModel: saved }, |
| 109 | + flags: { message: ["hello"], model: explicit }, |
| 110 | + } as never); |
| 111 | + expect(requestJson.mock.calls[0][0].body.model).toBe(expected); |
| 112 | + expect(requestJson.mock.calls[0][0].path).toBe(chatPath(origin)); |
| 113 | + } |
| 114 | + for (const origin of [ |
| 115 | + undefined, |
| 116 | + "https://gateway.example", |
| 117 | + "https://api.minimax.io.example", |
| 118 | + "http://api.minimax.io", |
| 119 | + ]) { |
| 120 | + expect(chatPath(origin)).toBe("/compatible-mode/v1/chat/completions"); |
| 121 | + expect(responsesPath(origin)).toBe("/compatible-mode/v1/responses"); |
| 122 | + } |
| 123 | + expect(getModelProfilePreset("token-plan")?.defaultTextModel).toBe("qwen3.8-max"); |
| 124 | + expect(getModelProfilePreset("token-plan")?.defaultSpeechModel).toBe("qwen-audio-3.0-tts-plus"); |
| 125 | +}); |
| 126 | + |
| 127 | +it("rejects a MiniMax thinking budget before sending a request", async () => { |
| 128 | + const requestJson = vi.fn(); |
| 129 | + await expect( |
| 130 | + chatCommand.run!({ |
| 131 | + client: { baseUrl: "https://api.minimax.io", requestJson }, |
| 132 | + settings: { output: "json" }, |
| 133 | + flags: { message: ["hello"], thinkingBudget: 128 }, |
| 134 | + } as never), |
| 135 | + ).rejects.toThrow("MiniMax does not support --thinking-budget"); |
| 136 | + expect(requestJson).not.toHaveBeenCalled(); |
| 137 | +}); |
0 commit comments