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

chat-client-stream-processing.test.ts

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