Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { findDanglingRefs } from '#/server/ingest/bundle.ts'
4import { classifyAndBundle } from '#/server/ingest/sync.ts'
5import type { OpenApiDocument } from '../types.ts'
6import { provider } from './stability.ts'
7import { STABILITY_PRICING_URL } from '../stability-pricing.ts'
8
9const SPEC_URL = 'https://api.stability.ai/v2alpha/openapi'
10const ENGINES_URL = 'https://api.stability.ai/v1/engines/list'
11
12const FIXTURE_SPEC: OpenApiDocument = {
13 openapi: '3.0.1',
14 info: { title: 'StabilityAI REST API', version: 'v2beta' },
15 servers: [{ url: 'https://api.stability.ai' }],
16 paths: {
17 '/v2beta/stable-image/generate/ultra': {
18 post: {
19 summary: 'Stable Image Ultra',
20 requestBody: {
21 content: {
22 'multipart/form-data': {
23 schema: {
24 type: 'object',
25 required: ['prompt'],
26 properties: {
27 prompt: { type: 'string' },
28 output_format: {
29 type: 'string',
30 enum: ['jpeg', 'png', 'webp'],
31 },
32 },
33 },
34 },
35 },
36 },
37 responses: {
38 '200': {
39 content: {
40 'application/json': {
41 schema: {
42 type: 'object',
43 properties: {
44 image: { type: 'string' },
45 finish_reason: { type: 'string' },
46 seed: { type: 'number' },
47 },
48 },
49 },
50 },
51 },
52 },
53 },
54 },
55 '/v2beta/audio/stable-audio-2/text-to-audio': {
56 post: {
57 summary: 'Text-to-Audio',
58 requestBody: {
59 content: {
60 'multipart/form-data': {
61 schema: {
62 type: 'object',
63 required: ['prompt'],
64 properties: { prompt: { type: 'string' } },
65 },
66 },
67 },
68 },
69 responses: {
70 '200': {
71 content: {
72 'application/json': {
73 schema: {
74 type: 'object',
75 properties: { audio: { type: 'string' } },
76 },
77 },
78 },
79 },
80 },
81 },
82 },
83 '/v2beta/3d/stable-fast-3d': {
84 post: {
85 summary: 'Stable Fast 3D',
86 requestBody: {
87 content: {
88 'multipart/form-data': {
89 schema: {
90 type: 'object',
91 properties: { image: { type: 'string' } },
92 },
93 },
94 },
95 },
96 responses: { '200': { description: 'model/gltf-binary' } },
97 },
98 },
99 },
100}
101
102async function withFetch<T>(
103 handler: (url: string, init?: RequestInit) => Response,
104 run: () => Promise<T>,
105): Promise<{ result: T; urls: Array<string> }> {
106 const original = globalThis.fetch
107 const urls: Array<string> = []
108 globalThis.fetch = (input: RequestInfo | URL, init?: RequestInit) => {
109 const href = String(input)
110 urls.push(href)
111 return Promise.resolve(handler(href, init))
112 }
113 try {
114 return { result: await run(), urls }
115 } finally {
116 globalThis.fetch = original
117 }
118}
119
120describe('stability classify', () => {
121 it('maps image and audio generation paths and drops platform ones', () => {
122 expect(provider.classify('/v2beta/stable-image/generate/ultra', {})).toBe(
123 'image',
124 )
125 expect(provider.classify('/v2beta/stable-image/upscale/fast', {})).toBe(
126 'image',
127 )
128 expect(provider.classify('/v2beta/stable-image/edit/inpaint', {})).toBe(
129 'image',
130 )
131 expect(
132 provider.classify('/v2alpha/generation/stable-image/upscale', {}),
133 ).toBe('image')
134 expect(
135 provider.classify('/v2beta/audio/stable-audio-2/text-to-audio', {}),
136 ).toBe('audio')
137 expect(provider.classify('/v2beta/results/{id}', {})).toBeNull()
138 expect(
139 provider.classify(
140 '/v2beta/stable-image/upscale/creative/result/{id}',
141 {},
142 ),
143 ).toBeNull()
144 expect(provider.classify('/v2beta/audio/results/{id}', {})).toBeNull()
145 expect(provider.classify('/v2beta/3d/stable-fast-3d', {})).toBeNull()
146 expect(provider.classify('/v1/user/account', {})).toBeNull()
147 expect(provider.classify('/v1/engines/list', {})).toBeNull()
148 })
149})
150
151describe('stability listModels', () => {
152 it('skips with an empty catalog when the secret is absent', async () => {
153 const result = await provider.listModels({})
154 expect(result.models).toEqual([])
155 expect(result.skipped).toBe(
156 'stability: STABILITY_API_KEY not set — skipped',
157 )
158 })
159
160 it('maps engines/list entries without calling a live host', async () => {
161 const { result, urls } = await withFetch(
162 (url) => {
163 if (url === STABILITY_PRICING_URL) {
164 return new Response(
165 '1 credit = \\$0.01.\n\n| Service | Description | Price (credits) |\n| - | - | - |\n| [Stable Image Ultra](/api-reference/generate/stable-image-ultra) | Flagship | 8 |\n',
166 )
167 }
168 expect(url).toBe(ENGINES_URL)
169 return new Response(
170 JSON.stringify([
171 {
172 id: 'stable-diffusion-xl-1024-v1-0',
173 name: 'Stable Diffusion XL 1.0',
174 type: 'PICTURE',
175 },
176 { id: 'stable-audio', name: 'Stable Audio', type: 'AUDIO' },
177 ]),
178 )
179 },
180 () => provider.listModels({ STABILITY_API_KEY: 'test-key' }),
181 )
182 expect(urls).toEqual([ENGINES_URL, STABILITY_PRICING_URL])
183 expect(result.skipped).toBeUndefined()
184 expect(result.docsFailures).toEqual({ failed: 0, skipped: 0, first: [] })
185 expect(result.models).toEqual([
186 {
187 rawId: 'stable-diffusion-xl-1024-v1-0',
188 displayName: 'Stable Diffusion XL 1.0',
189 activity: 'image',
190 },
191 {
192 rawId: 'stable-audio',
193 displayName: 'Stable Audio',
194 activity: 'audio',
195 },
196 ])
197 })
198
199 it('keeps the listing and reports the pricing page when it prices nothing', async () => {
200 const { result } = await withFetch(
201 (url) => {
202 if (url === STABILITY_PRICING_URL) {
203 return new Response('<!DOCTYPE html><html></html>')
204 }
205 expect(url).toBe(ENGINES_URL)
206 return new Response(
207 JSON.stringify([
208 {
209 id: 'stable-diffusion-xl-1024-v1-0',
210 name: 'Stable Diffusion XL 1.0',
211 type: 'PICTURE',
212 },
213 ]),
214 )
215 },
216 () => provider.listModels({ STABILITY_API_KEY: 'test-key' }),
217 )
218 expect(result.models).toEqual([
219 {
220 rawId: 'stable-diffusion-xl-1024-v1-0',
221 displayName: 'Stable Diffusion XL 1.0',
222 activity: 'image',
223 absent: { pricing: 'unavailable' },
224 },
225 ])
226 expect(result.docsFailures?.failed).toBe(1)
227 expect(result.docsFailures?.first[0]).toMatchObject({
228 source: STABILITY_PRICING_URL,
229 error: 'stability pricing page: parsed 0 model rows',
230 })
231 })
232})
233
234describe('stability fetchSpec', () => {
235 it('loads the public spec keylessly and bundles generation POSTs', async () => {
236 const { result: fetched, urls } = await withFetch(
237 (url) => {
238 expect(url).toBe(SPEC_URL)
239 return new Response(JSON.stringify(FIXTURE_SPEC))
240 },
241 () => provider.fetchSpec({}),
242 )
243 expect(urls).toEqual([SPEC_URL])
244 expect(fetched.outputStrategy).toBe('post-200')
245 expect(fetched.sources[0]?.url).toBe(SPEC_URL)
246 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
247
248 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
249 expect(warnings).toEqual([])
250 expect(endpoints.map((e) => [e.dbId, e.activity]).sort()).toEqual([
251 ['stability/v2beta/audio/stable-audio-2/text-to-audio', 'audio'],
252 ['stability/v2beta/stable-image/generate/ultra', 'image'],
253 ])
254 for (const endpoint of endpoints) {
255 expect(endpoint.input, endpoint.dbId).toBeDefined()
256 expect(endpoint.output, endpoint.dbId).toBeDefined()
257 if (endpoint.input) expect(findDanglingRefs(endpoint.input)).toEqual([])
258 if (endpoint.output) expect(findDanglingRefs(endpoint.output)).toEqual([])
259 }
260 })
261})
262
263describe('stability seed metadata', () => {
264 it('exports the adapter contract fields', () => {
265 expect(provider.id).toBe('stability')
266 expect(provider.displayName).toBe('Stability AI')
267 expect(provider.authEnvVar).toBe('STABILITY_API_KEY')
268 expect(provider.specSourceUrl).toBe(SPEC_URL)
269 expect(provider.modelsEndpoint).toBe(ENGINES_URL)
270 expect(provider.defaultDerivation).toBe('upstream-spec')
271 })
272})