Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { OPENAI_OPENAPI_URL } from '../openai-compat.ts'
4import type { OpenApiDocument } from '../types.ts'
5import { provider } from './sambanova.ts'
6
7const OPENAI_FIXTURE: OpenApiDocument = {
8 openapi: '3.1.0',
9 info: { title: 'OpenAI API', version: '2.3.0' },
10 servers: [{ url: 'https://api.openai.com/v1' }],
11 paths: {
12 '/chat/completions': { post: { operationId: 'createChatCompletion' } },
13 '/embeddings': { post: { operationId: 'createEmbedding' } },
14 '/files': { get: { operationId: 'listFiles' } },
15 },
16}
17
18describe('sambanova provider', () => {
19 it('is the generated SambaNova adapter', () => {
20 expect(provider.id).toBe('sambanova')
21 expect(provider.displayName).toBe('SambaNova')
22 expect(provider.authEnvVar).toBe('SAMBANOVA_API_KEY')
23 expect(provider.defaultDerivation).toBe('generated')
24 expect(provider.specSourceUrl).toBe(OPENAI_OPENAPI_URL)
25 expect(provider.modelsEndpoint).toBe('https://api.sambanova.ai/v1/models')
26 })
27})
28
29describe('sambanova classify', () => {
30 it('maps chat completions and drops platform paths', () => {
31 expect(provider.classify('/chat/completions', {})).toBe('chat')
32 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
33 expect(provider.classify('/v1/models', {})).toBeNull()
34 expect(provider.classify('/files', {})).toBeNull()
35 })
36})
37
38describe('sambanova listModels', () => {
39 it('skips when SAMBANOVA_API_KEY is absent', async () => {
40 const result = await provider.listModels({})
41 expect(result.models).toEqual([])
42 expect(result.skipped).toBe(
43 'sambanova: SAMBANOVA_API_KEY not set — skipped',
44 )
45 })
46})
47
48describe('sambanova fetchSpec', () => {
49 it('keeps chat/completions and rewrites the server to SambaNova', async () => {
50 const original = globalThis.fetch
51 const urls: Array<string> = []
52 globalThis.fetch = ((url: string) => {
53 urls.push(String(url))
54 return Promise.resolve(
55 new Response(JSON.stringify(OPENAI_FIXTURE), {
56 status: 200,
57 headers: { 'content-type': 'application/json' },
58 }),
59 )
60 }) as typeof fetch
61 try {
62 const fetched = await provider.fetchSpec({})
63 expect(urls).toEqual([OPENAI_OPENAPI_URL])
64 expect(fetched.outputStrategy).toBe('post-200')
65 expect(fetched.sources).toHaveLength(1)
66 expect(fetched.sources[0]?.url).toBe(OPENAI_OPENAPI_URL)
67 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
68 const spec = fetched.specs[0]
69 expect(spec?.info?.title).toBe('SambaNova')
70 expect(spec?.servers).toEqual([{ url: 'https://api.sambanova.ai/v1' }])
71 expect(Object.keys(spec?.paths ?? {})).toEqual(['/chat/completions'])
72 } finally {
73 globalThis.fetch = original
74 }
75 })
76})