Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
33.3 KB
Raw Blame
package metrics

import "testing"

func TestPriceForModelAliasAnthropicCurrentGeneration(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "claude-sonnet-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "anthropic:claude-sonnet-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "claude-5-sonnet",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "claude-fable-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-fable-5", InputPerM: 10, CacheReadPerM: 1, CacheWritePerM: 12.5, OutputPerM: 50},
		},
		{
			model: "anthropic/claude-fable-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-fable-5", InputPerM: 10, CacheReadPerM: 1, CacheWritePerM: 12.5, OutputPerM: 50},
		},
		{
			model: "claude-opus-4-8",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-4.8", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},
		},
		// Opus 5 sits on the same 5/25 Opus tier as 4.5-4.8.
		{
			model: "claude-opus-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},
		},
		{
			model: "anthropic/claude-opus-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},
		},
		// Claude Code reports the 1M-context beta with a bracketed suffix.
		{
			model: "claude-opus-5[1m]",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}
}

func TestPriceForModelAliasCodexCurrentGeneration(t *testing.T) {
	// OpenAI's standard short-context rates: cache read = 0.1x input (90%
	// cached-input discount; 0.05x on GPT-6.1), cache write = 1.25x input.
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "gpt-6-astra",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6-astra", InputPerM: 10, CacheReadPerM: 1, CacheWritePerM: 12.5, OutputPerM: 50},
		},
		{
			model: "gpt-5.6-sol",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.6-sol", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 30},
		},
		{
			model: "gpt-6.1-sol",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6.1-sol", InputPerM: 2, CacheReadPerM: 0.1, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "openai:gpt-6.1-sol[1m]",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6.1-sol", InputPerM: 2, CacheReadPerM: 0.1, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "gpt-6.1-terra",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6.1-terra", InputPerM: 1, CacheReadPerM: 0.05, CacheWritePerM: 1.25, OutputPerM: 5},
		},
		{
			model: "openai/gpt-6.1-terra[1m]",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6.1-terra", InputPerM: 1, CacheReadPerM: 0.05, CacheWritePerM: 1.25, OutputPerM: 5},
		},
		{
			model: "openai:gpt-6-sol",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6-sol", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "openai/gpt-6-luna",
			want:  ModelPrice{Provider: "openai", Model: "gpt-6-luna", InputPerM: 0.1, CacheReadPerM: 0.01, CacheWritePerM: 0.125, OutputPerM: 0.5},
		},
		{
			model: "openai:gpt-5.6-terra",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.6-terra", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 12},
		},
		{
			model: "openai/gpt-5.6-luna",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.6-luna", InputPerM: 0.2, CacheReadPerM: 0.02, CacheWritePerM: 0.25, OutputPerM: 1.2},
		},
		// Pre-5.6 SKUs don't bill cache writes separately: written tokens are
		// ordinary input, never the discounted cache-read rate.
		{
			model: "gpt-5.5",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 5, OutputPerM: 30},
		},
		{
			model: "gpt-5.4-mini",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.4-mini", InputPerM: 0.75, CacheReadPerM: 0.075, CacheWritePerM: 0.75, OutputPerM: 4.5},
		},
		{
			model: "gpt-5.3-codex",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.3-codex", InputPerM: 1.75, CacheReadPerM: 0.175, CacheWritePerM: 1.75, OutputPerM: 14},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// Unknown suffixed variants must NOT borrow a 5.6 tier — the alias is an
	// anchored exact match, mirroring the frontend's exact-match resolver.
	// The dash-normalized ids (`gpt-5-6-luna`) must also miss: the real Codex
	// slug is always dotted and the frontend does not dash-normalize, so both
	// sides surface these as unmapped instead of silently pricing them.
	for _, model := range []string{
		"gpt-6-astra-pro",
		"gpt-6.1-sol-pro",
		"gpt-6-1-sol",
		"gpt-6.1-terra-pro",
		"gpt-6-1-terra",
		"gpt-6-sol-high",
		"gpt-6-luna-pro",
		"gpt-6-astra/unknown",
		"gpt-6-astra-high",
		"gpt-5.6-luna-pro",
		"gpt-5.6-luna/unknown",
		"gpt-5.6-sol-high",
		"gpt-5.6-mini",
		"gpt-5-6-luna",
		"gpt-5-6-sol",
		"gpt-5-6-terra",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestPriceForModelAliasGrok pins the xAI catalog to the published rates
// (docs.x.ai/developers/pricing). Before these rows existed every Grok token
// took the unpriced branch in RecordLLMUsage, so llm_cost_usd reported zero
// Grok spend while the tokens piled up in llm_unpriced_tokens.
func TestPriceForModelAliasGrok(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "grok-4.6",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.6", InputPerM: 2, CacheReadPerM: 0.5, CacheWritePerM: 2, OutputPerM: 6},
		},
		{
			model: "xai:grok-4.6",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.6", InputPerM: 2, CacheReadPerM: 0.5, CacheWritePerM: 2, OutputPerM: 6},
		},
		{
			model: "grok-4.5",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.5", InputPerM: 2, CacheReadPerM: 0.3, CacheWritePerM: 2, OutputPerM: 6},
		},
		{
			model: "xai:grok-4.5",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.5", InputPerM: 2, CacheReadPerM: 0.3, CacheWritePerM: 2, OutputPerM: 6},
		},
		{
			model: "xai/grok-4.5",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.5", InputPerM: 2, CacheReadPerM: 0.3, CacheWritePerM: 2, OutputPerM: 6},
		},
		{
			model: "grok-4.3",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.3", InputPerM: 1.25, CacheReadPerM: 0.2, CacheWritePerM: 1.25, OutputPerM: 2.5},
		},
		{
			model: "grok-build-0.1",
			want:  ModelPrice{Provider: "xai", Model: "grok-build-0.1", InputPerM: 1, CacheReadPerM: 0.2, CacheWritePerM: 1, OutputPerM: 2},
		},
		{
			model: "grok-4.20-multi-agent-0309",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.20-multi-agent-0309", InputPerM: 1.25, CacheReadPerM: 0.2, CacheWritePerM: 1.25, OutputPerM: 2.5},
		},
		{
			model: "grok-4.20-0309-reasoning",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.20-0309-reasoning", InputPerM: 1.25, CacheReadPerM: 0.2, CacheWritePerM: 1.25, OutputPerM: 2.5},
		},
		{
			model: "grok-4.20-0309-non-reasoning",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.20-0309-non-reasoning", InputPerM: 1.25, CacheReadPerM: 0.2, CacheWritePerM: 1.25, OutputPerM: 2.5},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// `grok-composer-*` ships in the Grok Build catalog but xAI publishes no
	// rate for it, so it must stay unmapped rather than inherit grok-4.5's.
	// Suffixed and dash-spelled variants must miss for the same reason the
	// gpt-5.6 rows do: the frontend resolver is an exact match that does not
	// dash-normalize non-Anthropic ids, so both sides agree on "unmapped".
	for _, model := range []string{
		"grok-composer-2.5-fast",
		"grok-composer-2.5",
		"grok-4.6-fast",
		"grok-4-6",
		"grok-4.5-fast",
		"grok-4-5",
		"grok-4.20-0309",
		"grok",
		"unknown",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestPriceForModelAliasMinimaxM3 pins the standard-tier fallback estimate.
// Aggregate usage cannot identify per-request long-context tiers. Cache writes
// mirror input for newly cached tokens; existing M2.7 rates remain unchanged.
func TestPriceForModelAliasMinimaxM3(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "minimax-m3",
			want:  ModelPrice{Provider: "minimax", Model: "m3", InputPerM: 0.3, CacheReadPerM: 0.06, CacheWritePerM: 0.3, OutputPerM: 1.2},
		},
		{
			model: "codebuddy/minimax-m3",
			want:  ModelPrice{Provider: "minimax", Model: "m3", InputPerM: 0.3, CacheReadPerM: 0.06, CacheWritePerM: 0.3, OutputPerM: 1.2},
		},
		{
			model: "MiniMax-M3",
			want:  ModelPrice{Provider: "minimax", Model: "m3", InputPerM: 0.3, CacheReadPerM: 0.06, CacheWritePerM: 0.3, OutputPerM: 1.2},
		},
		// The older M2.7 row must stay separate and keep its distinct rates.
		{
			model: "minimax-m2.7",
			want:  ModelPrice{Provider: "minimax", Model: "m2.7", InputPerM: 0.3, CacheReadPerM: 0.06, CacheWritePerM: 0.375, OutputPerM: 1.2},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	for _, model := range []string{"minimax-m3.5", "minimax-m3-highspeed", "some-vendor/minimax-m3-preview"} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestGrokPricingMatchesRecordedTurn re-derives the cost of a real
// grok 0.2.106 turn from the table and checks it against the costUsdTicks xAI
// returned for that same turn (1 tick = 1e-10 USD). This is the end-to-end
// proof that both the rates and the cached-input bucketing are right.
func TestGrokPricingMatchesRecordedTurn(t *testing.T) {
	// Captured payload: inputTokens 12929, cachedReadTokens 10880,
	// outputTokens 29, totalTokens 12958, costUsdTicks 75360000. Grok counts
	// the cached prefix inside inputTokens, so the uncached remainder is
	// 12929 - 10880 = 2049 (see normalizeACPTokenUsage in pkg/agent/acp_usage.go).
	const (
		uncachedInput = int64(2049)
		cacheRead     = int64(10880)
		output        = int64(29)
		wantUSD       = 75360000 / 1e10
	)

	price, ok := PriceForModelAlias("grok-4.5")
	if !ok {
		t.Fatal("grok-4.5 did not resolve")
	}
	got := tokenCostUSD(uncachedInput, price.InputPerM) +
		tokenCostUSD(cacheRead, price.CacheReadPerM) +
		tokenCostUSD(output, price.OutputPerM)

	if diff := got - wantUSD; diff > 1e-12 || diff < -1e-12 {
		t.Fatalf("recomputed cost = %.10f, want %.10f (xAI costUsdTicks)", got, wantUSD)
	}
}

// TestPriceForModelAliasAlibabaMoonshotVolcengine pins the pay-as-you-go
// rates for the Chinese-model runtimes (Qwen / Kimi) added from models.dev,
// and the transport spellings that reach them: `provider:model` (Hermes
// custom providers), `provider/model` (opencode), and bare ids. Volcengine's
// `ark-code-latest` rolling alias is covered as unmapped in
// TestPriceForModelAliasNoFalseBorrowing.
func TestPriceForModelAliasAlibabaMoonshotVolcengine(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "qwen3.7-plus",
			want:  ModelPrice{Provider: "alibaba", Model: "qwen3.7-plus", InputPerM: 0.40, CacheReadPerM: 0.04, CacheWritePerM: 0.50, OutputPerM: 1.60},
		},
		{
			model: "alibaba-coding-plan:qwen3.7-plus",
			want:  ModelPrice{Provider: "alibaba", Model: "qwen3.7-plus", InputPerM: 0.40, CacheReadPerM: 0.04, CacheWritePerM: 0.50, OutputPerM: 1.60},
		},
		{
			model: "qwen3.6-flash",
			want:  ModelPrice{Provider: "alibaba", Model: "qwen3.6-flash", InputPerM: 0.25, CacheReadPerM: 0.025, CacheWritePerM: 0.3125, OutputPerM: 1.50},
		},
		{
			model: "alibaba-coding-plan:qwen3.8-max",
			want:  ModelPrice{Provider: "alibaba", Model: "qwen3.8-max", InputPerM: 2.00, CacheReadPerM: 0.17, CacheWritePerM: 2.50, OutputPerM: 6.00},
		},
		{
			model: "custom:qwen3.8-max-preview[1m]",
			want:  ModelPrice{Provider: "alibaba", Model: "qwen3.8-max-preview", InputPerM: 0, CacheReadPerM: 0, CacheWritePerM: 0, OutputPerM: 0},
		},
		{
			model: "kimi-coding:kimi-k3",
			want:  ModelPrice{Provider: "moonshotai", Model: "kimi-k3", InputPerM: 3.0, CacheReadPerM: 0.30, CacheWritePerM: 3.0, OutputPerM: 15.0},
		},
		{
			// Kimi Code CLI reports `kimi-code/k3`.
			model: "kimi-code/k3",
			want:  ModelPrice{Provider: "moonshotai", Model: "kimi-k3", InputPerM: 3.0, CacheReadPerM: 0.30, CacheWritePerM: 3.0, OutputPerM: 15.0},
		},
		{
			// `custom:anthropic/claude-opus-4.7` (provider prefix + nested
			// slash path) must still resolve to the anthropic Opus tier via
			// substring matching, mirroring the frontend stripProvider
			// regression case.
			model: "custom:anthropic/claude-opus-4.7",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-4.7", InputPerM: 5.00, CacheReadPerM: 0.50, CacheWritePerM: 6.25, OutputPerM: 25.00},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}
}

// TestPriceForModelAliasGLM53AndKimiK31Pro pins the newly priced SKUs and the
// transports that report them: Hermes' `custom:glm-5.3` and opencode's
// `zhipuai/glm-5.3` (routing prefixes reach the rows through the `(^|/|:)`
// anchors), Kimi Code's `kimi-code/k3.1-pro`. The distinct CodeBuddy SKU
// `kimi-k3-1`, the double-price `kimi-k3.1-pro-highspeed` variants and Kimi
// Code's subscription coding-route alias `kimi-for-coding` (absent from
// Moonshot's API price sheet, like `ark-code-latest`) stay unmapped rather
// than borrowing a guessed rate.
func TestPriceForModelAliasGLM53AndKimiK31Pro(t *testing.T) {
	glm53 := ModelPrice{Provider: "zai", Model: "glm-5.3", InputPerM: 1.4, CacheReadPerM: 0.26, CacheWritePerM: 1.4, OutputPerM: 4.4}
	glm53Flash := ModelPrice{Provider: "zai", Model: "glm-5.3-flash", InputPerM: 0.15, CacheReadPerM: 0.03, CacheWritePerM: 0.15, OutputPerM: 0.5}
	kimiK31 := ModelPrice{Provider: "moonshotai", Model: "kimi-k3.1-pro", InputPerM: 2.0, CacheReadPerM: 0.20, CacheWritePerM: 2.0, OutputPerM: 10.0}
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{"glm-5.3", glm53},
		{"custom:glm-5.3", glm53},
		{"zhipuai/glm-5.3", glm53},
		{"glm-5.3[1m]", glm53},
		{"glm-5.3-flash", glm53Flash},
		{"kimi-k3.1-pro", kimiK31},
		{"kimi-code/k3.1-pro", kimiK31},
		{"k3.1-pro", kimiK31},
		{"kimi-k3.1-pro[1m]", kimiK31},
	}
	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	for _, model := range []string{
		"kimi-for-coding",
		"kimi-code/kimi-for-coding",
		"kimi-k3-1",
		"kimi-k3.1-pro-highspeed",
		"kimi-code/k3.1-pro-highspeed",
		"k3.1-pro-highspeed",
		"glm-5.3-flashx",
		"glm-5.3-fast",
		"glm-5.3-extra",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestPriceForModelAliasNoFalseBorrowing guards the anchored rules: a preview
// SKU must not inherit the GA tier, a distinct CodeBuddy SKU must not inherit
// Kimi K3, unknown suffixed variants must stay unmapped, empty bracket tags
// (`qwen3.7-plus[]` etc.) must stay unmapped to match the frontend's
// `\[[^\]]+\]$` tag stripping, and the Volcengine `ark-code-latest` rolling
// alias must stay unmapped.
func TestPriceForModelAliasNoFalseBorrowing(t *testing.T) {
	for _, model := range []string{
		"qwen3.8-max-preview",
		"qwen3.8-max-preview[1m]",
		"kimi-k3-1",
		"qwen3.8-max-extra",
		"qwen3.8-max[",
		"qwen3.8-max[1m]-extra",
		"qwen3.8-max-preview[1m]-extra",
	} {
		got, ok := PriceForModelAlias(model)
		if !ok {
			continue
		}
		if got.Model == "qwen3.8-max" || got.Model == "kimi-k3" {
			t.Fatalf("PriceForModelAlias(%q) borrowed %s; want the SKU's own tier or unmapped", model, got.Model)
		}
	}

	// A distinct SKU that borrows nothing must resolve to its own row.
	for _, tc := range []struct {
		model     string
		wantModel string
	}{
		{"qwen3.8-max-preview[1m]", "qwen3.8-max-preview"},
		{"qwen3.8-max-preview[context]", "qwen3.8-max-preview"},
		{"qwen3.8-max[1m]", "qwen3.8-max"},
	} {
		got, ok := PriceForModelAlias(tc.model)
		if !ok || got.Model != tc.wantModel {
			t.Fatalf("PriceForModelAlias(%q) = %+v (ok=%v); want %s", tc.model, got, ok, tc.wantModel)
		}
	}

	for _, model := range []string{
		"qwen3.8-max-extra",
		"kimi-k3-1",
		"qwen3.7-plus-extra",
		"qwen3.6-flash-extra",
		"qwen3.8-max-preview-extra",
		"custom:ark-code-latest",
		// Empty bracket tags: the frontend's `\[[^\]]+\]$` tag stripper
		// leaves these unmapped, so the backend must too.
		"qwen3.7-plus[]",
		"qwen3.6-flash[]",
		"qwen3.8-max[]",
		"qwen3.8-max-preview[]",
	} {
		if _, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved", model)
		}
	}
}

// TestPriceForModelAliasContextTagStripping pins the `[1m]` context-variant
// suffix normalization across every rule, including the anchored Codex / Grok /
// Kimi rules that do not carry a per-rule optional bracket group. Claude Code
// (and other harnesses) append a context-window tag such as `[1m]` to the
// model id; it is the same SKU at the same tier, so the row must price instead
// of falling into the unpriced bucket in RecordLLMUsage. Mirrors the frontend's
// `stripContextTag` (`\[[^\]]+\]$`) in packages/views/runtimes/utils.ts.
func TestPriceForModelAliasContextTagStripping(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "grok-4.5[1m]",
			want:  ModelPrice{Provider: "xai", Model: "grok-4.5", InputPerM: 2.00, CacheReadPerM: 0.30, CacheWritePerM: 2.00, OutputPerM: 6.00},
		},
		{
			model: "gpt-5.6-luna[1m]",
			want:  ModelPrice{Provider: "openai", Model: "gpt-5.6-luna", InputPerM: 0.20, CacheReadPerM: 0.02, CacheWritePerM: 0.25, OutputPerM: 1.20},
		},
		{
			model: "kimi-k3[1m]",
			want:  ModelPrice{Provider: "moonshotai", Model: "kimi-k3", InputPerM: 3.0, CacheReadPerM: 0.30, CacheWritePerM: 3.0, OutputPerM: 15.0},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// The tag stripper is anchored at end-of-string with a non-empty tag, so it
	// must not turn these misses into hits: a trailing bracket that is not a
	// complete end-of-string tag, and an empty tag, both stay unmapped — the
	// same guard the frontend keeps.
	for _, model := range []string{
		"grok-4.5[1m]-extra",
		"gpt-5.6-luna[]",
		"kimi-k3[",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

func TestPriceForModelAliasAnthropicFable51(t *testing.T) {
	// Fable 5.1 is its own SKU on the same Mythos-class tier as Fable 5, but
	// with cache reads at 0.025x input ($0.25) instead of the usual 0.1x. A
	// Fable 5 alias that did not stop at the version would swallow the `-1`
	// suffix and bill those reads at 4x, so every spelling below must land on
	// the Fable 5.1 row specifically.
	fable51 := ModelPrice{Provider: "anthropic", Model: "claude-fable-5-1", InputPerM: 10, CacheReadPerM: 0.25, CacheWritePerM: 12.5, OutputPerM: 50}
	fable5 := ModelPrice{Provider: "anthropic", Model: "claude-fable-5", InputPerM: 10, CacheReadPerM: 1, CacheWritePerM: 12.5, OutputPerM: 50}
	// Fable 5.2 is its own, cheaper row.
	fable52 := ModelPrice{Provider: "anthropic", Model: "claude-fable-5-2", InputPerM: 8, CacheReadPerM: 0.2, CacheWritePerM: 10, OutputPerM: 40}
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{model: "claude-fable-5-1", want: fable51},
		{model: "anthropic/claude-fable-5-1", want: fable51},
		{model: "anthropic:claude-fable-5-1", want: fable51},
		// Copilot reports Claude models dotted.
		{model: "claude-fable-5.1", want: fable51},
		// Claude Code reports the 1M-context variant with a bracketed suffix.
		{model: "claude-fable-5-1[1m]", want: fable51},
		// Fable 5 must keep resolving to its own row, including its 1M form.
		{model: "claude-fable-5", want: fable5},
		{model: "claude-fable-5[1m]", want: fable5},
		// The frontend resolver strips a trailing date snapshot / `-latest`
		// before its exact-key lookup (`stripDate` in
		// packages/views/runtimes/utils.ts), so these forms price there. Both
		// rules have to admit them too, otherwise the dashboard and
		// RecordLLMUsage disagree on the same id.
		{model: "claude-fable-5-20260401", want: fable5},
		{model: "claude-fable-5-2026-04-01", want: fable5},
		{model: "claude-fable-5-latest", want: fable5},
		{model: "claude-fable-5-20260401[1m]", want: fable5},
		{model: "claude-fable-5-1-20260901", want: fable51},
		{model: "claude-fable-5-1-latest", want: fable51},
		{model: "claude-fable-5-1-20260901[1m]", want: fable51},
		{model: "claude-fable-5-2", want: fable52},
		{model: "claude-fable-5.2", want: fable52},
		{model: "anthropic/claude-fable-5-2", want: fable52},
		{model: "claude-fable-5-2[1m]", want: fable52},
		{model: "claude-fable-5-2-20261101", want: fable52},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// A later Fable minor is a distinct SKU at an unknown rate: it must stay
	// unmapped and surface in the unpriced diagnostic rather than borrow a
	// neighbour's tier. This is the same failure the `-1` suffix had against
	// the Fable 5 rule, so guard it on the 5.1 rule as well.
	for _, model := range []string{
		"claude-fable-5-3",
		"claude-fable-5.3",
		"claude-fable-5-10",
		"claude-fable-5.10",
		"claude-fable-5-2x",
		"claude-fable-5-1x",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
		}
	}

	// An admitted suffix only counts when it ENDS the id. These rules are
	// substring matches, so a terminator whose alternatives are not anchored
	// still fires on anything that merely starts with one — an unknown
	// qualifier would silently borrow the tier of whichever row it prefixed,
	// while the frontend (which anchors both `stripDate` and the bracket tag)
	// leaves it unmapped. Same id, two different costs.
	for _, model := range []string{
		"claude-fable-5-1-latest-preview",
		"claude-fable-5-1-20260901x",
		"claude-fable-5-1-2026-09-01-preview",
		"claude-fable-5-1[1m]junk",
		"claude-fable-5-latest-preview",
		"claude-fable-5-20260401-preview",
		"claude-fable-5[1m]junk",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
		}
	}

	// A doubly-tagged id must not sneak back in through the tag-stripping
	// retry: peeling `[2m]` leaves `[1m]`, which the rule above rejected on
	// the raw form for good reason. The frontend strips one tag and does not
	// re-strip, so pricing these here would put two different costs on one
	// usage row.
	for _, model := range []string{
		"claude-fable-5[1m][2m]",
		"claude-fable-5-1[1m][2m]",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
		}
	}
}

func TestPriceForModelAliasAnthropicSonnetAndHaiku55(t *testing.T) {
	cases := []struct {
		aliases []string
		want    ModelPrice
	}{
		{
			aliases: []string{
				"claude-sonnet-5",
				"claude-5-sonnet",
				"anthropic/claude-sonnet-5",
				"claude-sonnet-5[1m]",
				"claude-sonnet-5-20261001",
				"claude-sonnet-5-latest",
			},
			want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			aliases: []string{
				"claude-sonnet-5-5",
				"claude-sonnet-5.5",
				"anthropic/claude-sonnet-5-5",
				"anthropic:claude-sonnet-5.5",
				"claude-sonnet-5-5[1m]",
				"claude-sonnet-5-5-20261001",
				"claude-sonnet-5-5-2026-10-01[1m]",
				"claude-sonnet-5-5-latest",
			},
			want: ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5-5", InputPerM: 2, CacheReadPerM: 0.1, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			aliases: []string{
				"claude-haiku-5-5",
				"claude-haiku-5.5",
				"anthropic/claude-haiku-5-5",
				"anthropic:claude-haiku-5.5",
				"claude-haiku-5-5[1m]",
				"claude-haiku-5-5-20261001",
				"claude-haiku-5-5-2026-10-01[1m]",
				"claude-haiku-5-5-latest",
			},
			want: ModelPrice{Provider: "anthropic", Model: "claude-haiku-5-5", InputPerM: 0.1, CacheReadPerM: 0.01, CacheWritePerM: 0.125, OutputPerM: 0.5},
		},
	}
	for _, tc := range cases {
		for _, model := range tc.aliases {
			t.Run(model, func(t *testing.T) {
				got, ok := PriceForModelAlias(model)
				if !ok || got != tc.want {
					t.Fatalf("PriceForModelAlias(%q) = %+v, %v; want %+v, true", model, got, ok, tc.want)
				}
			})
		}
	}
}

func TestPriceForModelAliasAnthropicSonnetAndHaikuUnknownVariants(t *testing.T) {
	for _, model := range []string{
		"claude-sonnet-5-6",
		"claude-sonnet-5.6",
		"claude-sonnet-5-55",
		"claude-haiku-5-6",
		"claude-haiku-5.6",
		"claude-haiku-5-55",
		"claude-5-sonnet-preview",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
		}
	}
	for _, base := range []string{"claude-sonnet-5", "claude-sonnet-5-5", "claude-haiku-5-5"} {
		for _, suffix := range []string{"x", "-preview", "-latest-preview", "-20261001x", "[1m]junk", "[1m][2m]", "[]"} {
			model := base + suffix
			if got, ok := PriceForModelAlias(model); ok {
				t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
			}
		}
	}
}

func TestPriceForModelAliasAnthropicOpus55(t *testing.T) {
	// Opus 5.5 is its own SKU at $4 / $20 with cache reads at 0.05x input
	// ($0.20). An Opus 5 alias that did not stop at the version would swallow
	// the `-5` suffix and bill 5.5 at Opus 5's 5/25 tier, so every spelling
	// below must land on the Opus 5.5 row specifically.
	opus55 := ModelPrice{Provider: "anthropic", Model: "claude-opus-5-5", InputPerM: 4, CacheReadPerM: 0.2, CacheWritePerM: 5, OutputPerM: 20}
	opus5 := ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25}
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{model: "claude-opus-5-5", want: opus55},
		{model: "anthropic/claude-opus-5-5", want: opus55},
		{model: "anthropic:claude-opus-5-5", want: opus55},
		// Copilot reports Claude models dotted.
		{model: "claude-opus-5.5", want: opus55},
		// Claude Code reports the 1M-context variant with a bracketed suffix.
		{model: "claude-opus-5-5[1m]", want: opus55},
		{model: "claude-opus-5-5-20260901", want: opus55},
		{model: "claude-opus-5-5-latest", want: opus55},
		{model: "claude-opus-5-5-20260901[1m]", want: opus55},
		// Opus 5 must keep resolving to its own row in every suffix form the
		// frontend resolver also strips.
		{model: "claude-opus-5", want: opus5},
		{model: "claude-opus-5[1m]", want: opus5},
		{model: "claude-opus-5-20260401", want: opus5},
		{model: "claude-opus-5-latest", want: opus5},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// Another Opus 5 minor is a distinct SKU at an unknown rate: it must stay
	// unmapped rather than borrow Opus 5's or 5.5's tier, matching the
	// frontend's exact-key lookup.
	for _, model := range []string{
		"claude-opus-5-1",
		"claude-opus-5.6",
		"claude-opus-5-55",
		"claude-opus-5-5x",
		"claude-opus-5-5-latest-preview",
		"claude-opus-5-5[1m]junk",
		"claude-opus-5[1m][2m]",
		"claude-opus-5-5[1m][2m]",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestPriceForModelAliasBedrockAnthropicIDs pins the Bedrock shapes. Bedrock
// reports Anthropic ids with a region family and vendor prefix plus a version
// suffix. Newer Claude rules reject that suffix until it is normalized.
func TestPriceForModelAliasBedrockAnthropicIDs(t *testing.T) {
	cases := []struct {
		model string
		want  ModelPrice
	}{
		{
			model: "us.anthropic.claude-sonnet-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			// Europe / APAC / global profiles are the same SKU at the same rate.
			model: "eu.anthropic.claude-sonnet-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
		{
			model: "global.anthropic.claude-opus-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-opus-5", InputPerM: 5, CacheReadPerM: 0.5, CacheWritePerM: 6.25, OutputPerM: 25},
		},
		{
			// Vendor prefix without a region family.
			model: "anthropic.claude-haiku-5-5",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-haiku-5-5", InputPerM: 0.1, CacheReadPerM: 0.01, CacheWritePerM: 0.125, OutputPerM: 0.5},
		},
		{
			// Snapshot id with the inference-profile version suffix.
			model: "us.anthropic.claude-sonnet-4-5-20250929-v1:0",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-4.5", InputPerM: 3, CacheReadPerM: 0.3, CacheWritePerM: 3.75, OutputPerM: 15},
		},
		{
			model: "custom:bedrock/us.anthropic.claude-sonnet-5-v1:0[1m]",
			want:  ModelPrice{Provider: "anthropic", Model: "claude-sonnet-5", InputPerM: 2, CacheReadPerM: 0.2, CacheWritePerM: 2.5, OutputPerM: 10},
		},
	}

	for _, tc := range cases {
		got, ok := PriceForModelAlias(tc.model)
		if !ok {
			t.Fatalf("PriceForModelAlias(%q) did not resolve", tc.model)
		}
		if got != tc.want {
			t.Fatalf("PriceForModelAlias(%q) = %+v, want %+v", tc.model, got, tc.want)
		}
	}

	// Normalising the prefix must not invent a rate: a later SKU, a
	// non-Anthropic Bedrock model, or a qualifier that is its own SKU all stay
	// unmapped rather than borrowing the nearest Claude tier.
	for _, model := range []string{
		"us.anthropic.claude-sonnet-6",
		"us.anthropic.claude-sonnet-5-preview",
		"us.amazon.nova-pro-v1:0",
		"global.anthropic.claude-opus-5-mini",
		"us.anthropic.claude-sonnet-5-preview-v1:0",
		"us.anthropic.claude-sonnet-5-v1:0junk",
		"us.anthropic.claude-sonnet-5-v1:0[1m][2m]",
		"gpt-6.1-sol-v1:0",
		"claude-sonnet-5-v1:0",
		"us.anthropic.gpt-6.1-sol-v1:0",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Fatalf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}

// TestPriceForModelAliasDeepSeekV41Flash pins the V4.1 Flash row, including the
// dated OpenRouter snapshot that DeepSeek documents as the served model for the
// undated id (same SKU, same rate — one row, not two).
func TestPriceForModelAliasDeepSeekV41Flash(t *testing.T) {
	want := ModelPrice{Provider: "deepseek", Model: "v4.1-flash", InputPerM: 0.30, CacheReadPerM: 0.006, CacheWritePerM: 0.30, OutputPerM: 1.20}
	for _, model := range []string{
		"deepseek-flash",
		"deepseek/deepseek-flash",
		"custom:deepseek/deepseek-flash[1m]",
		"deepseek-v4-flash",
		"deepseek-v4-flash-vision-exp",
		"deepseek/deepseek-v4-flash-vision-exp",
		"deepseek-chat",
		"deepseek-reasoner",
		"deepseek-v4.1-flash",
		"deepseek/deepseek-v4.1-flash",
		"deepseek/deepseek-v4.1-flash-1017",
		"deepseek-v4.1-flash-1017",
	} {
		got, ok := PriceForModelAlias(model)
		if !ok {
			t.Errorf("PriceForModelAlias(%q) did not resolve", model)
			continue
		}
		if got != want {
			t.Errorf("PriceForModelAlias(%q) = %+v, want %+v", model, got, want)
		}
	}

	// Only documented Flash aliases and the known 1017 snapshot are priced.
	for _, model := range []string{
		"deepseek-v4.1-pro", "deepseek-v4.1-flash-preview",
		"deepseek-v4-1-flash", "deepseek-v4.1-flash-1020",
		"deepseek-v4.1-flash-1017-preview", "deepseek-flash-preview",
		"deepseek-v4-flash-preview", "deepseek-v4-flash-vision-exp-extra",
	} {
		if got, ok := PriceForModelAlias(model); ok {
			t.Errorf("PriceForModelAlias(%q) unexpectedly resolved to %+v; want unmapped", model, got)
		}
	}
}