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

prompt-cache-wire.spec.ts

51 lines1.9 KB

1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3​
4// The route runs each chat turn with this threadId. It is the default cache key.
5const THREAD_ID = 'thread-prompt-cache-wire'
6const EPHEMERAL = { type: 'ephemeral' }
7​
8interface WireResult {
9 error?: string
10 anthropic: { default: unknown; none: unknown }
11 openai: { default: unknown; none: unknown }
13​
14/** Gets the request bodies that `/api/prompt-cache-wire` captured. */
15async function captureWire(request: APIRequestContext) {
16 const res = await request.post('/api/prompt-cache-wire')
17 expect(res.ok()).toBe(true)
18 const result: WireResult = await res.json()
19 expect(result.error ?? null).toBeNull()
20 return result
22​
23test.describe('prompt cache: request wire', () => {
24 test('the default promptCache adds the cache fields', async ({ request }) => {
25 const { anthropic, openai } = await captureWire(request)
26​
27 expect(anthropic.default).toMatchObject({
28 system: [{ type: 'text', cache_control: EPHEMERAL }],
29 tools: [{ name: 'get_guitars', cache_control: EPHEMERAL }],
30 messages: [
31 {
32 role: 'user',
33 content: [{ type: 'text', cache_control: EPHEMERAL }],
34 },
35 ],
36 })
37 expect(openai.default).toMatchObject({ prompt_cache_key: THREAD_ID })
38 })
39​
40 test("promptCache 'none' sends no cache field", async ({ request }) => {
41 const { anthropic, openai } = await captureWire(request)
42​
43 // Both requests reached the fetch, with the tool on them.
44 expect(anthropic.none).toMatchObject({ tools: [{ name: 'get_guitars' }] })
45 expect(openai.none).toMatchObject({ tools: [{ name: 'get_guitars' }] })
46 // Custom tools always send `cache_control: null`, so look for a set
47 // marker only.
48 expect(JSON.stringify(anthropic.none)).not.toContain('"cache_control":{')
49 expect(JSON.stringify(openai.none)).not.toContain('prompt_cache')
50 })
51})