Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { afterEach, describe, expect, it, vi } from 'vitest'
3import { price } from '@modelschemas/rate-card'
4import {
5 claudeCardModels,
6 claudeCardPaths,
7 claudeGlobalPrices,
8 VERTEX_CLAUDE_PRICING_URL,
9 VERTEX_CLAUDE_URL,
10 vertexClaudeModels,
11} from './vertex-claude.ts'
12import { normModelName } from './vertex-text.ts'
13
14const fixture = (name: string) =>
15 readFileSync(
16 new URL(`./fixtures/vertex-claude/${name}.html.txt`, import.meta.url),
17 'utf8',
18 )
19const card = fixture('sonnet-4-6')
20const prices = fixture('global-pricing')
21const source = {
22 url: 'https://cloud.google.com/pricing',
23 hash: 'fixture-hash',
24 extractedAt: '2026-10-09',
25}
26
27describe('Google native Claude sources', () => {
28 it('reads exact hosted model identity and published facts without a body-schema guess', () => {
29 const [row] = claudeCardModels(
30 card,
31 'https://docs.cloud.google.com/card',
32 'hash',
33 )
34 expect(row).toMatchObject({
35 rawId: 'claude-sonnet-4-6',
36 contextWindow: 1_000_000,
37 maxOutput: 128_000,
38 modalities: { input: ['text', 'image', 'pdf'], output: ['text'] },
39 capabilities: ['tools', 'reasoning'],
40 reasoning: null,
41 requestMap: null,
42 schemaEndpointId: null,
43 })
44 expect(row?.factSources?.maxOutput?.sourceHash).toBe('hash')
45 })
46 it('reads the model name from a card title that leads with the vendor', () => {
47 const [row] = claudeCardModels(
48 card.replace(
49 'Claude Sonnet 4.6 on Google Cloud<devsite-actions',
50 'Anthropic's Claude Sonnet 4.6 on Google Cloud<devsite-actions',
51 ),
52 'https://docs.cloud.google.com/card',
53 'hash',
54 )
55 expect(row?.displayName).toBe('Claude Sonnet 4.6')
56 // vertexClaudeModels joins cards to prices on this key.
57 const key = normModelName(
58 (row?.displayName ?? '').replace(/^Claude\s+/i, ''),
59 )
60 expect(claudeGlobalPrices(prices, source).get(key)).toBeDefined()
61 })
62 it('does not turn unreadable identity or token limits into a successful empty listing', () => {
63 expect(() =>
64 claudeCardModels(
65 card.replaceAll('claude-sonnet-4-6', 'unknown'),
66 source.url,
67 'hash',
68 ),
69 ).toThrow('unreadable model id')
70 expect(() =>
71 claudeCardModels(
72 card.replace(
73 'Maximum output tokens: 128,000',
74 'Maximum output tokens: unknown',
75 ),
76 source.url,
77 'hash',
78 ),
79 ).toThrow('unreadable Maximum output tokens')
80 expect(() => claudeCardPaths('<article>No models</article>')).toThrow(
81 'no linked model cards',
82 )
83 })
84 it('uses the explicitly Global rates with separately published prompt-length tiers', () => {
85 const book = claudeGlobalPrices(prices, source)
86 const sonnet = book.get('sonnet 4 6')
87 expect(sonnet).toBeDefined()
88 expect(
89 price(sonnet!, {}, { input_tokens: 1_000_000, output_tokens: 1_000_000 }),
90 ).toBe(18)
91 const haiku = book.get('haiku 5 5')
92 expect(haiku).toBeDefined()
93 expect(
94 price(haiku!, {}, { input_tokens: 50_000, output_tokens: 1_000_000 }),
95 ).toBeCloseTo(0.505)
96 expect(
97 price(haiku!, {}, { input_tokens: 200_000, output_tokens: 1_000_000 }),
98 ).toBeCloseTo(2.6)
99 })
100 it('rejects malformed published prices and missing Global panels', () => {
101 expect(() =>
102 claudeGlobalPrices(prices.replace('$0.10', '$unknown'), source),
103 ).toThrow('unreadable price')
104 expect(() =>
105 claudeGlobalPrices(prices.replace(/>Global</g, '>Regional<'), source),
106 ).toThrow('no Global Claude price rows')
107 })
108 it('does not read a regional table after an empty Global panel', () => {
109 const table = prices.match(/<table\b[\s\S]*?<\/table>/i)?.[0] ?? ''
110 const emptyGlobal = prices.replace(table, '')
111 expect(() =>
112 claudeGlobalPrices(
113 `${emptyGlobal}<div role="tabpanel" aria-labelledby="regional">${table}</div>`,
114 source,
115 ),
116 ).toThrow('no Global Claude price rows')
117 })
118 it('leaves an incomplete published long tier unpriced instead of copying short rates', () => {
119 // The native Global table leaves Opus 5.5's long input cell blank while
120 // publishing other long-context prices. It supplies no complete card.
121 expect(claudeGlobalPrices(prices, source).has('opus 5 5')).toBe(false)
122 })
123})
124
125describe('vertexClaudeModels cache', () => {
126 afterEach(() => vi.unstubAllGlobals())
127
128 it('keeps the pages it read when a later page answers 429 (#516)', async () => {
129 const stored = new Map<string, string>()
130 const kv = {
131 get: async (key: string) => stored.get(key) ?? null,
132 put: async (key: string, value: string) => {
133 stored.set(key, value)
134 },
135 } as unknown as KVNamespace
136 const cardUrl = `${VERTEX_CLAUDE_URL}/sonnet-4-6`
137 let pricingStatus = 429
138 const fetched: string[] = []
139 vi.stubGlobal('fetch', async (url: string) => {
140 fetched.push(url)
141 if (url === VERTEX_CLAUDE_URL)
142 return new Response(
143 `<article><a href="${new URL(cardUrl).pathname}">Sonnet</a></article>`,
144 )
145 if (url === cardUrl) return new Response(card)
146 if (url === VERTEX_CLAUDE_PRICING_URL)
147 return new Response(pricingStatus === 200 ? prices : 'slow down', {
148 status: pricingStatus,
149 })
150 throw new Error(`unexpected fetch ${url}`)
151 })
152
153 await expect(vertexClaudeModels(kv)).rejects.toThrow('429')
154 pricingStatus = 200
155 const rows = await vertexClaudeModels(kv)
156
157 expect(fetched).toEqual([
158 VERTEX_CLAUDE_URL,
159 cardUrl,
160 VERTEX_CLAUDE_PRICING_URL,
161 VERTEX_CLAUDE_PRICING_URL,
162 ])
163 expect(rows[0]?.rawId).toBe('claude-sonnet-4-6')
164 expect(rows[0]?.pricing).not.toBeNull()
165 })
166})