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

retry-after.spec.ts

54 lines1.6 KB

1import { test, expect } from './fixtures'
2​
3/**
4 * aimock answers `[retry-after]` with a 429 and `Retry-After: 7`. The adapter
5 * reads the header, and the `RUN_ERROR` that `chat()` yields carries the wait
6 * as `metadata.tanstack.retryAfterMs`.
7 */
8for (const api of [
9 'anthropic',
10 'openai-responses',
11 'openai-chat-completions',
12] as const) {
13 test.describe(`${api} — 429 with retry-after`, () => {
14 test('RUN_ERROR carries retryAfterMs', async ({ request }) => {
15 const res = await request.post(`/api/retry-after?api=${api}`)
16 expect(res.ok()).toBe(true)
17​
18 const { ok, runError, error } = (await res.json()) as {
19 ok: boolean
20 error?: string
21 runError?: { message?: string; retryAfterMs?: number }
22 }
23​
24 expect(error ?? null).toBeNull()
25 expect(ok).toBe(true)
26 expect(runError?.retryAfterMs).toBe(7000)
27 })
28 })
30​
31/**
32 * `chat({ retry })`: aimock answers `[chat-retry]` with a 429 and
33 * `Retry-After: 1` first, then with text. `chat()` waits and calls the model
34 * again, so the stream has the text and no `RUN_ERROR`.
35 */
36test('chat({ retry }) calls the model again after a 429', async ({
37 request,
38 testId,
39}) => {
40 const res = await request.post(
41 `/api/retry-after?mode=retry&testId=${encodeURIComponent(testId)}`,
42 )
43 expect(res.ok()).toBe(true)
44​
45 const { types, text } = (await res.json()) as {
46 types: Array<string>
47 text: string
48 }
49​
50 expect(types).not.toContain('RUN_ERROR')
51 expect(types.filter((t) => t === 'RUN_STARTED')).toHaveLength(1)
52 expect(types).toContain('RUN_FINISHED')
53 expect(text).toBe('hello after the wait')
54})