78 lines
2.6 KiB
TypeScript
78 lines
2.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { createIpcTransport, type IpcStreamChunk, type IpcStreamStart } from '../src'
|
|
|
|
interface FakeSettings {
|
|
provider: string
|
|
}
|
|
|
|
function setup() {
|
|
let listener: ((chunk: IpcStreamChunk) => void) | undefined
|
|
const unsubscribe = vi.fn(() => {
|
|
listener = undefined
|
|
})
|
|
const started: IpcStreamStart<FakeSettings>[] = []
|
|
const cancelled: string[] = []
|
|
const transport = createIpcTransport<FakeSettings>({
|
|
onStream: (l) => {
|
|
listener = l
|
|
return unsubscribe
|
|
},
|
|
start: (request) => started.push(request),
|
|
cancel: (requestId) => cancelled.push(requestId),
|
|
getSettings: () => ({ provider: 'genspark' }),
|
|
unknownErrorText: () => 'unknown error',
|
|
})
|
|
const cb = {
|
|
onDelta: vi.fn(),
|
|
onToolCall: vi.fn(),
|
|
onDone: vi.fn(),
|
|
onError: vi.fn(),
|
|
}
|
|
const handle = transport.stream({ system: 'sys', messages: [], tools: [] }, cb)
|
|
const emit = (chunk: Omit<IpcStreamChunk, 'requestId'> & { requestId?: string }) =>
|
|
listener?.({ requestId: started[0]!.requestId, ...chunk })
|
|
return { started, cancelled, cb, handle, emit, unsubscribe }
|
|
}
|
|
|
|
describe('createIpcTransport', () => {
|
|
it('starts one request with settings and forwards deltas and tool calls', () => {
|
|
const { started, cb, emit } = setup()
|
|
expect(started).toHaveLength(1)
|
|
expect(started[0]!.settings).toEqual({ provider: 'genspark' })
|
|
expect(started[0]!.system).toBe('sys')
|
|
|
|
emit({ type: 'delta', text: 'hi' })
|
|
emit({ type: 'delta' })
|
|
emit({ type: 'tool-call', toolCall: { id: 'c1', name: 'read', input: {} } })
|
|
expect(cb.onDelta).toHaveBeenNthCalledWith(1, 'hi')
|
|
expect(cb.onDelta).toHaveBeenNthCalledWith(2, '')
|
|
expect(cb.onToolCall).toHaveBeenCalledWith({ id: 'c1', name: 'read', input: {} })
|
|
})
|
|
|
|
it('ignores chunks for other requestIds', () => {
|
|
const { cb, emit } = setup()
|
|
emit({ requestId: 'someone-else', type: 'delta', text: 'nope' })
|
|
expect(cb.onDelta).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('unsubscribes on done', () => {
|
|
const { cb, emit, unsubscribe } = setup()
|
|
emit({ type: 'done' })
|
|
expect(cb.onDone).toHaveBeenCalledTimes(1)
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('maps error chunks to onError with the localized fallback', () => {
|
|
const { cb, emit, unsubscribe } = setup()
|
|
emit({ type: 'error' })
|
|
expect(cb.onError).toHaveBeenCalledWith('unknown error')
|
|
expect(unsubscribe).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('cancel forwards the requestId to the bridge', () => {
|
|
const { started, cancelled, handle } = setup()
|
|
handle.cancel()
|
|
expect(cancelled).toEqual([started[0]!.requestId])
|
|
})
|
|
})
|