Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { classifyOpenAiCompat, filterOpenAiSpec } from './openai-compat.ts'
4import type { OpenApiDocument } from './types.ts'
5
6const spec: OpenApiDocument = {
7 info: { title: 'OpenAI API', version: '2.3.0' },
8 servers: [{ url: 'https://api.openai.com/v1' }],
9 paths: {
10 '/chat/completions': { post: { operationId: 'createChat' } },
11 '/embeddings': { post: { operationId: 'createEmbedding' } },
12 '/files': { get: { operationId: 'listFiles' } },
13 },
14}
15
16describe('filterOpenAiSpec', () => {
17 it('keeps only requested paths and rewrites title + servers', () => {
18 const filtered = filterOpenAiSpec(spec, {
19 title: 'Groq',
20 serverUrl: 'https://api.groq.com/openai/v1',
21 include: ['/chat/completions', '/embeddings'],
22 })
23 expect(filtered.info?.title).toBe('Groq')
24 expect(filtered.servers).toEqual([
25 { url: 'https://api.groq.com/openai/v1' },
26 ])
27 expect(Object.keys(filtered.paths ?? {})).toEqual([
28 '/chat/completions',
29 '/embeddings',
30 ])
31 expect(filtered.paths?.['/files']).toBeUndefined()
32 })
33
34 it('silently drops include entries the upstream spec lacks', () => {
35 const filtered = filterOpenAiSpec(spec, {
36 title: 'Thin',
37 serverUrl: 'https://example.test/v1',
38 include: ['/chat/completions', '/videos'],
39 })
40 expect(Object.keys(filtered.paths ?? {})).toEqual(['/chat/completions'])
41 })
42})
43
44describe('classifyOpenAiCompat', () => {
45 it('maps generation paths, with or without a /v1 prefix', () => {
46 expect(classifyOpenAiCompat('/chat/completions')).toBe('chat')
47 expect(classifyOpenAiCompat('/v1/chat/completions')).toBe('chat')
48 expect(classifyOpenAiCompat('/embeddings')).toBe('embeddings')
49 expect(classifyOpenAiCompat('/audio/speech')).toBe('audio')
50 expect(classifyOpenAiCompat('/images/generations')).toBe('image')
51 expect(classifyOpenAiCompat('/moderations')).toBe('moderation')
52 expect(classifyOpenAiCompat('/files')).toBeNull()
53 expect(classifyOpenAiCompat('/v1/models')).toBeNull()
54 })
55})