|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { createProvider } from './index.js'; |
| 3 | +import { sseLines } from './adapter-openai.js'; |
| 4 | + |
| 5 | +function jsonResponse(body: unknown, ok = true, status = 200): Response { |
| 6 | + return { |
| 7 | + ok, |
| 8 | + status, |
| 9 | + json: async () => body, |
| 10 | + text: async () => JSON.stringify(body), |
| 11 | + } as Response; |
| 12 | +} |
| 13 | + |
| 14 | +/** Builds a Response whose body streams the given SSE chunks. */ |
| 15 | +function sseResponse(chunks: string[]): Response { |
| 16 | + const encoder = new TextEncoder(); |
| 17 | + let i = 0; |
| 18 | + const body = { |
| 19 | + getReader() { |
| 20 | + return { |
| 21 | + read: async () => (i < chunks.length |
| 22 | + ? { value: encoder.encode(chunks[i++]), done: false } |
| 23 | + : { value: undefined, done: true }), |
| 24 | + }; |
| 25 | + }, |
| 26 | + } as unknown as ReadableStream<Uint8Array>; |
| 27 | + return { ok: true, status: 200, body } as Response; |
| 28 | +} |
| 29 | + |
| 30 | +describe('OpenAI-compatible adapter', () => { |
| 31 | + it('completes via /chat/completions', async () => { |
| 32 | + const fetchImpl = vi.fn(async () => |
| 33 | + jsonResponse({ |
| 34 | + choices: [{ message: { content: 'hi there' } }], |
| 35 | + usage: { prompt_tokens: 3, completion_tokens: 2 }, |
| 36 | + }), |
| 37 | + ) as unknown as typeof fetch; |
| 38 | + |
| 39 | + const provider = createProvider('deepseek', { apiKey: 'k', fetchImpl }); |
| 40 | + const res = await provider.complete({ model: 'deepseek-chat', messages: [{ role: 'user', content: 'hi' }] }); |
| 41 | + |
| 42 | + expect(res.text).toBe('hi there'); |
| 43 | + expect(res.usage).toEqual({ promptTokens: 3, completionTokens: 2 }); |
| 44 | + const [url, init] = (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 45 | + expect(url).toBe('https://api.deepseek.com/v1/chat/completions'); |
| 46 | + expect((init as RequestInit).headers).toMatchObject({ authorization: 'Bearer k' }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('streams deltas from SSE', async () => { |
| 50 | + const fetchImpl = vi.fn(async () => |
| 51 | + sseResponse([ |
| 52 | + 'data: {"choices":[{"delta":{"content":"Hel"}}]}\n', |
| 53 | + 'data: {"choices":[{"delta":{"content":"lo"}}]}\n', |
| 54 | + 'data: [DONE]\n', |
| 55 | + ]), |
| 56 | + ) as unknown as typeof fetch; |
| 57 | + |
| 58 | + const provider = createProvider('openai', { apiKey: 'k', fetchImpl }); |
| 59 | + const out: string[] = []; |
| 60 | + for await (const c of provider.stream({ model: 'gpt-4o', messages: [{ role: 'user', content: 'hi' }] })) { |
| 61 | + if (c.delta) out.push(c.delta); |
| 62 | + } |
| 63 | + expect(out.join('')).toBe('Hello'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('throws on a non-ok response', async () => { |
| 67 | + const fetchImpl = vi.fn(async () => jsonResponse({ error: 'nope' }, false, 401)) as unknown as typeof fetch; |
| 68 | + const provider = createProvider('openai', { apiKey: 'bad', fetchImpl }); |
| 69 | + await expect(provider.complete({ model: 'gpt-4o', messages: [] })).rejects.toThrow(/401/); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('Anthropic adapter', () => { |
| 74 | + it('lifts the system prompt out of messages', async () => { |
| 75 | + const fetchImpl = vi.fn(async () => |
| 76 | + jsonResponse({ |
| 77 | + content: [{ type: 'text', text: 'pong' }], |
| 78 | + usage: { input_tokens: 5, output_tokens: 1 }, |
| 79 | + }), |
| 80 | + ) as unknown as typeof fetch; |
| 81 | + |
| 82 | + const provider = createProvider('anthropic', { apiKey: 'sk-ant', fetchImpl }); |
| 83 | + const res = await provider.complete({ |
| 84 | + model: 'claude-opus-4-8', |
| 85 | + messages: [ |
| 86 | + { role: 'system', content: 'be terse' }, |
| 87 | + { role: 'user', content: 'ping' }, |
| 88 | + ], |
| 89 | + }); |
| 90 | + expect(res.text).toBe('pong'); |
| 91 | + |
| 92 | + const [url, init] = (fetchImpl as unknown as ReturnType<typeof vi.fn>).mock.calls[0]; |
| 93 | + expect(url).toBe('https://api.anthropic.com/v1/messages'); |
| 94 | + const sent = JSON.parse((init as RequestInit).body as string); |
| 95 | + expect(sent.system).toBe('be terse'); |
| 96 | + expect(sent.messages).toEqual([{ role: 'user', content: 'ping' }]); |
| 97 | + expect((init as RequestInit).headers).toMatchObject({ 'x-api-key': 'sk-ant' }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('sseLines', () => { |
| 102 | + it('extracts data payloads across chunk boundaries', async () => { |
| 103 | + const encoder = new TextEncoder(); |
| 104 | + let i = 0; |
| 105 | + const parts = ['data: a\nda', 'ta: b\n']; |
| 106 | + const body = { |
| 107 | + getReader: () => ({ |
| 108 | + read: async () => (i < parts.length |
| 109 | + ? { value: encoder.encode(parts[i++]), done: false } |
| 110 | + : { value: undefined, done: true }), |
| 111 | + }), |
| 112 | + } as unknown as ReadableStream<Uint8Array>; |
| 113 | + const got: string[] = []; |
| 114 | + for await (const d of sseLines(body)) got.push(d); |
| 115 | + expect(got).toEqual(['a', 'b']); |
| 116 | + }); |
| 117 | +}); |
0 commit comments