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

events.test.ts

326 lines9.2 KB

1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { aiEventClient } from '@tanstack/ai-event-client'
3import { DefaultChatClientEventEmitter } from '../src/events'
4import type { UIMessage } from '../src/types'
5​
6vi.mock('@tanstack/ai-event-client', () => ({
7 aiEventClient: {
8 emit: vi.fn(),
9 },
10 createAIDevtoolsEventEnvelope: (input: {
11 eventType: string
12 timestamp: number
13 }) => ({
14 ...input,
15 eventId: `event:${input.eventType}:${input.timestamp}`,
16 }),
17}))
18​
19describe('events', () => {
20 beforeEach(() => {
21 vi.clearAllMocks()
22 })
23​
24 function expectedEnvelope(
25 eventType: string,
26 visibility: 'client-state' | 'user-visible' = 'client-state',
27 ) {
28 return {
29 clientId: 'test-client-id',
30 hookId: 'test-client-id',
31 eventId: expect.any(String),
32 eventType,
33 source: 'client',
34 visibility,
35 timestamp: expect.any(Number),
36 }
37 }
38​
39 describe('DefaultChatClientEventEmitter', () => {
40 let emitter: DefaultChatClientEventEmitter
41​
42 beforeEach(() => {
43 emitter = new DefaultChatClientEventEmitter('test-client-id')
44 })
45​
46 it('emits client:created with client-state envelope fields', () => {
47 emitter.clientCreated(5)
48​
49 expect(aiEventClient.emit).toHaveBeenCalledWith('client:created', {
50 initialMessageCount: 5,
51 ...expectedEnvelope('client:created'),
52 })
53 })
54​
55 it('emits client:loading:changed with client-state envelope fields', () => {
56 emitter.loadingChanged(true)
57​
58 expect(aiEventClient.emit).toHaveBeenCalledWith(
59 'client:loading:changed',
60 {
61 isLoading: true,
62 ...expectedEnvelope('client:loading:changed'),
63 },
64 )
65 })
66​
67 it('emits client:error:changed with null', () => {
68 emitter.errorChanged(null)
69​
70 expect(aiEventClient.emit).toHaveBeenCalledWith('client:error:changed', {
71 error: null,
72 ...expectedEnvelope('client:error:changed'),
73 })
74 })
75​
76 it('emits client:error:changed with an error string', () => {
77 emitter.errorChanged('Something went wrong')
78​
79 expect(aiEventClient.emit).toHaveBeenCalledWith('client:error:changed', {
80 error: 'Something went wrong',
81 ...expectedEnvelope('client:error:changed'),
82 })
83 })
84​
85 it('emits text:chunk:content with user-visible envelope and run context', () => {
86 emitter.textUpdated('stream-1', 'msg-1', 'Hello world', {
87 threadId: 'thread-1',
88 runId: 'run-1',
89 })
90​
91 expect(aiEventClient.emit).toHaveBeenCalledWith('text:chunk:content', {
92 streamId: 'stream-1',
93 messageId: 'msg-1',
94 content: 'Hello world',
95 threadId: 'thread-1',
96 runId: 'run-1',
97 ...expectedEnvelope('text:chunk:content', 'user-visible'),
98 })
99 })
100​
101 it('emits text:chunk:thinking with user-visible envelope and run context', () => {
102 emitter.thinkingUpdated('stream-1', 'msg-1', 'reasoning', 'ing', {
103 threadId: 'thread-1',
104 runId: 'run-1',
105 })
106​
107 expect(aiEventClient.emit).toHaveBeenCalledWith('text:chunk:thinking', {
108 streamId: 'stream-1',
109 messageId: 'msg-1',
110 content: 'reasoning',
111 delta: 'ing',
112 threadId: 'thread-1',
113 runId: 'run-1',
114 ...expectedEnvelope('text:chunk:thinking', 'user-visible'),
115 })
116 })
117​
118 it('emits tools:call:updated with user-visible envelope and run context', () => {
119 emitter.toolCallStateChanged(
120 'stream-1',
121 'msg-1',
122 'call-1',
123 'get_weather',
124 'input-complete',
125 '{"city": "NYC"}',
126 { threadId: 'thread-1', runId: 'run-1' },
127 )
128​
129 expect(aiEventClient.emit).toHaveBeenCalledWith('tools:call:updated', {
130 streamId: 'stream-1',
131 messageId: 'msg-1',
132 toolCallId: 'call-1',
133 toolName: 'get_weather',
134 state: 'input-complete',
135 arguments: '{"city": "NYC"}',
136 threadId: 'thread-1',
137 runId: 'run-1',
138 ...expectedEnvelope('tools:call:updated', 'user-visible'),
139 })
140 })
141​
142 it('emits tools:approval:requested with user-visible envelope and run context', () => {
143 emitter.approvalRequested(
144 'stream-1',
145 'msg-1',
146 'call-1',
147 'get_weather',
148 { city: 'NYC' },
149 'approval-1',
150 { threadId: 'thread-1', runId: 'run-1' },
151 )
152​
153 expect(aiEventClient.emit).toHaveBeenCalledWith(
154 'tools:approval:requested',
155 {
156 streamId: 'stream-1',
157 messageId: 'msg-1',
158 toolCallId: 'call-1',
159 toolName: 'get_weather',
160 input: { city: 'NYC' },
161 approvalId: 'approval-1',
162 threadId: 'thread-1',
163 runId: 'run-1',
164 ...expectedEnvelope('tools:approval:requested', 'user-visible'),
165 },
166 )
167 })
168​
169 it('emits text:message:created with full content and run context', () => {
170 const uiMessage: UIMessage = {
171 id: 'msg-1',
172 role: 'user',
173 parts: [
174 { type: 'text', content: 'Hello' },
175 { type: 'text', content: 'World' },
176 ],
177 createdAt: new Date(),
178 }
179​
180 emitter.messageAppended(uiMessage, 'stream-1', {
181 threadId: 'thread-1',
182 runId: 'run-1',
183 })
184​
185 expect(aiEventClient.emit).toHaveBeenCalledWith('text:message:created', {
186 streamId: 'stream-1',
187 messageId: 'msg-1',
188 role: 'user',
189 content: 'Hello World',
190 parts: uiMessage.parts,
191 threadId: 'thread-1',
192 runId: 'run-1',
193 ...expectedEnvelope('text:message:created', 'user-visible'),
194 })
195 })
196​
197 it('handles a message with no text parts', () => {
198 const uiMessage: UIMessage = {
199 id: 'msg-1',
200 role: 'assistant',
201 parts: [
202 {
203 type: 'tool-call',
204 id: 'call-1',
205 name: 'tool1',
206 arguments: '{}',
207 state: 'input-complete',
208 },
209 ],
210 createdAt: new Date(),
211 }
212​
213 emitter.messageAppended(uiMessage)
214​
215 expect(aiEventClient.emit).toHaveBeenCalledWith('text:message:created', {
216 streamId: undefined,
217 messageId: 'msg-1',
218 role: 'assistant',
219 content: '',
220 parts: uiMessage.parts,
221 ...expectedEnvelope('text:message:created', 'user-visible'),
222 })
223 })
224​
225 it('emits text:message:created and text:message:user for sent messages', () => {
226 emitter.messageSent('msg-1', 'Hello world')
227​
228 expect(aiEventClient.emit).toHaveBeenCalledTimes(2)
229 expect(aiEventClient.emit).toHaveBeenNthCalledWith(
230 1,
231 'text:message:created',
232 {
233 messageId: 'msg-1',
234 role: 'user',
235 content: 'Hello world',
236 ...expectedEnvelope('text:message:created', 'user-visible'),
237 },
238 )
239 expect(aiEventClient.emit).toHaveBeenNthCalledWith(
240 2,
241 'text:message:user',
242 {
243 messageId: 'msg-1',
244 role: 'user',
245 content: 'Hello world',
246 ...expectedEnvelope('text:message:user', 'user-visible'),
247 },
248 )
249 })
250​
251 it('emits client:reloaded with client-state envelope fields', () => {
252 emitter.reloaded(3)
253​
254 expect(aiEventClient.emit).toHaveBeenCalledWith('client:reloaded', {
255 fromMessageIndex: 3,
256 ...expectedEnvelope('client:reloaded'),
257 })
258 })
259​
260 it('emits client:stopped with client-state envelope fields', () => {
261 emitter.stopped()
262​
263 expect(aiEventClient.emit).toHaveBeenCalledWith('client:stopped', {
264 ...expectedEnvelope('client:stopped'),
265 })
266 })
267​
268 it('emits client:messages:cleared with client-state envelope fields', () => {
269 emitter.messagesCleared()
270​
271 expect(aiEventClient.emit).toHaveBeenCalledWith(
272 'client:messages:cleared',
273 {
274 ...expectedEnvelope('client:messages:cleared'),
275 },
276 )
277 })
278​
279 it('emits tools:result:added with user-visible envelope and run context', () => {
280 emitter.toolResultAdded(
281 'call-1',
282 'get_weather',
283 { temp: 72 },
284 'output-available',
285 { threadId: 'thread-1', runId: 'run-1' },
286 )
287​
288 expect(aiEventClient.emit).toHaveBeenCalledWith('tools:result:added', {
289 toolCallId: 'call-1',
290 toolName: 'get_weather',
291 output: { temp: 72 },
292 state: 'output-available',
293 threadId: 'thread-1',
294 runId: 'run-1',
295 ...expectedEnvelope('tools:result:added', 'user-visible'),
296 })
297 })
298​
299 it('emits devtools:tool-fixture:applied with a user-visible envelope', () => {
300 emitter.toolFixtureApplied({
301 hookId: 'test-client-id',
302 threadId: 'thread-1',
303 runId: 'run-1',
304 toolName: 'get_weather',
305 input: { city: 'NYC' },
306 output: { temp: 72 },
307 messageId: 'msg-fixture',
308 toolCallId: 'call-fixture',
309 })
310​
311 expect(aiEventClient.emit).toHaveBeenCalledWith(
312 'devtools:tool-fixture:applied',
313 {
314 threadId: 'thread-1',
315 runId: 'run-1',
316 toolName: 'get_weather',
317 input: { city: 'NYC' },
318 output: { temp: 72 },
319 messageId: 'msg-fixture',
320 toolCallId: 'call-fixture',
321 ...expectedEnvelope('devtools:tool-fixture:applied', 'user-visible'),
322 },
323 )
324 })
325 })
326})