Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { FIXTURE_CARDS as ALL_CARDS } from './fixtures/index.ts'
3import { price, verifyExamples } from './evaluate.ts'
4import { rateCardSchema } from './rate-card.schema.ts'
5import type { RateCard } from './card.ts'
6
7const MINIMAL_REQUEST: Record<string, Record<string, unknown>> = {
8 'xai/grok-imagine-video/v1.5/reference-to-video': {
9 reference_image_urls: ['a'],
10 },
11 // Edit defaults `image_size` to `auto` (sized from the input), which no
12 // table prices; the app always sends a size.
13 'openai/gpt-image-2.5/flare/edit': { image_size: 'square_hd' },
14}
15
16const MINIMAL_USAGE: Record<string, Record<string, unknown>> = {
17 // Token cards price usage, not a request body.
18 'openai/gpt-4o': { input_tokens: 1, output_tokens: 1 },
19}
20
21// Pages of different shapes (per-second tiers, per-image multipliers, token
22// allowances, size × quality tables, token formulas with a minimum charge,
23// pixel tiers). Every worked example reproducing is the proof the vocabulary
24// is sufficient before an LLM writes a card.
25describe.each(Object.entries(ALL_CARDS))('rate card %s', (endpointId, card) => {
26 it('validates against the schema', () => {
27 expect(rateCardSchema.safeParse(card).success).toBe(true)
28 })
29
30 it('carries at least one worked example from the source', () => {
31 expect(card.examples.length).toBeGreaterThan(0)
32 expect(card.source.url).toContain('http')
33 })
34
35 it('reproduces every worked example within 1%', () => {
36 const results = verifyExamples(card)
37 const failures = results.filter((r) => !r.ok)
38 expect(
39 failures.map((f) => `${endpointId}: ${f.example.quote} → ${f.error}`),
40 ).toEqual([])
41 })
42
43 it('prices the smallest request the endpoint accepts', () => {
44 // Every input has a default or is a count; a required list param
45 // (Grok r2v needs 1–7 reference images) is supplied so the priced
46 // request is one fal would actually run.
47 const usd = price(
48 card,
49 MINIMAL_REQUEST[endpointId] ?? {},
50 MINIMAL_USAGE[endpointId] ?? {},
51 )
52 expect(usd).toBeGreaterThan(0)
53 })
54})
55
56const cardFor = (endpointId: string): RateCard => {
57 const card = ALL_CARDS[endpointId]
58 if (!card) throw new Error(`no card for ${endpointId}`)
59 return card
60}
61
62describe('GPT Image 2.5 prices the sizes the app sends at their band row', () => {
63 const card = cardFor('openai/gpt-image-2.5/flare/text-to-image')
64 const at = (image_size: unknown, quality = 'high') =>
65 price(card, { image_size, quality })
66
67 it('fal presets land in the row of their orientation', () => {
68 expect(at('landscape_16_9')).toBe(0.03612)
69 expect(at('portrait_16_9')).toBe(0.04116)
70 expect(at('square_hd')).toBe(0.05268)
71 expect(at('square')).toBe(0.05268)
72 })
73
74 it('tier pixels land in the row of their area band', () => {
75 expect(at({ width: 1280, height: 720 })).toBe(0.03612)
76 expect(at({ width: 720, height: 1280 })).toBe(0.04116)
77 expect(at({ width: 1072, height: 1072 })).toBe(0.05268)
78 expect(at({ width: 1080, height: 1920 })).toBe(0.0396)
79 expect(at({ width: 2160, height: 2160 })).toBe(0.05529)
80 expect(at({ width: 3840, height: 2160 }, 'max')).toBe(0.40026)
81 })
82
83 it('the edit endpoint carries the same table', () => {
84 const edit = cardFor('openai/gpt-image-2.5/flare/edit')
85 expect(price(edit, { image_size: 'landscape_16_9', num_images: 2 })).toBe(
86 0.07224,
87 )
88 })
89})
90
91describe('Ark Seedance prices every ratio at the resolution area', () => {
92 // Ark sizes a resolution class to the same pixel area whatever the ratio
93 // (frame jobs send `adaptive_<resolution>`), so 9:16 and 1:1 are not a
94 // refusal.
95 it.each(['dreamina-seedance-2-5-260628', 'dreamina-seedance-2-0-260128'])(
96 '%s',
97 (id) => {
98 const card = cardFor(id)
99 const at = (aspect_ratio: string) =>
100 price(card, {
101 resolution: '720p',
102 duration: 5,
103 aspect_ratio,
104 })
105 expect(at('9:16')).toBe(at('16:9'))
106 expect(at('1:1')).toBe(at('16:9'))
107 },
108 )
109
110 it('Seedance 2.0 prices the 4K tier the Ark route offers', () => {
111 const card = cardFor('dreamina-seedance-2-0-260128')
112 // 3840 × 2160 × 5 s × 24 / 1024 tokens at $4.0/1M
113 expect(price(card, { resolution: '4k', duration: 5 })).toBeCloseTo(3.888, 3)
114 })
115})
116
117describe('cards refuse what their source does not price', () => {
118 it('GPT Image 2.5: quality auto', () => {
119 const card = cardFor('openai/gpt-image-2.5/flare/text-to-image')
120 expect(() =>
121 price(card, { image_size: 'square_hd', quality: 'auto' }),
122 ).toThrow(/size_quality/)
123 })
124
125 it('Seedance 2.5: a with-video shape outside the transcribed minimum-token rows', () => {
126 const card = cardFor('dreamina-seedance-2-5-260628')
127 expect(() =>
128 price(card, { duration: 10, input_video_duration: 3 }),
129 ).toThrow(/min_tokens/)
130 })
131})