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

stability.test.ts

244 lines7.6 KB

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 },
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 }
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('<script src="/assets/pricing.js"></script>')
165 }
166 if (url === 'https://platform.stability.ai/assets/pricing.js') {
167 return new Response(
168 '1 credit = $0.01.\n{id:"generate-ultra",service:"Stable Image Ultra",description:"Flagship",price:"8"}',
169 )
170 }
171 expect(url).toBe(ENGINES_URL)
172 return new Response(
173 JSON.stringify([
174 {
175 id: 'stable-diffusion-xl-1024-v1-0',
176 name: 'Stable Diffusion XL 1.0',
177 type: 'PICTURE',
178 },
179 { id: 'stable-audio', name: 'Stable Audio', type: 'AUDIO' },
180 ]),
181 )
182 },
183 () => provider.listModels({ STABILITY_API_KEY: 'test-key' }),
184 )
185 expect(urls).toEqual([
186 ENGINES_URL,
187 STABILITY_PRICING_URL,
188 'https://platform.stability.ai/assets/pricing.js',
189 ])
190 expect(result.skipped).toBeUndefined()
191 expect(result.models).toEqual([
192 {
193 rawId: 'stable-diffusion-xl-1024-v1-0',
194 displayName: 'Stable Diffusion XL 1.0',
195 activity: 'image',
196 },
197 {
198 rawId: 'stable-audio',
199 displayName: 'Stable Audio',
200 activity: 'audio',
201 },
202 ])
203 })
204})
205​
206describe('stability fetchSpec', () => {
207 it('loads the public spec keylessly and bundles generation POSTs', async () => {
208 const { result: fetched, urls } = await withFetch(
209 (url) => {
210 expect(url).toBe(SPEC_URL)
211 return new Response(JSON.stringify(FIXTURE_SPEC))
212 },
213 () => provider.fetchSpec({}),
214 )
215 expect(urls).toEqual([SPEC_URL])
216 expect(fetched.outputStrategy).toBe('post-200')
217 expect(fetched.sources[0]?.url).toBe(SPEC_URL)
218 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
219​
220 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
221 expect(warnings).toEqual([])
222 expect(endpoints.map((e) => [e.dbId, e.activity]).sort()).toEqual([
223 ['stability/v2beta/audio/stable-audio-2/text-to-audio', 'audio'],
224 ['stability/v2beta/stable-image/generate/ultra', 'image'],
225 ])
226 for (const endpoint of endpoints) {
227 expect(endpoint.input, endpoint.dbId).toBeDefined()
228 expect(endpoint.output, endpoint.dbId).toBeDefined()
229 if (endpoint.input) expect(findDanglingRefs(endpoint.input)).toEqual([])
230 if (endpoint.output) expect(findDanglingRefs(endpoint.output)).toEqual([])
231 }
232 })
233})
234​
235describe('stability seed metadata', () => {
236 it('exports the adapter contract fields', () => {
237 expect(provider.id).toBe('stability')
238 expect(provider.displayName).toBe('Stability AI')
239 expect(provider.authEnvVar).toBe('STABILITY_API_KEY')
240 expect(provider.specSourceUrl).toBe(SPEC_URL)
241 expect(provider.modelsEndpoint).toBe(ENGINES_URL)
242 expect(provider.defaultDerivation).toBe('upstream-spec')
243 })
244})