also looking at this
chore: wip stx improvements
#1649
241 files
+19022
-1323
| @@ -0,0 +1,82 @@ | ||
| 1 | import { afterEach, describe, expect, test } from 'bun:test' | |
| 2 | import { configureAI, resetAI } from '../src/ai' | |
| 3 | import { MockProvider } from '../src/providers/mock' | |
| 4 | import { collectStream, streamChat } from '../src/streaming' | |
| 5 | ||
| 6 | afterEach(() => { | |
| 7 | resetAI() | |
| 8 | }) | |
| 9 | ||
| 10 | describe('streamChat', () => { | |
| 11 | test('yields StreamChunk objects', async () => { | |
| 12 | const mock = new MockProvider(['Hi!']) | |
| 13 | configureAI({ default: 'mock', providers: { mock } }) | |
| 14 | ||
| 15 | const chunks: Array<{ content: string, done: boolean }> = [] | |
| 16 | for await (const chunk of streamChat([{ role: 'user', content: 'Hello' }])) { | |
| 17 | chunks.push(chunk) | |
| 18 | } | |
| 19 | ||
| 20 | expect(chunks.length).toBe(3) // 'H', 'i', '!' | |
| 21 | expect(chunks[0].content).toBe('H') | |
| 22 | expect(chunks[0].done).toBe(false) | |
| 23 | expect(chunks[2].content).toBe('!') | |
| 24 | expect(chunks[2].done).toBe(true) | |
| 25 | }) | |
| 26 | ||
| 27 | test('uses specified provider', async () => { | |
| 28 | const mock1 = new MockProvider(['abc']) | |
| 29 | const mock2 = new MockProvider(['xy']) | |
| 30 | mock2.name = 'mock2' | |
| 31 | ||
| 32 | configureAI({ default: 'mock', providers: { mock: mock1, mock2 } }) | |
| 33 | ||
| 34 | const chunks: string[] = [] | |
| 35 | for await (const chunk of streamChat([{ role: 'user', content: 'Hi' }], { provider: 'mock2' })) { | |
| 36 | chunks.push(chunk.content) | |
| 37 | } | |
| 38 | ||
| 39 | expect(chunks.join('')).toBe('xy') | |
| 40 | }) | |
| 41 | ||
| 42 | test('throws if provider does not support streaming', async () => { | |
| 43 | const provider = { | |
| 44 | name: 'no-stream', | |
| 45 | chat: async () => ({ content: 'ok', model: 'test' }), | |
| 46 | } | |
| 47 | ||
| 48 | configureAI({ default: 'no-stream', providers: { 'no-stream': provider } }) | |
| 49 | ||
| 50 | const gen = streamChat([{ role: 'user', content: 'Hi' }]) | |
| 51 | try { | |
| 52 | // eslint-disable-next-line ts/no-unused-vars | |
| 53 | for await (const _chunk of gen) { | |
| 54 | // should not reach here | |
| 55 | } | |
| 56 | expect(true).toBe(false) // should not reach | |
| 57 | } | |
| 58 | catch (err: any) { | |
| 59 | expect(err.message).toContain('does not support streaming') | |
| 60 | } | |
| 61 | }) | |
| 62 | }) | |
| 63 | ||
| 64 | describe('collectStream', () => { | |
| 65 | test('concatenates all chunks into a string', async () => { | |
| 66 | const mock = new MockProvider(['Hello world']) | |
| 67 | configureAI({ default: 'mock', providers: { mock } }) | |
| 68 | ||
| 69 | const stream = streamChat([{ role: 'user', content: 'Hi' }]) | |
| 70 | const result = await collectStream(stream) | |
| 71 | expect(result).toBe('Hello world') | |
| 72 | }) | |
| 73 | ||
| 74 | test('handles empty stream', async () => { | |
| 75 | async function* emptyStream() { | |
| 76 | // yields nothing | |
| 77 | } | |
| 78 | ||
| 79 | const result = await collectStream(emptyStream()) | |
| 80 | expect(result).toBe('') | |
| 81 | }) | |
| 82 | }) | |