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