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

client-tool.spec.ts

266 lines8.3 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 * Client Tool E2E Tests
14 *
15 * These tests verify that client-side tool execution works correctly,
16 * including proper event flow, continuation, and state management.
17 *
18 * These tests catch race conditions and event flow issues in the
19 * client-side code (useChat hook, ChatClient).
20 */
21​
22test.describe('Client Tool E2E Tests', () => {
23 test('invalid client-tool input retries then runs the interrupt', async ({
24 page,
25 testId,
26 aimockPort,
27 }) => {
28 const requests: Array<string> = []
29 page.on('request', (request) => {
30 if (
31 request.method() === 'POST' &&
32 request.url().includes('/api/tools-test')
33 ) {
34 requests.push(request.url())
35 }
36 })
37​
38 await selectScenario(page, 'invalid-client-tool-retry', testId, aimockPort)
39 await runTest(page)
40 await waitForTestComplete(page, 15000, 2)
41​
42 const metadata = await getMetadata(page)
43 expect(requests).toHaveLength(2)
44 expect(metadata.executionCompleteCount).toBe('1')
45​
46 const toolCalls = await getToolCalls(page)
47 expect(toolCalls).toEqual(
48 expect.arrayContaining([
49 expect.objectContaining({ state: 'error' }),
50 expect.objectContaining({
51 name: 'show_notification',
52 state: 'complete',
53 }),
54 ]),
55 )
56​
57 const responseText = (await getMessages(page))
58 .flatMap((message) => message.parts)
59 .filter((part) => part.type === 'text')
60 .map((part) => part.content)
61 .join(' ')
62 expect(responseText).toContain('Recovered after client tool input retry.')
63 })
64​
65 test('single client tool executes and completes', async ({
66 page,
67 testId,
68 aimockPort,
69 }) => {
70 await selectScenario(page, 'client-tool-single', testId, aimockPort)
71 await runTest(page)
72​
73 // Wait for the test to complete
74 await waitForTestComplete(page)
75​
76 // Verify the results
77 const metadata = await getMetadata(page)
78 expect(metadata.testComplete).toBe('true')
79 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(1)
80 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(1)
81​
82 // Verify events were recorded
83 const events = await getEventLog(page)
84 const startEvents = events.filter((e) => e.type === 'execution-start')
85 const completeEvents = events.filter((e) => e.type === 'execution-complete')
86​
87 expect(startEvents.length).toBeGreaterThanOrEqual(1)
88 expect(completeEvents.length).toBeGreaterThanOrEqual(1)
89 expect(startEvents[0]?.toolName).toBe('show_notification')
90 })
91​
92 test('client tool preserves reasoning through continuation', async ({
93 page,
94 testId,
95 aimockPort,
96 }) => {
97 await selectScenario(page, 'client-tool-reasoning', testId, aimockPort)
98 await runTest(page)
99 await waitForTestComplete(page)
100​
101 const messages = await getMessages(page)
102 const toolCallMessage = messages.find(
103 (message) =>
104 message.role === 'assistant' &&
105 message.parts.some(
106 (part) =>
107 part.type === 'tool-call' && part.name === 'show_notification',
108 ),
109 )
110​
111 expect(toolCallMessage?.parts).toEqual(
112 expect.arrayContaining([
113 expect.objectContaining({
114 type: 'thinking',
115 content:
116 'I should show a notification before confirming it was displayed.',
117 }),
118 ]),
119 )
120​
121 const followUpText = messages
122 .flatMap((message) => message.parts)
123 .filter((part) => part.type === 'text')
124 .map((part) => part.content)
125 .join(' ')
126​
127 expect(followUpText).toContain(
128 'The notification has been shown after reasoning.',
129 )
130 })
131​
132 test('sequential client tools execute in order', async ({
133 page,
134 testId,
135 aimockPort,
136 }) => {
137 // This tests the specific issue: two client tool calls in a row
138 // The second call shouldn't be blocked by the first
139 await selectScenario(page, 'sequential-client-tools', testId, aimockPort)
140 await runTest(page)
141​
142 // Wait for the test to complete (expect 2 tools)
143 await waitForTestComplete(page, 15000, 2)
144​
145 // Wait for execution events to propagate
146 await page.waitForFunction(
147 () => {
148 const el = document.querySelector('#test-metadata')
149 return (
150 parseInt(el?.getAttribute('data-execution-complete-count') || '0') >=
151 2
152 )
153 },
154 { timeout: 10000 },
155 )
156​
157 // Verify the results
158 const metadata = await getMetadata(page)
159 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
160​
161 // Verify events show proper execution order
162 const events = await getEventLog(page)
163 const executionEvents = events.filter(
164 (e) => e.type === 'execution-start' || e.type === 'execution-complete',
165 )
166​
167 // Should have at least 4 events: start1, complete1, start2, complete2
168 expect(executionEvents.length).toBeGreaterThanOrEqual(4)
169 })
170​
171 test('parallel client tools execute concurrently', async ({
172 page,
173 testId,
174 aimockPort,
175 }) => {
176 await selectScenario(page, 'parallel-client-tools', testId, aimockPort)
177 await runTest(page)
178​
179 // Wait for the test to complete (expect 2 tools)
180 await waitForTestComplete(page, 15000, 2)
181​
182 // Verify the results
183 const metadata = await getMetadata(page)
184 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
185 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(2)
186​
187 // Verify both tools were executed
188 const events = await getEventLog(page)
189 const toolNames = new Set(events.map((e) => e.toolName))
190 expect(toolNames.has('show_notification')).toBe(true)
191 expect(toolNames.has('display_chart')).toBe(true)
192 })
193​
194 test('triple client sequence completes all three', async ({
195 page,
196 testId,
197 aimockPort,
198 }) => {
199 // Stress test: three client tools in sequence
200 // NOTE: This tests a complex multi-step continuation flow that requires
201 // additional investigation. Currently verifying at least 2 tools complete.
202 await selectScenario(page, 'triple-client-sequence', testId, aimockPort)
203 await runTest(page)
204​
205 // Wait for the test to complete (expect at least 2 tools)
206 await waitForTestComplete(page, 20000, 2)
207​
208 // Wait for execution events to propagate
209 await page.waitForFunction(
210 () => {
211 const el = document.querySelector('#test-metadata')
212 return (
213 parseInt(el?.getAttribute('data-execution-complete-count') || '0') >=
214 2
215 )
216 },
217 { timeout: 10000 },
218 )
219​
220 // Verify at least 2 tools complete
221 const metadata = await getMetadata(page)
222 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(2)
223 expect(parseInt(metadata.executionCompleteCount)).toBeGreaterThanOrEqual(2)
224 })
225​
226 test('server then two clients sequence completes', async ({
227 page,
228 testId,
229 aimockPort,
230 }) => {
231 // Tests complex flow: server tool -> client tool -> client tool
232 // NOTE: This tests a complex multi-step continuation flow involving
233 // both server and client tools. Currently verifying at least 1 client tool completes.
234 // Known issue: Server tool state tracking may need investigation.
235 await selectScenario(page, 'server-then-two-clients', testId, aimockPort)
236 await runTest(page)
237​
238 // Wait for at least 1 client tool to complete
239 await waitForTestComplete(page, 20000, 1)
240​
241 // Verify at least some tools appeared and 1 client tool completed
242 const metadata = await getMetadata(page)
243 expect(parseInt(metadata.toolCallCount)).toBeGreaterThanOrEqual(1)
244 expect(parseInt(metadata.completeToolCount)).toBeGreaterThanOrEqual(1)
245 })
246​
247 // Screenshot on failure
248 test.afterEach(async ({ page }, testInfo) => {
249 if (testInfo.status !== testInfo.expectedStatus) {
250 await page.screenshot({
251 path: `test-results/client-tool-failure-${testInfo.title.replace(/\s+/g, '-')}.png`,
252 fullPage: true,
253 })
254​
255 // Also log the event log and tool calls for debugging
256 const events = await getEventLog(page)
257 const toolCalls = await getToolCalls(page)
258 const metadata = await getMetadata(page)
259​
260 console.log('Test failed. Debug info:')
261 console.log('Metadata:', metadata)
262 console.log('Events:', events)
263 console.log('Tool calls:', toolCalls)
264 }
265 })
266})