Open History to see the last commit
1import { test, expect } from './fixtures'
2
3/**
4 * End-to-end coverage for `systemPrompts: [{ content, metadata: { cache_control } }]`
5 * on the Anthropic adapter.
6 *
7 * Wire-shape coverage lives in the unit test
8 * `packages/ai-anthropic/tests/anthropic-adapter.test.ts` —
9 * `it('attaches cache_control to system TextBlockParams via systemPrompts
10 * metadata')` directly inspects the body passed to the Anthropic SDK and
11 * asserts the structured `system: [{ type: 'text', text, cache_control }]`
12 * payload. Replicating that assertion here is impossible: aimock's
13 * journal normalises Anthropic requests into an OpenAI-shaped
14 * `ChatCompletionRequest` for storage and drops unknown fields like
15 * `cache_control` in the process.
16 *
17 * What this spec covers (which the unit test cannot):
18 * - `chatParamsFromRequestBody` accepts the request without rejecting
19 * `forwardedProps.systemPromptCacheControl`.
20 * - The object-form `systemPrompts` shape survives the JSON wire from
21 * test → route → adapter without throwing (in particular,
22 * `normalizeSystemPrompts`' runtime validation accepts the shape).
23 * - The Anthropic SDK accepts the request as built (a malformed
24 * `system` TextBlockParam would be rejected by the SDK or aimock).
25 * - The stream completes with `RUN_FINISHED`, proving the full
26 * middleware → adapter → SDK → server path is unaffected by the
27 * presence of `metadata.cache_control`.
28 */
29test.describe('Anthropic systemPrompts metadata — wire path', () => {
30 test('object-form systemPrompts with metadata.cache_control completes end-to-end on Anthropic', async ({
31 request,
32 testId,
33 aimockPort,
34 }) => {
35 const body = {
36 threadId: 'thread-sysprompt-meta-1',
37 runId: 'run-sysprompt-meta-1',
38 state: {},
39 messages: [
40 { id: 'u1', role: 'user', content: '[chat] recommend a guitar' },
41 ],
42 tools: [],
43 context: [],
44 forwardedProps: {
45 provider: 'anthropic',
46 feature: 'chat',
47 testId,
48 aimockPort,
49 // Opt-in flag handled by `api.chat.ts` — promotes the system
50 // prompt to object-form `{ content, metadata: { cache_control:
51 // { type: 'ephemeral' } } }`. Exercising this flag with the
52 // matching aimock fixture proves the full HTTP path tolerates the
53 // structured shape end-to-end.
54 systemPromptCacheControl: true,
55 },
56 }
57 const response = await request.post('/api/chat', {
58 data: body,
59 headers: { 'Content-Type': 'application/json' },
60 })
61 expect(
62 response.ok(),
63 `expected 200, got ${response.status()}: ${await response.text()}`,
64 ).toBe(true)
65 const text = await response.text()
66 expect(text).toContain('RUN_FINISHED')
67 // No RUN_ERROR — the adapter accepted the structured system prompt
68 // and the SDK accepted the resulting `TextBlockParam` array. (See the
69 // unit test for the actual wire-shape assertion.)
70 expect(text).not.toContain('RUN_ERROR')
71 })
72})