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

openai-aliases.test.ts

91 lines2.6 KB

1import { readFileSync } from 'node:fs'
2​
3import { requestUrl } from '#/lib/text.ts'
4import { afterEach, expect, it } from 'vitest'
5​
6import {
7 OPENAI_MODELS_INDEX_URL,
8 OPENAI_MODEL_PAGE,
9 openaiModelFacts,
10} from './openai-model-docs.ts'
11import { docsRun } from './model-facts.ts'
12​
13// Trimmed https://developers.openai.com/api/docs/models/gpt-4o.md, 2026-11-21.
14const GPT_4O = readFileSync(
15 new URL('./fixtures/openai-gpt-4o.md.txt', import.meta.url),
16 'utf8',
18​
19const originalFetch = globalThis.fetch
20afterEach(() => {
21 globalThis.fetch = originalFetch
22})
23​
24function serve(page: string | null) {
25 globalThis.fetch = (input) => {
26 const url = requestUrl(input)
27 if (url === OPENAI_MODELS_INDEX_URL) {
28 return Promise.resolve(
29 new Response('[GPT-4o](/api/docs/models/gpt-4o.md)'),
30 )
31 }
32 if (url === OPENAI_MODEL_PAGE('gpt-4o') && page !== null) {
33 return Promise.resolve(new Response(page))
34 }
35 return Promise.resolve(new Response('outage', { status: 503 }))
36 }
38​
39const listed = [
40 'gpt-4o',
41 'gpt-4o-2024-08-06',
42 'gpt-4o-2024-11-20',
43 'gpt-5-search-api-2025-10-14',
45​
46it('links the alias to its default snapshot and says which snapshots have none (#709)', async () => {
47 serve(GPT_4O)
48 const facts = await openaiModelFacts(listed)
49 const url = OPENAI_MODEL_PAGE('gpt-4o')
50​
51 const alias = facts('gpt-4o')
52 expect(alias.aliasOf).toBe('gpt-4o-2024-08-06')
53 expect(alias.factSources?.aliasOf).toMatchObject({
54 derivation: 'docs-derived',
55 sourceUrl: url,
56 path: 'Default snapshot',
57 })
58​
59 const snapshot = facts('gpt-4o-2024-08-06')
60 expect(snapshot.aliases).toEqual(['gpt-4o'])
61 expect(snapshot.aliasOf).toBeUndefined()
62 expect(snapshot.factSources?.aliases?.path).toBe('Default snapshot')
63​
64 // Listed on the page's snapshot list, and no alias resolves to it.
65 const older = facts('gpt-4o-2024-11-20')
66 expect(older.aliases).toBeUndefined()
67 expect(older.factSources?.aliases).toMatchObject({
68 sourceUrl: url,
69 path: 'Snapshots',
70 })
71​
72 // No page names it: the docs are silent, and say so.
73 const unpaged = facts('gpt-5-search-api-2025-10-14')
74 expect(unpaged.aliases).toBeUndefined()
75 expect(unpaged.factSources?.aliases).toEqual({
76 derivation: 'docs-derived',
77 sourceUrl: OPENAI_MODELS_INDEX_URL,
78 path: 'unstated',
79 })
80})
81​
82it('keeps the stored links when the page fails', async () => {
83 serve(null)
84 const facts = await openaiModelFacts(listed, undefined, docsRun())
85 for (const id of ['gpt-4o', 'gpt-4o-2024-08-06']) {
86 expect(facts(id).absent).toMatchObject({
87 aliases: 'unavailable',
88 aliasOf: 'unavailable',
89 })
90 }
91})