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

runtime-context.spec.ts

117 lines3.2 KB

1import { z } from 'zod'
2import { test, expect } from '../fixtures'
3import { selectScenario, runTest, waitForTestComplete } from './helpers'
4import type { Page } from '@playwright/test'
5​
6const RuntimeContextOutputSchema = z.object({
7 userId: z.string(),
8 tenantId: z.string(),
9 source: z.string(),
10})
11​
12const ToolCallPartSchema = z.object({
13 type: z.literal('tool-call'),
14 name: z.string(),
15 output: z.unknown().optional(),
16})
17​
18const MessageSchema = z.object({
19 parts: z.array(z.unknown()).optional(),
20})
21​
22const ToolEventSchema = z.object({
23 type: z.string(),
24 toolName: z.string(),
25 details: z.string().optional(),
26})
27​
28async function getToolOutput(page: Page, name: string) {
29 const text = await page.locator('#messages-json-content').textContent()
30 const messages = z.array(MessageSchema).parse(JSON.parse(text || '[]'))
31​
32 for (const message of messages) {
33 for (const partData of message.parts ?? []) {
34 const part = ToolCallPartSchema.safeParse(partData)
35 if (part.success && part.data.name === name && part.data.output) {
36 return part.data.output
37 }
38 }
39 }
40​
41 return undefined
43​
44async function getRuntimeContextOutput(page: Page, name: string) {
45 const output = await getToolOutput(page, name)
46 return output === undefined ? null : RuntimeContextOutputSchema.parse(output)
48​
49async function getEvents(page: Page) {
50 const text = await page.locator('#event-log-json').textContent()
51 return z.array(ToolEventSchema).parse(JSON.parse(text || '[]'))
53​
54test.describe('Runtime Context E2E Tests', () => {
55 test('server context is available to server tools', async ({
56 page,
57 testId,
58 aimockPort,
59 }) => {
60 await selectScenario(page, 'server-context', testId, aimockPort)
61 await runTest(page)
62 await waitForTestComplete(page)
63​
64 await expect
65 .poll(() => getRuntimeContextOutput(page, 'read_server_context'))
66 .toEqual({
67 userId: 'server-user-context',
68 tenantId: 'server-tenant-context',
69 source: 'server-route',
70 })
71 })
72​
73 test('client context is available to client tools', async ({
74 page,
75 testId,
76 aimockPort,
77 }) => {
78 await selectScenario(page, 'client-context', testId, aimockPort)
79 await runTest(page)
80 await waitForTestComplete(page)
81​
82 await expect
83 .poll(() => getRuntimeContextOutput(page, 'read_client_context'))
84 .toEqual({
85 userId: 'client-user-context',
86 tenantId: 'client-tenant-context',
87 source: 'client-local',
88 })
89​
90 const events = await getEvents(page)
91 expect(events).toContainEqual(
92 expect.objectContaining({
93 type: 'execution-complete',
94 toolName: 'read_client_context',
95 details: 'client-user-context/client-tenant-context',
96 }),
97 )
98 })
99​
100 test('client forwarded props can be mapped into server context', async ({
101 page,
102 testId,
103 aimockPort,
104 }) => {
105 await selectScenario(page, 'client-server-context', testId, aimockPort)
106 await runTest(page)
107 await waitForTestComplete(page)
108​
109 await expect
110 .poll(() => getRuntimeContextOutput(page, 'read_server_context'))
111 .toEqual({
112 userId: 'client-forwarded-user-context',
113 tenantId: 'server-tenant-context',
114 source: 'forwarded-props',
115 })
116 })
117})