|
| 1 | +import { |
| 2 | + type AssistantMessage, |
| 3 | + calculateUsageCost, |
| 4 | + clone, |
| 5 | + completePartialJson, |
| 6 | + createAssistantMessageEventStream, |
| 7 | + createEmptyUsage, |
| 8 | + type ModelDescriptor, |
| 9 | + normalizeBaseUrl, |
| 10 | + parsePartialJson, |
| 11 | +} from '../src/index.js'; |
| 12 | + |
| 13 | +const model: ModelDescriptor = { |
| 14 | + id: 'test-model', |
| 15 | + name: 'Test', |
| 16 | + api: 'test-api', |
| 17 | + provider: 'test', |
| 18 | + baseUrl: 'https://example.com/v1', |
| 19 | + input: ['text'], |
| 20 | + reasoning: false, |
| 21 | + cost: { input: 3, output: 15 }, |
| 22 | +}; |
| 23 | + |
| 24 | +describe('protocol kernel', () => { |
| 25 | + it('createEmptyUsage produces a fully zeroed usage', () => { |
| 26 | + expect(createEmptyUsage()).toEqual({ |
| 27 | + input: 0, |
| 28 | + output: 0, |
| 29 | + reasoning: 0, |
| 30 | + cacheRead: 0, |
| 31 | + cacheWrite: 0, |
| 32 | + totalTokens: 0, |
| 33 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('calculateUsageCost applies the per-million schedule', () => { |
| 38 | + const usage = createEmptyUsage(); |
| 39 | + usage.input = 1_000_000; |
| 40 | + usage.output = 2_000_000; |
| 41 | + calculateUsageCost(model, usage); |
| 42 | + expect(usage.cost.input).toBeCloseTo(3); |
| 43 | + expect(usage.cost.output).toBeCloseTo(30); |
| 44 | + expect(usage.cost.total).toBeCloseTo(33); |
| 45 | + }); |
| 46 | + |
| 47 | + it('parsePartialJson recovers truncated tool arguments', () => { |
| 48 | + expect(parsePartialJson('{"city": "Paris')).toEqual({ city: 'Paris' }); |
| 49 | + expect(parsePartialJson('{"items": [1, 2')).toEqual({ items: [1, 2] }); |
| 50 | + expect(parsePartialJson('')).toEqual({}); |
| 51 | + }); |
| 52 | + |
| 53 | + it('completePartialJson closes open brackets and strings', () => { |
| 54 | + expect(completePartialJson('{"a": [1, {"b": "x')).toBe('{"a": [1, {"b": "x"}]}'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('normalizeBaseUrl appends /v1 only when no version segment is present', () => { |
| 58 | + expect(normalizeBaseUrl('https://example.com')).toBe('https://example.com/v1'); |
| 59 | + expect(normalizeBaseUrl('https://example.com/v1')).toBe('https://example.com/v1'); |
| 60 | + expect(normalizeBaseUrl('https://example.com/v2/')).toBe('https://example.com/v2'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('clone deep-copies without sharing references', () => { |
| 64 | + const source = { a: { b: 1 }, list: [1, 2, 3] }; |
| 65 | + const copy = clone(source); |
| 66 | + expect(copy).toEqual(source); |
| 67 | + expect(copy.a).not.toBe(source.a); |
| 68 | + }); |
| 69 | + |
| 70 | + it('EventStream yields events in order and resolves the terminal result', async () => { |
| 71 | + const stream = createAssistantMessageEventStream(); |
| 72 | + const message: AssistantMessage = { |
| 73 | + role: 'assistant', |
| 74 | + content: [{ type: 'text', text: 'hi' }], |
| 75 | + api: model.api, |
| 76 | + provider: model.provider, |
| 77 | + model: model.id, |
| 78 | + usage: createEmptyUsage(), |
| 79 | + stopReason: 'stop', |
| 80 | + timestamp: Date.now(), |
| 81 | + }; |
| 82 | + |
| 83 | + stream.push({ type: 'start', partial: message }); |
| 84 | + stream.push({ type: 'done', reason: 'stop', message }); |
| 85 | + stream.end(message); |
| 86 | + |
| 87 | + const seen: string[] = []; |
| 88 | + for await (const event of stream) { |
| 89 | + seen.push(event.type); |
| 90 | + } |
| 91 | + expect(seen).toEqual(['start', 'done']); |
| 92 | + await expect(stream.result()).resolves.toBe(message); |
| 93 | + }); |
| 94 | +}); |
0 commit comments