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

per-call-body.spec.ts

62 lines2.2 KB

1import { test, expect } from './fixtures'
2import {
3 featureUrl,
4 getLastAssistantMessage,
5 sendMessage,
6 waitForResponse,
7} from './helpers'
8​
9function isRecord(value: unknown): value is Record<string, unknown> {
10 return value !== null && typeof value === 'object' && !Array.isArray(value)
12​
13// Fixture: fixtures/chat/basic.json ("[per-call-body] send with per-call body").
14//
15// Coverage for SendMessageOptions.body: the chat page sends the
16// `[per-call-body]` prompt via
17// `sendMessage(text, { body: { perCallBodyMarker: testId } })`.
18//
19// The load-bearing assertion is on the page → /api/chat POST body:
20// `forwardedProps` must contain the per-call marker MERGED with the
21// chat-level `body` keys (provider/feature/testId/…). A regression that
22// dropped `options.body` fails the marker assertion. One that replaced
23// the merge fails the provider/feature assertions.
24test.describe('per-call sendMessage body', () => {
25 test('options.body reaches the wire under forwardedProps, merged with chat-level body', async ({
26 page,
27 testId,
28 aimockPort,
29 }) => {
30 const chatRequestBodies: Array<Record<string, unknown>> = []
31 page.on('request', (req) => {
32 if (req.method() !== 'POST') return
33 if (!req.url().endsWith('/api/chat')) return
34 const raw = req.postData()
35 if (!raw) return
36 try {
37 const parsed: unknown = JSON.parse(raw)
38 if (isRecord(parsed)) {
39 chatRequestBodies.push(parsed)
40 }
41 } catch {
42 // Non-JSON bodies are unrelated to the chat round-trip.
43 }
44 })
45​
46 await page.goto(featureUrl('openai', 'chat', testId, aimockPort))
47​
48 await sendMessage(page, '[per-call-body] send with per-call body')
49 await waitForResponse(page)
50 const response = await getLastAssistantMessage(page)
51 expect(response).toContain('Acknowledged the per-call body send.')
52​
53 expect(chatRequestBodies).toHaveLength(1)
54 const fp = chatRequestBodies[0]?.forwardedProps
55 expect(isRecord(fp)).toBe(true)
56 if (!isRecord(fp)) return
57 expect(fp.perCallBodyMarker).toBe(testId)
58 expect(fp.provider).toBe('openai')
59 expect(fp.feature).toBe('chat')
60 expect(fp.testId).toBe(testId)
61 })
62})