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 // promptTokens is the total input: 125 uncached + 5760 cache read. The
42 // mount sends no cache write.
43 expect(usage).toMatchObject({
44 promptTokens: 5885,
45 completionTokens: 1346,
46 totalTokens: 7231,
47 promptTokensDetails: { cachedTokens: 5760 },
48 })
49 expect(timestamps.structuredOutputStart).toBeGreaterThanOrEqual(
50 timestamps.runStarted,
51 )
52 expect(timestamps.structuredOutputComplete).toBeGreaterThanOrEqual(
53 timestamps.structuredOutputStart,
54 )
55 expect(timestamps.runFinished).toBeGreaterThanOrEqual(
56 timestamps.structuredOutputComplete,
57 )
58 })
59})