package metered import ( "math" "os" "testing" "amadan.net/oficina/equipo/internal/wire" "amadan.net/rastrillo/rastrillo/budget" ) func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } func n(v int64) *int64 { return &v } // TestPricesNormalisedComponents is ยง2.3's cost rule: components priced // in integer micro-units, each part rounded up; the larger of the priced // and the reported cost; an unpriced model, an unpriceable component, an // incomplete row or an overflow is unknown. func TestPricesNormalisedComponents(t *testing.T) { table := Tables[Current[wire.ProviderTensorX]] row := wire.Usage{Provider: wire.ProviderTensorX, Model: "z-ai/glm-5.3", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: n(1_000_000), CachedInputTokens: n(500_000), CacheWriteTokens: n(0), OutputTokens: n(100_000), ReasoningOutputTokens: n(40_000)} got, ok := table.Price(row) // 1.75 + 0.5 * 0.44 + 0.1 * 4.50 = 2.42; reasoning is part of output. if !ok || got.CostMicros == nil || *got.CostMicros != 2_420_000 || got.Currency != "USD" || got.PriceVersion != "tensorx-2026-11-12" { t.Fatalf("priced %+v %v", got, ok) } reported := row reported.CostReportedMicros = n(3_000_000) if got, _ := table.Price(reported); *got.CostMicros != 3_000_000 || *got.CostReportedMicros != 3_000_000 { t.Fatalf("the reported cost was larger: %d", *got.CostMicros) } lower := row lower.CostReportedMicros = n(1) if got, _ := table.Price(lower); *got.CostMicros != 2_420_000 { t.Fatalf("the priced cost was larger: %d", *got.CostMicros) } one := row one.InputTokens, one.CachedInputTokens, one.OutputTokens = n(1), n(0), n(1) if got, _ := table.Price(one); *got.CostMicros != 2+5 { t.Fatalf("parts round up: %d", *got.CostMicros) } for name, bad := range map[string]func(*wire.Usage){ "unpriced model": func(u *wire.Usage) { u.Model = "z-ai/glm-9" }, "cache write": func(u *wire.Usage) { u.CacheWriteTokens = n(10) }, "incomplete": func(u *wire.Usage) { u.Complete = false }, "no input figure": func(u *wire.Usage) { u.InputTokens = nil }, "no output figure": func(u *wire.Usage) { u.OutputTokens = nil }, "negative": func(u *wire.Usage) { u.OutputTokens = n(-1) }, "overflowing input": func(u *wire.Usage) { u.InputTokens = n(math.MaxInt64) }, } { u := row bad(&u) if got, ok := table.Price(u); ok || got.CostMicros != nil { t.Fatalf("%s: priced %v", name, got.CostMicros) } } separate := Price{UncachedInput: n(1_000_000), Output: n(2_000_000), ReasoningSeparate: true} if c, ok := Normalise(row, separate); !ok || c.Reasoning != 40_000 { t.Fatalf("separate reasoning %+v", c) } if _, ok := Cost(Components{Reasoning: 1}, separate); ok { t.Fatal("reasoning priced with no price") } codexLike := Price{UncachedInput: n(1), CachedInput: n(1), Output: n(1), InputIncludesCached: true} if c, _ := Normalise(row, codexLike); c.UncachedInput != 500_000 { t.Fatalf("cached counted twice: %+v", c) } for _, tb := range Tables { for model, p := range tb.Models { if p.UncachedInput == nil || p.Output == nil || *p.UncachedInput <= 0 || *p.Output <= 0 { t.Fatalf("%s %s has no input or output price", tb.Version, model) } } } } // TestOpenCodeGoPrices: OpenCode Go's table is the Go page's per-model // prices read on 2026-11-12, chat-completions models only (the OpenCode // harness speaks chat completions to Go), cache writes priced only where // Go states a price, DeepSeek charged at its peak price (Equipo cannot // tell peak from off-peak), and the free models unpriced (spend // unknown). Reasoning is part of output. func TestOpenCodeGoPrices(t *testing.T) { version := Current[wire.ProviderOpenCodeGo] table, ok := TableFor(version) if !ok || version != "opencode-go-2026-11-12" || table.Provider != wire.ProviderOpenCodeGo || table.Currency != "USD" { t.Fatalf("table %q %+v", version, table) } price := func(model string, in, cached, write, out int64) (int64, bool) { got, ok := table.Price(wire.Usage{Provider: wire.ProviderOpenCodeGo, Model: model, Complete: true, InputTokens: n(in), CachedInputTokens: n(cached), CacheWriteTokens: n(write), OutputTokens: n(out), ReasoningOutputTokens: n(out / 2)}) if !ok { return 0, false } return *got.CostMicros, true } for _, c := range []struct { model string in, cached, write, out int64 want int64 }{ {"glm-5.3", 1_000_000, 0, 0, 1_000_000, 1_400_000 + 4_400_000}, {"glm-5.3", 0, 1_000_000, 0, 0, 260_000}, {"glm-5.3-flash", 1_000_000, 1_000_000, 0, 1_000_000, 150_000 + 30_000 + 500_000}, {"kimi-k3.1-pro", 0, 0, 1_000_000, 1_000_000, 2_000_000 + 10_000_000}, {"deepseek-v4-pro", 1_000_000, 1_000_000, 0, 1_000_000, 1_320_000 + 44_000 + 3_960_000}, {"deepseek-v4.1-flash", 1_000_000, 0, 0, 1_000_000, 300_000 + 1_200_000}, {"mimo-v2.6-pro", 0, 1_000_000, 0, 0, 3_625}, {"minimax-m3.1-flash", 1_000_000, 0, 0, 1_000_000, 150_000 + 600_000}, } { if got, ok := price(c.model, c.in, c.cached, c.write, c.out); !ok || got != c.want { t.Errorf("%s: %d %v, want %d", c.model, got, ok, c.want) } } for _, unknown := range []struct { model string write int64 }{{"glm-5.3", 10}, {"deepseek-v4-pro", 1}, {"beam-free", 0}, {"iceman-contributor-free", 0}, {"qwen3.8-max", 0}, {"gpt-6-luna", 0}, {"minimax-m3", 0}} { if got, ok := price(unknown.model, 100, 0, unknown.write, 10); ok { t.Errorf("%s with %d cache writes priced at %d", unknown.model, unknown.write, got) } } }