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

subagents.spec.ts

317 lines9.9 KB

1import { test, expect } from './fixtures'
2import type { Page } from '@playwright/test'
3import type { SubagentScenario } from '../src/lib/subagents-test'
4​
5/** Click Run once the page hydrated. A click before hydration does nothing. */
6async function run(page: Page) {
7 await page.waitForLoadState('networkidle')
8 await expect(async () => {
9 await page.getByTestId('run').click()
10 await expect(page.getByTestId('message-count')).not.toHaveText('0', {
11 timeout: 2_000,
12 })
13 }).toPass({ timeout: 15_000 })
15​
16function open(
17 page: Page,
18 scenario: SubagentScenario,
19 testId: string,
20 aimockPort: number,
21) {
22 return page.goto(
23 `/subagents-test?scenario=${scenario}&testId=${encodeURIComponent(testId)}&aimockPort=${aimockPort}`,
24 )
26​
27test.describe('subagents', () => {
28 test('a routed child keeps its reasoning, tool call, and reply on its card', async ({
29 page,
30 testId,
31 aimockPort,
32 }) => {
33 await open(page, 'route', testId, aimockPort)
34 await run(page)
35​
36 await expect(page.getByTestId('card-status-researcher')).toHaveText(
37 'finished',
38 )
39 const card = page.getByTestId('card-researcher')
40 await expect(card).toContainText('thinking:Look up the facts first.')
41 await expect(card).toContainText('tool:lookupFacts')
42 await expect(card).toContainText('result:')
43 await expect(card).toContainText('text:Squids have three hearts.')
44 // Nothing from the child lands on the parent message.
45 await expect(page.getByTestId('parent-part-types')).toHaveText('subagent')
46 })
47​
48 test('a child approval pauses the run, then the same child finishes', async ({
49 page,
50 testId,
51 aimockPort,
52 }) => {
53 await open(page, 'approval', testId, aimockPort)
54 await run(page)
55​
56 await expect(page.getByTestId('card-status-cleaner')).toHaveText(
57 'suspended',
58 )
59 await expect(page.getByTestId('card-cleaner')).toContainText(
60 'tool:deleteLogs:approval-requested',
61 )
62​
63 await page.getByTestId('approve-deleteLogs').click()
64​
65 await expect(page.getByTestId('card-status-cleaner')).toHaveText('finished')
66 const card = page.getByTestId('card-cleaner')
67 await expect(card).toContainText('result:')
68 await expect(card).toContainText('text:Deleted the old logs.')
69 await expect(page.getByTestId('approve-deleteLogs')).toHaveCount(0)
70 // One card: the resume continued the same child.
71 await expect(page.getByTestId('card-cleaner')).toHaveCount(1)
72 })
73​
74 test('with persistence, a child of a tool call resumes after its approval', async ({
75 page,
76 testId,
77 aimockPort,
78 }) => {
79 await open(page, 'tool-approval', testId, aimockPort)
80 await run(page)
81​
82 await expect(page.getByTestId('card-status-cleaner')).toHaveText(
83 'suspended',
84 )
85 await page.getByTestId('approve-deleteLogs').click()
86​
87 await expect(page.getByTestId('card-status-cleaner')).toHaveText('finished')
88 await expect(page.getByTestId('card-cleaner')).toContainText(
89 'text:Deleted the old logs.',
90 )
91 await expect(page.getByTestId('parent-text')).toHaveText('Cleanup is done.')
92 })
93​
94 test('a card overrides the kit widgets for its own parts', async ({
95 page,
96 testId,
97 aimockPort,
98 }) => {
99 await page.goto(
100 `/subagents-ui-test?testId=${encodeURIComponent(testId)}&aimockPort=${aimockPort}`,
101 )
102 await run(page)
103​
104 const card = page.getByTestId('card-researcher')
105 await expect(card.getByTestId('root-text')).toHaveText(
106 'Squids have three hearts.',
107 )
108 // The card's own thinking widget, not the root one.
109 await expect(card.getByTestId('card-thinking')).toHaveText(
110 'Look up the facts first.',
111 )
112 await expect(card.getByTestId('root-thinking')).toHaveCount(0)
113 // No card override for the tool, so the root widget renders it.
114 await expect(card.getByTestId('root-tool')).toContainText('lookupFacts:')
115 })
116​
117 test('without a router the model starts the child and reads its result', async ({
118 page,
119 request,
120 testId,
121 aimockPort,
122 }) => {
123 await open(page, 'tool', testId, aimockPort)
124 await run(page)
125​
126 await expect(page.getByTestId('parent-text')).toHaveText(
127 'Research is done.',
128 )
129 await expect(page.getByTestId('card-status-researcher')).toHaveText(
130 'finished',
131 )
132 await expect(page.getByTestId('card-researcher')).toContainText(
133 'text:Squids have three hearts.',
134 )
135​
136 const journal = await request.get(
137 `http://127.0.0.1:${aimockPort}/v1/_requests`,
138 )
139 const entries = (await journal.json()) as Array<{
140 headers?: Record<string, string>
141 body?: {
142 messages?: Array<{ role?: string; content?: string | null }>
143 } | null
144 }>
145 const parentRequest = entries.find(
146 (entry) =>
147 entry.headers?.['x-test-id'] === testId &&
148 entry.body?.messages?.some((message) => message.role === 'tool'),
149 )
150 const toolMessage = parentRequest?.body?.messages?.find(
151 (message) => message.role === 'tool',
152 )
153​
154 expect(toolMessage?.content).toBe(
155 JSON.stringify({ result: 'Squids have three hearts.' }),
156 )
157 })
158​
159 test('the parent model writes the brief the child runs on', async ({
160 page,
161 testId,
162 aimockPort,
163 }) => {
164 await open(page, 'brief', testId, aimockPort)
165 await run(page)
166​
167 await expect(page.getByTestId('parent-text')).toHaveText(
168 'The comparison is ready.',
169 )
170 await expect(page.getByTestId('card-status-researcher')).toHaveText(
171 'finished',
172 )
173 // The child's fixture matches only the brief. A child that got the
174 // transcript instead would get no reply.
175 await expect(page.getByTestId('card-researcher')).toContainText(
176 'text:Squids have three hearts and blue blood.',
177 )
178 // The card keeps the checked input the parent model wrote.
179 await expect(page.getByTestId('card-input-researcher')).toHaveText(
180 JSON.stringify({ task: '[subagent-brief-task] List two squid facts.' }),
181 )
182 })
183​
184 test('a child whose run resolves to a string reports it to the parent', async ({
185 page,
186 testId,
187 aimockPort,
188 }) => {
189 await open(page, 'result', testId, aimockPort)
190 await run(page)
191​
192 await expect(page.getByTestId('parent-text')).toHaveText(
193 'Pricing is ready.',
194 )
195 await expect(page.getByTestId('card-status-pricer')).toHaveText('finished')
196 await expect(page.getByTestId('card-pricer')).toContainText(
197 'text:Vendor A costs 42 EUR.',
198 )
199 })
200​
201 test('subagents.limits refuses a call past maxCalls with a tool error', async ({
202 page,
203 request,
204 testId,
205 aimockPort,
206 }) => {
207 await open(page, 'limits', testId, aimockPort)
208 await run(page)
209​
210 await expect(page.getByTestId('parent-text')).toHaveText(
211 'One topic is done.',
212 )
213 await expect(page.getByTestId('card-researcher')).toHaveCount(1)
214 await expect(page.getByTestId('card-researcher')).toContainText(
215 'text:Squids have three hearts.',
216 )
217​
218 const journal = await request.get(
219 `http://127.0.0.1:${aimockPort}/v1/_requests`,
220 )
221 const entries = (await journal.json()) as Array<{
222 headers?: Record<string, string>
223 body?: {
224 messages?: Array<{ role?: string; content?: string | null }>
225 } | null
226 }>
227 const toolMessages = entries
228 .filter((entry) => entry.headers?.['x-test-id'] === testId)
229 .flatMap((entry) => entry.body?.messages ?? [])
230 .filter((message) => message.role === 'tool')
231 expect(
232 toolMessages.some((message) =>
233 message.content?.includes('subagent limit reached (maxCalls 1)'),
234 ),
235 ).toBe(true)
236 })
237​
238 test('an agent tool continues a child by its sessionId', async ({
239 page,
240 testId,
241 aimockPort,
242 }) => {
243 await open(page, 'session', testId, aimockPort)
244 await run(page)
245​
246 // The parent model runs a third time only after the second call gave it
247 // the child's result.
248 await expect(page.getByTestId('parent-text')).toHaveText(
249 'Both notes are ready.',
250 )
251 await expect(page.getByTestId('card-status-researcher')).toHaveText(
252 'finished',
253 )
254 const card = page.getByTestId('card-researcher')
255 await expect(card).toContainText('text:Squids have three hearts.')
256 // The second call has no prompt, so the child's last user message is the
257 // task it stored from the first call. Only that fixture gives this reply.
258 // A child that did not get its earlier messages would get no reply.
259 await expect(card).toContainText('text:Squids also have blue blood.')
260 // One card: the second call continued the same child.
261 await expect(card).toHaveCount(1)
262 })
263​
264 test('subagents.keys gives the child its key', async ({
265 page,
266 request,
267 testId,
268 aimockPort,
269 }) => {
270 await open(page, 'keys', testId, aimockPort)
271 await run(page)
272​
273 await expect(page.getByTestId('parent-text')).toHaveText(
274 'The writer is done.',
275 )
276 await expect(page.getByTestId('card-writer')).toContainText(
277 'text:The draft is written.',
278 )
279​
280 const journal = await request.get(
281 `http://127.0.0.1:${aimockPort}/v1/_requests`,
282 )
283 const entries = (await journal.json()) as Array<{
284 headers?: Record<string, string>
285 body?: {
286 messages?: Array<{ role?: string; content?: string | null }>
287 } | null
288 }>
289 const child = entries.find(
290 (entry) =>
291 entry.headers?.['x-test-id'] === testId &&
292 entry.body?.messages?.some((message) =>
293 message.content?.includes('[subagent-keys-task]'),
294 ),
295 )
296 expect(child?.headers?.['x-e2e-authorization']).toBe(
297 'Bearer sk-e2e-subagent-key',
298 )
299 })
300​
301 test('the router gives the child its input', async ({
302 page,
303 testId,
304 aimockPort,
305 }) => {
306 await open(page, 'routed-brief', testId, aimockPort)
307 await run(page)
308​
309 await expect(page.getByTestId('card-status-researcher')).toHaveText(
310 'finished',
311 )
312 // The child's fixture matches only the task from the router pick.
313 await expect(page.getByTestId('card-researcher')).toContainText(
314 'text:Squids have three hearts and blue blood.',
315 )
316 })
317})