Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { classifyAndBundle } from '#/server/ingest/sync.ts'
4
5import { provider } from './cerebras.ts'
6
7const CEREBRAS_OPENAPI_URL =
8 'https://inference-docs.cerebras.ai/api-reference/openapi.yaml'
9
10const SPEC_FIXTURE = JSON.stringify({
11 openapi: '3.1.0',
12 info: { title: 'Cerebras Inference API', version: '1.0.0' },
13 paths: {
14 '/v1/chat/completions': {
15 post: {
16 summary: 'Create chat completion',
17 requestBody: {
18 content: {
19 'application/json': {
20 schema: {
21 type: 'object',
22 required: ['model', 'messages'],
23 properties: {
24 model: { type: 'string' },
25 messages: { type: 'array' },
26 },
27 },
28 },
29 },
30 },
31 responses: {
32 '200': {
33 content: {
34 'application/json': {
35 schema: {
36 type: 'object',
37 properties: { id: { type: 'string' } },
38 },
39 },
40 },
41 },
42 },
43 },
44 },
45 '/v1/models': { get: { summary: 'List models' } },
46 '/v1/files': { post: { summary: 'Upload file' } },
47 '/v1/batches': { post: { summary: 'Create batch' } },
48 },
49})
50
51async function withMockedSpec<T>(run: () => Promise<T>): Promise<T> {
52 const original = globalThis.fetch
53 globalThis.fetch = ((url: string) => {
54 if (String(url) === CEREBRAS_OPENAPI_URL) {
55 return Promise.resolve(new Response(SPEC_FIXTURE))
56 }
57 return Promise.reject(new Error(`unexpected fetch: ${String(url)}`))
58 }) as typeof fetch
59 try {
60 return await run()
61 } finally {
62 globalThis.fetch = original
63 }
64}
65
66describe('cerebras classify', () => {
67 it('maps chat completions (and legacy completions) to chat', () => {
68 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
69 expect(provider.classify('/chat/completions', {})).toBe('chat')
70 expect(provider.classify('/v1/completions', {})).toBe('chat')
71 })
72
73 it('drops models, files, batches, and dedicated-endpoint admin', () => {
74 expect(provider.classify('/v1/models', {})).toBeNull()
75 expect(provider.classify('/v1/files', {})).toBeNull()
76 expect(provider.classify('/v1/batches', {})).toBeNull()
77 expect(provider.classify('/v1/customer/endpoints', {})).toBeNull()
78 })
79})
80
81describe('cerebras listModels', () => {
82 it('skips when CEREBRAS_API_KEY is absent', async () => {
83 const result = await provider.listModels({})
84 expect(result.models).toEqual([])
85 expect(result.skipped).toBe('cerebras: CEREBRAS_API_KEY not set — skipped')
86 })
87})
88
89describe('cerebras fetchSpec', () => {
90 it('fetches the published spec and bundles chat completions only', async () => {
91 const fetched = await withMockedSpec(() => provider.fetchSpec({}))
92 expect(fetched.specs).toHaveLength(1)
93 expect(fetched.sources[0]?.url).toBe(CEREBRAS_OPENAPI_URL)
94 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
95 expect(fetched.outputStrategy).toBe('post-200')
96
97 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
98 expect(warnings).toEqual([])
99 expect(endpoints.map((e) => [e.dbId, e.activity])).toEqual([
100 ['cerebras/v1/chat/completions', 'chat'],
101 ])
102 })
103})