Open History to see the last commit
1import { beforeAll, describe, expect, it } from 'vitest'
2import { env } from 'cloudflare:test'
3import { eq, like } from 'drizzle-orm'
4
5import { GPT_4O } from '../../packages/rate-card/src/fixtures/gpt-4o.ts'
6import { getDb } from '../db/index.ts'
7import type { Db } from '../db/index.ts'
8import {
9 changes,
10 endpoints,
11 models,
12 providerModelNamespaces,
13 providers,
14 schemaVersions,
15} from '../db/schema.ts'
16import { exportCatalogStream, stableJson } from './export.ts'
17
18const NOW = 1_781_150_000
19// Key order is the provider's, not sorted: the export keeps it.
20const INPUT_TEXT = JSON.stringify({
21 type: 'object',
22 properties: { prompt: { type: 'string' }, model: { type: 'string' } },
23})
24const OUTPUT_TEXT = JSON.stringify({ type: 'object' })
25
26let db: Db
27
28beforeAll(async () => {
29 db = getDb(env)
30 await db.insert(providers).values([
31 {
32 id: 'exp-beta',
33 displayName: 'Export Beta',
34 specSourceUrl: 'https://example.com/b.json',
35 lastPolledAt: NOW,
36 lastSyncedAt: NOW,
37 },
38 {
39 id: 'exp-alpha',
40 displayName: 'Export Alpha',
41 specSourceUrl: 'https://example.com/a.json',
42 lastPolledAt: NOW,
43 },
44 // Sorts first with no rows at all; `exp-also` sits between alpha and
45 // beta with a model but no endpoint. Both test the comma joins.
46 {
47 id: 'exp-0none',
48 displayName: 'Export None',
49 specSourceUrl: 'https://example.com/0.json',
50 },
51 {
52 id: 'exp-also',
53 displayName: 'Export Also',
54 specSourceUrl: 'https://example.com/s.json',
55 },
56 ])
57 await db
58 .insert(providerModelNamespaces)
59 .values({ namespace: 'alpha', providerId: 'exp-alpha' })
60 await db.insert(models).values([
61 {
62 id: 'exp-alpha-chat',
63 providerId: 'exp-alpha',
64 rawId: 'chat-1',
65 activity: 'chat',
66 capabilities: { vision: true, tools: false },
67 pricing: GPT_4O,
68 firstSeenAt: NOW - 50,
69 lastSeenAt: NOW,
70 },
71 {
72 id: 'exp-also-chat',
73 providerId: 'exp-also',
74 rawId: 'chat',
75 activity: 'chat',
76 firstSeenAt: NOW - 50,
77 lastSeenAt: NOW,
78 },
79 {
80 id: 'exp-beta-old',
81 providerId: 'exp-beta',
82 rawId: 'old',
83 activity: 'chat',
84 firstSeenAt: NOW - 50,
85 lastSeenAt: NOW - 10,
86 deprecatedAt: NOW - 10,
87 },
88 ])
89 await db.insert(endpoints).values([
90 {
91 id: 'exp-alpha/v1/images',
92 providerId: 'exp-alpha',
93 activity: 'image',
94 method: 'POST',
95 path: '/v1/images',
96 },
97 {
98 id: 'exp-beta/v1/empty',
99 providerId: 'exp-beta',
100 activity: 'chat',
101 method: 'POST',
102 path: '/v1/empty',
103 },
104 ])
105 await db.insert(schemaVersions).values([
106 {
107 id: 'exp-alpha/v1/images:input:current',
108 endpointId: 'exp-alpha/v1/images',
109 kind: 'input',
110 contentHash: 'c'.repeat(64),
111 schema: INPUT_TEXT,
112 specRevision: 'rev-7',
113 sourceUrl: 'https://example.com/a.json',
114 sourceHash: 'e'.repeat(64),
115 extractorVersion: 'x1',
116 derivation: 'upstream-spec',
117 verifiedAt: '2026-10-01',
118 createdAt: NOW - 20,
119 },
120 {
121 id: 'exp-alpha/v1/images:input:old',
122 endpointId: 'exp-alpha/v1/images',
123 kind: 'input',
124 contentHash: 'b'.repeat(64),
125 schema: '{"type":"null"}',
126 createdAt: NOW - 40,
127 supersededAt: NOW - 20,
128 },
129 {
130 id: 'exp-alpha/v1/images:output:current',
131 endpointId: 'exp-alpha/v1/images',
132 kind: 'output',
133 contentHash: 'd'.repeat(64),
134 schema: OUTPUT_TEXT,
135 createdAt: NOW - 20,
136 },
137 ])
138 await db.insert(changes).values([
139 {
140 id: 'chg-a',
141 type: 'model.added',
142 providerId: 'exp-alpha',
143 subjectId: 'exp-alpha-chat',
144 summary: 'added',
145 createdAt: NOW - 50,
146 },
147 {
148 id: 'chg-b',
149 type: 'model.removed',
150 providerId: 'exp-beta',
151 subjectId: 'exp-beta-old',
152 summary: 'removed',
153 createdAt: NOW - 10,
154 },
155 ])
156})
157
158const exportText = () => new Response(exportCatalogStream(db)).text()
159
160interface ExportBody {
161 latestChange: unknown
162 models: Array<Record<string, unknown>>
163 providers: Array<Record<string, unknown>>
164 schemas: Array<Record<string, unknown>>
165}
166
167describe('exportCatalogStream', () => {
168 it('serves the whole catalog with the newest change', async () => {
169 const body = JSON.parse(await exportText()) as ExportBody
170 expect(Object.keys(body)).toEqual([
171 'latestChange',
172 'models',
173 'providers',
174 'schemas',
175 ])
176 expect(body.latestChange).toEqual({ createdAt: NOW - 10, id: 'chg-b' })
177 expect(body.models.map((m) => m.id)).toEqual([
178 'exp-alpha-chat',
179 'exp-also-chat',
180 'exp-beta-old',
181 ])
182 expect(body.models[0]?.pricing).toEqual(GPT_4O)
183 expect(body.models[2]?.deprecatedAt).toBe(NOW - 10)
184 expect(body.providers.map((p) => p.id)).toEqual([
185 'exp-0none',
186 'exp-alpha',
187 'exp-also',
188 'exp-beta',
189 ])
190 expect(
191 body.providers.filter((p) => p.id === 'exp-alpha' || p.id === 'exp-beta'),
192 ).toEqual([
193 {
194 authEnvVar: null,
195 displayName: 'Export Alpha',
196 id: 'exp-alpha',
197 modelNamespaces: ['alpha'],
198 modelsEndpoint: null,
199 specSourceUrl: 'https://example.com/a.json',
200 status: 'active',
201 },
202 {
203 authEnvVar: null,
204 displayName: 'Export Beta',
205 id: 'exp-beta',
206 modelNamespaces: [],
207 modelsEndpoint: null,
208 specSourceUrl: 'https://example.com/b.json',
209 status: 'active',
210 },
211 ])
212 })
213
214 it('leaves out fields that change on every poll and sorts keys', async () => {
215 const text = await exportText()
216 for (const field of ['lastSeenAt', 'lastPolledAt', 'lastSyncedAt']) {
217 expect(text).not.toContain(`"${field}"`)
218 }
219 const body = JSON.parse(text) as ExportBody
220 const model = body.models[0] ?? {}
221 expect(model._links).toBeUndefined()
222 expect(model.firstSeenAt).toBe(NOW - 50)
223 expect(Object.keys(model)).toEqual(Object.keys(model).sort())
224 expect(model.capabilities).toEqual({ tools: false, vision: true })
225 expect(text).toContain('"capabilities":{"tools":false,"vision":true}')
226 })
227
228 it('gives the same bytes after a poll that changed nothing else', async () => {
229 const before = await exportText()
230 await db
231 .update(models)
232 .set({ lastSeenAt: NOW + 900 })
233 .where(like(models.providerId, 'exp-%'))
234 await db
235 .update(providers)
236 .set({ lastPolledAt: NOW + 900, lastSyncedAt: NOW + 900 })
237 .where(like(providers.id, 'exp-%'))
238 expect(await exportText()).toBe(before)
239 })
240
241 it('streams one chunk per provider section, not one buffer', async () => {
242 const reader = exportCatalogStream(db).getReader()
243 let chunks = 0
244 while (!(await reader.read()).done) chunks++
245 // Opening, 3 model providers, the providers block, 2 schema providers,
246 // close. Empty providers send nothing.
247 expect(chunks).toBe(8)
248 })
249
250 it('serves current schema versions with their stored text', async () => {
251 const text = await exportText()
252 const body = JSON.parse(text) as ExportBody
253 expect(body.schemas.map((s) => s.endpointId)).toEqual([
254 'v1/images',
255 'v1/empty',
256 ])
257 const [images, empty] = body.schemas
258 expect(images).toMatchObject({
259 provider: 'exp-alpha',
260 activity: 'image',
261 method: 'POST',
262 path: '/v1/images',
263 input: {
264 contentHash: 'c'.repeat(64),
265 specRevision: 'rev-7',
266 provenance: {
267 sourceUrl: 'https://example.com/a.json',
268 sourceHash: 'e'.repeat(64),
269 fetchedAt: NOW - 20,
270 extractorVersion: 'x1',
271 derivation: 'upstream-spec',
272 verifiedAt: '2026-10-01',
273 },
274 },
275 output: { contentHash: 'd'.repeat(64) },
276 })
277 expect(text).toContain(`"schema":${INPUT_TEXT}`)
278 expect(text).not.toContain('"type":"null"')
279 expect(empty).toMatchObject({ input: null, output: null })
280 })
281})
282
283describe('exportCatalogStream failures', () => {
284 /** Reads until the stream errors; returns what arrived and the error. */
285 async function readUntilError() {
286 const reader = exportCatalogStream(db).getReader()
287 const decoder = new TextDecoder()
288 let text = ''
289 try {
290 for (;;) {
291 const next = await reader.read()
292 if (next.done) return { text, error: null }
293 text += decoder.decode(next.value)
294 }
295 } catch (error) {
296 return { text, error: error as Error }
297 }
298 }
299
300 async function withVersion(
301 row: typeof schemaVersions.$inferInsert,
302 run: () => Promise<void>,
303 ) {
304 await db.insert(schemaVersions).values(row)
305 try {
306 await run()
307 } finally {
308 await db.delete(schemaVersions).where(eq(schemaVersions.id, row.id))
309 }
310 }
311
312 it('errors part way on two current versions, leaving invalid JSON', async () => {
313 await withVersion(
314 {
315 id: 'exp-alpha/v1/images:output:dup',
316 endpointId: 'exp-alpha/v1/images',
317 kind: 'output',
318 contentHash: 'f'.repeat(64),
319 schema: '{}',
320 createdAt: NOW - 5,
321 },
322 async () => {
323 const { text, error } = await readUntilError()
324 expect(error?.message).toMatch(
325 /schemas of exp-alpha: .*more than one current output version/,
326 )
327 // Models went out before the failure; the body still does not parse.
328 expect(text).toContain('"exp-alpha-chat"')
329 expect(() => JSON.parse(text) as unknown).toThrow()
330 },
331 )
332 })
333
334 it('errors on stored schema text that is not a JSON object', async () => {
335 await withVersion(
336 {
337 id: 'exp-beta/v1/empty:input:bad',
338 endpointId: 'exp-beta/v1/empty',
339 kind: 'input',
340 contentHash: '0'.repeat(64),
341 schema: '',
342 createdAt: NOW - 5,
343 },
344 async () => {
345 const { error } = await readUntilError()
346 expect(error?.message).toMatch(
347 /exp-beta\/v1\/empty:input:bad is not a JSON object/,
348 )
349 },
350 )
351 })
352})
353
354describe('stableJson', () => {
355 it('sorts nested keys, keeps array order, and drops undefined', () => {
356 expect(stableJson({ b: [3, { y: 1, x: undefined }], a: null })).toBe(
357 '{"a":null,"b":[3,{"y":1}]}',
358 )
359 })
360
361 it('refuses a value JSON cannot hold instead of writing `undefined`', () => {
362 expect(() => stableJson({ a: () => 1 })).toThrow(/function is not JSON/)
363 })
364})