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

subagents.spec.ts

297 lines9.3 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('a card overrides the kit widgets for its own parts', async ({
75 page,
76 testId,
77 aimockPort,
78 }) => {
79 await page.goto(
80 `/subagents-ui-test?testId=${encodeURIComponent(testId)}&aimockPort=${aimockPort}`,
81 )
82 await run(page)
83​
84 const card = page.getByTestId('card-researcher')
85 await expect(card.getByTestId('root-text')).toHaveText(
86 'Squids have three hearts.',
87 )
88 // The card's own thinking widget, not the root one.
89 await expect(card.getByTestId('card-thinking')).toHaveText(
90 'Look up the facts first.',
91 )
92 await expect(card.getByTestId('root-thinking')).toHaveCount(0)
93 // No card override for the tool, so the root widget renders it.
94 await expect(card.getByTestId('root-tool')).toContainText('lookupFacts:')
95 })
96​
97 test('without a router the model starts the child and reads its result', async ({
98 page,
99 request,
100 testId,
101 aimockPort,
102 }) => {
103 await open(page, 'tool', testId, aimockPort)
104 await run(page)
105​
106 await expect(page.getByTestId('parent-text')).toHaveText(
107 'Research is done.',
108 )
109 await expect(page.getByTestId('card-status-researcher')).toHaveText(
110 'finished',
111 )
112 await expect(page.getByTestId('card-researcher')).toContainText(
113 'text:Squids have three hearts.',
114 )
115​
116 const journal = await request.get(
117 `http://127.0.0.1:${aimockPort}/v1/_requests`,
118 )
119 const entries = (await journal.json()) as Array<{
120 headers?: Record<string, string>
121 body?: {
122 messages?: Array<{ role?: string; content?: string | null }>
123 } | null
124 }>
125 const parentRequest = entries.find(
126 (entry) =>
127 entry.headers?.['x-test-id'] === testId &&
128 entry.body?.messages?.some((message) => message.role === 'tool'),
129 )
130 const toolMessage = parentRequest?.body?.messages?.find(
131 (message) => message.role === 'tool',
132 )
133​
134 expect(toolMessage?.content).toBe(
135 JSON.stringify({ result: 'Squids have three hearts.' }),
136 )
137 })
138​
139 test('the parent model writes the brief the child runs on', async ({
140 page,
141 testId,
142 aimockPort,
143 }) => {
144 await open(page, 'brief', testId, aimockPort)
145 await run(page)
146​
147 await expect(page.getByTestId('parent-text')).toHaveText(
148 'The comparison is ready.',
149 )
150 await expect(page.getByTestId('card-status-researcher')).toHaveText(
151 'finished',
152 )
153 // The child's fixture matches only the brief. A child that got the
154 // transcript instead would get no reply.
155 await expect(page.getByTestId('card-researcher')).toContainText(
156 'text:Squids have three hearts and blue blood.',
157 )
158 // The card keeps the checked input the parent model wrote.
159 await expect(page.getByTestId('card-input-researcher')).toHaveText(
160 JSON.stringify({ task: '[subagent-brief-task] List two squid facts.' }),
161 )
162 })
163​
164 test('a child whose run resolves to a string reports it to the parent', async ({
165 page,
166 testId,
167 aimockPort,
168 }) => {
169 await open(page, 'result', testId, aimockPort)
170 await run(page)
171​
172 await expect(page.getByTestId('parent-text')).toHaveText(
173 'Pricing is ready.',
174 )
175 await expect(page.getByTestId('card-status-pricer')).toHaveText('finished')
176 await expect(page.getByTestId('card-pricer')).toContainText(
177 'text:Vendor A costs 42 EUR.',
178 )
179 })
180​
181 test('subagents.limits refuses a call past maxCalls with a tool error', async ({
182 page,
183 request,
184 testId,
185 aimockPort,
186 }) => {
187 await open(page, 'limits', testId, aimockPort)
188 await run(page)
189​
190 await expect(page.getByTestId('parent-text')).toHaveText(
191 'One topic is done.',
192 )
193 await expect(page.getByTestId('card-researcher')).toHaveCount(1)
194 await expect(page.getByTestId('card-researcher')).toContainText(
195 'text:Squids have three hearts.',
196 )
197​
198 const journal = await request.get(
199 `http://127.0.0.1:${aimockPort}/v1/_requests`,
200 )
201 const entries = (await journal.json()) as Array<{
202 headers?: Record<string, string>
203 body?: {
204 messages?: Array<{ role?: string; content?: string | null }>
205 } | null
206 }>
207 const toolMessages = entries
208 .filter((entry) => entry.headers?.['x-test-id'] === testId)
209 .flatMap((entry) => entry.body?.messages ?? [])
210 .filter((message) => message.role === 'tool')
211 expect(
212 toolMessages.some((message) =>
213 message.content?.includes('subagent limit reached (maxCalls 1)'),
214 ),
215 ).toBe(true)
216 })
217​
218 test('an agent tool continues a child by its sessionId', async ({
219 page,
220 testId,
221 aimockPort,
222 }) => {
223 await open(page, 'session', testId, aimockPort)
224 await run(page)
225​
226 // The parent model runs a third time only after the second call gave it
227 // the child's result.
228 await expect(page.getByTestId('parent-text')).toHaveText(
229 'Both notes are ready.',
230 )
231 await expect(page.getByTestId('card-status-researcher')).toHaveText(
232 'finished',
233 )
234 const card = page.getByTestId('card-researcher')
235 await expect(card).toContainText('text:Squids have three hearts.')
236 // The second call has no prompt, so the child's last user message is the
237 // task it stored from the first call. Only that fixture gives this reply.
238 // A child that did not get its earlier messages would get no reply.
239 await expect(card).toContainText('text:Squids also have blue blood.')
240 // One card: the second call continued the same child.
241 await expect(card).toHaveCount(1)
242 })
243​
244 test('subagents.keys gives the child its key', async ({
245 page,
246 request,
247 testId,
248 aimockPort,
249 }) => {
250 await open(page, 'keys', testId, aimockPort)
251 await run(page)
252​
253 await expect(page.getByTestId('parent-text')).toHaveText(
254 'The writer is done.',
255 )
256 await expect(page.getByTestId('card-writer')).toContainText(
257 'text:The draft is written.',
258 )
259​
260 const journal = await request.get(
261 `http://127.0.0.1:${aimockPort}/v1/_requests`,
262 )
263 const entries = (await journal.json()) as Array<{
264 headers?: Record<string, string>
265 body?: {
266 messages?: Array<{ role?: string; content?: string | null }>
267 } | null
268 }>
269 const child = entries.find(
270 (entry) =>
271 entry.headers?.['x-test-id'] === testId &&
272 entry.body?.messages?.some((message) =>
273 message.content?.includes('[subagent-keys-task]'),
274 ),
275 )
276 expect(child?.headers?.['x-e2e-authorization']).toBe(
277 'Bearer sk-e2e-subagent-key',
278 )
279 })
280​
281 test('the router gives the child its input', async ({
282 page,
283 testId,
284 aimockPort,
285 }) => {
286 await open(page, 'routed-brief', testId, aimockPort)
287 await run(page)
288​
289 await expect(page.getByTestId('card-status-researcher')).toHaveText(
290 'finished',
291 )
292 // The child's fixture matches only the task from the router pick.
293 await expect(page.getByTestId('card-researcher')).toContainText(
294 'text:Squids have three hearts and blue blood.',
295 )
296 })
297})