|
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> = { NEBIUS_API_KEY: 'test-key' }, dryRun = false) => ({ |
| 8 | + secret: (key: string) => secrets[key], |
| 9 | + log: () => {}, |
| 10 | + dryRun, |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Nebius Token Factory chat completions 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({ NEBIUS_API_KEY: 'test-key' }, true), 'hello', {}, {}); |
| 23 | + |
| 24 | + expect(result).toEqual({ text: '[dry-run]', model: 'meta-llama/Llama-3.3-70B-Instruct' }); |
| 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 | + model: 'deepseek-ai/DeepSeek-R1-0528', |
| 33 | + choices: [{ message: { role: 'assistant', content: 'hi from nebius' } }], |
| 34 | + usage: { prompt_tokens: 13, completion_tokens: 5, total_tokens: 18 }, |
| 35 | + }), |
| 36 | + }); |
| 37 | + vi.stubGlobal('fetch', fetchMock); |
| 38 | + |
| 39 | + const result = await adapter.generate(ctx(), 'hello', { |
| 40 | + model: 'deepseek-ai/DeepSeek-R1-0528', |
| 41 | + system: 'be brief', |
| 42 | + maxTokens: 28, |
| 43 | + temperature: 0.3, |
| 44 | + extra: { top_p: 0.8, service_tier: 'auto' }, |
| 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.tokenfactory.nebius.com/v1/chat/completions'); |
| 52 | + expect(request.headers.authorization).toBe('Bearer test-key'); |
| 53 | + expect(request.headers['content-type']).toBe('application/json'); |
| 54 | + expect(JSON.parse(request.body)).toEqual({ |
| 55 | + stream: false, |
| 56 | + model: 'deepseek-ai/DeepSeek-R1-0528', |
| 57 | + messages: [ |
| 58 | + { role: 'system', content: 'be brief' }, |
| 59 | + { role: 'user', content: 'hello' }, |
| 60 | + ], |
| 61 | + max_tokens: 28, |
| 62 | + temperature: 0.3, |
| 63 | + top_p: 0.8, |
| 64 | + service_tier: 'auto', |
| 65 | + }); |
| 66 | + expect(result).toEqual({ |
| 67 | + text: 'hi from nebius', |
| 68 | + model: 'deepseek-ai/DeepSeek-R1-0528', |
| 69 | + inputTokens: 13, |
| 70 | + outputTokens: 5, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('supports text-style choices from compatible Nebius responses', async () => { |
| 75 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 76 | + ok: true, |
| 77 | + json: async () => ({ |
| 78 | + model: 'meta-llama/Llama-3.3-70B-Instruct', |
| 79 | + choices: [{ text: 'text choice response' }], |
| 80 | + }), |
| 81 | + })); |
| 82 | + |
| 83 | + const result = await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://nebius.test' }); |
| 84 | + |
| 85 | + expect(result).toEqual({ |
| 86 | + text: 'text choice response', |
| 87 | + model: 'meta-llama/Llama-3.3-70B-Instruct', |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('includes status and response body excerpt on errors', async () => { |
| 92 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 93 | + ok: false, |
| 94 | + status: 422, |
| 95 | + text: async () => 'invalid request'.repeat(30), |
| 96 | + })); |
| 97 | + |
| 98 | + await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow(/Nebius 422: invalid request/); |
| 99 | + }); |
| 100 | +}); |
0 commit comments