|
| 1 | +/** |
| 2 | + * Integration tests for MiniMax provider. |
| 3 | + * These tests call the real MiniMax API and require MINIMAX_API_KEY env var. |
| 4 | + * Run with: npx jest src/tests/minimax.integration.test.ts |
| 5 | + */ |
| 6 | + |
| 7 | +const MINIMAX_API_KEY = process.env.MINIMAX_API_KEY; |
| 8 | +const BASE_URL = 'https://api.minimax.io/v1'; |
| 9 | + |
| 10 | +const skipIfNoKey = MINIMAX_API_KEY ? describe : describe.skip; |
| 11 | + |
| 12 | +skipIfNoKey('MiniMax integration tests', () => { |
| 13 | + test('non-streaming chat completion with MiniMax-M2.7', async () => { |
| 14 | + const response = await fetch(`${BASE_URL}/chat/completions`, { |
| 15 | + method: 'POST', |
| 16 | + headers: { |
| 17 | + Authorization: `Bearer ${MINIMAX_API_KEY}`, |
| 18 | + 'Content-Type': 'application/json', |
| 19 | + }, |
| 20 | + body: JSON.stringify({ |
| 21 | + model: 'MiniMax-M2.7', |
| 22 | + messages: [ |
| 23 | + { role: 'system', content: 'You are a helpful assistant.' }, |
| 24 | + { role: 'user', content: 'Say hello in one word.' }, |
| 25 | + ], |
| 26 | + max_tokens: 30, |
| 27 | + temperature: 1.0, |
| 28 | + stream: false, |
| 29 | + }), |
| 30 | + }); |
| 31 | + |
| 32 | + expect(response.status).toBe(200); |
| 33 | + const data = await response.json(); |
| 34 | + expect(data).toHaveProperty('id'); |
| 35 | + expect(data).toHaveProperty('model'); |
| 36 | + expect(data).toHaveProperty('choices'); |
| 37 | + expect(data.choices.length).toBeGreaterThan(0); |
| 38 | + expect(data.choices[0]).toHaveProperty('message'); |
| 39 | + expect(data.choices[0].message).toHaveProperty('content'); |
| 40 | + expect(data).toHaveProperty('usage'); |
| 41 | + expect(data.usage).toHaveProperty('prompt_tokens'); |
| 42 | + expect(data.usage).toHaveProperty('completion_tokens'); |
| 43 | + expect(data.usage).toHaveProperty('total_tokens'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('streaming chat completion with MiniMax-M2.7-highspeed', async () => { |
| 47 | + const response = await fetch(`${BASE_URL}/chat/completions`, { |
| 48 | + method: 'POST', |
| 49 | + headers: { |
| 50 | + Authorization: `Bearer ${MINIMAX_API_KEY}`, |
| 51 | + 'Content-Type': 'application/json', |
| 52 | + }, |
| 53 | + body: JSON.stringify({ |
| 54 | + model: 'MiniMax-M2.7-highspeed', |
| 55 | + messages: [{ role: 'user', content: 'Say hi' }], |
| 56 | + max_tokens: 20, |
| 57 | + temperature: 1.0, |
| 58 | + stream: true, |
| 59 | + }), |
| 60 | + }); |
| 61 | + |
| 62 | + expect(response.status).toBe(200); |
| 63 | + const text = await response.text(); |
| 64 | + const lines = text |
| 65 | + .split('\n') |
| 66 | + .filter((line: string) => line.startsWith('data: ')); |
| 67 | + expect(lines.length).toBeGreaterThan(0); |
| 68 | + |
| 69 | + // Check that the last meaningful chunk or a data chunk has proper structure |
| 70 | + const firstLine = lines[0]; |
| 71 | + const firstData = JSON.parse(firstLine.replace('data: ', '')); |
| 72 | + expect(firstData).toHaveProperty('id'); |
| 73 | + expect(firstData).toHaveProperty('model'); |
| 74 | + expect(firstData).toHaveProperty('choices'); |
| 75 | + |
| 76 | + // Verify at least one chunk has finish_reason |
| 77 | + const hasFinishReason = lines.some((line: string) => { |
| 78 | + try { |
| 79 | + const parsed = JSON.parse(line.replace('data: ', '')); |
| 80 | + return parsed.choices?.[0]?.finish_reason !== null; |
| 81 | + } catch { |
| 82 | + return false; |
| 83 | + } |
| 84 | + }); |
| 85 | + expect(hasFinishReason).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + test('error handling with invalid API key', async () => { |
| 89 | + const response = await fetch(`${BASE_URL}/chat/completions`, { |
| 90 | + method: 'POST', |
| 91 | + headers: { |
| 92 | + Authorization: 'Bearer invalid-key', |
| 93 | + 'Content-Type': 'application/json', |
| 94 | + }, |
| 95 | + body: JSON.stringify({ |
| 96 | + model: 'MiniMax-M2.7', |
| 97 | + messages: [{ role: 'user', content: 'Hello' }], |
| 98 | + max_tokens: 10, |
| 99 | + }), |
| 100 | + }); |
| 101 | + |
| 102 | + expect(response.status).not.toBe(200); |
| 103 | + }); |
| 104 | +}); |
0 commit comments