Open History to see the last commit
1import { test, expect } from './fixtures'
2import {
3 sendMessage,
4 waitForResponse,
5 getLastAssistantMessage,
6 featureUrl,
7} from './helpers'
8import { providersFor } from './test-matrix'
9
10test('OpenAI Responses structured output reports EOF without response.completed', async ({
11 request,
12}) => {
13 const response = await request.post(
14 '/api/openai-completed-response-text?scenario=structured-missing-terminal',
15 )
16
17 expect(response.ok()).toBe(true)
18 const result = await response.json()
19 expect(result.text).toBe('{"answer":"partial"}')
20 expect(result.events).toContain('TEXT_MESSAGE_CONTENT')
21 expect(result.events.at(-1)).toBe('RUN_ERROR')
22 expect(result.errorCode).toBe('incomplete-stream')
23 expect(result.completed).toBe(false)
24 expect(result.events).not.toContain('RUN_FINISHED')
25})
26
27for (const provider of providersFor('structured-output-stream')) {
28 test.describe(`${provider} — structured-output-stream`, () => {
29 test('streams structured JSON deltas in a single request', async ({
30 page,
31 testId,
32 aimockPort,
33 }) => {
34 await page.goto(
35 featureUrl(provider, 'structured-output-stream', testId, aimockPort),
36 )
37
38 await sendMessage(page, '[structured-stream] recommend a guitar as json')
39 await waitForResponse(page)
40
41 const response = await getLastAssistantMessage(page)
42 expect(response).toContain('Fender Stratocaster')
43 expect(response).toContain('1299')
44
45 // Routing assertion: per `docs/structured-outputs/streaming.md`, when
46 // `outputSchema` is set, `TEXT_MESSAGE_CONTENT` deltas accumulate into
47 // a `structured-output` part — NOT into a `text` part with raw JSON
48 // bytes. The harness `ChatUI` renders both part types with distinct
49 // testids, so the assistant message must have exactly one
50 // `structured-output-part` and zero `text-part`s.
51 const assistantMessage = page.getByTestId('assistant-message').last()
52 await expect(
53 assistantMessage.getByTestId('structured-output-part'),
54 ).toHaveCount(1)
55 await expect(assistantMessage.getByTestId('text-part')).toHaveCount(0)
56
57 // Verify the terminal `structured-output.complete` CUSTOM event
58 // reached the client and carries the parsed object — protects against
59 // a regression where the event is dropped but the JSON text still
60 // happens to render.
61 const completeEl = page.getByTestId('structured-output-complete')
62 await expect(completeEl).toBeAttached()
63 const structuredAttr = await completeEl.getAttribute(
64 'data-structured-output',
65 )
66 expect(structuredAttr).toBeTruthy()
67 const parsed = JSON.parse(structuredAttr!)
68 expect(parsed.name).toContain('Fender Stratocaster')
69 expect(parsed.price).toBe(1299)
70
71 // `condition` is `.optional()`, so strict-mode widening made the provider
72 // return `null` for it (see the fixture). The engine must un-widen that
73 // synthesized null before the streamed `structured-output.complete` event
74 // reaches the consumer, so the field reads back as ABSENT — not `null`.
75 // Pre-fix, null-preserving adapters (e.g. openrouter) leaked the `null`
76 // straight through on the streaming path.
77 expect('condition' in parsed).toBe(false)
78
79 // Verify the response actually streamed (more than one content delta).
80 // A regression that silently fell back to the synthetic single-delta
81 // path would still pass the substring assertion above but fail here.
82 const countAttr = await page
83 .getByTestId('content-delta-count')
84 .getAttribute('data-count')
85 expect(Number(countAttr)).toBeGreaterThan(1)
86 })
87
88 test('aborting mid-stream stops the run cleanly', async ({
89 page,
90 testId,
91 aimockPort,
92 }) => {
93 await page.goto(
94 featureUrl(provider, 'structured-output-stream', testId, aimockPort),
95 )
96
97 // Uses the slow-streaming fixture (tokensPerSecond + small chunkSize)
98 // so the stop button is reliably visible mid-stream.
99 await sendMessage(
100 page,
101 '[structured-stream-abort] recommend a guitar slowly',
102 )
103
104 await expect(page.getByTestId('loading-indicator')).toBeVisible({
105 timeout: 10_000,
106 })
107
108 const stopButton = page.getByTestId('stop-button')
109 await expect(stopButton).toBeVisible({ timeout: 5_000 })
110 await stopButton.click()
111
112 await expect(page.getByTestId('loading-indicator')).not.toBeVisible({
113 timeout: 10_000,
114 })
115
116 // The structured-output.complete event must not have reached the
117 // client — aborting before the JSON finished streaming should leave
118 // the run terminated, not "completed with empty result".
119 await expect(page.getByTestId('structured-output-complete')).toHaveCount(
120 0,
121 )
122 })
123
124 test('invalid JSON does not emit structured-output.complete', async ({
125 page,
126 testId,
127 aimockPort,
128 }) => {
129 test.skip(
130 provider !== 'gemini',
131 'Pins the Gemini native parse-error path added with structuredOutputStream',
132 )
133 await page.goto(
134 featureUrl(provider, 'structured-output-stream', testId, aimockPort),
135 )
136
137 await sendMessage(
138 page,
139 '[structured-stream-invalid] recommend a guitar as json',
140 )
141
142 await expect(page.getByTestId('loading-indicator')).not.toBeVisible({
143 timeout: 15_000,
144 })
145 await expect(page.getByTestId('structured-output-complete')).toHaveCount(
146 0,
147 )
148 })
149 })
150}