also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,192 @@ | ||
| 1 | import { afterEach, describe, expect, test } from 'bun:test' | |
| 2 | import { MockProvider } from '../src/providers/mock' | |
| 3 | import { anthropicProvider } from '../src/providers/anthropic' | |
| 4 | import { openaiProvider } from '../src/providers/openai' | |
| 5 | import { configureAI, resetAI } from '../src/ai' | |
| 6 | ||
| 7 | afterEach(() => { | |
| 8 | resetAI() | |
| 9 | }) | |
| 10 | ||
| 11 | describe('MockProvider', () => { | |
| 12 | test('chat returns responses in order', async () => { | |
| 13 | const mock = new MockProvider(['first', 'second', 'third']) | |
| 14 | ||
| 15 | const r1 = await mock.chat([{ role: 'user', content: 'a' }]) | |
| 16 | const r2 = await mock.chat([{ role: 'user', content: 'b' }]) | |
| 17 | const r3 = await mock.chat([{ role: 'user', content: 'c' }]) | |
| 18 | ||
| 19 | expect(r1.content).toBe('first') | |
| 20 | expect(r2.content).toBe('second') | |
| 21 | expect(r3.content).toBe('third') | |
| 22 | }) | |
| 23 | ||
| 24 | test('chat cycles through responses', async () => { | |
| 25 | const mock = new MockProvider(['a', 'b']) | |
| 26 | ||
| 27 | await mock.chat([{ role: 'user', content: '1' }]) | |
| 28 | await mock.chat([{ role: 'user', content: '2' }]) | |
| 29 | const r3 = await mock.chat([{ role: 'user', content: '3' }]) | |
| 30 | ||
| 31 | expect(r3.content).toBe('a') // cycles back | |
| 32 | }) | |
| 33 | ||
| 34 | test('records calls', async () => { | |
| 35 | const mock = new MockProvider() | |
| 36 | ||
| 37 | await mock.chat([{ role: 'user', content: 'hello' }], { temperature: 0.5 }) | |
| 38 | ||
| 39 | expect(mock.calls).toHaveLength(1) | |
| 40 | expect(mock.calls[0].messages[0].content).toBe('hello') | |
| 41 | expect(mock.calls[0].options?.temperature).toBe(0.5) | |
| 42 | }) | |
| 43 | ||
| 44 | test('getLastCall returns the most recent call', async () => { | |
| 45 | const mock = new MockProvider() | |
| 46 | ||
| 47 | await mock.chat([{ role: 'user', content: 'first' }]) | |
| 48 | await mock.chat([{ role: 'user', content: 'second' }]) | |
| 49 | ||
| 50 | expect(mock.getLastCall()?.messages[0].content).toBe('second') | |
| 51 | }) | |
| 52 | ||
| 53 | test('getLastCall returns undefined when no calls', () => { | |
| 54 | const mock = new MockProvider() | |
| 55 | expect(mock.getLastCall()).toBeUndefined() | |
| 56 | }) | |
| 57 | ||
| 58 | test('reset clears state', async () => { | |
| 59 | const mock = new MockProvider(['custom']) | |
| 60 | ||
| 61 | await mock.chat([{ role: 'user', content: 'test' }]) | |
| 62 | mock.reset() | |
| 63 | ||
| 64 | expect(mock.calls).toHaveLength(0) | |
| 65 | expect(mock.responses).toEqual(['Mock response']) | |
| 66 | ||
| 67 | const r = await mock.chat([{ role: 'user', content: 'after reset' }]) | |
| 68 | expect(r.content).toBe('Mock response') | |
| 69 | }) | |
| 70 | ||
| 71 | test('addResponse appends to responses', () => { | |
| 72 | const mock = new MockProvider() | |
| 73 | mock.addResponse('new response') | |
| 74 | ||
| 75 | expect(mock.responses).toContain('new response') | |
| 76 | }) | |
| 77 | ||
| 78 | test('stream yields chunks character by character', async () => { | |
| 79 | const mock = new MockProvider(['Hi!']) | |
| 80 | ||
| 81 | const chunks: Array<{ content: string, done: boolean, finishReason?: string }> = [] | |
| 82 | for await (const chunk of mock.stream!([{ role: 'user', content: 'test' }])) { | |
| 83 | chunks.push(chunk) | |
| 84 | } | |
| 85 | ||
| 86 | expect(chunks).toHaveLength(3) | |
| 87 | expect(chunks[0]).toEqual({ content: 'H', done: false, finishReason: undefined }) | |
| 88 | expect(chunks[1]).toEqual({ content: 'i', done: false, finishReason: undefined }) | |
| 89 | expect(chunks[2]).toEqual({ content: '!', done: true, finishReason: 'stop' }) | |
| 90 | }) | |
| 91 | ||
| 92 | test('stream records the call', async () => { | |
| 93 | const mock = new MockProvider(['ok']) | |
| 94 | ||
| 95 | // eslint-disable-next-line ts/no-unused-vars | |
| 96 | for await (const _chunk of mock.stream!([{ role: 'user', content: 'stream test' }])) { | |
| 97 | // consume | |
| 98 | } | |
| 99 | ||
| 100 | expect(mock.calls).toHaveLength(1) | |
| 101 | expect(mock.calls[0].messages[0].content).toBe('stream test') | |
| 102 | }) | |
| 103 | ||
| 104 | test('embed returns an embedding array', async () => { | |
| 105 | const mock = new MockProvider() | |
| 106 | ||
| 107 | const result = await mock.embed!('test text') | |
| 108 | ||
| 109 | expect(result.embedding).toHaveLength(128) | |
| 110 | expect(result.model).toBe('mock-embedding') | |
| 111 | expect(result.usage?.totalTokens).toBeGreaterThan(0) | |
| 112 | expect(typeof result.embedding[0]).toBe('number') | |
| 113 | }) | |
| 114 | ||
| 115 | test('embed returns consistent results for same input', async () => { | |
| 116 | const mock = new MockProvider() | |
| 117 | ||
| 118 | const r1 = await mock.embed!('same text') | |
| 119 | const r2 = await mock.embed!('same text') | |
| 120 | ||
| 121 | expect(r1.embedding).toEqual(r2.embedding) | |
| 122 | }) | |
| 123 | ||
| 124 | test('default constructor provides Mock response', async () => { | |
| 125 | const mock = new MockProvider() | |
| 126 | const r = await mock.chat([{ role: 'user', content: 'hi' }]) | |
| 127 | expect(r.content).toBe('Mock response') | |
| 128 | }) | |
| 129 | }) | |
| 130 | ||
| 131 | describe('anthropicProvider', () => { | |
| 132 | test('creates a provider with name "anthropic"', () => { | |
| 133 | const provider = anthropicProvider({ apiKey: 'test-key' }) | |
| 134 | expect(provider.name).toBe('anthropic') | |
| 135 | }) | |
| 136 | ||
| 137 | test('chat is a function', () => { | |
| 138 | const provider = anthropicProvider({ apiKey: 'test-key' }) | |
| 139 | expect(typeof provider.chat).toBe('function') | |
| 140 | }) | |
| 141 | ||
| 142 | test('stream is a function', () => { | |
| 143 | const provider = anthropicProvider({ apiKey: 'test-key' }) | |
| 144 | expect(typeof provider.stream).toBe('function') | |
| 145 | }) | |
| 146 | ||
| 147 | test('throws without API key', async () => { | |
| 148 | const provider = anthropicProvider({ apiKey: '' }) | |
| 149 | await expect(provider.chat([{ role: 'user', content: 'Hi' }])).rejects.toThrow('API key is required') | |
| 150 | }) | |
| 151 | ||
| 152 | test('can be registered with configureAI', () => { | |
| 153 | const provider = anthropicProvider({ apiKey: 'test-key' }) | |
| 154 | configureAI({ default: 'anthropic', providers: { anthropic: provider } }) | |
| 155 | ||
| 156 | const result = require('../src/ai').useAI() | |
| 157 | expect(result.name).toBe('anthropic') | |
| 158 | }) | |
| 159 | }) | |
| 160 | ||
| 161 | describe('openaiProvider', () => { | |
| 162 | test('creates a provider with name "openai"', () => { | |
| 163 | const provider = openaiProvider({ apiKey: 'test-key' }) | |
| 164 | expect(provider.name).toBe('openai') | |
| 165 | }) | |
| 166 | ||
| 167 | test('chat is a function', () => { | |
| 168 | const provider = openaiProvider({ apiKey: 'test-key' }) | |
| 169 | expect(typeof provider.chat).toBe('function') | |
| 170 | }) | |
| 171 | ||
| 172 | test('stream is a function', () => { | |
| 173 | const provider = openaiProvider({ apiKey: 'test-key' }) | |
| 174 | expect(typeof provider.stream).toBe('function') | |
| 175 | }) | |
| 176 | ||
| 177 | test('embed is a function', () => { | |
| 178 | const provider = openaiProvider({ apiKey: 'test-key' }) | |
| 179 | expect(typeof provider.embed).toBe('function') | |
| 180 | }) | |
| 181 | ||
| 182 | test('throws without API key', async () => { | |
| 183 | const provider = openaiProvider({ apiKey: '' }) | |
| 184 | await expect(provider.chat([{ role: 'user', content: 'Hi' }])).rejects.toThrow('API key is required') | |
| 185 | }) | |
| 186 | ||
| 187 | test('accepts custom baseUrl', () => { | |
| 188 | const provider = openaiProvider({ apiKey: 'key', baseUrl: 'https://custom.api.com/v1' }) | |
| 189 | expect(provider.name).toBe('openai') | |
| 190 | // Just verifying it constructs without error | |
| 191 | }) | |
| 192 | }) | |