Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { verifyExamples } from '@modelschemas/rate-card'
4import type { RateCard } from '@modelschemas/rate-card'
5
6import {
7 compileFalUnitCard,
8 parseFalUnitRate,
9 priceFalNamedSection,
10 usdAmounts,
11} from './fal-unit-rate.ts'
12
13const SOURCE: RateCard['source'] = {
14 url: 'https://fal.ai/models/fal-ai/x/llms.txt',
15 hash: 'a'.repeat(64),
16 extractedAt: '2026-09-21T06:00:00.000Z',
17}
18
19function compile(section: string, properties: Array<string> = []) {
20 return compileFalUnitCard(section, new Set(properties), SOURCE)
21}
22
23describe('usdAmounts', () => {
24 it('reads plain, bold, and suffix-$ spellings', () => {
25 expect(usdAmounts('$0.04')).toEqual([0.04])
26 expect(usdAmounts('$**0.04**')).toEqual([0.04])
27 expect(usdAmounts('**0.17** $')).toEqual([0.17])
28 expect(usdAmounts('**$0.08** per image')).toEqual([0.08])
29 })
30
31 it('ignores zeros and sections with no price', () => {
32 expect(usdAmounts('- **Price**: $0 per compute seconds')).toEqual([])
33 expect(usdAmounts('See [fal.ai pricing](https://fal.ai/pricing).')).toEqual(
34 [],
35 )
36 })
37})
38
39describe('parseFalUnitRate', () => {
40 it('reads the common FAL price line', () => {
41 expect(parseFalUnitRate('- **Price**: $0.04 per megapixels')).toMatchObject(
42 { amount: 0.04, count: 1, param: 'megapixels' },
43 )
44 expect(
45 parseFalUnitRate('- **Price**: $0.05 per 1000 characters'),
46 ).toMatchObject({ amount: 0.05, count: 1000, param: 'characters' })
47 expect(parseFalUnitRate('- **Price**: $0 per compute seconds')).toBeNull()
48 })
49
50 it('reads prose rates, slashes, and bold money', () => {
51 expect(
52 parseFalUnitRate('Your request will cost **$0.08** per image.'),
53 ).toMatchObject({ amount: 0.08, param: 'images' })
54 expect(
55 parseFalUnitRate('it will cost **$0.045/sec** of generated video.'),
56 ).toMatchObject({ amount: 0.045, param: 'seconds' })
57 expect(parseFalUnitRate('- **Price**: $**0.04** per images')).toMatchObject(
58 { amount: 0.04, param: 'images' },
59 )
60 expect(
61 parseFalUnitRate('- **Price**: **0.17** $ per second'),
62 ).toMatchObject({ amount: 0.17, param: 'seconds' })
63 expect(
64 parseFalUnitRate(
65 'Generation will cost **$0.025** per output video second.',
66 ),
67 ).toMatchObject({ amount: 0.025, param: 'video_seconds' })
68 })
69
70 it('ignores the "for $1.00 you can run this N times" restatement', () => {
71 expect(
72 parseFalUnitRate(
73 'Your request will cost **$0.039** per image. For **$1.00**, you can run this model **25 times.**',
74 ),
75 ).toMatchObject({ amount: 0.039, param: 'images' })
76 })
77
78 it('refuses anything with more than one rate, or an unknown unit', () => {
79 expect(
80 parseFalUnitRate(
81 'Video costs **$0.025** per second at **480p**, and **$0.08** per second at **1080p**.',
82 ),
83 ).toBeNull()
84 expect(
85 parseFalUnitRate(
86 'Your request will cost **$0.08** per image. If web search is used, an additional $0.015 will be charged.',
87 ),
88 ).toBeNull()
89 expect(parseFalUnitRate('- **Price**: $0.02 per widgets')).toBeNull()
90 expect(parseFalUnitRate('- **Price**: $0.02 per ')).toBeNull()
91 })
92})
93
94describe('compileFalUnitCard', () => {
95 it('synthesizes a one-unit example that verifies against the quote', () => {
96 const card = compile('- **Price**: $0.04 per megapixels')
97 expect(card).not.toBeNull()
98 if (!card) return
99 expect(card.examples).toEqual([
100 {
101 params: { megapixels: 1 },
102 usd: 0.04,
103 quote: '- **Price**: $0.04 per megapixels',
104 },
105 ])
106 expect(card.inputs.megapixels).toMatchObject({ bound: 'usage' })
107 expect(verifyExamples(card).every((result) => result.ok)).toBe(true)
108 })
109
110 it('prices the quoted denominator, not one of it', () => {
111 const card = compile('- **Price**: $0.05 per 1000 characters')
112 expect(card?.examples[0]).toMatchObject({
113 params: { characters: 1000 },
114 usd: 0.05,
115 })
116 expect(card && verifyExamples(card).every((r) => r.ok)).toBe(true)
117 })
118
119 it('binds to the request only when the endpoint really has that field', () => {
120 expect(
121 compile('- **Price**: $0.04 per images', ['images'])?.inputs.images,
122 ).toMatchObject({ bound: 'request' })
123 expect(
124 compile('- **Price**: $0.04 per images', ['prompt'])?.inputs.images,
125 ).toMatchObject({ bound: 'usage' })
126 })
127
128 it('returns null for stubs and multi-rate tables', () => {
129 expect(compile('- **Price**: $0 per compute seconds')).toBeNull()
130 expect(
131 compile(
132 'Video costs **$0.025** per second at **480p**, and **$0.08** per second at **1080p**.',
133 ),
134 ).toBeNull()
135 })
136})
137
138describe('priceFalNamedSection', () => {
139 it('writes a card when the section names one dollar amount', () => {
140 const card = priceFalNamedSection(
141 '- **Price**: $0.3 per requests',
142 new Set<string>(),
143 SOURCE,
144 )
145 expect(card?.examples[0]).toMatchObject({
146 params: { requests: 1 },
147 usd: 0.3,
148 })
149 expect(card && verifyExamples(card).every((result) => result.ok)).toBe(true)
150 })
151
152 it('stays null when the section names no price', () => {
153 const none = [
154 '',
155 'For more details, see [fal.ai pricing](https://fal.ai/pricing).',
156 '- **Price**: $0 per compute seconds',
157 '- **Price**: $0.00 per images',
158 ]
159 for (const section of none) {
160 expect(priceFalNamedSection(section, new Set(), SOURCE)).toBeNull()
161 }
162 })
163
164 it('does not invent a card for a named price with no single unit', () => {
165 expect(
166 priceFalNamedSection(
167 'Your request will cost **$0.02** per image for 480p or **$0.03** per image for 720p.',
168 new Set(),
169 SOURCE,
170 ),
171 ).toBeNull()
172 expect(
173 priceFalNamedSection('- **Price**: $0.01 per widgets', new Set(), SOURCE),
174 ).toBeNull()
175 })
176})