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

replicate-pricing.test.ts

207 lines6.1 KB

1import { describe, expect, it } from 'vitest'
2​
3import {
4 compileReplicateBilling,
5 replicateBillingFromHtml,
6 replicatePagePricing,
7} from './replicate-pricing.ts'
8​
9const SOURCE = {
10 url: 'https://replicate.com/black-forest-labs/flux-schnell',
11 hash: 'abc',
12 extractedAt: '2026-10-03T00:00:00.000Z',
14​
15describe('replicate prediction prices', () => {
16 it('compiles a per-thousand output image price', () => {
17 const card = compileReplicateBilling(
18 {
19 current_tiers: [
20 {
21 criteria: [],
22 prices: [
23 {
24 metric: 'image_output_count',
25 price: '$3',
26 title: 'per thousand output images',
27 type: 'per-unit',
28 },
29 ],
30 },
31 ],
32 },
33 SOURCE,
34 )
35 expect(card?.price).toEqual({
36 '*': [{ var: 'output_images' }, 3 / 1000],
37 })
38 expect(card?.inputs.output_images).toMatchObject({ bound: 'usage' })
39 })
40​
41 it('compiles input and output token prices', () => {
42 const card = compileReplicateBilling(
43 {
44 current_tiers: [
45 {
46 criteria: [],
47 prices: [
48 {
49 metric: 'token_output_count',
50 price: '$0.01',
51 title: 'per thousand output tokens',
52 type: 'per-unit',
53 },
54 {
55 metric: 'token_input_count',
56 price: '$3.75',
57 title: 'per million input tokens',
58 type: 'per-unit',
59 },
60 ],
61 },
62 ],
63 },
64 SOURCE,
65 )
66 expect(card?.tables.rate).toEqual({
67 base: {
68 output_tokens: 0.01 / 1000,
69 input_tokens: 3.75 / 1_000_000,
70 },
71 })
72 })
73​
74 it('compiles a resolution tier as a usage-bound lookup', () => {
75 const card = compileReplicateBilling(
76 {
77 current_tiers: [
78 {
79 criteria: [
80 { title: 'target resolution', type: 'equals', value: '480p' },
81 ],
82 prices: [
83 {
84 metric: 'video_output_duration_seconds',
85 price: '$0.03',
86 title: 'per second of output video',
87 type: 'per-unit',
88 },
89 ],
90 },
91 {
92 criteria: [
93 { title: 'target resolution', type: 'equals', value: '720p' },
94 ],
95 prices: [
96 {
97 metric: 'video_output_duration_seconds',
98 price: '$0.06',
99 title: 'per second of output video',
100 type: 'per-unit',
101 },
102 ],
103 },
104 ],
105 },
106 SOURCE,
107 )
108 expect(card?.tables.rate).toEqual({ '480p': 0.03, '720p': 0.06 })
109 expect(card?.inputs.target_resolution).toMatchObject({
110 bound: 'usage',
111 values: ['480p', '720p'],
112 })
113 })
114​
115 it('leaves an empty billing config and an unknown unit null', () => {
116 expect(compileReplicateBilling({ current_tiers: [] }, SOURCE)).toBeNull()
117 expect(
118 compileReplicateBilling(
119 {
120 current_tiers: [
121 {
122 criteria: [],
123 prices: [
124 {
125 metric: 'image_output_count',
126 price: '$1',
127 title: 'per request',
128 type: 'per-unit',
129 },
130 ],
131 },
132 ],
133 },
134 SOURCE,
135 ),
136 ).toBeNull()
137 })
138​
139 it('reads billingConfig from the model page and ignores a page with none', () => {
140 const html = `<section id="pricing"><script type="application/json">{"billingConfig":{"current_tiers":[{"criteria":[],"prices":[{"metric":"image_output_count","price":"$0.04","title":"per output image","type":"per-unit"}]}]}}</script>`
141 expect(replicateBillingFromHtml(html)?.current_tiers).toHaveLength(1)
142 const priced = replicatePagePricing(html, SOURCE)
143 expect(priced.pricing).toMatchObject({
144 price: { '*': [{ var: 'output_images' }, 0.04] },
145 })
146 expect(replicatePagePricing('<p>no price</p>', SOURCE)).toEqual({})
147 })
148})
149​
150describe('replicate prompt-length token tiers', () => {
151 // https://replicate.com/google/gemini-3-pro billingConfig.
152 const tier = (
153 range: [number | null, number | null],
154 subtype: string,
155 input: string,
156 output: string,
157 ) => ({
158 criteria: [{ title: 'input token', type: 'range', subtype, value: range }],
159 prices: [
160 {
161 metric: 'token_input_count',
162 price: input,
163 title: 'per million input tokens',
164 type: 'per-unit',
165 },
166 {
167 metric: 'token_output_count',
168 price: output,
169 title: 'per thousand output tokens',
170 type: 'per-unit',
171 },
172 ],
173 })
174 const low = tier([null, 200000], 'open-closed', '$2', '$0.012')
175 const high = tier([200000, null], 'open', '$4', '$0.018')
176​
177 it('compiles the base tier and the re-quote above the threshold', () => {
178 const card = compileReplicateBilling({ current_tiers: [low, high] }, SOURCE)
179 expect(card?.tables.rate).toEqual({
180 base: { input_tokens: 2e-6, output_tokens: 1.2e-5 },
181 '200000': { input_tokens: 4e-6, output_tokens: 1.8e-5 },
182 })
183 })
184​
185 it('refuses any other range shape', () => {
186 const compile = (...tiers: Array<ReturnType<typeof tier>>) =>
187 compileReplicateBilling({ current_tiers: tiers }, SOURCE)
188 // One tier alone, a gap between the tiers, a closed upper tier.
189 expect(compile(low)).toBeNull()
190 expect(compile(high)).toBeNull()
191 expect(compile(low, tier([256000, null], 'open', '$4', '$0.018'))).toBe(
192 null,
193 )
194 expect(
195 compile(low, tier([200000, 400000], 'open-closed', '$4', '$0.018')),
196 ).toBeNull()
197 expect(compile(low, { ...high, criteria: [] })).toBeNull()
198 // A range over something other than prompt tokens.
199 const seconds = structuredClone(high)
200 if (seconds.criteria[0]) seconds.criteria[0].title = 'output second'
201 expect(compile(low, seconds)).toBeNull()
202 // A tier that prices only input tokens.
203 expect(compile(low, { ...high, prices: high.prices.slice(0, 1) })).toBe(
204 null,
205 )
206 })
207})