| 1 | package metered |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | "amadan.net/rastrillo/rastrillo/budget" |
| 10 | ) |
| 11 | |
| 12 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 13 | |
| 14 | func n(v int64) *int64 { return &v } |
| 15 | |
| 16 | // TestPricesNormalisedComponents is §2.3's cost rule: components priced |
| 17 | // in integer micro-units, each part rounded up; the larger of the priced |
| 18 | // and the reported cost; an unpriced model, an unpriceable component, an |
| 19 | // incomplete row or an overflow is unknown. |
| 20 | func TestPricesNormalisedComponents(t *testing.T) { |
| 21 | table := Tables[Current[wire.ProviderTensorX]] |
| 22 | 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)} |
| 23 | got, ok := table.Price(row) |
| 24 | // 1.75 + 0.5 * 0.44 + 0.1 * 4.50 = 2.42; reasoning is part of output. |
| 25 | if !ok || got.CostMicros == nil || *got.CostMicros != 2_420_000 || got.Currency != "USD" || got.PriceVersion != "tensorx-2026-11-12" { |
| 26 | t.Fatalf("priced %+v %v", got, ok) |
| 27 | } |
| 28 | reported := row |
| 29 | reported.CostReportedMicros = n(3_000_000) |
| 30 | if got, _ := table.Price(reported); *got.CostMicros != 3_000_000 || *got.CostReportedMicros != 3_000_000 { |
| 31 | t.Fatalf("the reported cost was larger: %d", *got.CostMicros) |
| 32 | } |
| 33 | lower := row |
| 34 | lower.CostReportedMicros = n(1) |
| 35 | if got, _ := table.Price(lower); *got.CostMicros != 2_420_000 { |
| 36 | t.Fatalf("the priced cost was larger: %d", *got.CostMicros) |
| 37 | } |
| 38 | one := row |
| 39 | one.InputTokens, one.CachedInputTokens, one.OutputTokens = n(1), n(0), n(1) |
| 40 | if got, _ := table.Price(one); *got.CostMicros != 2+5 { |
| 41 | t.Fatalf("parts round up: %d", *got.CostMicros) |
| 42 | } |
| 43 | for name, bad := range map[string]func(*wire.Usage){ |
| 44 | "unpriced model": func(u *wire.Usage) { u.Model = "z-ai/glm-9" }, |
| 45 | "cache write": func(u *wire.Usage) { u.CacheWriteTokens = n(10) }, |
| 46 | "incomplete": func(u *wire.Usage) { u.Complete = false }, |
| 47 | "no input figure": func(u *wire.Usage) { u.InputTokens = nil }, |
| 48 | "no output figure": func(u *wire.Usage) { u.OutputTokens = nil }, |
| 49 | "negative": func(u *wire.Usage) { u.OutputTokens = n(-1) }, |
| 50 | "overflowing input": func(u *wire.Usage) { u.InputTokens = n(math.MaxInt64) }, |
| 51 | } { |
| 52 | u := row |
| 53 | bad(&u) |
| 54 | if got, ok := table.Price(u); ok || got.CostMicros != nil { |
| 55 | t.Fatalf("%s: priced %v", name, got.CostMicros) |
| 56 | } |
| 57 | } |
| 58 | separate := Price{UncachedInput: n(1_000_000), Output: n(2_000_000), ReasoningSeparate: true} |
| 59 | if c, ok := Normalise(row, separate); !ok || c.Reasoning != 40_000 { |
| 60 | t.Fatalf("separate reasoning %+v", c) |
| 61 | } |
| 62 | if _, ok := Cost(Components{Reasoning: 1}, separate); ok { |
| 63 | t.Fatal("reasoning priced with no price") |
| 64 | } |
| 65 | codexLike := Price{UncachedInput: n(1), CachedInput: n(1), Output: n(1), InputIncludesCached: true} |
| 66 | if c, _ := Normalise(row, codexLike); c.UncachedInput != 500_000 { |
| 67 | t.Fatalf("cached counted twice: %+v", c) |
| 68 | } |
| 69 | for _, tb := range Tables { |
| 70 | for model, p := range tb.Models { |
| 71 | if p.UncachedInput == nil || p.Output == nil || *p.UncachedInput <= 0 || *p.Output <= 0 { |
| 72 | t.Fatalf("%s %s has no input or output price", tb.Version, model) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // TestOpenCodeGoPrices: OpenCode Go's table is the Go page's per-model |
| 79 | // prices read on 2026-11-12, chat-completions models only (the OpenCode |
| 80 | // harness speaks chat completions to Go), cache writes priced only where |
| 81 | // Go states a price, DeepSeek charged at its peak price (Equipo cannot |
| 82 | // tell peak from off-peak), and the free models unpriced (spend |
| 83 | // unknown). Reasoning is part of output. |
| 84 | func TestOpenCodeGoPrices(t *testing.T) { |
| 85 | version := Current[wire.ProviderOpenCodeGo] |
| 86 | table, ok := TableFor(version) |
| 87 | if !ok || version != "opencode-go-2026-11-12" || table.Provider != wire.ProviderOpenCodeGo || table.Currency != "USD" { |
| 88 | t.Fatalf("table %q %+v", version, table) |
| 89 | } |
| 90 | price := func(model string, in, cached, write, out int64) (int64, bool) { |
| 91 | 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)}) |
| 92 | if !ok { |
| 93 | return 0, false |
| 94 | } |
| 95 | return *got.CostMicros, true |
| 96 | } |
| 97 | for _, c := range []struct { |
| 98 | model string |
| 99 | in, cached, write, out int64 |
| 100 | want int64 |
| 101 | }{ |
| 102 | {"glm-5.3", 1_000_000, 0, 0, 1_000_000, 1_400_000 + 4_400_000}, |
| 103 | {"glm-5.3", 0, 1_000_000, 0, 0, 260_000}, |
| 104 | {"glm-5.3-flash", 1_000_000, 1_000_000, 0, 1_000_000, 150_000 + 30_000 + 500_000}, |
| 105 | {"kimi-k3.1-pro", 0, 0, 1_000_000, 1_000_000, 2_000_000 + 10_000_000}, |
| 106 | {"deepseek-v4-pro", 1_000_000, 1_000_000, 0, 1_000_000, 1_320_000 + 44_000 + 3_960_000}, |
| 107 | {"deepseek-v4.1-flash", 1_000_000, 0, 0, 1_000_000, 300_000 + 1_200_000}, |
| 108 | {"mimo-v2.6-pro", 0, 1_000_000, 0, 0, 3_625}, |
| 109 | {"minimax-m3.1-flash", 1_000_000, 0, 0, 1_000_000, 150_000 + 600_000}, |
| 110 | } { |
| 111 | if got, ok := price(c.model, c.in, c.cached, c.write, c.out); !ok || got != c.want { |
| 112 | t.Errorf("%s: %d %v, want %d", c.model, got, ok, c.want) |
| 113 | } |
| 114 | } |
| 115 | for _, unknown := range []struct { |
| 116 | model string |
| 117 | write int64 |
| 118 | }{{"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}} { |
| 119 | if got, ok := price(unknown.model, 100, 0, unknown.write, 10); ok { |
| 120 | t.Errorf("%s with %d cache writes priced at %d", unknown.model, unknown.write, got) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |