Open History to see the last commit
1import { readFileSync } from 'node:fs'
2import { join } from 'node:path'
3
4import { describe, expect, expectTypeOf, it } from 'vitest'
5import type { z } from 'zod'
6
7import type { RateCard } from './card.ts'
8import { RATE_CARD_LANGUAGE, evaluateCard } from './evaluator.ts'
9import {
10 ESTIMATE_CASES,
11 packageExpected,
12 settle,
13} from './fixtures/estimate-cases.ts'
14import type { rateCardSchema } from './rate-card.schema.ts'
15
16describe('evaluateCard', () => {
17 it.each(ESTIMATE_CASES.map((c) => [c.name, c] as const))('%s', (_, c) => {
18 expect(
19 settle(() =>
20 evaluateCard(c.card, { request: c.request, usage: c.usage }),
21 ),
22 ).toEqual(packageExpected(c))
23 })
24
25 it('takes no inputs for a card whose inputs all have defaults', () => {
26 const card: RateCard = {
27 inputs: { n: { param: 'n', kind: 'number', default: 2 } },
28 tables: {},
29 price: { '*': [{ var: 'n' }, 0.5] },
30 examples: [],
31 source: {
32 url: 'https://example.com/pricing',
33 hash: 'a'.repeat(64),
34 extractedAt: '2026-10-01T00:00:00Z',
35 },
36 }
37 expect(evaluateCard(card)).toEqual({
38 amount: 1,
39 currency: 'USD',
40 estimated: [],
41 })
42 })
43})
44
45describe('the evaluator entry', () => {
46 it('parses cards to exactly the zod-free RateCard type', () => {
47 expectTypeOf<z.infer<typeof rateCardSchema>>().toExtend<RateCard>()
48 expectTypeOf<RateCard>().toExtend<z.infer<typeof rateCardSchema>>()
49 })
50
51 it('is versioned with the card language: MAJOR.MINOR is RATE_CARD_LANGUAGE', () => {
52 const manifest = JSON.parse(
53 readFileSync(join(import.meta.dirname, '..', 'package.json'), 'utf8'),
54 ) as { version: string }
55 expect(manifest.version.split('.').slice(0, 2).join('.')).toBe(
56 RATE_CARD_LANGUAGE,
57 )
58 })
59})