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

chat-client-stream-processing.test.ts

118 lines3.4 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { ChatClient } from '../src/chat-client'
3import { createMockConnectionAdapter, createTextChunks } from './test-utils'
4​
5afterEach(() => {
6 vi.restoreAllMocks()
7 vi.unstubAllGlobals()
8})
9​
10describe('ChatClient stream processing', () => {
11 it('does not wait for a macrotask after each live chunk', async () => {
12 vi.spyOn(performance, 'now').mockReturnValue(0)
13 const client = new ChatClient({
14 threadId: 'thread-1',
15 connection: createMockConnectionAdapter({
16 chunks: createTextChunks('ab'),
17 }),
18 })
19 let macrotaskRan = false
20 setTimeout(() => {
21 macrotaskRan = true
22 }, 0)
23​
24 await client.sendMessage('Hi')
25​
26 expect(macrotaskRan).toBe(false)
27 })
28​
29 it('falls back to a timer after a full processing slice', async () => {
30 vi.stubGlobal('scheduler', {})
31 let time = 0
32 vi.spyOn(performance, 'now').mockImplementation(() => (time += 9))
33 const client = new ChatClient({
34 threadId: 'thread-1',
35 connection: createMockConnectionAdapter({
36 chunks: createTextChunks('ab'),
37 }),
38 })
39 let macrotaskRan = false
40 setTimeout(() => {
41 macrotaskRan = true
42 }, 0)
43​
44 await client.sendMessage('Hi')
45​
46 expect(macrotaskRan).toBe(true)
47 })
48​
49 it('uses the scheduler after a full processing slice', async () => {
50 const schedulerYield = vi.fn(() => Promise.resolve())
51 vi.stubGlobal('scheduler', { yield: schedulerYield })
52 let time = 0
53 vi.spyOn(performance, 'now').mockImplementation(() => (time += 9))
54 const client = new ChatClient({
55 threadId: 'thread-1',
56 connection: createMockConnectionAdapter({
57 chunks: createTextChunks('ab'),
58 }),
59 })
60 let macrotaskRan = false
61 setTimeout(() => {
62 macrotaskRan = true
63 }, 0)
64​
65 await client.sendMessage('Hi')
66​
67 expect(schedulerYield).toHaveBeenCalled()
68 expect(macrotaskRan).toBe(false)
69 })
70​
71 it('does not yield in a hidden document', async () => {
72 vi.stubGlobal('document', { hidden: true })
73 const schedulerYield = vi.fn(() => Promise.resolve())
74 vi.stubGlobal('scheduler', { yield: schedulerYield })
75 let time = 0
76 vi.spyOn(performance, 'now').mockImplementation(() => (time += 9))
77 const client = new ChatClient({
78 threadId: 'thread-1',
79 connection: createMockConnectionAdapter({
80 chunks: createTextChunks('ab'),
81 }),
82 })
83 let macrotaskRan = false
84 setTimeout(() => {
85 macrotaskRan = true
86 }, 0)
87​
88 await client.sendMessage('Hi')
89​
90 expect(macrotaskRan).toBe(false)
91 expect(schedulerYield).not.toHaveBeenCalled()
92 })
93​
94 it('does not carry hidden-page processing time into a visible page', async () => {
95 const page = { hidden: true }
96 vi.stubGlobal('document', page)
97 const schedulerYield = vi.fn(() => Promise.resolve())
98 vi.stubGlobal('scheduler', { yield: schedulerYield })
99 let time = 0
100 vi.spyOn(performance, 'now').mockImplementation(() => time)
101 const client = new ChatClient({
102 threadId: 'thread-1',
103 connection: createMockConnectionAdapter({
104 chunks: createTextChunks('ab'),
105 }),
106 onChunk(chunk) {
107 // Each chunk costs 5 ms. The two text chunks fill one 8 ms budget
108 // while hidden; the page is visible for the final chunk only.
109 time += 5
110 if (chunk.type === 'RUN_FINISHED') page.hidden = false
111 },
112 })
113​
114 await client.sendMessage('Hi')
115​
116 expect(schedulerYield).not.toHaveBeenCalled()
117 })
118})