fix(frontend): import structured models.dev pricing

This commit is contained in:
MMEXA
2026-07-11 18:12:55 +08:00
parent 3014fd50c6
commit 59d37ae1dd
4 changed files with 206 additions and 18 deletions
@@ -0,0 +1,101 @@
import { describe, expect, it } from 'vitest'
import { buildModelsDevTieredPricing } from '@/api/models-dev-pricing'
describe('buildModelsDevTieredPricing', () => {
it('maps context bands and cache prices without flattening them', () => {
expect(buildModelsDevTieredPricing({
input: 5,
output: 30,
cache_read: 0.5,
cache_write: 6.25,
tiers: [{
input: 10,
output: 45,
cache_read: 1,
cache_write: 12.5,
tier: { type: 'context', size: 272_000 },
}],
})).toEqual({
tiers: [
{
up_to: 271_999,
input_price_per_1m: 5,
output_price_per_1m: 30,
cache_creation_price_per_1m: 6.25,
cache_read_price_per_1m: 0.5,
},
{
up_to: null,
input_price_per_1m: 10,
output_price_per_1m: 45,
cache_creation_price_per_1m: 12.5,
cache_read_price_per_1m: 1,
},
],
})
})
it('sorts multiple context boundaries into contiguous Aether bands', () => {
const cost = {
input: 1,
output: 2,
tiers: [
{ input: 5, output: 6, tier: { type: 'context' as const, size: 200_000 } },
{ input: 3, output: 4, tier: { type: 'context' as const, size: 100_000 } },
],
}
expect(buildModelsDevTieredPricing(cost)?.tiers).toEqual([
{ up_to: 99_999, input_price_per_1m: 1, output_price_per_1m: 2 },
{ up_to: 199_999, input_price_per_1m: 3, output_price_per_1m: 4 },
{ up_to: null, input_price_per_1m: 5, output_price_per_1m: 6 },
])
expect(cost.tiers.map(tier => tier.tier.size)).toEqual([200_000, 100_000])
})
it('keeps flat token pricing as one unbounded band', () => {
expect(buildModelsDevTieredPricing({ input: 0, output: 0.1 })).toEqual({
tiers: [{ up_to: null, input_price_per_1m: 0, output_price_per_1m: 0.1 }],
})
})
it('omits an empty base band when context pricing starts at zero', () => {
expect(buildModelsDevTieredPricing({
input: 1,
output: 2,
tiers: [
{ input: 3, output: 4, tier: { type: 'context', size: 0 } },
{ input: 5, output: 6, tier: { type: 'context', size: 100_000 } },
],
})?.tiers).toEqual([
{ up_to: 99_999, input_price_per_1m: 3, output_price_per_1m: 4 },
{ up_to: null, input_price_per_1m: 5, output_price_per_1m: 6 },
])
})
it.each([
{ input: -1, output: 2 },
{ input: 1, output: Number.POSITIVE_INFINITY },
{
input: 1,
output: 2,
tiers: [{ input: 3, output: 4, tier: { type: 'context', size: Number.MAX_SAFE_INTEGER + 1 } }],
},
{
input: 1,
output: 2,
tiers: [{ input: 3, output: 4, tier: { type: 'context', size: -1 } }],
},
{
input: 1,
output: 2,
tiers: [
{ input: 3, output: 4, tier: { type: 'context', size: 100 } },
{ input: 5, output: 6, tier: { type: 'context', size: 100 } },
],
},
])('fails closed for malformed structured pricing', (cost) => {
expect(buildModelsDevTieredPricing(cost)).toBeNull()
})
})