Open History to see the last commit
1import { expect, it, vi } from 'vitest'
2import {
3 OPENAI_GPT51_GUIDE_URL,
4 OPENAI_GPT52_GUIDE_URL,
5 OPENAI_GPT5_GUIDE_URL,
6} from './openai-gpt5-guides.ts'
7import {
8 OPENAI_MODELS_INDEX_URL,
9 OPENAI_MODEL_PAGE,
10 OPENAI_PRICING_URL,
11} from './openai-model-docs.ts'
12import { OPENAI_REALTIME_EFFORT_URL } from './openai-realtime-efforts.ts'
13import { openaiProvider } from './openai.ts'
14
15it('loads the docs through the US egress (#505)', async () => {
16 const original = globalThis.fetch
17 // The listing itself stays on the global fetch; a docs load there fails.
18 globalThis.fetch = ((url: string) =>
19 String(url).includes('developers.openai.com')
20 ? Promise.reject(new Error(`global fetch called for ${String(url)}`))
21 : Promise.resolve(
22 Response.json({
23 data: [{ id: 'gpt-5-mini' }, { id: 'gpt-realtime-2' }],
24 }),
25 )) as typeof fetch
26 const egressed: Array<string> = []
27 const ns = {
28 idFromName: () => 'id',
29 get: () => ({
30 fetch(url: string) {
31 egressed.push(String(url))
32 if (url === OPENAI_MODELS_INDEX_URL)
33 return Promise.resolve(
34 new Response('- [GPT-5 mini](/api/docs/models/gpt-5-mini.md)'),
35 )
36 return Promise.resolve(new Response('# Docs'))
37 },
38 }),
39 }
40 try {
41 const result = await openaiProvider.listModels({
42 OPENAI_API_KEY: 'test-key',
43 US_EGRESS: ns as unknown as DurableObjectNamespace,
44 })
45 for (const failure of result.docsFailures?.first ?? []) {
46 expect(failure.error).not.toMatch(/global fetch called/)
47 }
48 expect(egressed).toEqual(
49 expect.arrayContaining([
50 OPENAI_MODELS_INDEX_URL,
51 OPENAI_PRICING_URL,
52 OPENAI_MODEL_PAGE('gpt-5-mini'),
53 OPENAI_REALTIME_EFFORT_URL,
54 OPENAI_GPT5_GUIDE_URL,
55 OPENAI_GPT51_GUIDE_URL,
56 OPENAI_GPT52_GUIDE_URL,
57 ]),
58 )
59 } finally {
60 globalThis.fetch = original
61 }
62})
63
64/** Listing answers from `responses` in order; docs loads get an empty page. */
65async function listWithListing(responses: Array<() => Response>) {
66 const original = globalThis.fetch
67 const auth: Array<string | null> = []
68 globalThis.fetch = ((url: string, init?: RequestInit) => {
69 if (String(url).includes('developers.openai.com'))
70 return Promise.resolve(new Response('# Docs'))
71 auth.push(new Headers(init?.headers).get('authorization'))
72 const next = responses[auth.length - 1]
73 if (!next) throw new Error('listing called more often than stubbed')
74 return Promise.resolve(next())
75 }) as typeof fetch
76 vi.useFakeTimers()
77 try {
78 const settled = openaiProvider
79 .listModels({ OPENAI_API_KEY: 'test-key' })
80 .then(
81 (result) => ({ result }),
82 (error: unknown) => ({ error }),
83 )
84 await vi.runAllTimersAsync()
85 return { auth, ...(await settled) }
86 } finally {
87 vi.useRealTimers()
88 globalThis.fetch = original
89 }
90}
91
92const gatewayTimeout = () =>
93 new Response('error code: 504', {
94 status: 504,
95 statusText: 'Gateway Timeout',
96 })
97
98it('retries a 504 on the listing (#100004)', async () => {
99 const out = await listWithListing([
100 gatewayTimeout,
101 () => Response.json({ data: [{ id: 'gpt-5-mini' }] }),
102 ])
103 expect('result' in out && out.result.models.map((m) => m.rawId)).toEqual([
104 'gpt-5-mini',
105 ])
106 expect(out.auth).toEqual(['Bearer test-key', 'Bearer test-key'])
107})
108
109it('throws with the URL when every listing attempt is a 504', async () => {
110 const out = await listWithListing(
111 Array.from({ length: 5 }, () => gatewayTimeout),
112 )
113 expect('error' in out && String(out.error)).toMatch(
114 /fetch failed after 5 attempts: https:\/\/api\.openai\.com\/v1\/models → 504 Gateway Timeout/,
115 )
116})