Open History to see the last commit
1import { renderHook, waitFor } from '@testing-library/react'
2import { describe, expect, it, vi } from 'vitest'
3import type { ChatFetcher } from '@tanstack/ai-client'
4import type { StreamChunk } from '@tanstack/ai'
5import { useChat } from '../src/use-chat'
6import { createTextChunks } from './test-utils'
7
8describe('useChat — fetcher transport', () => {
9 it('streams text into messages via an AsyncIterable fetcher', async () => {
10 const chunks = createTextChunks('Hello world', 'msg-1')
11 const fetcher: ChatFetcher = async function* () {
12 for (const chunk of chunks) {
13 yield chunk
14 }
15 }
16
17 const { result } = renderHook(() =>
18 useChat({ threadId: 'thread-1', fetcher }),
19 )
20
21 await result.current.sendMessage('hi')
22
23 await waitFor(() => {
24 const assistant = result.current.messages[1]
25 const textPart = assistant?.parts.find((p) => p.type === 'text')
26 expect(textPart && 'content' in textPart && textPart.content).toBe(
27 'Hello world',
28 )
29 })
30 })
31
32 it('parses an SSE Response returned by the fetcher', async () => {
33 const sseBody =
34 [
35 `data: ${JSON.stringify({
36 type: 'TEXT_MESSAGE_CONTENT',
37 messageId: 'm1',
38 model: 'test',
39 timestamp: Date.now(),
40 delta: 'Hi',
41 content: 'Hi',
42 })}`,
43 `data: ${JSON.stringify({
44 type: 'RUN_FINISHED',
45 runId: 'r1',
46 threadId: 't1',
47 model: 'test',
48 timestamp: Date.now(),
49 finishReason: 'stop',
50 })}`,
51 '',
52 ].join('\n') + '\n'
53
54 const fetcher: ChatFetcher = async () =>
55 new Response(sseBody, {
56 status: 200,
57 headers: { 'content-type': 'text/event-stream' },
58 })
59
60 const { result } = renderHook(() =>
61 useChat({ threadId: 'thread-1', fetcher }),
62 )
63
64 await result.current.sendMessage('hi')
65
66 await waitFor(() => {
67 const assistant = result.current.messages[1]
68 const textPart = assistant?.parts.find((p) => p.type === 'text')
69 expect(textPart && 'content' in textPart && textPart.content).toBe('Hi')
70 })
71 })
72
73 it('surfaces fetcher errors as the hook error state', async () => {
74 const fetcher: ChatFetcher = async () => {
75 throw new Error('boom')
76 }
77
78 const { result } = renderHook(() =>
79 useChat({ threadId: 'thread-1', fetcher }),
80 )
81
82 await result.current.sendMessage('hi')
83
84 await waitFor(() => {
85 expect(result.current.error).toBeDefined()
86 })
87 expect(result.current.error!.message).toBe('boom')
88 expect(result.current.status).toBe('error')
89 })
90
91 it('passes the merged body and full message history to the fetcher', async () => {
92 const fetcher = vi.fn<ChatFetcher>(async function* () {
93 yield {
94 type: 'RUN_FINISHED',
95 runId: 'r1',
96 threadId: 't1',
97 model: 'test',
98 timestamp: Date.now(),
99 finishReason: 'stop',
100 } as StreamChunk
101 })
102
103 const { result } = renderHook(() =>
104 useChat({
105 threadId: 'thread-1',
106 fetcher,
107 forwardedProps: { provider: 'openai' },
108 }),
109 )
110
111 await result.current.sendMessage('hello')
112
113 expect(fetcher).toHaveBeenCalledTimes(1)
114 const [input] = fetcher.mock.calls[0]!
115 expect(input.messages).toHaveLength(1)
116 expect(input.messages[0]!.role).toBe('user')
117 expect(input.data).toMatchObject({ provider: 'openai' })
118 })
119})