|
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> = { ATLASCLOUD_API_KEY: 'test-key' }, dryRun = false) => ({ |
| 8 | + secret: (key: string) => secrets[key], |
| 9 | + log: () => {}, |
| 10 | + dryRun, |
| 11 | +}); |
| 12 | + |
| 13 | +describe('AtlasCloud 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({ ATLASCLOUD_API_KEY: 'test-key' }, true), 'hello', {}, {}); |
| 23 | + |
| 24 | + expect(result).toEqual({ text: '[dry-run]', model: 'deepseek-ai/DeepSeek-V3-0324' }); |
| 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 atlascloud' } }], |
| 33 | + model: 'qwen/qwen3-32b', |
| 34 | + usage: { prompt_tokens: 7, completion_tokens: 3 }, |
| 35 | + }), |
| 36 | + }); |
| 37 | + vi.stubGlobal('fetch', fetchMock); |
| 38 | + |
| 39 | + const result = await adapter.generate(ctx(), 'hello', { |
| 40 | + model: 'qwen/qwen3-32b', |
| 41 | + system: 'be brief', |
| 42 | + maxTokens: 20, |
| 43 | + temperature: 0.2, |
| 44 | + extra: { top_p: 0.9 }, |
| 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.atlascloud.ai/v1/chat/completions'); |
| 52 | + expect(request.headers.authorization).toBe('Bearer test-key'); |
| 53 | + expect(JSON.parse(request.body)).toEqual({ |
| 54 | + model: 'qwen/qwen3-32b', |
| 55 | + messages: [ |
| 56 | + { role: 'system', content: 'be brief' }, |
| 57 | + { role: 'user', content: 'hello' }, |
| 58 | + ], |
| 59 | + max_tokens: 20, |
| 60 | + temperature: 0.2, |
| 61 | + top_p: 0.9, |
| 62 | + }); |
| 63 | + expect(result).toEqual({ |
| 64 | + text: 'hi from atlascloud', |
| 65 | + model: 'qwen/qwen3-32b', |
| 66 | + inputTokens: 7, |
| 67 | + outputTokens: 3, |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('normalizes configured base URLs with trailing slashes', async () => { |
| 72 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 73 | + ok: true, |
| 74 | + json: async () => ({ choices: [{ message: { content: 'ok' } }], model: 'deepseek-ai/DeepSeek-V3-0324' }), |
| 75 | + }); |
| 76 | + vi.stubGlobal('fetch', fetchMock); |
| 77 | + |
| 78 | + await adapter.generate(ctx(), 'hello', {}, { baseUrl: 'https://proxy.example.com/' }); |
| 79 | + |
| 80 | + const [url] = fetchMock.mock.calls[0]!; |
| 81 | + expect(url).toBe('https://proxy.example.com/v1/chat/completions'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('includes status and redacted response body excerpt on errors', async () => { |
| 85 | + const apiKey = 'test-key-crossing-truncation-boundary'; |
| 86 | + const prefix = 'x'.repeat(190); |
| 87 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 88 | + ok: false, |
| 89 | + status: 401, |
| 90 | + text: async () => `${prefix}${apiKey} invalid token`, |
| 91 | + })); |
| 92 | + |
| 93 | + let error: unknown; |
| 94 | + try { |
| 95 | + await adapter.generate(ctx({ ATLASCLOUD_API_KEY: apiKey }), 'hello', {}, {}); |
| 96 | + } catch (exc) { |
| 97 | + error = exc; |
| 98 | + } |
| 99 | + expect(error).toBeInstanceOf(Error); |
| 100 | + expect((error as Error).message).toContain('AtlasCloud 401:'); |
| 101 | + expect((error as Error).message).toContain('[redacted]'); |
| 102 | + expect((error as Error).message).not.toContain(apiKey); |
| 103 | + expect((error as Error).message).not.toContain(apiKey.slice(0, 10)); |
| 104 | + }); |
| 105 | + |
| 106 | + it('throws when the API key is missing from the vault', async () => { |
| 107 | + await expect(adapter.generate(ctx({}), 'hello', {}, {})).rejects.toThrow('ATLASCLOUD_API_KEY not in vault'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments