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

ag-ui-compliance.spec.ts

64 lines2.1 KB

1import { test, expect } from './fixtures'
2import { sendMessage, waitForResponse, featureUrl } from './helpers'
3​
4test.describe('AG-UI client-to-server compliance', () => {
5 test('POST body has RunAgentInput shape and persists threadId across sends', async ({
6 page,
7 testId,
8 aimockPort,
9 }) => {
10 const requestBodies: Array<any> = []
11 page.on('request', (request) => {
12 if (request.url().includes('/api/chat') && request.method() === 'POST') {
13 const body = request.postDataJSON()
14 if (body) requestBodies.push(body)
15 }
16 })
17​
18 await page.goto(featureUrl('openai', 'chat', testId, aimockPort))
19​
20 // Send first message
21 await sendMessage(page, '[chat] hello')
22 await waitForResponse(page)
23​
24 // Send second message in the same session
25 await sendMessage(page, '[chat] another message')
26 await waitForResponse(page)
27​
28 expect(requestBodies.length).toBeGreaterThanOrEqual(2)
29​
30 const first = requestBodies[0]!
31 const second = requestBodies[1]!
32​
33 // Wire shape: every field required by RunAgentInput must be present
34 for (const body of [first, second]) {
35 expect(body).toHaveProperty('threadId')
36 expect(body).toHaveProperty('runId')
37 expect(body).toHaveProperty('state')
38 expect(body).toHaveProperty('messages')
39 expect(body).toHaveProperty('tools')
40 expect(body).toHaveProperty('context')
41 expect(body).toHaveProperty('forwardedProps')
42 expect(Array.isArray(body.messages)).toBe(true)
43 expect(Array.isArray(body.tools)).toBe(true)
44 }
45​
46 // threadId continuity: same session → same threadId
47 expect(first.threadId).toBe(second.threadId)
48​
49 // runId freshness: each send generates a new runId
50 expect(first.runId).not.toBe(second.runId)
51​
52 // Anchors are spec AG-UI messages: id + role, no TanStack `parts`.
53 const anchors = second.messages.filter(
54 (m: any) =>
55 m.role === 'user' || m.role === 'system' || m.role === 'assistant',
56 )
57 expect(anchors.length).toBeGreaterThan(0)
58 for (const a of anchors) {
59 expect(a).not.toHaveProperty('parts')
60 expect(a).toHaveProperty('id')
61 expect(a).toHaveProperty('role')
62 }
63 })
64})