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

max-tool-calls.spec.ts

47 lines1.6 KB

1import { expect, test } from '@playwright/test'
2​
3/**
4 * Regression for issue #964: maxIterations counts model turns, not tool calls.
5 * App-owned tool-budget middleware (onBeforeToolCall + onShouldContinue) must
6 * bound fan-out from a single fat turn.
7 *
8 * Uses a synthetic in-process adapter (no real provider HTTP call), so aimock
9 * wiring is not applicable here.
10 */
11test.describe('chat() tool-call budget middleware (#964)', () => {
12 test('caps parallel fan-out and cumulative tool calls', async ({
13 request,
14 }) => {
15 const res = await request.post('/api/max-tool-calls-wire')
16 expect(res.ok()).toBe(true)
17​
18 const body = (await res.json()) as {
19 chunks: Array<Record<string, unknown>>
20 error: string | null
21 executeCount: number
22 }
23​
24 expect(body.error).toBeNull()
25​
26 // 8 parallel calls emitted; maxPerTurn=3 → only 3 execute
27 expect(body.executeCount).toBe(3)
28​
29 const toolResults = body.chunks.filter((c) => c.type === 'TOOL_CALL_RESULT')
30 // Every model-emitted call still gets a tool result (3 real + 5 skipped)
31 expect(toolResults.length).toBe(8)
32​
33 const skipped = toolResults.filter((c) => {
34 const content = c.content
35 return (
36 typeof content === 'string' &&
37 content.includes('exceeded maxToolCallsPerTurn')
38 )
39 })
40 expect(skipped.length).toBe(5)
41​
42 // max: 5 — after the fat turn toolCallCount=8 (>= 5), so no further
43 // model turn runs. executeCount stays at 3 either way.
44 const runFinished = body.chunks.filter((c) => c.type === 'RUN_FINISHED')
45 expect(runFinished.length).toBe(1)
46 })
47})