Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import chatSpec from '../fixtures/jina-chat-openapi.json'
4import { provider } from './jina.ts'
5
6describe('jina provider', () => {
7 it('exports the jina adapter with seed metadata', () => {
8 expect(provider.id).toBe('jina')
9 expect(provider.displayName).toBe('Jina AI')
10 expect(provider.authEnvVar).toBe('JINA_API_KEY')
11 expect(provider.specSourceUrl).toBe('https://api.jina.ai/openapi.json')
12 expect(provider.modelsEndpoint).toBe('https://api.jina.ai/v1/models')
13 expect(provider.defaultDerivation).toBe('upstream-spec')
14 })
15})
16
17describe('jina classify', () => {
18 it('maps embeddings and chat, and drops rerank/classifier/platform', () => {
19 expect(provider.classify('/v1/embeddings', {})).toBe('embeddings')
20 expect(provider.classify('/embeddings', {})).toBe('embeddings')
21 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
22 expect(provider.classify('/v1/rerank', {})).toBeNull()
23 expect(provider.classify('/v1/classify', {})).toBeNull()
24 expect(provider.classify('/v1/train', {})).toBeNull()
25 expect(provider.classify('/v1/reader', {})).toBeNull()
26 expect(provider.classify('/v1/models', {})).toBeNull()
27 })
28})
29
30describe('jina listModels', () => {
31 it('skips when JINA_API_KEY is absent', async () => {
32 const original = globalThis.fetch
33 globalThis.fetch = () => {
34 throw new Error('fetch')
35 }
36 try {
37 const result = await provider.listModels({})
38 expect(result.skipped).toBe('jina: JINA_API_KEY not set — skipped')
39 expect(result.models).toEqual([])
40 } finally {
41 globalThis.fetch = original
42 }
43 })
44
45 it('classifies chat from the OpenAPI model const and maps that request', async () => {
46 const original = globalThis.fetch
47 globalThis.fetch = ((url: string) => {
48 const body =
49 String(url) === 'https://api.jina.ai/openapi.json'
50 ? chatSpec
51 : {
52 data: [
53 {
54 id: 'jina-ai/jina-ocr-v1',
55 output_modalities: ['text'],
56 input_modalities: ['text', 'image'],
57 context_length: 32768,
58 max_output_length: 8192,
59 pricing: { prompt: '0.0000005', completion: '0.000002' },
60 },
61 {
62 id: 'jina-ai/ReaderLM-v2',
63 output_modalities: ['text'],
64 input_modalities: ['text'],
65 context_length: 524288,
66 max_output_length: 0,
67 pricing: { prompt: '0.00000005', completion: '0' },
68 },
69 {
70 id: 'jina-embeddings-v3',
71 output_modalities: ['embeddings'],
72 pricing: { prompt: '0.00000005', completion: '0' },
73 },
74 {
75 id: 'jina-reranker-v3',
76 output_modalities: ['text'],
77 pricing: { prompt: '0', completion: '0' },
78 },
79 ],
80 }
81 return Promise.resolve(
82 new Response(JSON.stringify(body), { status: 200 }),
83 )
84 }) as typeof fetch
85 try {
86 const { models } = await provider.listModels({ JINA_API_KEY: 'test' })
87 const ocr = models.find((model) => model.rawId.endsWith('jina-ocr-v1'))
88 const reader = models.find((model) => model.rawId.endsWith('ReaderLM-v2'))
89 const embed = models.find((model) => model.rawId.includes('embeddings'))
90 const rerank = models.find((model) => model.rawId.includes('reranker'))
91 expect(ocr?.activity).toBe('chat')
92 expect(ocr?.maxOutput).toBe(8192)
93 expect(ocr?.requestMap).toMatchObject({
94 maxTokensField: 'max_completion_tokens',
95 developerRole: true,
96 reasoningEffort: null,
97 })
98 expect(reader?.activity ?? null).toBeNull()
99 expect(reader?.requestMap).toBeUndefined()
100 expect(reader?.maxOutput).toBeUndefined()
101 expect(embed?.activity).toBe('embeddings')
102 expect(rerank?.activity ?? null).toBeNull()
103 expect(rerank?.requestMap).toBeUndefined()
104 } finally {
105 globalThis.fetch = original
106 }
107 })
108
109 it('throws when the chat schema names no model and does not list models', async () => {
110 const original = globalThis.fetch
111 const urls: Array<string> = []
112 globalThis.fetch = ((url: string) => {
113 urls.push(String(url))
114 return Promise.resolve(
115 new Response(
116 JSON.stringify({ openapi: '3.1.0', components: { schemas: {} } }),
117 { status: 200 },
118 ),
119 )
120 }) as typeof fetch
121 try {
122 await expect(
123 provider.listModels({ JINA_API_KEY: 'test' }),
124 ).rejects.toThrow(/ChatCompletionRequest missing/)
125 expect(urls).toEqual(['https://api.jina.ai/openapi.json'])
126 } finally {
127 globalThis.fetch = original
128 }
129 })
130})
131
132describe('jina fetchSpec', () => {
133 it('parses the public OpenAPI document without hitting the network', async () => {
134 const original = globalThis.fetch
135 const spec = {
136 openapi: '3.1.0',
137 info: { title: 'Jina Search Foundation API', version: '1.0.0' },
138 paths: {
139 '/v1/embeddings': { post: { operationId: 'createEmbedding' } },
140 },
141 }
142 const urls: Array<string> = []
143 globalThis.fetch = ((url: string) => {
144 urls.push(String(url))
145 return Promise.resolve(
146 new Response(JSON.stringify(spec), { status: 200 }),
147 )
148 }) as typeof fetch
149 try {
150 const fetched = await provider.fetchSpec({})
151 expect(urls).toEqual(['https://api.jina.ai/openapi.json'])
152 expect(fetched.outputStrategy).toBe('post-200')
153 expect(fetched.specs).toHaveLength(1)
154 expect(fetched.specs[0]?.paths?.['/v1/embeddings']).toBeDefined()
155 expect(fetched.sources[0]?.url).toBe('https://api.jina.ai/openapi.json')
156 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
157 } finally {
158 globalThis.fetch = original
159 }
160 })
161})