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

stability-pricing.test.ts

101 lines3.5 KB

1import { describe, expect, it, vi } from 'vitest'
2​
3import { compileUnitCard } from '@modelschemas/rate-card'
4​
5import type { cachedDocs } from './model-facts.ts'
6import {
7 parseStabilityPricing,
8 stabilityModelPricing,
9} from './stability-pricing.ts'
10​
11/** Excerpt of https://docs.stability.ai/pricing.md (2026-10-19). */
12const PAGE = String.raw`# Pricing
13​
14<Columns cols={2}>
15 <Card title="How does pricing work?" icon="coins">
16 API usage is based on credits. 1 credit = \$0.01.
17 </Card>
18</Columns>
19​
20### Generate
21​
22| Service | Description | Price (credits) |
23| - | - | - |
24| [Stable Image Ultra](/api-reference/generate/stable-image-ultra) | Flagship image service based on Stable Diffusion 3.5 Large, offering the highest quality and detail | 8 |
25| [Stable Diffusion 3.5 Large](/api-reference/generate/stable-diffusion-35) | SD 3.5 is our most powerful 8 billion parameter base model with superior quality and prompt adherence | 6.5 |
26| [SDXL 1.0](/api-reference/sdxl-10/text-to-image) | Legacy base model for straightforward image generation | From 0.9 |
27​
28### Upscale
29​
30| Service | Description | Price (credits) |
31| - | - | - |
32| [Fast Upscaler](/api-reference/upscale/fast) | Simple, low-cost upscaler to increase image resolution by 4, up to 4 megapixels | 2 |
34​
35const SOURCE = {
36 url: 'https://docs.stability.ai/pricing.md',
37 hash: 'c'.repeat(64),
38 extractedAt: '2026-10-03T00:00:00.000Z',
40​
41describe('stability pricing page', () => {
42 it('prices an exact credit count and leaves an inexact model null', () => {
43 const rates = parseStabilityPricing(PAGE)
44 expect(rates.get('Stable Image Ultra')).toBeCloseTo(0.08)
45 expect(rates.get('Stable Diffusion 3.5 Large')).toBeCloseTo(0.065)
46 expect(rates.get('Fast Upscaler')).toBeCloseTo(0.02)
47 expect(
48 compileUnitCard({ rates: rates.get('Stable Image Ultra') ?? 0 }, SOURCE),
49 ).toMatchObject({ price: 0.08 })
50 expect(rates.has('SDXL 1.0')).toBe(false)
51 expect(rates.has('Service')).toBe(false)
52 expect(rates.size).toBe(3)
53 })
54​
55 it('reads only tables priced in credits', () => {
56 const page = `${PAGE}
57| Pack | Description | Price (USD) |
58| - | - | - |
59| Starter | 1,000 credits | 10 |
61 expect(parseStabilityPricing(page).has('Starter')).toBe(false)
62 expect(parseStabilityPricing(page).size).toBe(3)
63 })
64​
65 it('refuses a service the page prices twice', () => {
66 const page = `${PAGE}
67| Service | Description | Price (credits) |
68| - | - | - |
69| [Fast Upscaler](/api-reference/upscale/fast) | Again | 3 |
71 expect(() => parseStabilityPricing(page)).toThrow(
72 'stability pricing: Fast Upscaler has two prices',
73 )
74 })
75​
76 it('prices nothing without the stated credit rate', () => {
77 expect(
78 parseStabilityPricing(PAGE.replace('1 credit', 'One credit')).size,
79 ).toBe(0)
80 })
81​
82 it('attaches a card only to an id the page names', async () => {
83 const fetchMock = vi
84 .spyOn(globalThis, 'fetch')
85 .mockResolvedValue(new Response(PAGE))
86 const uncached: typeof cachedDocs = (_kv, _url, load) => load()
87 try {
88 const lookup = await stabilityModelPricing(undefined, uncached)
89 const named = lookup('Stable Image Ultra')
90 expect(named.pricing).toMatchObject({ price: 0.08 })
91 expect(named.factSources?.pricing).toMatchObject({
92 sourceUrl: 'https://docs.stability.ai/pricing.md',
93 })
94 // Engine ids from /v1/engines/list are not page service names.
95 expect(lookup('stable-diffusion-xl-1024-v1-0')).toEqual({})
96 expect(lookup('SDXL 1.0')).toEqual({})
97 } finally {
98 fetchMock.mockRestore()
99 }
100 })
101})