Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

create-chat.test.ts

124 lines3.1 KB

1import { EventType } from '@tanstack/ai/client'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { ChatClient } from '@tanstack/ai-client'
4import { createChat } from '../src/create-chat'
5import type { AnyClientTool } from '@tanstack/ai-client'
6import type { Handle } from 'remix/ui'
7import type { StreamChunk } from '@tanstack/ai/client'
8​
9const abortControllers: Array<AbortController> = []
10​
11afterEach(() => {
12 for (const abort of abortControllers) {
13 abort.abort()
14 }
15 abortControllers.length = 0
16 vi.restoreAllMocks()
17})
18​
19function createHandle() {
20 const abort = new AbortController()
21 abortControllers.push(abort)
22 const frame = Object.assign(new EventTarget(), {
23 src: '',
24 reload: async () => abort.signal,
25 replace: async () => {},
26 })
27 const handle: Handle = {
28 id: 'handle-1',
29 update: vi.fn(async () => abort.signal),
30 signal: abort.signal,
31 props: {},
32 context: {
33 set() {},
34 get() {
35 return undefined
36 },
37 },
38 queueTask() {},
39 frame,
40 frames: {
41 top: frame,
42 get() {
43 return undefined
44 },
45 },
46 }
47 return { handle, abort }
49​
50function textChunk(delta: string, messageId = 'assistant-1'): StreamChunk {
51 return {
52 type: EventType.TEXT_MESSAGE_CONTENT,
53 messageId,
54 timestamp: Date.now(),
55 delta,
56 }
58​
59function runFinished(runId = 'run-1'): StreamChunk {
60 return {
61 type: EventType.RUN_FINISHED,
62 runId,
63 threadId: 'thread-1',
64 timestamp: Date.now(),
65 }
67​
68function createConnection(chunks: Array<StreamChunk>) {
69 return {
70 async *connect() {
71 for (const chunk of chunks) {
72 yield chunk
73 }
74 },
75 }
77​
78describe('createChat', () => {
79 it('sends the current getter tools when a run starts', async () => {
80 const { handle } = createHandle()
81 const updateOptions = vi.spyOn(ChatClient.prototype, 'updateOptions')
82 let tools: Array<AnyClientTool> = []
83 const chat = createChat(handle, {
84 connection: createConnection([textChunk('Hi'), runFinished()]),
85 get tools() {
86 return tools
87 },
88 })
89​
90 tools = [{ __toolSide: 'client', name: 'late', description: 'Late' }]
91 await chat.sendMessage('Hello')
92​
93 expect(updateOptions).toHaveBeenCalledWith({ tools })
94 })
95​
96 it('records user and assistant messages and calls handle.update after sendMessage', async () => {
97 const { handle } = createHandle()
98 const chat = createChat(handle, {
99 connection: createConnection([textChunk('Hi'), runFinished()]),
100 })
101​
102 await chat.sendMessage('Hello')
103​
104 expect(chat.messages.map((message) => message.role)).toEqual([
105 'user',
106 'assistant',
107 ])
108 expect(handle.update).toHaveBeenCalled()
109 })
110​
111 it('does not call handle.update after the handle signal aborts', async () => {
112 const { handle, abort } = createHandle()
113 const chat = createChat(handle, {
114 connection: createConnection([textChunk('Hi'), runFinished()]),
115 })
116​
117 vi.mocked(handle.update).mockClear()
118 abort.abort()
119​
120 await chat.sendMessage('Hello').catch(() => {})
121​
122 expect(handle.update).not.toHaveBeenCalled()
123 })
124})