|
1 | 1 | import { smokeTest } from '@profullstack/sh1pt-core/testing'; |
| 2 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
2 | 3 | import adapter from './index.js'; |
3 | 4 |
|
4 | 5 | smokeTest(adapter, { idPrefix: 'ai' }); |
| 6 | + |
| 7 | +const ctx = (secrets: Record<string, string> = { MISTRAL_API_KEY: 'test-key' }, dryRun = false) => ({ |
| 8 | + secret: (key: string) => secrets[key], |
| 9 | + log: () => {}, |
| 10 | + dryRun, |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Mistral OpenAI-compatible generation', () => { |
| 14 | + afterEach(() => { |
| 15 | + vi.unstubAllGlobals(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('short-circuits dry-run before network calls', async () => { |
| 19 | + const fetchMock = vi.fn(); |
| 20 | + vi.stubGlobal('fetch', fetchMock); |
| 21 | + |
| 22 | + const result = await adapter.generate(ctx({ MISTRAL_API_KEY: 'test-key' }, true), 'hello', {}, {}); |
| 23 | + |
| 24 | + expect(result).toEqual({ text: '[dry-run]', model: 'mistral-large-latest' }); |
| 25 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('posts chat completions requests and maps usage tokens', async () => { |
| 29 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 30 | + ok: true, |
| 31 | + json: async () => ({ |
| 32 | + choices: [{ message: { content: 'hi from mistral' } }], |
| 33 | + model: 'mistral-small-latest', |
| 34 | + usage: { prompt_tokens: 8, completion_tokens: 4 }, |
| 35 | + }), |
| 36 | + }); |
| 37 | + vi.stubGlobal('fetch', fetchMock); |
| 38 | + |
| 39 | + const result = await adapter.generate(ctx(), 'hello', { |
| 40 | + model: 'mistral-small-latest', |
| 41 | + system: 'be direct', |
| 42 | + maxTokens: 24, |
| 43 | + temperature: 0.3, |
| 44 | + extra: { top_p: 0.7 }, |
| 45 | + }, {}); |
| 46 | + |
| 47 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 48 | + const call = fetchMock.mock.calls[0]; |
| 49 | + expect(call).toBeDefined(); |
| 50 | + const [url, request] = call!; |
| 51 | + expect(url).toBe('https://api.mistral.ai/v1/chat/completions'); |
| 52 | + expect(request.headers.authorization).toBe('Bearer test-key'); |
| 53 | + expect(JSON.parse(request.body)).toEqual({ |
| 54 | + model: 'mistral-small-latest', |
| 55 | + messages: [ |
| 56 | + { role: 'system', content: 'be direct' }, |
| 57 | + { role: 'user', content: 'hello' }, |
| 58 | + ], |
| 59 | + max_tokens: 24, |
| 60 | + temperature: 0.3, |
| 61 | + top_p: 0.7, |
| 62 | + }); |
| 63 | + expect(result).toEqual({ |
| 64 | + text: 'hi from mistral', |
| 65 | + model: 'mistral-small-latest', |
| 66 | + inputTokens: 8, |
| 67 | + outputTokens: 4, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('includes status and response body excerpt on errors', async () => { |
| 72 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 73 | + ok: false, |
| 74 | + status: 400, |
| 75 | + text: async () => 'bad request'.repeat(30), |
| 76 | + })); |
| 77 | + |
| 78 | + await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow(/Mistral 400: bad request/); |
| 79 | + }); |
| 80 | +}); |
0 commit comments