Open History to see the last commit
1import { beforeAll, describe, expect, it, vi } from 'vitest'
2import { env } from 'cloudflare:test'
3
4import { compileTokenCard, evaluateCard } from '@modelschemas/rate-card'
5import type { RateCard } from '@modelschemas/rate-card'
6
7import { ESTIMATE_CASES } from '../../packages/rate-card/src/fixtures/estimate-cases.ts'
8import { GPT_4O } from '../../packages/rate-card/src/fixtures/gpt-4o.ts'
9import { getDb } from '../db/index.ts'
10import type { Db } from '../db/index.ts'
11import { models, providers } from '../db/schema.ts'
12import { estimateCost, parseEstimateBody } from './estimate.ts'
13import type { EstimateResult } from './estimate.ts'
14import { anthropicModelPricing } from './providers/anthropic-pricing.ts'
15import {
16 compilePeriodCard,
17 compileSeedanceCard,
18} from './providers/byteplus-pricing.ts'
19import { ANTHROPIC_PRICING_PAGE } from './providers/fixtures/anthropic-pricing.ts'
20import { parseStoredRateCard } from './rate-card.ts'
21
22const NOW = 1_781_150_000
23let db: Db
24
25beforeAll(async () => {
26 db = getDb(env)
27 await db.insert(providers).values({
28 id: 'est-openai',
29 displayName: 'Estimate OpenAI',
30 specSourceUrl: 'https://example.com/o.json',
31 })
32 await db.insert(models).values([
33 {
34 id: 'est-openai-gpt-4o',
35 providerId: 'est-openai',
36 rawId: 'gpt-4o',
37 activity: 'chat',
38 displayName: 'GPT-4o',
39 pricing: GPT_4O,
40 firstSeenAt: NOW,
41 lastSeenAt: NOW,
42 },
43 {
44 id: 'est-openai-kimi-k3',
45 providerId: 'est-openai',
46 rawId: 'kimi-k3',
47 activity: 'chat',
48 pricing: compileTokenCard(
49 { input_tokens: 20 / 1e6, output_tokens: 100 / 1e6 },
50 [],
51 GPT_4O.source,
52 { currency: 'CNY' },
53 ),
54 firstSeenAt: NOW,
55 lastSeenAt: NOW,
56 },
57 {
58 id: 'est-openai-gpt-5',
59 providerId: 'est-openai',
60 rawId: 'gpt-5',
61 activity: 'chat',
62 pricing: compileTokenCard(
63 {
64 input_tokens: 1.25 / 1e6,
65 cache_read_tokens: 0.125 / 1e6,
66 output_tokens: 10 / 1e6,
67 },
68 [],
69 GPT_4O.source,
70 ),
71 firstSeenAt: NOW,
72 lastSeenAt: NOW,
73 },
74 {
75 id: 'est-openai-free',
76 providerId: 'est-openai',
77 rawId: 'free',
78 activity: 'chat',
79 firstSeenAt: NOW,
80 lastSeenAt: NOW,
81 },
82 ])
83 await db.insert(providers).values({
84 id: 'est-byteplus',
85 displayName: 'Estimate BytePlus',
86 specSourceUrl: 'https://example.com/b.json',
87 })
88 await db.insert(models).values({
89 id: 'est-byteplus-seedance',
90 providerId: 'est-byteplus',
91 rawId: 'seedance-1-0-pro-250528',
92 activity: 'video',
93 pricing: compileSeedanceCard(
94 { default: { '*': { all: 2.5 } } },
95 GPT_4O.source,
96 {
97 model: { fps: 24, dims: { '1080p': { '16:9': { w: 1920, h: 1088 } } } },
98 url: 'https://example.com/guide',
99 hash: 'b'.repeat(64),
100 },
101 ),
102 firstSeenAt: NOW,
103 lastSeenAt: NOW,
104 })
105 await db.insert(providers).values({
106 id: 'est-deepseek',
107 displayName: 'Estimate DeepSeek',
108 specSourceUrl: 'https://example.com/deepseek.json',
109 })
110 await db.insert(models).values({
111 id: 'est-deepseek-flash',
112 providerId: 'est-deepseek',
113 rawId: 'deepseek-flash',
114 activity: 'chat',
115 pricing: compilePeriodCard(
116 {
117 periods: {
118 off_peak: {
119 input_tokens: 0.15e-6,
120 cache_read_tokens: 0.003e-6,
121 output_tokens: 0.6e-6,
122 },
123 peak: {
124 input_tokens: 0.3e-6,
125 cache_read_tokens: 0.006e-6,
126 output_tokens: 1.2e-6,
127 },
128 },
129 },
130 GPT_4O.source,
131 ),
132 firstSeenAt: NOW,
133 lastSeenAt: NOW,
134 })
135 await db.insert(providers).values({
136 id: 'est-anthropic',
137 displayName: 'Estimate Anthropic',
138 specSourceUrl: 'https://example.com/anthropic.json',
139 })
140 vi.stubGlobal(
141 'fetch',
142 vi.fn().mockResolvedValue(new Response(ANTHROPIC_PRICING_PAGE)),
143 )
144 try {
145 const pricingFor = await anthropicModelPricing()
146 for (const [rawId, displayName] of [
147 ['claude-opus-5-5', 'Claude Opus 5.5'],
148 ['claude-fable-5-1', 'Claude Fable 5.1'],
149 ] as const) {
150 const pricing = parseStoredRateCard(pricingFor(displayName).pricing)
151 expect(pricing?.inputs.cache_read_tokens).toBeDefined()
152 await db.insert(models).values({
153 id: `est-anthropic-${rawId}`,
154 providerId: 'est-anthropic',
155 rawId,
156 displayName,
157 activity: 'chat',
158 pricing,
159 firstSeenAt: NOW,
160 lastSeenAt: NOW,
161 })
162 }
163 } finally {
164 vi.unstubAllGlobals()
165 }
166})
167
168describe('parseEstimateBody', () => {
169 it('requires provider and model, and objects for request/usage', () => {
170 expect(parseEstimateBody(null)).toBeNull()
171 expect(parseEstimateBody({ provider: 'openai' })).toBeNull()
172 expect(
173 parseEstimateBody({ provider: 'openai', model: 'gpt-4o', request: [] }),
174 ).toBeNull()
175 expect(
176 parseEstimateBody({
177 provider: 'openai',
178 model: 'gpt-4o',
179 usage: { input_tokens: 1 },
180 }),
181 ).toEqual({
182 provider: 'openai',
183 model: 'gpt-4o',
184 usage: { input_tokens: 1 },
185 })
186 })
187})
188
189describe('estimateCost', () => {
190 it.each([
191 ['claude-opus-5-5', 0.2],
192 ['claude-fable-5-1', 0.25],
193 ])(
194 'prices cache reads for %s from docs-derived cards',
195 async (model, usd) => {
196 const outcome = await estimateCost(db, {
197 provider: 'est-anthropic',
198 model,
199 usage: {
200 input_tokens: 0,
201 output_tokens: 0,
202 cache_read_tokens: 1_000_000,
203 },
204 })
205 expect(outcome).toMatchObject({ ok: true, result: { usd } })
206 },
207 )
208
209 it.each([
210 [
211 'Responses',
212 {
213 input_tokens: 12_000,
214 input_tokens_details: { cached_tokens: 4000 },
215 output_tokens: 800,
216 output_tokens_details: { reasoning_tokens: 0 },
217 total_tokens: 12_800,
218 },
219 ],
220 [
221 'Chat',
222 {
223 prompt_tokens: 12_000,
224 prompt_tokens_details: { cached_tokens: 4000, audio_tokens: 0 },
225 completion_tokens: 800,
226 total_tokens: 12_800,
227 },
228 ],
229 ])(
230 'prices native OpenAI %s usage with cached tokens counted once',
231 async (_, usage) => {
232 const outcome = await estimateCost(db, {
233 provider: 'est-openai',
234 model: 'gpt-5',
235 usage,
236 })
237 // 8000 × 1.25/M + 4000 × 0.125/M + 800 × 10/M, not 0.0235.
238 expect(outcome).toMatchObject({ ok: true, result: { defaulted: [] } })
239 if (outcome.ok && 'amount' in outcome.result) {
240 expect(outcome.result.amount).toBeCloseTo(0.0185, 10)
241 }
242 },
243 )
244
245 it.each([
246 ['claude-opus-5-5', 0.2],
247 ['claude-fable-5-1', 0.25],
248 ])('prices native Anthropic cache reads for %s', async (model, usd) => {
249 const outcome = await estimateCost(db, {
250 provider: 'est-anthropic',
251 model,
252 usage: {
253 input_tokens: 0,
254 cache_creation_input_tokens: 0,
255 cache_read_input_tokens: 1_000_000,
256 output_tokens: 0,
257 service_tier: 'standard',
258 },
259 })
260 expect(outcome).toMatchObject({ ok: true, result: { usd } })
261 })
262
263 it('returns 422 mixed_usage for native fields beside card levers', async () => {
264 const outcome = await estimateCost(db, {
265 provider: 'est-openai',
266 model: 'gpt-5',
267 usage: {
268 input_tokens: 12_000,
269 input_tokens_details: { cached_tokens: 4000 },
270 output_tokens: 800,
271 cache_read_tokens: 4000,
272 },
273 })
274 expect(outcome).toMatchObject({
275 ok: false,
276 status: 422,
277 code: 'mixed_usage',
278 })
279 })
280
281 it('prices a stored token card', async () => {
282 const outcome = await estimateCost(db, {
283 provider: 'est-openai',
284 model: 'gpt-4o',
285 request: { model: 'gpt-4o', messages: [], max_tokens: 400 },
286 usage: { input_tokens: 1200, output_tokens: 400 },
287 })
288 expect(outcome).toEqual({
289 ok: true,
290 result: {
291 amount: 0.007,
292 currency: 'USD',
293 usd: 0.007,
294 cardSource: GPT_4O.source,
295 estimated: [],
296 defaulted: ['cache_read_tokens'],
297 ignored: ['model', 'messages', 'max_tokens'],
298 },
299 })
300 })
301
302 it('prices a yuan card in CNY, with no usd', async () => {
303 const outcome = await estimateCost(db, {
304 provider: 'est-openai',
305 model: 'kimi-k3',
306 usage: { input_tokens: 1_000_000, output_tokens: 500_000 },
307 })
308 // ¥20 in + ¥100 out per 1M: 20 + 50. Not converted.
309 expect(outcome).toEqual({
310 ok: true,
311 result: {
312 amount: 70,
313 currency: 'CNY',
314 cardSource: GPT_4O.source,
315 estimated: [],
316 defaulted: [],
317 ignored: [],
318 },
319 })
320 })
321
322 it('returns 422 unknown_usage_key for a usage key the card does not read', async () => {
323 const outcome = await estimateCost(db, {
324 provider: 'est-anthropic',
325 model: 'claude-opus-5-5',
326 usage: {
327 input_tokens: 12_000,
328 output_tokens: 800,
329 cache_hits: 4000,
330 },
331 })
332 expect(outcome).toMatchObject({
333 ok: false,
334 status: 422,
335 code: 'unknown_usage_key',
336 })
337 if (!outcome.ok) {
338 expect(outcome.message).toContain(
339 'usage.cache_hits: not read by this card; it reads',
340 )
341 expect(outcome.message).toContain('usage.cache_read_tokens')
342 expect(outcome.message).toContain('usage.input_tokens')
343 }
344 })
345
346 it('returns 404 unknown_model when the model is missing', async () => {
347 const outcome = await estimateCost(db, {
348 provider: 'est-openai',
349 model: 'nope',
350 })
351 expect(outcome.ok).toBe(false)
352 if (!outcome.ok) {
353 expect(outcome.status).toBe(404)
354 expect(outcome.code).toBe('unknown_model')
355 }
356 })
357
358 it('returns 404 unknown_pricing when the model has no card', async () => {
359 const outcome = await estimateCost(db, {
360 provider: 'est-openai',
361 model: 'free',
362 })
363 expect(outcome.ok).toBe(false)
364 if (!outcome.ok) {
365 expect(outcome.status).toBe(404)
366 expect(outcome.code).toBe('unknown_pricing')
367 expect(outcome.message).toContain('GET /v1/models/est-openai/free')
368 }
369 })
370
371 it('prices both periods when a DeepSeek estimate omits pricing_period', async () => {
372 const outcome = await estimateCost(db, {
373 provider: 'est-deepseek',
374 model: 'deepseek-flash',
375 usage: { input_tokens: 1_000_000, output_tokens: 0 },
376 })
377 if (!outcome.ok || !('byInput' in outcome.result)) {
378 throw new Error(JSON.stringify(outcome))
379 }
380 expect(outcome.result.byInput.input).toBe('pricing_period')
381 expect(outcome.result.byInput.values.off_peak?.amount).toBeCloseTo(0.15, 9)
382 expect(outcome.result.byInput.values.peak?.amount).toBeCloseTo(0.3, 9)
383 })
384
385 it('refuses a DeepSeek estimate with an unknown pricing_period', async () => {
386 const outcome = await estimateCost(db, {
387 provider: 'est-deepseek',
388 model: 'deepseek-flash',
389 usage: {
390 input_tokens: 1_000_000,
391 output_tokens: 0,
392 pricing_period: 'night',
393 },
394 })
395 expect(outcome.ok).toBe(false)
396 if (!outcome.ok) {
397 expect(outcome.message).toContain('pricing_period')
398 expect(outcome.message).toContain('night')
399 }
400 })
401
402 it('returns 422 unbound_input naming the missing lever', async () => {
403 const outcome = await estimateCost(db, {
404 provider: 'est-openai',
405 model: 'gpt-4o',
406 })
407 expect(outcome.ok).toBe(false)
408 if (!outcome.ok) {
409 expect(outcome.status).toBe(422)
410 expect(outcome.code).toBe('unbound_input')
411 expect(outcome.message).toContain('usage.input_tokens')
412 }
413 })
414
415 it('labels an estimated price and names both ways to supply it', async () => {
416 const body = { provider: 'est-byteplus', model: 'seedance-1-0-pro-250528' }
417 // Seedance 1.0 Pro usage table: 1080p 16:9 10 s = 489600 tokens.
418 const estimated = await estimateCost(db, {
419 ...body,
420 request: { resolution: '1080p', ratio: '16:9', duration: 10 },
421 })
422 expect(estimated).toMatchObject({
423 ok: true,
424 result: { usd: 1.224, estimated: ['completion_tokens'] },
425 })
426 const billed = await estimateCost(db, {
427 ...body,
428 usage: { completion_tokens: 489_600 },
429 })
430 expect(billed).toMatchObject({
431 ok: true,
432 result: { usd: 1.224, estimated: [] },
433 })
434 const neither = await estimateCost(db, {
435 ...body,
436 request: { resolution: '1080p', duration: 10 },
437 })
438 expect(neither).toMatchObject({ ok: false, code: 'unbound_input' })
439 if (!neither.ok) {
440 expect(neither.message).toContain('requires usage.completion_tokens')
441 expect(neither.message).toContain('ratio (ratio): required')
442 }
443 // A draft's tokens are not what the formula gives: no estimate.
444 const draft = await estimateCost(db, {
445 ...body,
446 request: {
447 resolution: '1080p',
448 ratio: '16:9',
449 duration: 10,
450 draft: true,
451 },
452 })
453 expect(draft).toMatchObject({ ok: false, code: 'unbound_input' })
454 })
455})
456
457// The shared cases (packages/rate-card/src/fixtures/estimate-cases.ts) run
458// the route's path: JSON body → parseEstimateBody → estimateCost → JSON.
459// Each must match evaluateCard on amount, currency, and estimated.
460describe('POST /v1/estimate conformance with evaluateCard', () => {
461 beforeAll(async () => {
462 await db.insert(providers).values({
463 id: 'conformance',
464 displayName: 'Conformance',
465 specSourceUrl: 'https://example.com/c.json',
466 })
467 await db.insert(models).values(
468 ESTIMATE_CASES.map((c, i) => ({
469 id: `conformance-case-${i}`,
470 providerId: 'conformance',
471 rawId: `case-${i}`,
472 activity: 'chat' as const,
473 pricing: c.card,
474 firstSeenAt: NOW,
475 lastSeenAt: NOW,
476 })),
477 )
478 })
479
480 it.each(ESTIMATE_CASES.map((c, i) => [c.name, c, i] as const))(
481 '%s',
482 async (_, c, i) => {
483 const body = parseEstimateBody(
484 JSON.parse(
485 JSON.stringify({
486 provider: 'conformance',
487 model: `case-${i}`,
488 request: c.request,
489 usage: c.usage,
490 }),
491 ),
492 )
493 if (!body) throw new Error('body did not parse')
494 const outcome = await estimateCost(db, body)
495 if ('error' in c.expected) {
496 expect(outcome).toMatchObject({
497 ok: false,
498 status: 422,
499 code: c.expected.apiError,
500 })
501 return
502 }
503 if (!outcome.ok) throw new Error(`${outcome.code}: ${outcome.message}`)
504 if (!('amount' in outcome.result)) throw new Error('unexpected range')
505 const { amount, currency, estimated } = JSON.parse(
506 JSON.stringify(outcome.result),
507 ) as EstimateResult
508 expect({ amount, currency, estimated }).toEqual(
509 evaluateCard(c.card, { request: c.request, usage: c.usage }),
510 )
511 },
512 )
513})
514
515describe('estimateCost on a routed id (#406)', () => {
516 const card = (input: number, output: number) =>
517 compileTokenCard(
518 { input_tokens: input / 1e6, output_tokens: output / 1e6 },
519 [],
520 GPT_4O.source,
521 )
522 const range = (routes: Array<string>) => ({
523 currency: 'USD',
524 routes: routes.map((model) => ({
525 model,
526 pricing: { currency: 'USD', per: 'token' as const },
527 })),
528 inputPerMillion: { min: 0, max: 0 },
529 })
530 const row = (
531 rawId: string,
532 extra: Partial<typeof models.$inferInsert> = {},
533 ): typeof models.$inferInsert => ({
534 id: `est-gw-${rawId.replaceAll('/', '-')}`,
535 providerId: 'est-gw',
536 rawId,
537 activity: 'chat',
538 firstSeenAt: NOW,
539 lastSeenAt: NOW,
540 ...extra,
541 })
542
543 beforeAll(async () => {
544 await db.insert(providers).values({
545 id: 'est-gw',
546 displayName: 'Estimate gateway',
547 specSourceUrl: 'https://example.com/g.json',
548 })
549 await db
550 .insert(models)
551 .values([
552 row('oss', { pricingRange: range(['a/oss', 'b/oss', 'c/oss']) }),
553 row('a/oss', { pricing: card(0.15, 0.75) }),
554 row('b/oss', { pricing: card(0.35, 0.6) }),
555 row('c/oss', { pricing: card(0.05, 2) }),
556 row('gap', { pricingRange: range(['a/gap', 'b/gap']) }),
557 row('a/gap', { pricing: card(1, 1) }),
558 row('b/gap'),
559 ])
560 })
561
562 it('returns min and max over every route, naming each', async () => {
563 const outcome = await estimateCost(db, {
564 provider: 'est-gw',
565 model: 'oss',
566 usage: { input_tokens: 1_000_000, output_tokens: 100_000 },
567 })
568 if (!outcome.ok || !('min' in outcome.result)) {
569 throw new Error(JSON.stringify(outcome))
570 }
571 const { min, max, routes, currency } = outcome.result
572 expect(currency).toBe('USD')
573 expect(routes).toEqual(['a/oss', 'b/oss', 'c/oss'])
574 // a: 0.15 + 0.075, b: 0.35 + 0.06, c: 0.05 + 0.2
575 expect(min.model).toBe('a/oss')
576 expect(min.amount).toBeCloseTo(0.225, 10)
577 expect(max.model).toBe('b/oss')
578 expect(max.amount).toBeCloseTo(0.41, 10)
579 })
580
581 it('prices inline route cards and names each end by provider (#451)', async () => {
582 await db.insert(models).values(
583 row('qwen', {
584 pricingRange: {
585 currency: 'USD',
586 routes: [
587 {
588 model: 'deepinfra',
589 provider: 'deepinfra',
590 card: { ...(card(0.09, 0.55) as RateCard), examples: [] as [] },
591 pricing: { currency: 'USD', per: 'token' },
592 },
593 {
594 model: 'vertex',
595 provider: 'vertex',
596 card: { ...(card(0.22, 0.88) as RateCard), examples: [] as [] },
597 pricing: { currency: 'USD', per: 'token' },
598 },
599 ],
600 inputPerMillion: { min: 0.09, max: 0.22 },
601 },
602 }),
603 )
604 const outcome = await estimateCost(db, {
605 provider: 'est-gw',
606 model: 'qwen',
607 usage: { input_tokens: 1_000_000, output_tokens: 0 },
608 })
609 if (!outcome.ok || !('min' in outcome.result)) {
610 throw new Error(JSON.stringify(outcome))
611 }
612 expect(outcome.result.routes).toEqual(['deepinfra', 'vertex'])
613 expect(outcome.result.min.model).toBe('deepinfra')
614 expect(outcome.result.min.amount).toBeCloseTo(0.09, 10)
615 expect(outcome.result.max.model).toBe('vertex')
616 expect(outcome.result.max.amount).toBeCloseTo(0.22, 10)
617 })
618
619 it('refuses when a route has no card', async () => {
620 const outcome = await estimateCost(db, {
621 provider: 'est-gw',
622 model: 'gap',
623 usage: { input_tokens: 1, output_tokens: 1 },
624 })
625 expect(outcome).toMatchObject({
626 ok: false,
627 status: 404,
628 code: 'unknown_pricing',
629 })
630 if (!outcome.ok) expect(outcome.message).toContain('est-gw/b/gap')
631 })
632})
633
634describe('estimateCost with one required choice missing (#450)', () => {
635 const periods = {
636 off_peak: { input_tokens: 0.3 / 1e6, output_tokens: 1.2 / 1e6 },
637 peak: { input_tokens: 0.6 / 1e6, output_tokens: 2.4 / 1e6 },
638 }
639 const usage = { input_tokens: 1_000_000, output_tokens: 1_000_000 }
640 const row = (rawId: string, pricing: RateCard) => ({
641 id: `est-period-${rawId}`,
642 providerId: 'est-period',
643 rawId,
644 activity: 'chat' as const,
645 pricing,
646 firstSeenAt: NOW,
647 lastSeenAt: NOW,
648 })
649
650 beforeAll(async () => {
651 const period = compilePeriodCard({ periods }, GPT_4O.source)
652 await db.insert(providers).values({
653 id: 'est-period',
654 displayName: 'Estimate period',
655 specSourceUrl: 'https://example.com/p.json',
656 })
657 await db.insert(models).values([
658 row('deepseek', period),
659 row('two-choices', {
660 ...period,
661 inputs: {
662 ...period.inputs,
663 input_video: { param: 'input_video', kind: 'boolean' },
664 },
665 }),
666 // A cache-read rate off-peak only: peak cannot price the call.
667 row(
668 'peak-gap',
669 compilePeriodCard(
670 {
671 periods: {
672 off_peak: { ...periods.off_peak, cache_read_tokens: 0.1 / 1e6 },
673 peak: periods.peak,
674 },
675 },
676 GPT_4O.source,
677 ),
678 ),
679 ])
680 })
681
682 const estimate = (model: string, u: Record<string, unknown>) =>
683 estimateCost(db, { provider: 'est-period', model, usage: u })
684
685 it('prices each value when pricing_period is missing', async () => {
686 const outcome = await estimate('deepseek', usage)
687 if (!outcome.ok || !('byInput' in outcome.result)) {
688 throw new Error(JSON.stringify(outcome))
689 }
690 const { currency, byInput } = outcome.result
691 expect(currency).toBe('USD')
692 expect(byInput.input).toBe('pricing_period')
693 expect(Object.keys(byInput.values)).toEqual(['off_peak', 'peak'])
694 expect(byInput.values.off_peak?.amount).toBeCloseTo(1.5, 10)
695 expect(byInput.values.peak?.amount).toBeCloseTo(3, 10)
696 expect(byInput.values.peak).toMatchObject({
697 estimated: [],
698 ignored: [],
699 cardSource: GPT_4O.source,
700 })
701 })
702
703 it('returns one result when pricing_period is supplied', async () => {
704 const outcome = await estimate('deepseek', {
705 ...usage,
706 pricing_period: 'peak',
707 })
708 if (!outcome.ok || !('amount' in outcome.result)) {
709 throw new Error(JSON.stringify(outcome))
710 }
711 expect(outcome.result.amount).toBeCloseTo(3, 10)
712 })
713
714 it('refuses two missing choices, naming both and their values', async () => {
715 const outcome = await estimate('two-choices', usage)
716 expect(outcome).toMatchObject({ ok: false, code: 'unbound_input' })
717 if (!outcome.ok) {
718 expect(outcome.message).toContain(
719 'usage.pricing_period (off_peak | peak)',
720 )
721 expect(outcome.message).toContain('request.input_video (true | false)')
722 }
723 })
724
725 it('refuses a missing quantity even with a choice missing', async () => {
726 const outcome = await estimate('deepseek', { input_tokens: 1 })
727 expect(outcome).toMatchObject({ ok: false, code: 'unbound_input' })
728 if (!outcome.ok) expect(outcome.message).toContain('usage.output_tokens')
729 })
730
731 it('refuses the whole estimate when one value cannot be priced', async () => {
732 const outcome = await estimate('peak-gap', {
733 ...usage,
734 cache_read_tokens: 1000,
735 })
736 expect(outcome).toMatchObject({ ok: false, code: 'missing_key' })
737 if (!outcome.ok) {
738 expect(outcome.message).toContain('usage.pricing_period = "peak"')
739 }
740 })
741})