Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { provider } from './groq.ts'
4
5describe('groq classify', () => {
6 it('maps generation paths and drops platform surfaces', () => {
7 expect(provider.classify('/openai/v1/chat/completions', {})).toBe('chat')
8 expect(provider.classify('/openai/v1/responses', {})).toBe('chat')
9 expect(provider.classify('/openai/v1/audio/speech', {})).toBe('audio')
10 expect(provider.classify('/openai/v1/audio/transcriptions', {})).toBe(
11 'audio',
12 )
13 expect(provider.classify('/openai/v1/audio/translations', {})).toBe('audio')
14 expect(provider.classify('/openai/v1/embeddings', {})).toBe('embeddings')
15 expect(provider.classify('/openai/v1/batches', {})).toBeNull()
16 expect(provider.classify('/openai/v1/files', {})).toBeNull()
17 expect(provider.classify('/openai/v1/reranking', {})).toBeNull()
18 expect(provider.classify('/v1/fine_tunings', {})).toBeNull()
19 expect(provider.classify('/openai/v1/models', {})).toBeNull()
20 })
21})
22
23describe('groq listModels', () => {
24 it('skips with an empty catalog when GROQ_API_KEY is absent', async () => {
25 const result = await provider.listModels({})
26 expect(result.models).toEqual([])
27 expect(result.skipped).toBe('groq: GROQ_API_KEY not set — skipped')
28 })
29})
30
31describe('groq fetchSpec', () => {
32 const spec = {
33 openapi: '3.0.1',
34 info: { title: 'GroqCloud API' },
35 paths: { '/openai/v1/chat/completions': { post: { summary: 'chat' } } },
36 }
37
38 const mockScript = (embedded: string | null): string =>
39 embedded === null
40 ? '#!/usr/bin/env bash'
41 : `#!/usr/bin/env bash\n EMBEDDED_SPEC="${embedded}"`
42
43 async function withScript<T>(
44 script: string,
45 run: () => Promise<T>,
46 ): Promise<T> {
47 const original = globalThis.fetch
48 globalThis.fetch = ((url: string) =>
49 Promise.resolve(
50 String(url) === provider.specSourceUrl
51 ? new Response(script)
52 : new Response('not found', { status: 404 }),
53 )) as typeof fetch
54 try {
55 return await run()
56 } finally {
57 globalThis.fetch = original
58 }
59 }
60
61 async function gzipBase64(text: string): Promise<string> {
62 const bytes = new Uint8Array(
63 await new Response(
64 new Blob([text]).stream().pipeThrough(new CompressionStream('gzip')),
65 ).arrayBuffer(),
66 )
67 return btoa(String.fromCharCode(...bytes))
68 }
69
70 it('decodes the base64+gzip EMBEDDED_SPEC from scripts/mock', async () => {
71 const script = mockScript(await gzipBase64(JSON.stringify(spec)))
72 const fetched = await withScript(script, () => provider.fetchSpec({}))
73 expect(fetched.outputStrategy).toBe('post-200')
74 expect(fetched.specs[0]?.info?.title).toBe('GroqCloud API')
75 expect(fetched.sources[0]?.url).toBe(provider.specSourceUrl)
76 expect(fetched.specRevision).toMatch(/^[0-9a-f]{64}$/)
77 expect(fetched.specRevision).toBe(fetched.sources[0]?.hash)
78 })
79
80 it('throws when EMBEDDED_SPEC is missing', async () => {
81 await expect(
82 withScript(mockScript(null), () => provider.fetchSpec({})),
83 ).rejects.toThrow(/groq: no EMBEDDED_SPEC/)
84 })
85
86 it('throws when EMBEDDED_SPEC is not gzip', async () => {
87 await expect(
88 withScript(mockScript(btoa('plain text')), () => provider.fetchSpec({})),
89 ).rejects.toThrow(/not gzipped JSON/)
90 })
91})