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

tool-approval.spec.ts

78 lines2.7 KB

1import { test, expect } from './fixtures'
2import {
3 sendMessage,
4 waitForResponse,
5 approveToolCall,
6 denyToolCall,
7 waitForAssistantText,
8 featureUrl,
9} from './helpers'
10import { providersFor } from './test-matrix'
11​
12for (const provider of providersFor('tool-approval')) {
13 test.describe(`${provider} — tool-approval`, () => {
14 test('shows approval prompt and completes on approve', async ({
15 page,
16 testId,
17 aimockPort,
18 }) => {
19 await page.goto(featureUrl(provider, 'tool-approval', testId, aimockPort))
20​
21 await sendMessage(page, '[approval] add the stratocaster to my cart')
22​
23 await expect(page.getByTestId('approval-prompt-addToCart')).toBeVisible({
24 timeout: 20000,
25 })
26 await approveToolCall(page, 'addToCart')
27​
28 // Wait for text response after approval + tool execution
29 await waitForAssistantText(page, 'added')
30 })
31​
32 test('follow-up message after approval does not produce empty tool_use.name (issue #532)', async ({
33 page,
34 testId,
35 aimockPort,
36 }) => {
37 await page.goto(featureUrl(provider, 'tool-approval', testId, aimockPort))
38​
39 await sendMessage(page, '[approval] add the stratocaster to my cart')
40​
41 await expect(page.getByTestId('approval-prompt-addToCart')).toBeVisible({
42 timeout: 20000,
43 })
44 await approveToolCall(page, 'addToCart')
45 await waitForAssistantText(page, 'added')
46​
47 // The approved tool call now lives in message history. Sending a new
48 // message previously produced a 400 from Anthropic because the
49 // tool_use block had an empty `name` (issue #532). The follow-up must
50 // round-trip without error.
51 await sendMessage(page, '[approval] follow-up: anything else?')
52 await waitForAssistantText(page, 'follow-up')
53 })
54​
55 test('handles denial', async ({ page, testId, aimockPort }) => {
56 await page.goto(featureUrl(provider, 'tool-approval', testId, aimockPort))
57​
58 await sendMessage(page, '[approval-deny] add the stratocaster to my cart')
59​
60 await expect(page.getByTestId('approval-prompt-addToCart')).toBeVisible({
61 timeout: 20000,
62 })
63 await denyToolCall(page, 'addToCart')
64 await waitForResponse(page)
65​
66 // After denial, verify the approval prompt is no longer showing
67 // (the tool state transitions from 'approval-requested' to 'approval-responded')
68 await expect(
69 page.getByTestId('approval-prompt-addToCart'),
70 ).not.toBeVisible({ timeout: 10000 })
71​
72 // An assistant message should exist (the LLM responds after denial)
73 const messages = page.getByTestId('assistant-message')
74 const count = await messages.count()
75 expect(count).toBeGreaterThanOrEqual(1)
76 })
77 })