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

structured-output-stream.spec.ts

99 lines3.7 KB

1import { test, expect } from './fixtures'
2import {
3 sendMessage,
4 waitForResponse,
5 getLastAssistantMessage,
6 featureUrl,
7} from './helpers'
8import { providersFor } from './test-matrix'
9​
10for (const provider of providersFor('structured-output-stream')) {
11 test.describe(`${provider} — structured-output-stream`, () => {
12 test('streams structured JSON deltas in a single request', async ({
13 page,
14 testId,
15 aimockPort,
16 }) => {
17 await page.goto(
18 featureUrl(provider, 'structured-output-stream', testId, aimockPort),
19 )
20​
21 await sendMessage(page, '[structured-stream] recommend a guitar as json')
22 await waitForResponse(page)
23​
24 const response = await getLastAssistantMessage(page)
25 expect(response).toContain('Fender Stratocaster')
26 expect(response).toContain('1299')
27​
28 // Routing assertion: per `docs/structured-outputs/streaming.md`, when
29 // `outputSchema` is set, `TEXT_MESSAGE_CONTENT` deltas accumulate into
30 // a `structured-output` part — NOT into a `text` part with raw JSON
31 // bytes. The harness `ChatUI` renders both part types with distinct
32 // testids, so the assistant message must have exactly one
33 // `structured-output-part` and zero `text-part`s.
34 const assistantMessage = page.getByTestId('assistant-message').last()
35 await expect(
36 assistantMessage.getByTestId('structured-output-part'),
37 ).toHaveCount(1)
38 await expect(assistantMessage.getByTestId('text-part')).toHaveCount(0)
39​
40 // Verify the terminal `structured-output.complete` CUSTOM event
41 // reached the client and carries the parsed object — protects against
42 // a regression where the event is dropped but the JSON text still
43 // happens to render.
44 const completeEl = page.getByTestId('structured-output-complete')
45 await expect(completeEl).toBeAttached()
46 const structuredAttr = await completeEl.getAttribute(
47 'data-structured-output',
48 )
49 expect(structuredAttr).toBeTruthy()
50 const parsed = JSON.parse(structuredAttr!)
51 expect(parsed.name).toContain('Fender Stratocaster')
52 expect(parsed.price).toBe(1299)
53​
54 // Verify the response actually streamed (more than one content delta).
55 // A regression that silently fell back to the synthetic single-delta
56 // path would still pass the substring assertion above but fail here.
57 const countAttr = await page
58 .getByTestId('content-delta-count')
59 .getAttribute('data-count')
60 expect(Number(countAttr)).toBeGreaterThan(1)
61 })
62​
63 test('aborting mid-stream stops the run cleanly', async ({
64 page,
65 testId,
66 aimockPort,
67 }) => {
68 await page.goto(
69 featureUrl(provider, 'structured-output-stream', testId, aimockPort),
70 )
71​
72 // Uses the slow-streaming fixture (tokensPerSecond + small chunkSize)
73 // so the stop button is reliably visible mid-stream.
74 await sendMessage(
75 page,
76 '[structured-stream-abort] recommend a guitar slowly',
77 )
78​
79 await expect(page.getByTestId('loading-indicator')).toBeVisible({
80 timeout: 10_000,
81 })
82​
83 const stopButton = page.getByTestId('stop-button')
84 await expect(stopButton).toBeVisible({ timeout: 5_000 })
85 await stopButton.click()
86​
87 await expect(page.getByTestId('loading-indicator')).not.toBeVisible({
88 timeout: 10_000,
89 })
90​
91 // The structured-output.complete event must not have reached the
92 // client — aborting before the JSON finished streaming should leave
93 // the run terminated, not "completed with empty result".
94 await expect(page.getByTestId('structured-output-complete')).toHaveCount(
95 0,
96 )
97 })
98 })