Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

voyage.test.ts

213 lines6.0 KB

1import { describe, expect, it } from 'vitest'
2​
3import { classifyAndBundle } from '#/server/ingest/sync.ts'
4​
5import { provider, voyageModelsFromSpec } from './voyage.ts'
6import { VOYAGE_PRICING_URL } from '../voyage-pricing.ts'
7​
8const SPEC_URL =
9 'https://raw.githubusercontent.com/voyage-ai/openapi/main/voyage-openapi.yml'
10​
11const FIXTURE_SPEC = `openapi: 3.0.2
12info:
13 title: Voyage API
14 version: "1.1"
15paths:
16 /embeddings:
17 post:
18 summary: Text embedding models
19 requestBody:
20 content:
21 application/json:
22 schema:
23 type: object
24 required: [input, model]
25 properties:
26 input: { type: string }
27 model: { type: string }
28 responses:
29 "200":
30 description: Success
31 content:
32 application/json:
33 schema:
34 type: object
35 /multimodalembeddings:
36 post:
37 summary: Multimodal embedding models
38 requestBody:
39 content:
40 application/json:
41 schema:
42 type: object
43 required: [inputs, model]
44 properties:
45 inputs: { type: array }
46 model: { type: string }
47 responses:
48 "200":
49 description: Success
50 content:
51 application/json:
52 schema:
53 type: object
54 /rerank:
55 post:
56 summary: Rerankers
57 requestBody:
58 content:
59 application/json:
60 schema:
61 type: object
62 responses:
63 "200":
64 description: Success
65 content:
66 application/json:
67 schema:
68 type: object
70​
71function withStubbedFetch<T>(
72 handler: (url: string, init?: RequestInit) => Response,
73 run: () => Promise<T>,
74): Promise<T> {
75 const original = globalThis.fetch
76 globalThis.fetch = ((url: string, init?: RequestInit) =>
77 Promise.resolve(handler(String(url), init))) as typeof fetch
78 return run().finally(() => {
79 globalThis.fetch = original
80 })
82​
83describe('voyage adapter', () => {
84 it('exports seed metadata for auto-registration', () => {
85 expect(provider.id).toBe('voyage')
86 expect(provider.displayName).toBe('Voyage AI')
87 expect(provider.authEnvVar).toBe('VOYAGE_API_KEY')
88 expect(provider.defaultDerivation).toBe('upstream-spec')
89 expect(provider.specSourceUrl).toBe(SPEC_URL)
90 expect(provider.modelsEndpoint).toBe(SPEC_URL)
91 })
92})
93​
94describe('voyage classify', () => {
95 it('maps embedding endpoints and drops rerank/platform', () => {
96 expect(provider.classify('/embeddings', {})).toBe('embeddings')
97 expect(provider.classify('/multimodalembeddings', {})).toBe('embeddings')
98 expect(provider.classify('/contextualizedembeddings', {})).toBe(
99 'embeddings',
100 )
101 expect(provider.classify('/v1/embeddings', {})).toBe('embeddings')
102 expect(provider.classify('/rerank', {})).toBeNull()
103 expect(provider.classify('/v1/rerank', {})).toBeNull()
104 expect(provider.classify('/models', {})).toBeNull()
105 expect(provider.classify('/batches', {})).toBeNull()
106 })
107})
108​
109describe('voyageModelsFromSpec', () => {
110 it('collects documented voyage-* ids and drops rerankers', () => {
111 const models = voyageModelsFromSpec({
112 paths: {
113 '/embeddings': {
114 post: {
115 requestBody: {
116 content: {
117 'application/json': {
118 schema: {
119 properties: {
120 model: {
121 type: 'string',
122 description:
123 'Recommended: `voyage-3-large`, `voyage-3`. Also `rerank-2`.',
124 },
125 },
126 },
127 },
128 },
129 },
130 },
131 },
132 },
133 })
134 expect(models).toEqual([
135 {
136 rawId: 'voyage-3',
137 activity: 'embeddings',
138 modalities: { input: ['text'], output: [] },
139 },
140 {
141 rawId: 'voyage-3-large',
142 activity: 'embeddings',
143 modalities: { input: ['text'], output: [] },
144 },
145 ])
146 })
147})
148​
149describe('voyage listModels', () => {
150 it('reads the catalog from the published spec, not /v1/models', async () => {
151 const urls: Array<string> = []
152 const result = await withStubbedFetch(
153 (url) => {
154 urls.push(url)
155 if (url === VOYAGE_PRICING_URL) {
156 return new Response(
157 '| Model | Price per million tokens |\n| --- | --- |\n| `voyage-4` | $0.06 |\n',
158 )
159 }
160 return new Response(`openapi: 3.0.2
161info: { title: Voyage API, version: "1.1" }
162paths:
163 /embeddings:
164 post:
165 requestBody:
166 content:
167 application/json:
168 schema:
169 properties:
170 model:
171 type: string
172 description: Recommended \`voyage-3-large\`.
173`)
174 },
175 () => provider.listModels({}),
176 )
177​
178 expect(urls).toEqual([SPEC_URL, VOYAGE_PRICING_URL])
179 expect(result.skipped).toBeUndefined()
180 expect(result.models).toEqual([
181 {
182 rawId: 'voyage-3-large',
183 activity: 'embeddings',
184 modalities: { input: ['text'], output: [] },
185 },
186 ])
187 })
188})
189​
190describe('voyage fetchSpec', () => {
191 it('fetches the published yaml and classifies generation posts', async () => {
192 const fetched = await withStubbedFetch(
193 (url) => {
194 expect(url).toBe(SPEC_URL)
195 return new Response(FIXTURE_SPEC)
196 },
197 () => provider.fetchSpec({}),
198 )
199​
200 expect(fetched.outputStrategy).toBe('post-200')
201 expect(fetched.sources).toHaveLength(1)
202 expect(fetched.sources[0]?.url).toBe(SPEC_URL)
203 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
204​
205 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
206 expect(warnings).toEqual([])
207 expect(endpoints.map((e) => [e.path, e.activity])).toEqual([
208 ['/embeddings', 'embeddings'],
209 ['/multimodalembeddings', 'embeddings'],
210 ])
211 expect(endpoints.every((e) => e.derivation === 'upstream-spec')).toBe(true)
212 })
213})