Open History to see the last commit
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 toolCalls = messages.flatMap((msg) =>
99 (msg.parts || []).filter((part: any) => part.type === 'tool-call'),
100 )
101 const toolResults = messages.flatMap((msg) =>
102 (msg.parts || []).filter((part: any) => part.type === 'tool-result'),
103 )
104
105 const fetchData = toolCalls.find((part) => part.name === 'fetch_data')
106 expect(fetchData).toMatchObject({
107 state: 'complete',
108 output: {
109 source: 'api',
110 data: [1, 2, 3, 4, 5],
111 },
112 })
113
114 const displayChart = toolCalls.find((part) => part.name === 'display_chart')
115 expect(displayChart?.state).toBe('complete')
116 expect(displayChart?.output).toMatchObject({ rendered: true })
117 expect(typeof displayChart?.output?.chartId).toBe('string')
118
119 expect(
120 toolResults.some(
121 (part) =>
122 part.toolCallId === fetchData?.id &&
123 part.state === 'complete' &&
124 JSON.parse(part.content).source === 'api',
125 ),
126 ).toBe(true)
127 })
128
129 test('server then two client tools in sequence', async ({
130 page,
131 testId,
132 aimockPort,
133 }) => {
134 // Tests complex continuation: server -> client -> client
135 await selectScenario(page, 'server-then-two-clients', testId, aimockPort)
136 await runTest(page)
137
138 // Wait for the test to complete — expect 3 tools (1 server + 2 clients)
139 await waitForTestComplete(page, 30000, 3)
140
141 // Wait for client tool executions to propagate
142 await page.waitForFunction(
143 () => {
144 const el = document.querySelector('#test-metadata')
145 return (
146 parseInt(el?.getAttribute('data-execution-complete-count') || '0') >=
147 2
148 )
149 },
150 { timeout: 10000 },
151 )
152
153 // Verify results
154 const metadata = await getMetadata(page)
155 expect(metadata.testComplete).toBe('true')
156
157 // Should have 3 tool calls
158 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(3)
159
160 // Verify both client tools executed
161 const events = await getEventLog(page)
162 const notificationExecution = events.find(
163 (e) =>
164 e.toolName === 'show_notification' && e.type === 'execution-complete',
165 )
166 const chartExecution = events.find(
167 (e) => e.toolName === 'display_chart' && e.type === 'execution-complete',
168 )
169
170 expect(notificationExecution).toBeTruthy()
171 expect(chartExecution).toBeTruthy()
172
173 // Verify order: notification should complete before chart starts
174 // (they're in sequence, not parallel)
175 const notificationCompleteTime = events.find(
176 (e) =>
177 e.toolName === 'show_notification' && e.type === 'execution-complete',
178 )?.timestamp
179 const chartStartTime = events.find(
180 (e) => e.toolName === 'display_chart' && e.type === 'execution-start',
181 )?.timestamp
182
183 if (notificationCompleteTime && chartStartTime) {
184 expect(notificationCompleteTime).toBeLessThanOrEqual(chartStartTime)
185 }
186 })
187
188 test('parallel server tools complete correctly', async ({
189 page,
190 testId,
191 aimockPort,
192 }) => {
193 await selectScenario(page, 'parallel-tools', testId, aimockPort)
194 await runTest(page)
195
196 // Wait for the test to complete
197 await waitForTestComplete(page)
198
199 // Verify results
200 const metadata = await getMetadata(page)
201 expect(metadata.testComplete).toBe('true')
202
203 // Should have 2 tool calls (get_weather, get_time)
204 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
205 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(2)
206
207 // Verify both tools are in the tool calls list
208 const toolCalls = await getToolCalls(page)
209 const toolNames = toolCalls.map((tc) => tc.name)
210 expect(toolNames).toContain('get_weather')
211 expect(toolNames).toContain('get_time')
212 })
213
214 test('parallel server tools run at the same time, results in call order', async ({
215 page,
216 testId,
217 aimockPort,
218 }) => {
219 await selectScenario(page, 'parallel-server-timing', testId, aimockPort)
220 await runTest(page)
221 await waitForTestComplete(page, 15000, 2)
222
223 const messages = await getMessages(page)
224 const results: Array<{ toolCallId: string; content: string }> = messages
225 .flatMap((message) => message.parts ?? [])
226 .filter((part) => part.type === 'tool-result')
227 const calls: Array<{ id: string; name: string }> = messages
228 .flatMap((message) => message.parts ?? [])
229 .filter((part) => part.type === 'tool-call')
230 const nameOf = (toolCallId: string) =>
231 calls.find((call) => call.id === toolCallId)?.name
232 const timing = (name: string) => {
233 const result = results.find((r) => nameOf(r.toolCallId) === name)
234 return JSON.parse(result?.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})