|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { CerebrasAdapter } from '../cerebras-adapter.js'; |
| 3 | + |
| 4 | +describe('CerebrasAdapter', () => { |
| 5 | + let adapter: CerebrasAdapter; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + adapter = new CerebrasAdapter({ |
| 9 | + apiKey: 'test-key', |
| 10 | + baseUrl: 'https://api.cerebras.ai/v1', |
| 11 | + }); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should have correct id and name', () => { |
| 15 | + expect(adapter.id).toBe('cerebras'); |
| 16 | + expect(adapter.name).toBe('Cerebras'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should not support any provider extensions', () => { |
| 20 | + expect(adapter.supportsExtension('claude')).toBe(false); |
| 21 | + expect(adapter.supportsExtension('gpt')).toBe(false); |
| 22 | + expect(adapter.supportsExtension('gemini')).toBe(false); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should list Cerebras models', async () => { |
| 26 | + const models = await adapter.listModels(); |
| 27 | + expect(models).toContain('llama-4-scout-17b-16e-instruct'); |
| 28 | + expect(models.length).toBeGreaterThan(0); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should use Cerebras base URL by default', () => { |
| 32 | + const defaultAdapter = new CerebrasAdapter({ apiKey: 'key' }); |
| 33 | + // Validate by attempting a completion with a mock — the URL is set internally |
| 34 | + expect(defaultAdapter).toBeDefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should format request as OpenAI-compatible', async () => { |
| 38 | + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce( |
| 39 | + new Response( |
| 40 | + JSON.stringify({ |
| 41 | + choices: [{ message: { content: 'hello' }, finish_reason: 'stop' }], |
| 42 | + usage: { prompt_tokens: 10, completion_tokens: 5 }, |
| 43 | + }), |
| 44 | + { status: 200 } |
| 45 | + ) |
| 46 | + ); |
| 47 | + |
| 48 | + const result = await adapter.complete([{ role: 'user', content: 'test' }], { |
| 49 | + model: 'llama-4-scout-17b-16e-instruct', |
| 50 | + maxTokens: 100, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(result.content[0]).toMatchObject({ type: 'text', text: 'hello' }); |
| 54 | + expect(result.usage.inputTokens).toBe(10); |
| 55 | + expect(result.usage.outputTokens).toBe(5); |
| 56 | + |
| 57 | + const [url, opts] = fetchSpy.mock.calls[0]; |
| 58 | + expect(url).toContain('cerebras.ai'); |
| 59 | + expect((opts as any).headers?.Authorization).toBe('Bearer test-key'); |
| 60 | + |
| 61 | + fetchSpy.mockRestore(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should throw on API error during complete', async () => { |
| 65 | + const fetchSpy = vi |
| 66 | + .spyOn(globalThis, 'fetch') |
| 67 | + .mockResolvedValueOnce( |
| 68 | + new Response('Unauthorized', { |
| 69 | + status: 401, |
| 70 | + statusText: 'Unauthorized', |
| 71 | + }) |
| 72 | + ); |
| 73 | + |
| 74 | + await expect( |
| 75 | + adapter.complete([{ role: 'user', content: 'test' }], { |
| 76 | + model: 'llama3.1-8b', |
| 77 | + maxTokens: 100, |
| 78 | + }) |
| 79 | + ).rejects.toThrow('GPT API error: 401'); |
| 80 | + |
| 81 | + fetchSpy.mockRestore(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments