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(() => useChat({ fetcher }))
18
19 await result.current.sendMessage('hi')
20
21 await waitFor(() => {
22 const assistant = result.current.messages[1]
23 const textPart = assistant?.parts.find((p) => p.type === 'text')
24 expect(textPart && 'content' in textPart && textPart.content).toBe(
25 'Hello world',
26 )
27 })
28 })
29
30 it('parses an SSE Response returned by the fetcher', async () => {
31 const sseBody =
32 [
33 `data: ${JSON.stringify({
34 type: 'TEXT_MESSAGE_CONTENT',
35 messageId: 'm1',
36 model: 'test',
37 timestamp: Date.now(),
38 delta: 'Hi',
39 content: 'Hi',
40 })}`,
41 `data: ${JSON.stringify({
42 type: 'RUN_FINISHED',
43 runId: 'r1',
44 threadId: 't1',
45 model: 'test',
46 timestamp: Date.now(),
47 finishReason: 'stop',
48 })}`,
49 '',
50 ].join('\n') + '\n'
51
52 const fetcher: ChatFetcher = async () =>
53 new Response(sseBody, {
54 status: 200,
55 headers: { 'content-type': 'text/event-stream' },
56 })
57
58 const { result } = renderHook(() => useChat({ fetcher }))
59
60 await result.current.sendMessage('hi')
61
62 await waitFor(() => {
63 const assistant = result.current.messages[1]
64 const textPart = assistant?.parts.find((p) => p.type === 'text')
65 expect(textPart && 'content' in textPart && textPart.content).toBe('Hi')
66 })
67 })
68
69 it('surfaces fetcher errors as the hook error state', async () => {
70 const fetcher: ChatFetcher = async () => {
71 throw new Error('boom')
72 }
73
74 const { result } = renderHook(() => useChat({ fetcher }))
75
76 await result.current.sendMessage('hi')
77
78 await waitFor(() => {
79 expect(result.current.error).toBeDefined()
80 })
81 expect(result.current.error!.message).toBe('boom')
82 expect(result.current.status).toBe('error')
83 })
84
85 it('passes the merged body and full message history to the fetcher', async () => {
86 const fetcher = vi.fn<ChatFetcher>(async function* () {
87 yield {
88 type: 'RUN_FINISHED',
89 runId: 'r1',
90 threadId: 't1',
91 model: 'test',
92 timestamp: Date.now(),
93 finishReason: 'stop',
94 } as StreamChunk
95 })
96
97 const { result } = renderHook(() =>
98 useChat({ fetcher, body: { provider: 'openai' } }),
99 )
100
101 await result.current.sendMessage('hello')
102
103 expect(fetcher).toHaveBeenCalledTimes(1)
104 const [input] = fetcher.mock.calls[0]!
105 expect(input.messages).toHaveLength(1)
106 expect(input.messages[0]!.role).toBe('user')
107 expect(input.data).toMatchObject({ provider: 'openai' })
108 })
109})