Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * Regressions for #758 and #1125. `AnthropicTextAdapter` has no native
5 * `structuredOutputStream`, so `chat({ outputSchema, stream: true })` runs
6 * through the activity layer's `fallbackStructuredOutputStream`. That wrapper
7 * used to drop the `usage` returned by `structuredOutput()`, so consumers
8 * reading `RUN_FINISHED.usage` (and the `runOnUsage` middleware hook) saw
9 * `undefined` on every fallback-path provider.
10 *
11 * The `/api/anthropic-structured-usage` route drives the adapter against an
12 * aimock mount whose tool-forced `structured_output` response carries
13 * `input_tokens` / `output_tokens` / `cache_read_input_tokens`. This is the
14 * end-to-end proof that usage survives the fallback path and its timestamps
15 * follow stream order.
16 */
17test.describe('anthropic — structured-output fallback', () => {
18 test('preserves usage and timestamp ordering', async ({ request }) => {
19 const res = await request.post('/api/anthropic-structured-usage')
20 expect(res.ok()).toBe(true)
21
22 const { ok, usage, timestamps, error } = (await res.json()) as {
23 ok: boolean
24 error?: string
25 usage?: {
26 promptTokens?: number
27 completionTokens?: number
28 totalTokens?: number
29 promptTokensDetails?: { cachedTokens?: number }
30 }
31 timestamps: {
32 runStarted: number
33 structuredOutputStart: number
34 structuredOutputComplete: number
35 runFinished: number
36 }
37 }
38
39 expect(error ?? null).toBeNull()
40 expect(ok).toBe(true)
41 expect(usage).toMatchObject({
42 promptTokens: 125,
43 completionTokens: 1346,
44 totalTokens: 1471,
45 promptTokensDetails: { cachedTokens: 5760 },
46 })
47 expect(timestamps.structuredOutputStart).toBeGreaterThanOrEqual(
48 timestamps.runStarted,
49 )
50 expect(timestamps.structuredOutputComplete).toBeGreaterThanOrEqual(
51 timestamps.structuredOutputStart,
52 )
53 expect(timestamps.runFinished).toBeGreaterThanOrEqual(
54 timestamps.structuredOutputComplete,
55 )
56 })
57})