|
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 = ( |
| 8 | + secrets: Record<string, string> = { FIREWORKS_API_KEY: 'test-key' }, |
| 9 | + dryRun = false, |
| 10 | +) => ({ |
| 11 | + secret: (key: string) => secrets[key], |
| 12 | + log: () => {}, |
| 13 | + dryRun, |
| 14 | +}); |
| 15 | + |
| 16 | +describe('Fireworks AI generation', () => { |
| 17 | + afterEach(() => { |
| 18 | + vi.unstubAllGlobals(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('requires a Fireworks API key', async () => { |
| 22 | + await expect(adapter.generate(ctx({}, false), 'hello', {}, {})).rejects.toThrow( |
| 23 | + /FIREWORKS_API_KEY/, |
| 24 | + ); |
| 25 | + }); |
| 26 | + |
| 27 | + it('short-circuits dry-run before network calls', async () => { |
| 28 | + const fetchMock = vi.fn(); |
| 29 | + vi.stubGlobal('fetch', fetchMock); |
| 30 | + |
| 31 | + const result = await adapter.generate( |
| 32 | + ctx({ FIREWORKS_API_KEY: 'test-key' }, true), |
| 33 | + 'hello', |
| 34 | + {}, |
| 35 | + {}, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(result).toEqual({ |
| 39 | + text: '[dry-run]', |
| 40 | + model: 'accounts/fireworks/models/llama-v3p3-70b-instruct', |
| 41 | + }); |
| 42 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('posts chat completions requests and maps usage tokens', async () => { |
| 46 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 47 | + ok: true, |
| 48 | + json: async () => ({ |
| 49 | + model: 'accounts/fireworks/models/llama-v3p3-70b-instruct', |
| 50 | + choices: [{ message: { role: 'assistant', content: 'hi from fireworks' } }], |
| 51 | + usage: { prompt_tokens: 11, completion_tokens: 4, total_tokens: 15 }, |
| 52 | + }), |
| 53 | + }); |
| 54 | + vi.stubGlobal('fetch', fetchMock); |
| 55 | + |
| 56 | + const result = await adapter.generate( |
| 57 | + ctx(), |
| 58 | + 'hello', |
| 59 | + { |
| 60 | + system: 'be direct', |
| 61 | + maxTokens: 80, |
| 62 | + temperature: 0.5, |
| 63 | + extra: { top_p: 0.9, request_id: 'req-fireworks' }, |
| 64 | + }, |
| 65 | + {}, |
| 66 | + ); |
| 67 | + |
| 68 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 69 | + const call = fetchMock.mock.calls[0]; |
| 70 | + expect(call).toBeDefined(); |
| 71 | + const [url, request] = call!; |
| 72 | + expect(url).toBe('https://api.fireworks.ai/inference/v1/chat/completions'); |
| 73 | + expect(request.headers.authorization).toBe('Bearer test-key'); |
| 74 | + expect(request.headers['content-type']).toBe('application/json'); |
| 75 | + expect(JSON.parse(request.body)).toEqual({ |
| 76 | + model: 'accounts/fireworks/models/llama-v3p3-70b-instruct', |
| 77 | + messages: [ |
| 78 | + { role: 'system', content: 'be direct' }, |
| 79 | + { role: 'user', content: 'hello' }, |
| 80 | + ], |
| 81 | + stream: false, |
| 82 | + max_tokens: 80, |
| 83 | + temperature: 0.5, |
| 84 | + top_p: 0.9, |
| 85 | + request_id: 'req-fireworks', |
| 86 | + }); |
| 87 | + expect(result).toEqual({ |
| 88 | + text: 'hi from fireworks', |
| 89 | + model: 'accounts/fireworks/models/llama-v3p3-70b-instruct', |
| 90 | + inputTokens: 11, |
| 91 | + outputTokens: 4, |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('supports text-style choices and custom base URLs', async () => { |
| 96 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 97 | + ok: true, |
| 98 | + json: async () => ({ |
| 99 | + choices: [{ text: 'legacy text response' }], |
| 100 | + }), |
| 101 | + }); |
| 102 | + vi.stubGlobal('fetch', fetchMock); |
| 103 | + |
| 104 | + const result = await adapter.generate( |
| 105 | + ctx(), |
| 106 | + 'hello', |
| 107 | + { model: 'accounts/fireworks/models/llama-v3p1-8b-instruct' }, |
| 108 | + { baseUrl: 'https://fireworks.test/inference/v1/' }, |
| 109 | + ); |
| 110 | + |
| 111 | + expect(fetchMock.mock.calls[0]?.[0]).toBe('https://fireworks.test/inference/v1/chat/completions'); |
| 112 | + expect(result).toEqual({ |
| 113 | + text: 'legacy text response', |
| 114 | + model: 'accounts/fireworks/models/llama-v3p1-8b-instruct', |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('includes status and redacted response body excerpts on errors', async () => { |
| 119 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 120 | + ok: false, |
| 121 | + status: 401, |
| 122 | + text: async () => 'invalid api key test-key', |
| 123 | + })); |
| 124 | + |
| 125 | + await expect(adapter.generate(ctx(), 'hello', {}, {})).rejects.toThrow( |
| 126 | + /Fireworks AI 401: invalid api key \[redacted\]/, |
| 127 | + ); |
| 128 | + }); |
| 129 | +}); |
0 commit comments