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

server-tool-history.spec.ts

64 lines1.8 KB

1import { test, expect } from '../fixtures'
2import { getMessages } from './helpers'
3​
4test.describe('Server Tool History Hydration', () => {
5 test('hydrates server tool-call output from a matching tool result', async ({
6 page,
7 testId,
8 aimockPort,
9 }) => {
10 const params = new URLSearchParams({
11 historyFixture: 'server-tool-result',
12 testId,
13 aimockPort: String(aimockPort),
14 })
15​
16 await page.goto(`/tools-test?${params.toString()}`)
17 await page.waitForSelector('#run-test-button')
18​
19 await page.waitForFunction(
20 () => {
21 const messagesEl = document.getElementById('messages-json-content')
22 const messages = JSON.parse(messagesEl?.textContent || '[]')
23 const toolCall = messages
24 .flatMap((msg: any) => msg.parts || [])
25 .find(
26 (part: any) =>
27 part.type === 'tool-call' && part.id === 'history-tc-1',
28 )
29​
30 return toolCall?.state === 'complete' && toolCall.output !== undefined
31 },
32 { timeout: 10000 },
33 )
34​
35 const messages = await getMessages(page)
36 const assistant = messages.find((message) => message.role === 'assistant')
37 const toolCall = assistant?.parts.find(
38 (part: any) => part.type === 'tool-call' && part.id === 'history-tc-1',
39 )
40 const toolResult = assistant?.parts.find(
41 (part: any) =>
42 part.type === 'tool-result' && part.toolCallId === 'history-tc-1',
43 )
44​
45 expect(toolCall).toMatchObject({
46 type: 'tool-call',
47 id: 'history-tc-1',
48 name: 'getWeather',
49 arguments: '{"city":"NYC"}',
50 state: 'complete',
51 output: {
52 temp: 72,
53 condition: 'sunny',
54 },
55 })
56​
57 expect(toolResult).toMatchObject({
58 type: 'tool-result',
59 toolCallId: 'history-tc-1',
60 content: '{"temp":72,"condition":"sunny"}',
61 state: 'complete',
62 })
63 })
64})