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

token-card.test.ts

124 lines4.2 KB

1import { describe, expect, it } from 'vitest'
2import { cardCurrency, price } from './evaluate.ts'
3import { GPT_4O } from './fixtures/gpt-4o.ts'
4import { rateCardSchema } from './rate-card.schema.ts'
5import { compileTokenCard } from './token-card.ts'
6​
7const source = GPT_4O.source
8​
9const compile = (
10 base: Record<string, number>,
11 tiers: Parameters<typeof compileTokenCard>[1] = [],
12) => {
13 const card = compileTokenCard(base, tiers, source)
14 if (!card) throw new Error('did not compile')
15 expect(rateCardSchema.safeParse(card).success).toBe(true)
16 return card
18​
19describe('compileTokenCard', () => {
20 it('prices each lever at its own rate', () => {
21 const card = compile({
22 input_tokens: 1.25 / 1e6,
23 cache_read_tokens: 0.125 / 1e6,
24 output_tokens: 10 / 1e6,
25 })
26 expect(
27 price(card, {}, { input_tokens: 1e6, output_tokens: 0 }),
28 ).toBeCloseTo(1.25, 9)
29 expect(
30 price(
31 card,
32 {},
33 { input_tokens: 0, cache_read_tokens: 2e6, output_tokens: 1e6 },
34 ),
35 ).toBeCloseTo(10.25, 9)
36 })
37​
38 it('re-quotes above a prompt-token threshold, cache reads included', () => {
39 const card = compile({ input_tokens: 1.25e-6, output_tokens: 10e-6 }, [
40 { minPromptTokens: 200_000, rates: { input_tokens: 2.5e-6 } },
41 ])
42 // At the threshold the base rate still applies (strictly greater wins).
43 expect(price(card, {}, { input_tokens: 200_000, output_tokens: 0 })).toBe(
44 0.25,
45 )
46 expect(price(card, {}, { input_tokens: 200_001, output_tokens: 0 })).toBe(
47 0.5000025,
48 )
49 // A tier that re-quotes only the input keeps the base output rate.
50 expect(price(card, {}, { input_tokens: 300_000, output_tokens: 1e6 })).toBe(
51 10.75,
52 )
53 })
54​
55 it('counts extra prompt levers toward the tier', () => {
56 const base = {
57 input_tokens: 1e-6,
58 audio_tokens: 2e-6,
59 output_tokens: 1e-6,
60 }
61 const tiers = [
62 {
63 minPromptTokens: 128_000,
64 rates: {
65 input_tokens: 2e-6,
66 audio_tokens: 4e-6,
67 output_tokens: 2e-6,
68 },
69 },
70 ]
71 const withAudio = compileTokenCard(base, tiers, source, {
72 extraPromptLevers: ['audio_tokens', 'audio_cache_tokens'],
73 })
74 const textOnly = compileTokenCard(base, tiers, source)
75 if (!withAudio || !textOnly) throw new Error('did not compile')
76 const usage = { input_tokens: 0, audio_tokens: 200_000, output_tokens: 0 }
77 expect(price(withAudio, {}, usage)).toBeCloseTo(0.8, 9)
78 expect(price(textOnly, {}, usage)).toBeCloseTo(0.4, 9)
79 })
80​
81 it('requires token counts but defaults every other lever', () => {
82 const card = compile({ input_tokens: 1e-6, output_tokens: 2e-6 })
83 expect(() => price(card, {}, { input_tokens: 1 })).toThrow(
84 expect.objectContaining({ code: 'bad-input' }),
85 )
86 })
87​
88 it('prices an output-less card (embeddings)', () => {
89 const card = compile({ input_tokens: 0.02 / 1e6 })
90 expect(price(card, {}, { input_tokens: 1e6 })).toBeCloseTo(0.02, 9)
91 })
92​
93 it('labels a card with the currency its rates are in, USD unlabelled', () => {
94 const yuan = compileTokenCard({ input_tokens: 20 / 1e6 }, [], source, {
95 currency: 'CNY',
96 })
97 if (!yuan) throw new Error('did not compile')
98 const usd = compile({ input_tokens: 20 / 1e6 })
99 // One representation: the price wrapper. A USD card has none, and
100 // naming USD adds none, so it stays what 0.1.0 already reads.
101 expect(yuan).toEqual({ ...usd, price: { currency: ['CNY', usd.price] } })
102 expect(
103 compileTokenCard({ input_tokens: 20 / 1e6 }, [], source, {
104 currency: 'USD',
105 }),
106 ).toEqual(usd)
107 expect(rateCardSchema.safeParse(yuan).success).toBe(true)
108 expect(cardCurrency(yuan)).toBe('CNY')
109 expect(price(yuan, {}, { input_tokens: 1e6 })).toBe(
110 price(usd, {}, { input_tokens: 1e6 }),
111 )
112 })
113​
114 it('is null when nothing is priced', () => {
115 expect(compileTokenCard({}, [], source)).toBeNull()
116 expect(
117 compileTokenCard({ input_tokens: 0, output_tokens: 0 }, [], source),
118 ).toBeNull()
119 expect(compileTokenCard({ input_tokens: -1e-6 }, [], source)).toBeNull()
120 expect(
121 compileTokenCard({ input_tokens: Number.NaN }, [], source),
122 ).toBeNull()
123 })
124})