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

server-client-sequence.spec.ts

313 lines9.7 KB

1import { test, expect } from '../fixtures'
2import {
3 selectScenario,
4 runTest,
5 waitForTestComplete,
6 getMetadata,
7 getEventLog,
8 getToolCalls,
9 getMessages,
10} from './helpers'
11​
12/**
13 * Server-Client Sequence E2E Tests
14 *
15 * These tests verify that mixed server/client tool sequences work correctly,
16 * including:
17 * - Server tool followed by client tool
18 * - Client tool followed by server tool
19 * - Complex multi-step sequences
20 * - Proper state management across tool types
21 */
22​
23test.describe('Server-Client Sequence E2E Tests', () => {
24 test('server tool followed by client tool', async ({
25 page,
26 testId,
27 aimockPort,
28 }) => {
29 await selectScenario(page, 'sequence-server-client', testId, aimockPort)
30 await runTest(page)
31​
32 // Wait for the test to complete
33 await waitForTestComplete(page, 15000, 2)
34​
35 // Wait for client execution events to propagate
36 await page.waitForFunction(
37 () => {
38 const el = document.querySelector('#test-metadata')
39 return (
40 parseInt(el?.getAttribute('data-execution-complete-count') || '0') >=
41 1
42 )
43 },
44 { timeout: 10000 },
45 )
46​
47 // Verify results
48 const metadata = await getMetadata(page)
49 expect(metadata.testComplete).toBe('true')
50​
51 // Should have 2 tool calls (fetch_data server, display_chart client)
52 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
53​
54 // Verify client tool executed
55 const events = await getEventLog(page)
56 const chartExecution = events.find(
57 (e) => e.toolName === 'display_chart' && e.type === 'execution-complete',
58 )
59 expect(chartExecution).toBeTruthy()
60 })
61​
62 test('server and client tool results populate tool-call output and complete state', async ({
63 page,
64 testId,
65 aimockPort,
66 }) => {
67 await selectScenario(page, 'sequence-server-client', testId, aimockPort)
68 await runTest(page)
69​
70 await waitForTestComplete(page, 15000, 2)
71​
72 await page.waitForFunction(
73 () => {
74 const messagesEl = document.getElementById('messages-json-content')
75 const messages = JSON.parse(messagesEl?.textContent || '[]')
76 const toolCalls = messages.flatMap((msg: any) =>
77 (msg.parts || []).filter((part: any) => part.type === 'tool-call'),
78 )
79​
80 const fetchData = toolCalls.find(
81 (part: any) => part.name === 'fetch_data',
82 )
83 const displayChart = toolCalls.find(
84 (part: any) => part.name === 'display_chart',
85 )
86​
87 return (
88 fetchData?.state === 'complete' &&
89 fetchData.output !== undefined &&
90 displayChart?.state === 'complete' &&
91 displayChart.output !== undefined
92 )
93 },
94 { timeout: 10000 },
95 )
96​
97 const messages = await getMessages(page)
98 const parts = messages.flatMap((msg) => msg.parts)
99 const toolCalls = parts.filter((part) => part.type === 'tool-call')
100 const toolResults = parts.filter((part) => part.type === 'tool-result')
101​
102 const fetchData = toolCalls.find((part) => part.name === 'fetch_data')
103 expect(fetchData).toMatchObject({
104 state: 'complete',
105 output: {
106 source: 'api',
107 data: [1, 2, 3, 4, 5],
108 },
109 })
110​
111 const displayChart = toolCalls.find((part) => part.name === 'display_chart')
112 expect(displayChart?.state).toBe('complete')
113 expect(displayChart?.output).toMatchObject({ rendered: true })
114 expect(typeof displayChart?.output?.chartId).toBe('string')
115​
116 expect(
117 toolResults.some(
118 (part) =>
119 part.toolCallId === fetchData?.id &&
120 part.state === 'complete' &&
121 typeof part.content === 'string' &&
122 JSON.parse(part.content).source === 'api',
123 ),
124 ).toBe(true)
125 })
126​
127 test('server then two client tools in sequence', async ({
128 page,
129 testId,
130 aimockPort,
131 }) => {
132 // Tests complex continuation: server -> client -> client
133 await selectScenario(page, 'server-then-two-clients', testId, aimockPort)
134 await runTest(page)
135​
136 // Wait for the test to complete — expect 3 tools (1 server + 2 clients)
137 await waitForTestComplete(page, 30000, 3)
138​
139 // Wait for client tool executions to propagate
140 await page.waitForFunction(
141 () => {
142 const el = document.querySelector('#test-metadata')
143 return (
144 parseInt(el?.getAttribute('data-execution-complete-count') || '0') >=
145 2
146 )
147 },
148 { timeout: 10000 },
149 )
150​
151 // Verify results
152 const metadata = await getMetadata(page)
153 expect(metadata.testComplete).toBe('true')
154​
155 // Should have 3 tool calls
156 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(3)
157​
158 // Verify both client tools executed
159 const events = await getEventLog(page)
160 const notificationExecution = events.find(
161 (e) =>
162 e.toolName === 'show_notification' && e.type === 'execution-complete',
163 )
164 const chartExecution = events.find(
165 (e) => e.toolName === 'display_chart' && e.type === 'execution-complete',
166 )
167​
168 expect(notificationExecution).toBeTruthy()
169 expect(chartExecution).toBeTruthy()
170​
171 // Verify order: notification should complete before chart starts
172 // (they're in sequence, not parallel)
173 const notificationCompleteTime = events.find(
174 (e) =>
175 e.toolName === 'show_notification' && e.type === 'execution-complete',
176 )?.timestamp
177 const chartStartTime = events.find(
178 (e) => e.toolName === 'display_chart' && e.type === 'execution-start',
179 )?.timestamp
180​
181 if (notificationCompleteTime && chartStartTime) {
182 expect(notificationCompleteTime).toBeLessThanOrEqual(chartStartTime)
183 }
184 })
185​
186 test('parallel server tools complete correctly', async ({
187 page,
188 testId,
189 aimockPort,
190 }) => {
191 await selectScenario(page, 'parallel-tools', testId, aimockPort)
192 await runTest(page)
193​
194 // Wait for the test to complete
195 await waitForTestComplete(page)
196​
197 // Verify results
198 const metadata = await getMetadata(page)
199 expect(metadata.testComplete).toBe('true')
200​
201 // Should have 2 tool calls (get_weather, get_time)
202 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
203 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(2)
204​
205 // Verify both tools are in the tool calls list
206 const toolCalls = await getToolCalls(page)
207 const toolNames = toolCalls.map((tc) => tc.name)
208 expect(toolNames).toContain('get_weather')
209 expect(toolNames).toContain('get_time')
210 })
211​
212 test('parallel server tools run at the same time, results in call order', async ({
213 page,
214 testId,
215 aimockPort,
216 }) => {
217 await selectScenario(page, 'parallel-server-timing', testId, aimockPort)
218 await runTest(page)
219 await waitForTestComplete(page, 15000, 2)
220​
221 const messages = await getMessages(page)
222 const parts = messages.flatMap((message) => message.parts)
223 const results = parts.filter((part) => part.type === 'tool-result')
224 const calls = parts.filter((part) => part.type === 'tool-call')
225 const nameOf = (toolCallId: string) =>
226 calls.find((call) => call.id === toolCallId)?.name
227 const timing = (name: string) => {
228 const content = results.find(
229 (r) => nameOf(r.toolCallId) === name,
230 )?.content
231 if (typeof content !== 'string') {
232 throw new Error(`no string tool result for ${name}`)
233 }
234 return JSON.parse(content)
235 }
236​
237 // Each tool waits 300 ms. Run one after another, they cannot overlap.
238 const weather = timing('get_weather')
239 const time = timing('get_time')
240 expect(weather.startedAt).toBeLessThan(time.endedAt)
241 expect(time.startedAt).toBeLessThan(weather.endedAt)
242 expect(results.map((result) => nameOf(result.toolCallId))).toEqual([
243 'get_weather',
244 'get_time',
245 ])
246 })
247​
248 test('single server tool completes', async ({ page, testId, aimockPort }) => {
249 await selectScenario(page, 'server-tool-single', testId, aimockPort)
250 await runTest(page)
251​
252 // Wait for the test to complete
253 await waitForTestComplete(page)
254​
255 // Verify results
256 const metadata = await getMetadata(page)
257 expect(metadata.testComplete).toBe('true')
258​
259 // Should have 1 tool call (get_weather)
260 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(1)
261 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(1)
262​
263 // Verify tool is in the calls list
264 const toolCalls = await getToolCalls(page)
265 const weatherTool = toolCalls.find((tc) => tc.name === 'get_weather')
266 expect(weatherTool).toBeTruthy()
267 expect(weatherTool?.state).toBe('complete')
268 })
269​
270 test('text only scenario has no tool calls', async ({
271 page,
272 testId,
273 aimockPort,
274 }) => {
275 await selectScenario(page, 'text-only', testId, aimockPort)
276 await runTest(page)
277​
278 // Wait for loading to finish
279 await page.waitForFunction(
280 () => {
281 const metadata = document.getElementById('test-metadata')
282 return metadata?.getAttribute('data-is-loading') === 'false'
283 },
284 { timeout: 10000 },
285 )
286​
287 // Give it a moment to settle
288 await page.waitForTimeout(500)
289​
290 // Verify no tool calls
291 const metadata = await getMetadata(page)
292 expect(parseInt(metadata.toolCallCount)).toBe(0)
293 })
294​
295 // Screenshot on failure
296 test.afterEach(async ({ page }, testInfo) => {
297 if (testInfo.status !== testInfo.expectedStatus) {
298 await page.screenshot({
299 path: `test-results/sequence-failure-${testInfo.title.replace(/\s+/g, '-')}.png`,
300 fullPage: true,
301 })
302​
303 const events = await getEventLog(page)
304 const toolCalls = await getToolCalls(page)
305 const metadata = await getMetadata(page)
306​
307 console.log('Test failed. Debug info:')
308 console.log('Metadata:', metadata)
309 console.log('Events:', events)
310 console.log('Tool calls:', toolCalls)
311 }
312 })
313})