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

chat-client-queue.test.ts

843 lines26.1 KB

1import { describe, expect, it, vi } from 'vitest'
2import { EventType } from '@tanstack/ai/client'
3import { ChatClient, normalizeQueueOption } from '../src/chat-client'
4import { createTextChunks } from './test-utils'
5import type { ConnectConnectionAdapter } from '../src/connection-adapters'
6import type { StreamChunk } from '@tanstack/ai/client'
7​
8describe('normalizeQueueOption', () => {
9 it('defaults to queue + fifo + reject', () => {
10 expect(normalizeQueueOption(undefined)).toEqual({
11 whenBusy: 'queue',
12 drain: 'fifo',
13 onOverflow: 'reject',
14 })
15 })
16​
17 it('treats a string as whenBusy shorthand', () => {
18 expect(normalizeQueueOption('interrupt')).toMatchObject({
19 whenBusy: 'interrupt',
20 drain: 'fifo',
21 })
22 })
23​
24 it('carries a function as strategy and forces fifo', () => {
25 const fn = () => ({ action: 'queue' as const })
26 const cfg = normalizeQueueOption(fn)
27 expect(cfg.strategy).toBe(fn)
28 expect(cfg.drain).toBe('fifo')
29 })
30​
31 it('merges a config object over defaults', () => {
32 expect(normalizeQueueOption({ whenBusy: 'drop', maxSize: 3 })).toEqual({
33 whenBusy: 'drop',
34 drain: 'fifo',
35 onOverflow: 'reject',
36 maxSize: 3,
37 })
38 })
39​
40 it('rejects invalid maxSize', () => {
41 expect(() => normalizeQueueOption({ maxSize: -1 })).toThrow(/maxSize/)
42 expect(() => normalizeQueueOption({ maxSize: 1.5 })).toThrow(/maxSize/)
43 })
44})
45​
46/**
47 * Creates a deferred promise. `resolve` releases anything awaiting `promise`.
48 */
49function createDeferred<T>() {
50 let resolve!: (value: T) => void
51 const promise = new Promise<T>((promiseResolve) => {
52 resolve = promiseResolve
53 })
54 return { promise, resolve }
56​
57/**
58 * A `ConnectConnectionAdapter` whose stream stays open (keeping `isLoading`
59 * true) until `release()` is called, at which point it finishes with a
60 * simple text response.
61 */
62function createHoldingConnection(): {
63 connection: ConnectConnectionAdapter
64 release: () => void
65} {
66 const deferred = createDeferred<void>()
67 const connection: ConnectConnectionAdapter = {
68 async *connect() {
69 await deferred.promise
70 yield* createTextChunks('done', 'msg-1')
71 },
72 }
73 return { connection, release: () => deferred.resolve() }
75​
76describe('ChatClient message queue', () => {
77 it('enqueues (not drops) a send while streaming and reports via onQueueChange', async () => {
78 const { connection, release } = createHoldingConnection()
79 const onQueueChange = vi.fn()
80 const client = new ChatClient({ connection, onQueueChange })
81​
82 const firstSend = client.sendMessage('first')
83 await vi.waitFor(() => {
84 expect(client.getIsLoading()).toBe(true)
85 })
86​
87 await client.sendMessage('second')
88​
89 const queue = client.getQueue()
90 expect(queue).toHaveLength(1)
91 expect(queue[0]?.content).toBe('second')
92 expect(onQueueChange).toHaveBeenCalledWith(
93 expect.arrayContaining([expect.objectContaining({ content: 'second' })]),
94 )
95 expect(onQueueChange.mock.calls.at(-1)?.[0]).toHaveLength(1)
96​
97 release()
98 await firstSend
99 })
100​
101 it('cancelQueued removes a queued item before it drains', async () => {
102 const { connection, release } = createHoldingConnection()
103 const client = new ChatClient({ connection })
104​
105 const firstSend = client.sendMessage('first')
106 await vi.waitFor(() => {
107 expect(client.getIsLoading()).toBe(true)
108 })
109​
110 await client.sendMessage('second')
111 const queued = client.getQueue()
112 expect(queued).toHaveLength(1)
113 const queuedId = queued[0]!.id
114​
115 client.cancelQueued(queuedId)
116 expect(client.getQueue()).toEqual([])
117​
118 release()
119 await firstSend
120 })
121​
122 it("whenBusy: 'drop' ignores a mid-stream send", async () => {
123 const { connection, release } = createHoldingConnection()
124 const client = new ChatClient({ connection, queue: 'drop' })
125​
126 const firstSend = client.sendMessage('first')
127 await vi.waitFor(() => {
128 expect(client.getIsLoading()).toBe(true)
129 })
130​
131 await client.sendMessage('second')
132 expect(client.getQueue()).toEqual([])
133​
134 release()
135 await firstSend
136​
137 // The dropped message must never have become a real message either —
138 // it should be gone entirely, not just missing from the queue.
139 const userMessages = client.getMessages().filter((m) => m.role === 'user')
140 expect(userMessages.map((m) => m.parts[0])).toEqual([
141 { type: 'text', content: 'first' },
142 ])
143 })
144})
145​
146/**
147 * Like {@link createHoldingConnection}, but only the first `connect()` call
148 * waits on the deferred; every subsequent call (i.e. a drained queue item's
149 * own send) resolves immediately with a unique messageId so chained drains
150 * don't collide on the same assistant message id.
151 */
152function createSequencedHoldingConnection(): {
153 connection: ConnectConnectionAdapter
154 release: () => void
155} {
156 const deferred = createDeferred<void>()
157 let call = 0
158 const connection: ConnectConnectionAdapter = {
159 async *connect() {
160 call += 1
161 if (call === 1) {
162 await deferred.promise
163 }
164 yield* createTextChunks('done', `msg-${call}`)
165 },
166 }
167 return { connection, release: () => deferred.resolve() }
169​
170/**
171 * Like {@link createHoldingConnection}, but `release()` settles the stream
172 * with a `RUN_ERROR` chunk instead of a successful text response.
173 */
174function createErroringHoldingConnection(): {
175 connection: ConnectConnectionAdapter
176 release: () => void
177} {
178 const deferred = createDeferred<void>()
179 const connection: ConnectConnectionAdapter = {
180 async *connect(): AsyncGenerator<StreamChunk> {
181 await deferred.promise
182 yield {
183 type: EventType.RUN_ERROR,
184 threadId: 'thread-1',
185 timestamp: Date.now(),
186 message: 'boom',
187 error: { message: 'boom' },
188 }
189 },
190 }
191 return { connection, release: () => deferred.resolve() }
193​
194describe('ChatClient queue drain', () => {
195 it('drains FIFO after the stream settles, in order', async () => {
196 const { connection, release } = createSequencedHoldingConnection()
197 const client = new ChatClient({ connection })
198​
199 const firstSend = client.sendMessage('first')
200 await vi.waitFor(() => {
201 expect(client.getIsLoading()).toBe(true)
202 })
203​
204 await client.sendMessage('second')
205 await client.sendMessage('third')
206 expect(client.getQueue().map((m) => m.content)).toEqual(['second', 'third'])
207​
208 release()
209 await firstSend
210​
211 expect(client.getQueue()).toEqual([])
212 const userMessages = client.getMessages().filter((m) => m.role === 'user')
213 expect(userMessages.map((m) => m.parts[0])).toEqual([
214 { type: 'text', content: 'first' },
215 { type: 'text', content: 'second' },
216 { type: 'text', content: 'third' },
217 ])
218 })
219​
220 it('batch drain merges string queued items with newlines', async () => {
221 const { connection, release } = createSequencedHoldingConnection()
222 const client = new ChatClient({ connection, queue: { drain: 'batch' } })
223​
224 const firstSend = client.sendMessage('first')
225 await vi.waitFor(() => {
226 expect(client.getIsLoading()).toBe(true)
227 })
228​
229 await client.sendMessage('a')
230 await client.sendMessage('b')
231 expect(client.getQueue().map((m) => m.content)).toEqual(['a', 'b'])
232​
233 release()
234 await firstSend
235​
236 expect(client.getQueue()).toEqual([])
237 const userMessages = client.getMessages().filter((m) => m.role === 'user')
238 expect(userMessages).toHaveLength(2)
239 expect(userMessages[1]?.parts[0]).toEqual({
240 type: 'text',
241 content: 'a\nb',
242 })
243 })
244​
245 it('batch drain flattens multimodal queued items into ContentPart[]', async () => {
246 const { connection, release } = createSequencedHoldingConnection()
247 const client = new ChatClient({ connection, queue: { drain: 'batch' } })
248​
249 const firstSend = client.sendMessage('first')
250 await vi.waitFor(() => {
251 expect(client.getIsLoading()).toBe(true)
252 })
253​
254 // Queued item 1: multimodal content (array of ContentPart).
255 await client.sendMessage({
256 content: [
257 { type: 'text', content: 'look' },
258 {
259 type: 'image',
260 source: { type: 'url', value: 'https://example.com/a.png' },
261 },
262 ],
263 })
264 // Queued item 2: plain string content.
265 await client.sendMessage('note')
266 expect(client.getQueue()).toHaveLength(2)
267​
268 release()
269 await firstSend
270​
271 expect(client.getQueue()).toEqual([])
272 const userMessages = client.getMessages().filter((m) => m.role === 'user')
273 // 'first' streamed immediately; the two queued sends above are merged
274 // into a single batched send, so there should be exactly 2 user messages.
275 expect(userMessages).toHaveLength(2)
276 // The merged message's parts must be the multimodal item's parts
277 // (flattened, in order) followed by the string item flattened to text —
278 // proving the MULTIMODAL branch of `mergeQueuedMessages` ran, not just
279 // the all-string join.
280 expect(userMessages[1]?.parts).toEqual([
281 { type: 'text', content: 'look' },
282 {
283 type: 'image',
284 source: { type: 'url', value: 'https://example.com/a.png' },
285 },
286 { type: 'text', content: 'note' },
287 ])
288 })
289​
290 it('flushes the queue on stop()', async () => {
291 const { connection, release } = createHoldingConnection()
292 const client = new ChatClient({ connection })
293​
294 const firstSend = client.sendMessage('first')
295 await vi.waitFor(() => {
296 expect(client.getIsLoading()).toBe(true)
297 })
298​
299 await client.sendMessage('second')
300 expect(client.getQueue()).toHaveLength(1)
301​
302 client.stop()
303 expect(client.getQueue()).toEqual([])
304​
305 // Let the held-open stream settle so it doesn't leak into other tests.
306 release()
307 await firstSend
308 })
309​
310 it('flushes the queue when the stream errors', async () => {
311 const { connection, release } = createErroringHoldingConnection()
312 const client = new ChatClient({ connection })
313​
314 const firstSend = client.sendMessage('first')
315 await vi.waitFor(() => {
316 expect(client.getIsLoading()).toBe(true)
317 })
318​
319 await client.sendMessage('second')
320 expect(client.getQueue()).toHaveLength(1)
321​
322 release()
323 await firstSend
324​
325 // The queue must be flushed, not stranded or auto-drained into the
326 // now-broken endpoint.
327 expect(client.getQueue()).toEqual([])
328 const userMessages = client.getMessages().filter((m) => m.role === 'user')
329 expect(userMessages.map((m) => m.parts[0])).toEqual([
330 { type: 'text', content: 'first' },
331 ])
332 })
333​
334 it('flushes the queue on clear()', async () => {
335 const { connection, release } = createHoldingConnection()
336 const client = new ChatClient({ connection })
337​
338 const firstSend = client.sendMessage('first')
339 await vi.waitFor(() => {
340 expect(client.getIsLoading()).toBe(true)
341 })
342​
343 await client.sendMessage('second')
344 expect(client.getQueue()).toHaveLength(1)
345​
346 client.clear()
347 expect(client.getQueue()).toEqual([])
348​
349 release()
350 await firstSend
351 })
352})
353​
354describe('ChatClient queue policy branches', () => {
355 it("whenBusy: 'interrupt' aborts the in-flight stream and sends immediately", async () => {
356 const { connection, release } = createSequencedHoldingConnection()
357 const client = new ChatClient({ connection, queue: 'interrupt' })
358​
359 const firstSend = client.sendMessage('first')
360 await vi.waitFor(() => {
361 expect(client.getIsLoading()).toBe(true)
362 })
363​
364 const secondSend = client.sendMessage('urgent')
365 // Interrupt does not enqueue.
366 expect(client.getQueue()).toEqual([])
367​
368 // The interrupting send should be in messages immediately (and loading).
369 await vi.waitFor(() => {
370 const users = client.getMessages().filter((m) => m.role === 'user')
371 expect(users.map((m) => m.parts[0])).toEqual([
372 { type: 'text', content: 'first' },
373 { type: 'text', content: 'urgent' },
374 ])
375 })
376​
377 release()
378 await Promise.all([firstSend, secondSend])
379​
380 expect(client.getQueue()).toEqual([])
381 })
382​
383 it('per-call whenBusy override beats the client default', async () => {
384 const { connection, release } = createSequencedHoldingConnection()
385 // Default is queue; override one send to interrupt.
386 const client = new ChatClient({ connection })
387​
388 const firstSend = client.sendMessage('first')
389 await vi.waitFor(() => {
390 expect(client.getIsLoading()).toBe(true)
391 })
392​
393 await client.sendMessage('queued-one')
394 expect(client.getQueue()).toHaveLength(1)
395​
396 const interruptSend = client.sendMessage('urgent', undefined, {
397 whenBusy: 'interrupt',
398 })
399 // Interrupt does not flush existing queue; item stays pending.
400 expect(client.getQueue()).toHaveLength(1)
401​
402 release()
403 await Promise.all([firstSend, interruptSend])
404​
405 // After interrupt settles, the previously queued item drains.
406 await vi.waitFor(() => {
407 expect(client.getQueue()).toEqual([])
408 })
409​
410 const userMessages = client.getMessages().filter((m) => m.role === 'user')
411 expect(userMessages.map((m) => m.parts[0])).toEqual([
412 { type: 'text', content: 'first' },
413 { type: 'text', content: 'urgent' },
414 { type: 'text', content: 'queued-one' },
415 ])
416 })
417​
418 it("maxSize + onOverflow: 'reject' drops the newest when full", async () => {
419 const { connection, release } = createHoldingConnection()
420 const client = new ChatClient({
421 connection,
422 queue: { maxSize: 1, onOverflow: 'reject' },
423 })
424​
425 const firstSend = client.sendMessage('first')
426 await vi.waitFor(() => {
427 expect(client.getIsLoading()).toBe(true)
428 })
429​
430 await client.sendMessage('a')
431 await client.sendMessage('b')
432 expect(client.getQueue().map((m) => m.content)).toEqual(['a'])
433​
434 release()
435 await firstSend
436 })
437​
438 it("maxSize + onOverflow: 'drop-oldest' rotates the queue", async () => {
439 const { connection, release } = createHoldingConnection()
440 const client = new ChatClient({
441 connection,
442 queue: { maxSize: 1, onOverflow: 'drop-oldest' },
443 })
444​
445 const firstSend = client.sendMessage('first')
446 await vi.waitFor(() => {
447 expect(client.getIsLoading()).toBe(true)
448 })
449​
450 await client.sendMessage('a')
451 await client.sendMessage('b')
452 expect(client.getQueue().map((m) => m.content)).toEqual(['b'])
453​
454 release()
455 await firstSend
456 })
457​
458 it('maxSize: 0 never enqueues under drop-oldest', async () => {
459 const { connection, release } = createHoldingConnection()
460 const client = new ChatClient({
461 connection,
462 queue: { maxSize: 0, onOverflow: 'drop-oldest' },
463 })
464​
465 const firstSend = client.sendMessage('first')
466 await vi.waitFor(() => {
467 expect(client.getIsLoading()).toBe(true)
468 })
469​
470 await client.sendMessage('should-not-queue')
471 expect(client.getQueue()).toEqual([])
472​
473 release()
474 await firstSend
475 })
476​
477 it('QueueStrategy pending.id matches the enqueued item id', async () => {
478 const { connection, release } = createHoldingConnection()
479 let seenId: string | undefined
480 let seenBusyReason: string | undefined
481 const client = new ChatClient({
482 connection,
483 queue: ({ pending, busyReason }) => {
484 seenId = pending.id
485 seenBusyReason = busyReason
486 return { action: 'queue' }
487 },
488 })
489​
490 const firstSend = client.sendMessage('first')
491 await vi.waitFor(() => {
492 expect(client.getIsLoading()).toBe(true)
493 })
494​
495 await client.sendMessage('second')
496 const queue = client.getQueue()
497 expect(queue).toHaveLength(1)
498 expect(seenId).toBe(queue[0]?.id)
499 expect(seenBusyReason).toBe('streaming')
500​
501 // cancelQueued with the strategy-visible id must work.
502 client.cancelQueued(seenId!)
503 expect(client.getQueue()).toEqual([])
504​
505 release()
506 await firstSend
507 })
508​
509 it('QueueStrategy drop and per-call whenBusy override', async () => {
510 const { connection, release } = createHoldingConnection()
511 const client = new ChatClient({
512 connection,
513 queue: () => ({ action: 'drop' }),
514 })
515​
516 const firstSend = client.sendMessage('first')
517 await vi.waitFor(() => {
518 expect(client.getIsLoading()).toBe(true)
519 })
520​
521 await client.sendMessage('ignored-by-strategy')
522 expect(client.getQueue()).toEqual([])
523​
524 // Per-call override beats strategy.
525 await client.sendMessage('queued-via-override', undefined, {
526 whenBusy: 'queue',
527 })
528 expect(client.getQueue().map((m) => m.content)).toEqual([
529 'queued-via-override',
530 ])
531​
532 release()
533 await firstSend
534 })
535​
536 it('concurrent rapid sends do not strand user messages without streams', async () => {
537 const { connection, release } = createSequencedHoldingConnection()
538 const client = new ChatClient({ connection })
539​
540 // Fire several sends without awaiting between them (composer spam).
541 const sends = [
542 client.sendMessage('first'),
543 client.sendMessage('second'),
544 client.sendMessage('third'),
545 ]
546​
547 await vi.waitFor(() => {
548 expect(client.getIsLoading()).toBe(true)
549 })
550​
551 // While the first stream is open, at least one of the later sends must be
552 // sitting in the queue (not all three racing as concurrent streams).
553 await vi.waitFor(() => {
554 expect(client.getQueue().length).toBeGreaterThanOrEqual(1)
555 })
556​
557 release()
558 await Promise.all(sends)
559​
560 // Every user message must have been delivered with no stranded leftovers
561 // in the queue, and all three must appear in order.
562 expect(client.getQueue()).toEqual([])
563 const userMessages = client.getMessages().filter((m) => m.role === 'user')
564 expect(userMessages.map((m) => m.parts[0])).toEqual([
565 { type: 'text', content: 'first' },
566 { type: 'text', content: 'second' },
567 { type: 'text', content: 'third' },
568 ])
569 // Each user message should have a corresponding assistant reply after
570 // the full FIFO drain completes.
571 const assistantMessages = client
572 .getMessages()
573 .filter((m) => m.role === 'assistant')
574 expect(assistantMessages.length).toBe(3)
575 })
576​
577 it('flushes the queue on reload()', async () => {
578 const { connection, release } = createSequencedHoldingConnection()
579 const client = new ChatClient({ connection })
580​
581 const firstSend = client.sendMessage('first')
582 await vi.waitFor(() => {
583 expect(client.getIsLoading()).toBe(true)
584 })
585​
586 await client.sendMessage('queued')
587 expect(client.getQueue()).toHaveLength(1)
588​
589 // Reload while the first stream is held open and something is queued.
590 const reloadPromise = client.reload()
591 expect(client.getQueue()).toEqual([])
592​
593 release()
594 await Promise.all([firstSend, reloadPromise])
595 expect(client.getQueue()).toEqual([])
596 })
597​
598 it('flushes the queue on unsubscribe()', async () => {
599 const { connection, release } = createHoldingConnection()
600 const client = new ChatClient({ connection })
601​
602 const firstSend = client.sendMessage('first')
603 await vi.waitFor(() => {
604 expect(client.getIsLoading()).toBe(true)
605 })
606​
607 await client.sendMessage('queued')
608 expect(client.getQueue()).toHaveLength(1)
609​
610 client.unsubscribe()
611 expect(client.getQueue()).toEqual([])
612​
613 release()
614 await firstSend
615 })
616​
617 it('preserves per-message body through FIFO drain and batch last-wins', async () => {
618 const seenBodies: Array<Record<string, unknown> | undefined> = []
619 const deferred = createDeferred<void>()
620 let call = 0
621 const connection: ConnectConnectionAdapter = {
622 async *connect(_messages, data) {
623 call += 1
624 seenBodies.push(
625 data && typeof data === 'object'
626 ? (data as Record<string, unknown>)
627 : undefined,
628 )
629 if (call === 1) {
630 await deferred.promise
631 }
632 yield* createTextChunks('done', `msg-${call}`)
633 },
634 }
635​
636 // FIFO: each drained send keeps its own body.
637 const fifoClient = new ChatClient({ connection })
638 const firstFifo = fifoClient.sendMessage('first', { tag: 'seed' })
639 await vi.waitFor(() => {
640 expect(fifoClient.getIsLoading()).toBe(true)
641 })
642 await fifoClient.sendMessage('a', { tag: 'a' })
643 await fifoClient.sendMessage('b', { tag: 'b' })
644 deferred.resolve()
645 await firstFifo
646 await vi.waitFor(() => {
647 expect(fifoClient.getQueue()).toEqual([])
648 })
649 // First send + two drained: tags seed, a, b (merged with base body).
650 expect(seenBodies.map((b) => b?.tag)).toEqual(['seed', 'a', 'b'])
651​
652 // Batch: last item's body wins.
653 seenBodies.length = 0
654 call = 0
655 const deferredBatch = createDeferred<void>()
656 const batchConnection: ConnectConnectionAdapter = {
657 async *connect(_messages, data) {
658 call += 1
659 seenBodies.push(
660 data && typeof data === 'object'
661 ? (data as Record<string, unknown>)
662 : undefined,
663 )
664 if (call === 1) {
665 await deferredBatch.promise
666 }
667 yield* createTextChunks('done', `batch-${call}`)
668 },
669 }
670 const batchClient = new ChatClient({
671 connection: batchConnection,
672 queue: { drain: 'batch' },
673 })
674 const firstBatch = batchClient.sendMessage('first', { tag: 'seed' })
675 await vi.waitFor(() => {
676 expect(batchClient.getIsLoading()).toBe(true)
677 })
678 await batchClient.sendMessage('x', { tag: 'x' })
679 await batchClient.sendMessage('y', { tag: 'y' })
680 deferredBatch.resolve()
681 await firstBatch
682 await vi.waitFor(() => {
683 expect(batchClient.getQueue()).toEqual([])
684 })
685 expect(seenBodies.map((b) => b?.tag)).toEqual(['seed', 'y'])
686 })
687​
688 it('batch drain does not strand messages enqueued during the batch stream', async () => {
689 const deferred1 = createDeferred<void>()
690 const deferred2 = createDeferred<void>()
691 let call = 0
692 const connection: ConnectConnectionAdapter = {
693 async *connect() {
694 call += 1
695 if (call === 1) {
696 await deferred1.promise
697 } else if (call === 2) {
698 await deferred2.promise
699 }
700 yield* createTextChunks('done', `msg-${call}`)
701 },
702 }
703 const client = new ChatClient({ connection, queue: { drain: 'batch' } })
704​
705 const firstSend = client.sendMessage('first')
706 await vi.waitFor(() => {
707 expect(client.getIsLoading()).toBe(true)
708 })
709 await client.sendMessage('a')
710 await client.sendMessage('b')
711​
712 deferred1.resolve()
713 // Wait until the batch of a+b is streaming.
714 await vi.waitFor(() => {
715 const users = client.getMessages().filter((m) => m.role === 'user')
716 expect(users).toHaveLength(2)
717 expect(users[1]?.parts[0]).toEqual({ type: 'text', content: 'a\nb' })
718 expect(client.getIsLoading()).toBe(true)
719 })
720​
721 await client.sendMessage('during-batch')
722 expect(client.getQueue().map((m) => m.content)).toEqual(['during-batch'])
723​
724 deferred2.resolve()
725 await firstSend
726 await vi.waitFor(() => {
727 expect(client.getQueue()).toEqual([])
728 })
729​
730 const users = client.getMessages().filter((m) => m.role === 'user')
731 expect(users.map((m) => m.parts[0])).toEqual([
732 { type: 'text', content: 'first' },
733 { type: 'text', content: 'a\nb' },
734 { type: 'text', content: 'during-batch' },
735 ])
736 })
737​
738 it('error during FIFO drain flushes remaining queued items', async () => {
739 const deferred = createDeferred<void>()
740 let call = 0
741 const connection: ConnectConnectionAdapter = {
742 async *connect(): AsyncGenerator<StreamChunk> {
743 call += 1
744 if (call === 1) {
745 await deferred.promise
746 yield* createTextChunks('done', 'msg-1')
747 return
748 }
749 if (call === 2) {
750 yield {
751 type: EventType.RUN_ERROR,
752 threadId: 'thread-1',
753 timestamp: Date.now(),
754 message: 'boom',
755 error: { message: 'boom' },
756 }
757 return
758 }
759 yield* createTextChunks('should-not-run', `msg-${call}`)
760 },
761 }
762 const client = new ChatClient({ connection })
763​
764 const firstSend = client.sendMessage('first')
765 await vi.waitFor(() => {
766 expect(client.getIsLoading()).toBe(true)
767 })
768 await client.sendMessage('drains-then-errors')
769 await client.sendMessage('should-be-flushed')
770 expect(client.getQueue()).toHaveLength(2)
771​
772 deferred.resolve()
773 await firstSend
774 await vi.waitFor(() => {
775 expect(client.getQueue()).toEqual([])
776 })
777​
778 const users = client.getMessages().filter((m) => m.role === 'user')
779 // first + the drained item that errored; the third never sends.
780 expect(users.map((m) => m.parts[0])).toEqual([
781 { type: 'text', content: 'first' },
782 { type: 'text', content: 'drains-then-errors' },
783 ])
784 })
785​
786 it('interrupt during FIFO drain keeps remaining items for after the interrupt', async () => {
787 const deferred1 = createDeferred<void>()
788 const deferred2 = createDeferred<void>()
789 let call = 0
790 const connection: ConnectConnectionAdapter = {
791 async *connect() {
792 call += 1
793 if (call === 1) {
794 await deferred1.promise
795 } else if (call === 2) {
796 await deferred2.promise
797 }
798 yield* createTextChunks('done', `msg-${call}`)
799 },
800 }
801 const client = new ChatClient({ connection })
802​
803 const firstSend = client.sendMessage('first')
804 await vi.waitFor(() => {
805 expect(client.getIsLoading()).toBe(true)
806 })
807 await client.sendMessage('second')
808 await client.sendMessage('third')
809​
810 deferred1.resolve()
811 // Wait until FIFO drain has started streaming "second".
812 await vi.waitFor(() => {
813 const users = client.getMessages().filter((m) => m.role === 'user')
814 expect(users.map((m) => m.parts[0])).toEqual([
815 { type: 'text', content: 'first' },
816 { type: 'text', content: 'second' },
817 ])
818 expect(client.getIsLoading()).toBe(true)
819 })
820 // "third" should still be queued while "second" streams.
821 expect(client.getQueue().map((m) => m.content)).toEqual(['third'])
822​
823 const interruptSend = client.sendMessage('urgent', undefined, {
824 whenBusy: 'interrupt',
825 })
826 // Interrupt does not flush remaining queue.
827 expect(client.getQueue().map((m) => m.content)).toEqual(['third'])
828​
829 deferred2.resolve()
830 await Promise.all([firstSend, interruptSend])
831 await vi.waitFor(() => {
832 expect(client.getQueue()).toEqual([])
833 })
834​
835 const users = client.getMessages().filter((m) => m.role === 'user')
836 expect(users.map((m) => m.parts[0])).toEqual([
837 { type: 'text', content: 'first' },
838 { type: 'text', content: 'second' },
839 { type: 'text', content: 'urgent' },
840 { type: 'text', content: 'third' },
841 ])
842 })
843})