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

chat.spec.ts

241 lines7.0 KB

1import { test, expect } from './fixtures'
2import {
3 sendMessage,
4 waitForResponse,
5 getLastAssistantMessage,
6 featureUrl,
7} from './helpers'
8import { providersFor } from './test-matrix'
9​
10// The server conversion test in this spec does not call a provider HTTP
11// endpoint, so it intentionally does not configure aimock.
12​
13for (const provider of providersFor('chat')) {
14 test.describe(`${provider} — chat`, () => {
15 test('sends a message and receives a streaming response', async ({
16 page,
17 testId,
18 aimockPort,
19 }) => {
20 await page.goto(featureUrl(provider, 'chat', testId, aimockPort))
21​
22 await sendMessage(page, '[chat] recommend a guitar')
23 await waitForResponse(page)
24​
25 const response = await getLastAssistantMessage(page)
26 expect(response).toContain('Fender Stratocaster')
27 })
28​
29 test('fetcher mode — streams an SSE Response through useChat({ fetcher })', async ({
30 page,
31 testId,
32 aimockPort,
33 }) => {
34 await page.goto(
35 featureUrl(provider, 'chat', testId, aimockPort, 'fetcher'),
36 )
37​
38 // Positively assert the fetcher path executed by waiting for the
39 // POST that carries our sentinel header. Without this, a silent
40 // fallback to the connection adapter would still make the response
41 // assertion pass (both paths return the same SSE).
42 const fetcherRequest = page.waitForRequest(
43 (req) =>
44 req.url().endsWith('/api/chat') &&
45 req.method() === 'POST' &&
46 req.headers()['x-tanstack-ai-transport'] === 'fetcher',
47 )
48​
49 await sendMessage(page, '[chat] recommend a guitar')
50 await fetcherRequest
51 await waitForResponse(page)
52​
53 const response = await getLastAssistantMessage(page)
54 expect(response).toContain('Fender Stratocaster')
55 })
56 })
58​
59test('preserves UI message IDs at the server conversion boundary', async ({
60 request,
61}) => {
62 const response = await request.post('/api/message-ids', {
63 data: {
64 messages: [
65 {
66 id: 'user-1',
67 role: 'user',
68 parts: [{ type: 'text', content: 'Hello' }],
69 },
70 {
71 id: 'assistant-1',
72 role: 'assistant',
73 parts: [
74 { type: 'text', content: 'Let me check.' },
75 {
76 type: 'tool-call',
77 id: 'tool-1',
78 name: 'getWeather',
79 arguments: '{}',
80 state: 'input-complete',
81 },
82 {
83 type: 'tool-result',
84 toolCallId: 'tool-1',
85 content: '{"temp":72}',
86 state: 'complete',
87 },
88 ],
89 },
90 ],
91 },
92 })
93​
94 expect(response.ok()).toBe(true)
95 const modelMessages = await response.json()
96​
97 expect(modelMessages).toEqual([
98 { id: 'user-1', role: 'user', content: 'Hello' },
99 {
100 id: 'assistant-1',
101 role: 'assistant',
102 content: 'Let me check.',
103 toolCalls: [
104 {
105 id: 'tool-1',
106 type: 'function',
107 function: { name: 'getWeather', arguments: '{}' },
108 },
109 ],
110 },
111 {
112 id: 'assistant-1',
113 role: 'tool',
114 content: '{"temp":72}',
115 toolCallId: 'tool-1',
116 },
117 ])
118})
119​
120test('rejects malformed JSON at the server conversion boundary', async ({
121 request,
122}) => {
123 const response = await request.post('/api/message-ids', {
124 data: '{',
125 headers: { 'Content-Type': 'application/json' },
126 })
127​
128 expect(response.status()).toBe(400)
129})
130​
131test('rejects invalid message parts at the server conversion boundary', async ({
132 request,
133}) => {
134 const response = await request.post('/api/message-ids', {
135 data: {
136 messages: [
137 {
138 id: 'user-1',
139 role: 'user',
140 parts: [{ type: 'text', content: 42 }],
141 },
142 ],
143 },
144 })
145​
146 expect(response.status()).toBe(400)
147})
148​
149test.describe('openai chat persistence', () => {
150 test('persists chat messages across browser reload with localStorage', async ({
151 page,
152 testId,
153 aimockPort,
154 }) => {
155 await page.goto(
156 `${featureUrl('openai', 'chat', testId, aimockPort)}&persistence=localStorage`,
157 )
158​
159 await sendMessage(page, '[chat] recommend a guitar')
160 await waitForResponse(page)
161​
162 await expect(page.getByTestId('user-message')).toContainText(
163 '[chat] recommend a guitar',
164 )
165 await expect(page.getByTestId('assistant-message')).toContainText(
166 'Fender Stratocaster',
167 )
168​
169 await page.reload()
170​
171 await expect(page.getByTestId('user-message')).toContainText(
172 '[chat] recommend a guitar',
173 )
174 await expect(page.getByTestId('assistant-message')).toContainText(
175 'Fender Stratocaster',
176 )
177 })
178​
179 test('clear() removes the persisted conversation so a reload starts empty', async ({
180 page,
181 testId,
182 aimockPort,
183 }) => {
184 await page.goto(
185 `${featureUrl('openai', 'chat', testId, aimockPort)}&persistence=localStorage`,
186 )
187​
188 await sendMessage(page, '[chat] recommend a guitar')
189 await waitForResponse(page)
190 await expect(page.getByTestId('user-message')).toContainText(
191 '[chat] recommend a guitar',
192 )
193​
194 await page.getByTestId('clear-button').click()
195 await expect(page.getByTestId('user-message')).toHaveCount(0)
196 await expect(page.getByTestId('assistant-message')).toHaveCount(0)
197​
198 // The conversation was removed from storage, not just from memory — a
199 // reload must not resurrect it.
200 await page.reload()
201 await expect(page.getByTestId('message-list')).toBeVisible()
202 await expect(page.getByTestId('user-message')).toHaveCount(0)
203 await expect(page.getByTestId('assistant-message')).toHaveCount(0)
204 })
205​
206 test('switches per-thread history when the chat id changes in place', async ({
207 page,
208 testId,
209 aimockPort,
210 }) => {
211 await page.goto(
212 `${featureUrl('openai', 'chat', testId, aimockPort)}&persistence=localStorage`,
213 )
214​
215 // The page loads on thread A. Send a message (persisted under A's own id).
216 await sendMessage(page, '[chat] recommend a guitar')
217 await waitForResponse(page)
218 await expect(page.getByTestId('user-message')).toHaveCount(1)
219​
220 // Switch to thread B in place — its own (empty) history loads, proving the
221 // id swap doesn't leak thread A's messages into thread B.
222 await page.getByTestId('select-thread-b').click()
223 await expect(page.getByTestId('user-message')).toHaveCount(0)
224 await expect(page.getByTestId('assistant-message')).toHaveCount(0)
225​
226 await sendMessage(page, '[chat] recommend a guitar')
227 await waitForResponse(page)
228 await expect(page.getByTestId('user-message')).toHaveCount(1)
229​
230 // Switch back to thread A — its persisted history is restored from storage
231 // on the in-place swap (render-from-getMessages), exactly one message.
232 await page.getByTestId('select-thread-a').click()
233 await expect(page.getByTestId('user-message')).toHaveCount(1)
234 await expect(page.getByTestId('user-message')).toContainText(
235 '[chat] recommend a guitar',
236 )
237 await expect(page.getByTestId('assistant-message')).toContainText(
238 'Fender Stratocaster',
239 )
240 })
241})