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

rate-card-form.test.ts

165 lines5.4 KB

1import { compileTokenCard, price, priceDetailed } from '@modelschemas/rate-card'
2import { describe, expect, it } from 'vitest'
3​
4import { FIXTURE_CARDS } from '../../packages/rate-card/src/fixtures/index.ts'
5import { compileSeedanceCard } from '../server/providers/byteplus-pricing.ts'
6import { formFields, seedValues, toEstimateParts } from './rate-card-form.ts'
7​
8const quote = (
9 card: (typeof FIXTURE_CARDS)[string],
10 values = seedValues(card),
11) => {
12 const { request, usage } = toEstimateParts(card.inputs, values)
13 return price(card, request, usage)
15​
16describe('rate card form', () => {
17 it('seeds a token card from the example that covers the most inputs', () => {
18 const card = FIXTURE_CARDS['openai/gpt-4o']!
19 expect(seedValues(card)).toEqual({
20 input_tokens: '10000',
21 output_tokens: '2000',
22 cache_read_tokens: '40000',
23 })
24 expect(quote(card)).toBeCloseTo(0.095)
25 })
26​
27 it('defaults token counts to 1M when no example matches', () => {
28 const source = FIXTURE_CARDS['openai/gpt-4o']!.source
29 const card = compileTokenCard(
30 { input_tokens: 1e-6, output_tokens: 2e-6 },
31 [
32 {
33 minPromptTokens: 200_000,
34 rates: { input_tokens: 4e-6, output_tokens: 8e-6 },
35 },
36 ],
37 source,
38 )!
39 const values = seedValues(card)
40 expect(values.input_tokens).toBe('1000000')
41 // 1M prompt tokens sits above the 200k tier: the tier rate applies.
42 expect(quote(card, values)).toBeCloseTo(4 + 8)
43 expect(quote(card, { ...values, input_tokens: '100000' })).toBeCloseTo(
44 0.1 + 2,
45 )
46 })
47​
48 it('prices a dimensions card and re-quotes on edit', () => {
49 const card = FIXTURE_CARDS['dola-seedream-5-0-pro-260628']!
50 const values = seedValues(card)
51 expect(values.image_size).toEqual({ width: '2048', height: '2048' })
52 expect(quote(card, values)).toBeCloseTo(0.18)
53 expect(
54 quote(card, { ...values, image_size: { width: '1024', height: '1024' } }),
55 ).toBeCloseTo(0.09)
56 })
57​
58 it('turns a count field into a list of that length', () => {
59 const inputs = {
60 image_urls: { param: 'image_urls', kind: 'count' as const },
61 }
62 const sent = (value: string) =>
63 toEstimateParts(inputs, { image_urls: value }).request.image_urls
64 expect(sent('3')).toEqual(['', '', ''])
65 // Anything else goes through raw for the estimator to refuse — never a
66 // guessed length, never a huge list built in memory.
67 for (const bad of ['', 'abc', '-3', '2.7', '1e10', '5000']) {
68 expect(sent(bad)).toBe(bad)
69 }
70 const card = { ...FIXTURE_CARDS['openai/gpt-4o']!, inputs, examples: [] }
71 expect(() => price(card, { image_urls: sent('') })).toThrow(
72 /expected a list/,
73 )
74 })
75​
76 it('leaves completion_tokens blank and prices 720p 5s from duration', () => {
77 const card = compileSeedanceCard(
78 {
79 default: {
80 '720p': { no_video: 10.7, video: 6.4 },
81 '1080p': { no_video: 11.7, video: 7 },
82 },
83 },
84 {
85 url: 'https://example.test/pricing',
86 hash: 'a'.repeat(64),
87 extractedAt: '2026-10-04T00:00:00.000Z',
88 },
89 {
90 model: {
91 fps: 24,
92 dims: {
93 '720p': { '16:9': { w: 1280, h: 720 } },
94 '1080p': { '16:9': { w: 1920, h: 1080 } },
95 },
96 },
97 url: 'https://example.test/guide',
98 hash: 'b'.repeat(64),
99 },
100 )
101 if (!card) throw new Error('no card')
102 const values = seedValues(card)
103 expect(values.completion_tokens).toBe('')
104 expect(values.duration).toBe('5')
105 expect(values.ratio).toBe('16:9')
106 expect(values.resolution).toBe('720p')
107 expect(formFields(card.inputs).map((field) => field.input.param)).toEqual([
108 'service_tier',
109 'draft',
110 'ratio',
111 'duration',
112 'completion_tokens',
113 'resolution',
114 'input_video',
115 ])
116 const { request, usage } = toEstimateParts(card.inputs, values)
117 expect(usage).toEqual({ input_video: false })
118 expect(request).toMatchObject({
119 resolution: '720p',
120 ratio: '16:9',
121 duration: '5',
122 })
123 expect(request).not.toHaveProperty('completion_tokens')
124 const result = priceDetailed(card, request, usage)
125 expect(result.estimated).toEqual(['completion_tokens'])
126 expect(result.usd).toBeCloseTo(1.156, 2)
127 })
128​
129 it('seeds a preset-name example to that preset size', () => {
130 const card = {
131 inputs: {
132 image_size: {
133 param: 'image_size',
134 kind: 'dimensions' as const,
135 presets: {
136 square_hd: [1024, 1024] as [number, number],
137 landscape: [1536, 1024] as [number, number],
138 },
139 },
140 },
141 examples: [{ params: { image_size: 'landscape' }, usd: 1, quote: 'q' }],
142 }
143 expect(seedValues(card).image_size).toEqual({
144 width: '1536',
145 height: '1024',
146 })
147 })
148​
149 // The loader quotes the seeded fields, so the first number on the page
150 // must reproduce the example those fields came from.
151 it.each(Object.entries(FIXTURE_CARDS))(
152 '%s: seeded quote reproduces its example',
153 (_id, card) => {
154 const params = Object.values(card.inputs).map((input) => input.param)
155 const covered = (example: (typeof card.examples)[number]) =>
156 params.filter((param) => param in example.params).length
157 const best = card.examples.reduce((top, example) =>
158 covered(example) > covered(top) ? example : top,
159 )
160 expect(Math.abs(quote(card) - best.usd)).toBeLessThanOrEqual(
161 best.usd * 0.01,
162 )
163 },
164 )
165})