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

vertex-pricing.test.ts

155 lines6.6 KB

1import { describe, expect, it } from 'vitest'
2​
3import { compileTokenCard } from '@modelschemas/rate-card'
4​
5import { normModelName } from './vertex-text.ts'
6import { baseRates, parseVertexPricing, priceLevers } from './vertex-pricing.ts'
7​
8const NOW = Date.UTC(2026, 9, 6)
9​
10function table(header: string, rows: Array<string>): string {
11 const head = header
12 .split('|')
13 .map((cell) => `<th>${cell.trim()}</th>`)
14 .join('')
15 const body = rows
16 .map(
17 (row) =>
18 `<tr>${row
19 .split('|')
20 .map((cell) => `<td>${cell.trim()}</td>`)
21 .join('')}</tr>`,
22 )
23 .join('')
24 return `<table><tr>${head}</tr>${body}</table>`
26​
27const HEADER =
28 'Model | Type | Region | Price (/1M tokens) <= 200K input tokens | Price (/1M tokens) > 200K input tokens | Price (/1M tokens) <= 200K cached input tokens | Price (/1M tokens) > 200K cached input tokens'
29​
30const PRICING = [
31 table(HEADER, [
32 'Gemini 3.1 Pro Preview | Input (text, image, video, audio) | Global | $2.00 | $4.00 | $0.20 | $0.40',
33 '| Text output (response and reasoning) | Global | $12.00 | $18.00 | N/A | N/A',
34 'Gemini 3.1 Pro Preview | Input (text, image, video, audio) | Non-global | $2.20 | $4.40 | $0.22 | $0.44',
35 '| Text output (response and reasoning) | Non-global | $13.20 | $19.80 | N/A | N/A',
36 'Gemini 3.8 Flash* through December 31, 2026 | Input (text, image, video, audio) | Global | $0.75 | $0.75 | $0.075 | $0.075',
37 '| Text output (response and reasoning) | Global | $3.75 | $3.75 | N/A | N/A',
38 'Gemini 3.8 Flash Starting January 1, 2027 | Input (text, image, video, audio) | Global | $1.50 | $1.50 | $0.15 | $0.15',
39 '| Text output (response and reasoning) | Global | $7.50 | $7.50 | N/A | N/A',
40 'Gemini 2.5 Flash | Input (text, image, video) | Global | $0.30 | $0.30 | $0.03 | $0.03',
41 '| Audio Input | Global | $1.00 | $1.00 | $0.10 | $0.10',
42 '| Text output (response and reasoning) | Global | $2.50 | $2.50 | N/A | N/A',
43 'Gemini 3.1 Flash-Lite | Input (text, image, video) | Global | $0.25 | $0.25 | $0.025 | $0.025',
44 '| Input (audio) | Global | $0.50 | $0.50 | $0.05 | $0.05',
45 '| Text output (response and reasoning) | Global | $1.50 | $1.50 | N/A | N/A',
46 'Gemini 3 Pro Image | Input (text, image) | Global | $2.00 | $4.00 | N/A | N/A',
47 '| Text output (response and reasoning) | Global | $12.00 | $18.00 | N/A | N/A',
48 '| Image output per picture | Global | $0.13 | N/A | N/A | N/A',
49 'Gemini 2.5 Pro Computer Use-Preview | Input (text, image, video, audio) | | $1.25 | $2.50 | N/A | N/A',
50 '| Text output (response and reasoning) | | $10.00 | $15.00 | N/A | N/A',
51 ]),
52 table(
53 'Model | Type | Region | Price (/1M tokens) <= 200K input tokens with Priority | Price (/1M tokens) > 200K input tokens with Priority | Price (/1M tokens) <= 200K cached input tokens with Priority | Price (/1M tokens) > 200K cached input tokens with Priority',
54 [
55 'Gemini 3.1 Pro Preview | Input (text, image, video, audio) | Global | $9.00 | $9.00 | N/A | N/A',
56 '| Text output (response and reasoning) | Global | $90.00 | $90.00 | N/A | N/A',
57 ],
58 ),
59].join('')
60​
61describe('parseVertexPricing', () => {
62 const parsed = parseVertexPricing(PRICING, NOW)
63​
64 it('keeps the global standard rate and the long-context tier', () => {
65 const rates = parsed.get(normModelName('Gemini 3.1 Pro'))
66 expect(rates?.base.input_tokens).toBe(2 / 1e6)
67 expect(rates?.base.output_tokens).toBe(12 / 1e6)
68 expect(rates?.base.cache_read_tokens).toBe(0.2 / 1e6)
69 expect(rates?.tiers).toEqual([
70 {
71 minPromptTokens: 200_000,
72 rates: {
73 input_tokens: 4 / 1e6,
74 output_tokens: 18 / 1e6,
75 cache_read_tokens: 0.4 / 1e6,
76 },
77 },
78 ])
79 const card = compileTokenCard(rates?.base ?? {}, rates?.tiers ?? [], {
80 url: 'https://cloud.google.com/gemini-enterprise-agent-platform/generative-ai/pricing',
81 hash: 'a'.repeat(64),
82 extractedAt: '2026-10-06T00:00:00.000Z',
83 })
84 expect(card && baseRates(card).input_tokens).toBe(2 / 1e6)
85 })
86​
87 it('uses the introductory row that is in effect', () => {
88 const rates = parsed.get(normModelName('Gemini 3.8 Flash'))
89 expect(rates?.base.input_tokens).toBe(0.75 / 1e6)
90 expect(rates?.base.output_tokens).toBe(3.75 / 1e6)
91 expect(rates?.expiresAt).toBe('2027-01-01T00:00:00.000Z')
92 expect(rates?.tiers).toEqual([])
93 })
94​
95 it('prices audio on its own lever', () => {
96 const rates = parsed.get(normModelName('Gemini 2.5 Flash'))
97 expect(rates?.base.audio_tokens).toBe(1 / 1e6)
98 expect(rates?.base.input_tokens).toBe(0.3 / 1e6)
99 const lite = parsed.get(normModelName('Gemini 3.1 Flash-Lite'))
100 expect(lite?.base.input_tokens).toBe(0.25 / 1e6)
101 expect(lite?.base.audio_tokens).toBe(0.5 / 1e6)
102 expect(lite?.base.output_tokens).toBe(1.5 / 1e6)
103 })
104​
105 it('drops a model whose output price is not a token rate', () => {
106 expect(parsed.has(normModelName('Gemini 3 Pro Image'))).toBe(false)
107 })
108​
109 it('ignores computer-use and priority tables', () => {
110 expect(parsed.has(normModelName('Gemini 2.5 Pro Computer Use'))).toBe(false)
111 expect(priceLevers('Image output per picture')).toBeNull()
112 })
113​
114 it('prices the image models from the table with no 200K split', () => {
115 const flat = parseVertexPricing(
116 [
117 table(
118 'Model | Type | Region | Price (/1M tokens) | Price (/1M tokens) cached input tokens',
119 [
120 'Gemini 3.1 Flash Image (Nano Banana 2) | Input (text, image, video) | Global | $0.50 | $0.05',
121 '| | Non-global | $0.55 | $0.055',
122 '| Text output (response and reasoning) | Global | $3.00 | N/A',
123 '| | Non-global | $3.30 | N/A',
124 '| Image Output | Global | $60.00 | N/A',
125 '| | Non-global | $66.00 | N/A',
126 ],
127 ),
128 table(
129 'Model | Type | Region | Price (/1M tokens) with Priority | Price (/1M tokens) cached input tokens with Priority',
130 [
131 'Gemini 3.1 Flash Image (Nano Banana 2) | Input (text, image, video) | Global | $0.90 | $0.09',
132 ],
133 ),
134 // No cached column: an open-model table, not the standard one.
135 table('Model | Type | Price (/1M tokens)', [
136 'Gemini Omni Flash | Input (text, image, video, audio) | $1.50',
137 '| Text output (response and reasoning) | $9.00',
138 ]),
139 ].join(''),
140 NOW,
141 )
142 expect(
143 flat.get(normModelName('Gemini 3.1 Flash Image (Nano Banana 2)')),
144 ).toEqual({
145 base: {
146 input_tokens: 0.5 / 1e6,
147 cache_read_tokens: 0.05 / 1e6,
148 output_tokens: 3 / 1e6,
149 image_output_tokens: 60 / 1e6,
150 },
151 tiers: [],
152 })
153 expect(flat.size).toBe(1)
154 })
155})