Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { compileTokenCard } from '@modelschemas/rate-card'
4import type { RateCard } from '@modelschemas/rate-card'
5
6import { changedFactPaths, modelUpdatedPayload } from './change-facts.ts'
7import { updateStormOf } from './docs-failing.ts'
8
9const source = {
10 url: 'https://example.com/pricing',
11 hash: 'a'.repeat(64),
12 extractedAt: '2026-01-01T00:00:00.000Z',
13}
14
15function card(
16 rates: Record<string, number>,
17 stamp: { hash: string; extractedAt: string } = source,
18): RateCard {
19 const compiled = compileTokenCard(rates, [], { ...source, ...stamp })
20 if (!compiled) throw new Error('card did not compile')
21 return compiled
22}
23
24describe('changedFactPaths', () => {
25 it('ignores source stamps, key order, and + operand order', () => {
26 const before = card({
27 input_tokens: 1,
28 cache_read_tokens: 0.1,
29 output_tokens: 2,
30 })
31 const after = card(
32 { output_tokens: 2, input_tokens: 1, cache_read_tokens: 0.1 },
33 { hash: 'b'.repeat(64), extractedAt: '2026-10-13T00:00:00.000Z' },
34 )
35 expect(changedFactPaths({ pricing: before }, { pricing: after })).toEqual(
36 [],
37 )
38 })
39
40 it('ignores an estimate source stamp and values order', () => {
41 const hash = 'c'.repeat(64)
42 const estimate = {
43 inputs: {
44 ratio: {
45 param: 'ratio',
46 kind: 'enum' as const,
47 values: ['16:9', '1:1', '9:16'],
48 default: '16:9',
49 },
50 },
51 value: { var: 'ratio' },
52 source: { url: 'https://example.com/est', hash },
53 }
54 const before: RateCard = {
55 ...card({ input_tokens: 1, output_tokens: 2 }),
56 inputs: {
57 resolution: {
58 param: 'resolution',
59 kind: 'enum',
60 values: ['720p', '1080p'],
61 default: '720p',
62 },
63 output_tokens: {
64 param: 'output_tokens',
65 kind: 'number',
66 estimate,
67 },
68 },
69 }
70 const after: RateCard = structuredClone(before)
71 after.source = {
72 ...after.source,
73 hash: 'd'.repeat(64),
74 extractedAt: '2026-10-13T00:00:00.000Z',
75 }
76 const resolution = after.inputs.resolution
77 if (resolution?.kind === 'enum') resolution.values = ['1080p', '720p']
78 const output = after.inputs.output_tokens
79 if (output?.kind === 'number' && output.estimate) {
80 output.estimate.source = {
81 ...output.estimate.source,
82 hash: 'e'.repeat(64),
83 }
84 const ratio = output.estimate.inputs.ratio
85 if (ratio?.kind === 'enum') ratio.values = ['9:16', '16:9', '1:1']
86 }
87 const price: unknown = after.price
88 if (
89 typeof price === 'object' &&
90 price !== null &&
91 '+' in price &&
92 Array.isArray(price['+'])
93 ) {
94 price['+'].reverse()
95 }
96 expect(changedFactPaths({ pricing: before }, { pricing: after })).toEqual(
97 [],
98 )
99 const stamped = {
100 pricing: {
101 inputs: {
102 n: {
103 kind: 'number',
104 param: 'n',
105 estimate: {
106 inputs: {},
107 value: 1,
108 source: {
109 url: 'https://example.com/est',
110 hash,
111 extractedAt: '2026-01-01T00:00:00.000Z',
112 },
113 },
114 },
115 },
116 },
117 }
118 const restamped = structuredClone(stamped)
119 restamped.pricing.inputs.n.estimate.source.hash = 'e'.repeat(64)
120 restamped.pricing.inputs.n.estimate.source.extractedAt =
121 '2026-10-13T00:00:00.000Z'
122 expect(changedFactPaths(stamped, restamped)).toEqual([])
123 })
124
125 it('reports a rate, a term, and expiresAt', () => {
126 const before = card({ input_tokens: 1, output_tokens: 2 })
127 const raised = card({ input_tokens: 5, output_tokens: 2 })
128 expect(
129 changedFactPaths({ pricing: before }, { pricing: raised }),
130 ).toContain('pricing.tables.rate.base.input_tokens')
131
132 const extra = card({
133 input_tokens: 1,
134 output_tokens: 2,
135 cache_read_tokens: 0.1,
136 })
137 const added = changedFactPaths({ pricing: before }, { pricing: extra })
138 expect(added.some((path) => path.includes('cache_read_tokens'))).toBe(true)
139 const removed = changedFactPaths({ pricing: extra }, { pricing: before })
140 expect(removed.some((path) => path.includes('cache_read_tokens'))).toBe(
141 true,
142 )
143
144 const dated = structuredClone(before)
145 dated.source = { ...dated.source, expiresAt: '2026-12-01T00:00:00.000Z' }
146 expect(changedFactPaths({ pricing: before }, { pricing: dated })).toEqual([
147 'pricing.source.expiresAt',
148 ])
149 })
150
151 it('sorts paths and names a non-price fact', () => {
152 expect(
153 changedFactPaths(
154 { contextWindow: 100, displayName: 'A' },
155 { contextWindow: 200, displayName: 'B' },
156 ),
157 ).toEqual(['contextWindow', 'displayName'])
158 })
159
160 it('refuses an empty changed list', () => {
161 expect(() => modelUpdatedPayload({ a: 1 }, { a: 1 }, [])).toThrow(
162 /changed fact/,
163 )
164 expect(modelUpdatedPayload({ a: 1 }, { a: 2 }, ['a'])).toEqual({
165 before: { a: 1 },
166 after: { a: 2 },
167 changed: ['a'],
168 })
169 })
170})
171
172describe('updateStormOf', () => {
173 it('marks more than half of the live rows, at least 20', () => {
174 expect(updateStormOf(21, 40)).toEqual({ updated: 21, live: 40 })
175 expect(updateStormOf(20, 40)).toBeNull()
176 expect(updateStormOf(19, 19)).toBeNull()
177 expect(updateStormOf(20, 20)).toEqual({ updated: 20, live: 20 })
178 })
179})