package provider import ( "context" "errors" "io" "math" "net/http" "net/http/httptest" "strings" "testing" ) func TestUsageCacheHitRatioIncludesAllPromptTokens(t *testing.T) { u := Usage{InputTokens: 84_000, CacheReadTokens: 123_000, CacheWriteTokens: 3_000, CacheMeasuredPromptTokens: 210_000, CacheMeasuredReadTokens: 123_000} if got := u.PromptTokens(); got != 210_000 { t.Fatalf("prompt tokens = %d, want 210000", got) } ratio, ok := u.CacheHitRatio() if !ok || math.Abs(ratio-123.0/210.0) > 1e-9 { t.Fatalf("cache hit ratio = %v, %v, want %v, true", ratio, ok, 123.0/210.0) } } func TestUsageCacheHitRatioDistinguishesKnownZeroFromHistoricalUnknown(t *testing.T) { knownMiss := Usage{InputTokens: 84_000, CacheMeasuredPromptTokens: 84_000} if ratio, ok := knownMiss.CacheHitRatio(); !ok || ratio != 0 { t.Fatalf("known cache miss ratio = %v, %v, want 0, true", ratio, ok) } historicalUnknown := Usage{InputTokens: 84_000} if ratio, ok := historicalUnknown.CacheHitRatio(); ok || ratio != 0 { t.Fatalf("historical cache ratio = %v, %v, want 0, false", ratio, ok) } } func TestUsageAddPreservesOnlyMeasuredCacheDenominators(t *testing.T) { measured := Usage{InputTokens: 60, CacheReadTokens: 40, CacheMeasuredPromptTokens: 100, CacheMeasuredReadTokens: 40} unknown := Usage{InputTokens: 200, CacheReadTokens: 100} got := measured.Add(unknown) if got.CacheMeasuredPromptTokens != 100 { t.Fatalf("measured cache denominator = %d, want 100", got.CacheMeasuredPromptTokens) } if ratio, ok := got.CacheHitRatio(); !ok || math.Abs(ratio-0.4) > 1e-9 { t.Fatalf("cache hit ratio = %v, %v, want 0.4, true", ratio, ok) } } func TestSSEParse(t *testing.T) { r := strings.NewReader("event: foo\ndata: {\"a\":1}\n\ndata: hello\ndata: world\n\n") ch := make(chan sseEvent, 4) go readSSE(r, ch) e := <-ch if e.Event != "foo" || e.Data != `{"a":1}` { t.Fatalf("event 1: %+v", e) } e = <-ch if e.Event != "" || e.Data != "hello\nworld" { t.Fatalf("event 2: %+v", e) } if _, ok := <-ch; ok { t.Fatalf("channel not closed") } } type errorAfterReader struct { data *strings.Reader err error } func (r *errorAfterReader) Read(p []byte) (int, error) { if r.data.Len() > 0 { return r.data.Read(p) } return 0, r.err } func TestSSEReadErrorIsDeliveredBeforeChannelClose(t *testing.T) { want := io.ErrUnexpectedEOF ch := make(chan sseEvent, 2) go readSSE(&errorAfterReader{ data: strings.NewReader("data: hello\n\n"), err: want, }, ch) if event := <-ch; event.Data != "hello" { t.Fatalf("event = %+v, want payload before read error", event) } terminal, ok := <-ch if !ok { t.Fatal("channel closed without scanner error") } if !errors.Is(terminal.Err, want) { t.Fatalf("scanner error = %v, want %v", terminal.Err, want) } if _, ok := <-ch; ok { t.Fatal("channel remained open after scanner error") } } func TestStreamsReportSSEReadFailures(t *testing.T) { want := io.ErrUnexpectedEOF response := func() *http.Response { return &http.Response{Body: io.NopCloser(&errorAfterReader{ data: strings.NewReader(""), err: want, })} } cases := []struct { name string run func(chan<- Event) }{ { name: "anthropic", run: func(out chan<- Event) { (&anthropicClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out) }, }, { name: "openai", run: func(out chan<- Event) { (&openaiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out) }, }, { name: "gemini", run: func(out chan<- Event) { (&geminiClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out) }, }, { name: "openai codex", run: func(out chan<- Event) { (&codexClient{}).runStream(context.Background(), response(), Request{Model: "test"}, out) }, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { out := make(chan Event, 4) go tc.run(out) var done EventDone foundDone := false for event := range out { if e, ok := event.(EventDone); ok { done = e foundDone = true } } if !foundDone { t.Fatal("stream ended without EventDone") } if done.Stop != StopError { t.Fatalf("stop = %s, want %s", done.Stop, StopError) } if !errors.Is(done.Err, want) { t.Fatalf("stream error = %v, want %v", done.Err, want) } }) } } func TestModelCatalog(t *testing.T) { if len(Catalog) == 0 { t.Fatal("empty catalog") } if _, err := FindModel("anthropic", "claude-sonnet-4-5"); err != nil { t.Fatal(err) } if _, err := FindModel("openai", "gpt-5"); err != nil { t.Fatal(err) } if _, err := FindModel("", "nope"); err == nil { t.Fatal("expected error") } } func TestVertexGeminiFlashCatalogLimits(t *testing.T) { for _, id := range []string{"gemini-3.1-flash-lite", "gemini-3.5-flash"} { m, err := FindModel("google-vertex", id) if err != nil { t.Fatal(err) } if m.ContextWindow != 1048576 || m.MaxOutput != 65535 || !m.Reasoning { t.Errorf("%s metadata = %+v", id, m) } } } func TestComputeCost(t *testing.T) { m, _ := FindModel("anthropic", "claude-sonnet-4-5") cost := ComputeCost(m, Usage{InputTokens: 1_000_000, OutputTokens: 1_000_000}) want := m.PriceInput + m.PriceOutput if cost != want { t.Fatalf("cost=%v want=%v", cost, want) } } func TestComputeCostInputTier(t *testing.T) { m := Model{ PriceInput: 1, PriceOutput: 2, PriceCacheRead: 0.1, PriceTierInputTokens: 100, PriceInputAbove: 3, PriceOutputAbove: 4, PriceCacheReadAbove: 0.2, } below := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 49}) wantBelow := (50.0*1 + 10*2 + 49*0.1) / 1_000_000 if math.Abs(below-wantBelow) > 1e-12 { t.Fatalf("below-tier cost=%v want=%v", below, wantBelow) } atThreshold := ComputeCost(m, Usage{InputTokens: 50, OutputTokens: 10, CacheReadTokens: 50}) wantAtThreshold := (50.0*3 + 10*4 + 50*0.2) / 1_000_000 if math.Abs(atThreshold-wantAtThreshold) > 1e-12 { t.Fatalf("at-tier cost=%v want=%v", atThreshold, wantAtThreshold) } } func TestComputeCostMultipleInputTiers(t *testing.T) { m := Model{ PriceInput: 1, PriceTiers: []ModelPriceTier{ {InputTokens: 500000, PriceInput: 5}, {InputTokens: 272000, PriceInput: 3}, }, } for _, tc := range []struct { name string input int price float64 }{ {"below_first", 271999, 1}, {"at_first", 272000, 3}, {"between", 499999, 3}, {"at_second", 500000, 5}, {"above_second", 600000, 5}, } { t.Run(tc.name, func(t *testing.T) { got := ComputeCost(m, Usage{InputTokens: tc.input}) want := float64(tc.input) * tc.price / 1_000_000 if math.Abs(got-want) > 1e-12 { t.Fatalf("cost = %.12f, want %.12f", got, want) } }) } } func TestAnthropicErrorStatus(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) _, _ = w.Write([]byte(`{"error":{"type":"auth","message":"bad key"}}`)) })) defer srv.Close() c := NewAnthropic("x", srv.URL) _, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"}) if err == nil || !strings.Contains(err.Error(), "401") { t.Fatalf("want 401 err, got %v", err) } } func TestOpenAIErrorStatus(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) _, _ = w.Write([]byte(`{"error":{"message":"nope"}}`)) })) defer srv.Close() c := NewOpenAI("x", srv.URL) _, err := c.Stream(context.Background(), Request{Model: "gpt-5"}) if err == nil || !strings.Contains(err.Error(), "400") { t.Fatalf("want 400 err, got %v", err) } } func TestOpenAIUserContentPreservesProjectedTextBlocks(t *testing.T) { c := NewOpenAI("x", "").(*openaiClient) wire, err := c.buildRequest(Request{ Model: "gpt-5", Messages: []Message{{ Role: RoleUser, Content: []Content{ TextBlock{Text: "[message time: 2026-08-08T12:34:56-07:00]"}, TextBlock{Text: "fail"}, }, }}, }) if err != nil { t.Fatal(err) } blocks, ok := wire.Messages[0].Content.([]interface{}) if !ok { t.Fatalf("user content = %T, want structured text blocks", wire.Messages[0].Content) } if len(blocks) != 2 { t.Fatalf("user content blocks = %d, want 2", len(blocks)) } if got := blocks[0].(oaiContentText).Text; got != "[message time: 2026-08-08T12:34:56-07:00]" { t.Fatalf("timestamp block = %q", got) } if got := blocks[1].(oaiContentText).Text; got != "fail" { t.Fatalf("user text block = %q", got) } } func TestMiniMaxM3CatalogAndRequest(t *testing.T) { for _, provider := range []string{"minimax", "minimax-cn"} { m, err := FindModel(provider, "MiniMax-M3") if err != nil { t.Fatal(err) } if m.ContextWindow != 1000000 || m.MaxOutput != 131072 || !m.Reasoning || !m.AdaptiveThinkingCompat { t.Errorf("%s metadata = %+v", provider, m) } if m.PriceInput != 0.3 || m.PriceOutput != 1.2 || m.PriceCacheRead != 0.06 || m.PriceTierInputTokens != 512000 || m.PriceInputAbove != 0.6 || m.PriceOutputAbove != 2.4 || m.PriceCacheReadAbove != 0.12 { t.Errorf("%s pricing = %+v", provider, m) } } c := NewAnthropicCompat("minimax", "x", "https://api.minimax.io/anthropic").(*anthropicClient) temp := float32(0.7) wire, err := c.buildRequest(Request{ Model: "MiniMax-M3", Reasoning: "high", Temperature: &temp, Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.Thinking == nil || wire.Thinking.Type != "adaptive" || wire.Thinking.BudgetTokens != 0 { t.Fatalf("want adaptive thinking without a budget, got %+v", wire.Thinking) } if wire.OutputConfig != nil { t.Fatalf("MiniMax-M3 must not send output_config, got %+v", wire.OutputConfig) } if wire.Temperature == nil || *wire.Temperature != temp { t.Fatalf("MiniMax-M3 should keep temperature, got %v", wire.Temperature) } } func TestAnthropicAdaptiveThinking(t *testing.T) { c := NewAnthropic("x", "").(*anthropicClient) temp := float32(0.7) // Opus 5 -> adaptive thinking, effort set, no budget, no temperature. wire, err := c.buildRequest(Request{ Model: "claude-opus-5", Reasoning: "high", Temperature: &temp, Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.Thinking == nil || wire.Thinking.Type != "adaptive" { t.Fatalf("want adaptive thinking, got %+v", wire.Thinking) } if wire.Thinking.BudgetTokens != 0 { t.Fatalf("adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens) } if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" { t.Fatalf("want effort=high, got %+v", wire.OutputConfig) } if wire.Temperature != nil { t.Fatalf("adaptive must drop temperature, got %v", *wire.Temperature) } // maximum -> xhigh effort. wire, err = c.buildRequest(Request{ Model: "claude-opus-5", Reasoning: "maximum", Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.OutputConfig == nil || wire.OutputConfig.Effort != "xhigh" { t.Fatalf("want effort=xhigh, got %+v", wire.OutputConfig) } // max is a separate native tier above xhigh on adaptive models. wire, err = c.buildRequest(Request{ Model: "claude-opus-5", Reasoning: "max", Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.OutputConfig == nil || wire.OutputConfig.Effort != "max" { t.Fatalf("want effort=max, got %+v", wire.OutputConfig) } // Sonnet 5 -> adaptive thinking, effort set, no budget, no temperature. wire, err = c.buildRequest(Request{ Model: "claude-sonnet-5", Reasoning: "high", Temperature: &temp, Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.Thinking == nil || wire.Thinking.Type != "adaptive" { t.Fatalf("want adaptive thinking for Sonnet 5, got %+v", wire.Thinking) } if wire.Thinking.BudgetTokens != 0 { t.Fatalf("Sonnet 5 adaptive must not send budget_tokens, got %d", wire.Thinking.BudgetTokens) } if wire.OutputConfig == nil || wire.OutputConfig.Effort != "high" { t.Fatalf("want Sonnet 5 effort=high, got %+v", wire.OutputConfig) } if wire.Temperature != nil { t.Fatalf("Sonnet 5 adaptive must drop temperature, got %v", *wire.Temperature) } // Opus 4.5 -> budget-based thinking, no output_config, temperature kept. wire, err = c.buildRequest(Request{ Model: "claude-opus-4-5", Reasoning: "high", Temperature: &temp, Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.Thinking == nil || wire.Thinking.Type != "enabled" || wire.Thinking.BudgetTokens <= 0 { t.Fatalf("want budget thinking, got %+v", wire.Thinking) } if wire.OutputConfig != nil { t.Fatalf("budget models must not send output_config, got %+v", wire.OutputConfig) } if wire.Temperature == nil || *wire.Temperature != temp { t.Fatalf("budget model should keep temperature, got %v", wire.Temperature) } } func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) { c := NewAnthropic("x", "").(*anthropicClient) wire, err := c.buildRequest(Request{ Model: "claude-sonnet-4-5", Messages: []Message{ {Role: RoleUser, Content: []Content{TextBlock{Text: "make an image"}}}, {Role: RoleAssistant, Content: []Content{ TextBlock{Text: "done"}, ImageBlock{MimeType: "image/png", Data: []byte("png")}, TextBlock{Text: "Saved image: `zut-gemini-image-x.png`"}, }}, {Role: RoleUser, Content: []Content{TextBlock{Text: "hello"}}}, }, }) if err != nil { t.Fatal(err) } if len(wire.Messages) != 3 { t.Fatalf("messages=%d", len(wire.Messages)) } assistant := wire.Messages[1] if assistant.Role != "assistant" { t.Fatalf("role=%q", assistant.Role) } if len(assistant.Content) != 2 { t.Fatalf("assistant content=%+v", assistant.Content) } for _, b := range assistant.Content { if _, ok := b.(anthImageBlock); ok { t.Fatalf("assistant image block was not stripped: %+v", assistant.Content) } } } func TestRepairOrphanedToolResultsDeduplicates(t *testing.T) { text := func(s string) TextBlock { return TextBlock{Text: s} } result := func(id, s string) ToolResultBlock { return ToolResultBlock{CallID: id, Content: []Content{text(s)}} } msgs := []Message{ {Role: RoleAssistant, Content: []Content{ ToolCallBlock{ID: "tool-1", Name: "read"}, ToolCallBlock{ID: "tool-2", Name: "read"}, }}, {Role: RoleTool, Content: []Content{result("tool-1", "first"), text("note"), result("tool-1", "dup-same-msg")}}, {Role: RoleTool, Content: []Content{result("tool-1", "dup-across-msg")}}, {Role: RoleTool, Content: []Content{result("tool-2", "second")}}, {Role: RoleTool, Content: []Content{result("orphan", "orphan")}}, } got := RepairOrphanedToolResults(msgs) if len(got) != 3 { t.Fatalf("messages=%d, want 3 after dropping duplicates and orphans", len(got)) } first := got[1].Content if len(first) != 2 { t.Fatalf("first result message blocks=%d, want result+note", len(first)) } tr, ok := first[0].(ToolResultBlock) if !ok || tr.CallID != "tool-1" { t.Fatalf("first block=%T %+v, want tool-1 result", first[0], first[0]) } if txt, ok := tr.Content[0].(TextBlock); !ok || txt.Text != "first" { t.Fatalf("first result content=%+v, want first", tr.Content) } if txt, ok := first[1].(TextBlock); !ok || txt.Text != "note" { t.Fatalf("mixed content=%+v, want preserved note", first[1]) } last, ok := got[2].Content[0].(ToolResultBlock) if !ok || last.CallID != "tool-2" { t.Fatalf("last block=%T %+v, want tool-2 result", got[2].Content[0], got[2].Content[0]) } } func TestAnthropicStreamHappyPath(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/event-stream") w.WriteHeader(http.StatusOK) fl, _ := w.(http.Flusher) write := func(s string) { _, _ = w.Write([]byte(s)) if fl != nil { fl.Flush() } } write("event: message_start\ndata: {\"message\":{\"usage\":{\"input_tokens\":10,\"output_tokens\":0}}}\n\n") write("event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n") write("event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"hi\"}}\n\n") write("event: content_block_stop\ndata: {\"index\":0}\n\n") write("event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"end_turn\"},\"usage\":{\"output_tokens\":5}}\n\n") write("event: message_stop\ndata: {}\n\n") })) defer srv.Close() c := NewAnthropic("x", srv.URL) evs, err := c.Stream(context.Background(), Request{Model: "claude-sonnet-4-5"}) if err != nil { t.Fatal(err) } var gotText string var done EventDone for ev := range evs { switch e := ev.(type) { case EventTextDelta: gotText += e.Delta case EventDone: done = e } } if gotText != "hi" { t.Fatalf("text=%q", gotText) } if done.Stop != StopEnd { t.Fatalf("stop=%v", done.Stop) } } func anthropicStreamUsage(t *testing.T, messageStart string) Usage { t.Helper() return anthropicStreamUsageWithDelta(t, messageStart, `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}`) } func anthropicStreamUsageWithDelta(t *testing.T, messageStart, messageDelta string) Usage { t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/event-stream") w.WriteHeader(http.StatusOK) _, _ = w.Write([]byte("event: message_start\ndata: " + messageStart + "\n\n" + "event: message_delta\ndata: " + messageDelta + "\n\n" + "event: message_stop\ndata: {}\n\n")) })) defer srv.Close() evs, err := NewAnthropic("x", srv.URL).Stream(context.Background(), Request{Model: "claude-sonnet-4-5"}) if err != nil { t.Fatal(err) } var usage Usage for ev := range evs { if e, ok := ev.(EventUsage); ok { usage = e.Usage } } return usage } func TestAnthropicStreamReportsCacheHitRatio(t *testing.T) { usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":20,"output_tokens":0,"cache_read_input_tokens":900,"cache_creation_input_tokens":80}}}`) if usage.CacheReadTokens != 900 || usage.CacheWriteTokens != 80 { t.Fatalf("cache tokens = R%d W%d, want R900 W80", usage.CacheReadTokens, usage.CacheWriteTokens) } ratio, ok := usage.CacheHitRatio() if !ok || math.Abs(ratio-0.9) > 1e-9 { t.Fatalf("cache hit ratio = %v, %v, want 0.9, true", ratio, ok) } } func TestAnthropicStreamKnownCacheMissReportsZeroRatio(t *testing.T) { usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}}`) if ratio, ok := usage.CacheHitRatio(); !ok || ratio != 0 { t.Fatalf("cache hit ratio = %v, %v, want 0, true", ratio, ok) } } func TestAnthropicStreamCacheFieldsInDeltaOnly(t *testing.T) { start := `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}` for _, tc := range []struct { name string delta string want float64 }{ {"known miss", `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5,"cache_read_input_tokens":0,"cache_creation_input_tokens":0}}`, 0}, {"hit", `{"delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5,"cache_read_input_tokens":300,"cache_creation_input_tokens":0}}`, 0.75}, } { t.Run(tc.name, func(t *testing.T) { usage := anthropicStreamUsageWithDelta(t, start, tc.delta) if ratio, ok := usage.CacheHitRatio(); !ok || math.Abs(ratio-tc.want) > 1e-9 { t.Fatalf("cache hit ratio = %v, %v, want %v, true", ratio, ok, tc.want) } }) } } func TestAnthropicStreamWithoutCacheFieldsLeavesRatioUnknown(t *testing.T) { usage := anthropicStreamUsage(t, `{"message":{"usage":{"input_tokens":100,"output_tokens":0}}}`) if ratio, ok := usage.CacheHitRatio(); ok { t.Fatalf("cache hit ratio = %v, true, want unknown", ratio) } } func TestClaudeOpus5Catalog(t *testing.T) { m, err := FindModel("anthropic", "claude-opus-5") if err != nil { t.Fatal(err) } if m.DisplayName != "Claude Opus 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative { t.Fatalf("unexpected Opus 5 model: %+v", m) } if m.PriceInput != 5 || m.PriceOutput != 25 || m.PriceCacheRead != 0.5 || m.PriceCacheWrite != 6.25 { t.Fatalf("unexpected Opus 5 pricing: %+v", m) } } func TestClaudeOpus55Catalog(t *testing.T) { m, err := FindModel("anthropic", "claude-opus-5-5") if err != nil { t.Fatal(err) } if m.DisplayName != "Claude Opus 5.5" || m.ContextWindow != 500000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative { t.Fatalf("unexpected Opus 5.5 model: %+v", m) } if m.PriceInput != 4 || m.PriceOutput != 20 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 5 { t.Fatalf("unexpected Opus 5.5 pricing: %+v", m) } } func TestClaudeFable51Catalog(t *testing.T) { m, err := FindModel("anthropic", "claude-fable-5-1") if err != nil { t.Fatal(err) } if m.DisplayName != "Claude Fable 5.1" || m.ContextWindow != 500000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative { t.Fatalf("unexpected Fable 5.1 model: %+v", m) } if m.PriceInput != 10 || m.PriceOutput != 50 || m.PriceCacheRead != 0.25 || m.PriceCacheWrite != 12.5 { t.Fatalf("unexpected Fable 5.1 pricing: %+v", m) } } func TestClaudeSonnet55Catalog(t *testing.T) { m, err := FindModel("anthropic", "claude-sonnet-5-5") if err != nil { t.Fatal(err) } if m.DisplayName != "Claude Sonnet 5.5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking || m.Speculative { t.Fatalf("unexpected Sonnet 5.5 model: %+v", m) } if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 { t.Fatalf("unexpected Sonnet 5.5 pricing: %+v", m) } } func TestClaudeSonnet5Catalog(t *testing.T) { m, err := FindModel("anthropic", "claude-sonnet-5") if err != nil { t.Fatal(err) } if m.DisplayName != "Claude Sonnet 5" || m.ContextWindow != 1000000 || m.MaxOutput != 128000 || !m.Reasoning || !m.AdaptiveThinking { t.Fatalf("unexpected Sonnet 5 model: %+v", m) } if m.PriceInput != 2 || m.PriceOutput != 10 || m.PriceCacheRead != 0.2 || m.PriceCacheWrite != 2.5 { t.Fatalf("unexpected Sonnet 5 pricing: %+v", m) } } func TestOpenAICompatAnthropicReasoningEffort(t *testing.T) { c := NewOpenRouter("token", "").(*openaiClient) wire, err := c.buildRequest(Request{ Model: "anthropic/claude-opus-4.8", Reasoning: "maximum", Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.ReasoningEffort != "xhigh" { t.Fatalf("want xhigh for adaptive Anthropic model over OpenAI-compatible wire, got %q", wire.ReasoningEffort) } wire, err = c.buildRequest(Request{ Model: "anthropic/claude-sonnet-5", Reasoning: "maximum", Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.ReasoningEffort != "xhigh" { t.Fatalf("want xhigh for Claude Sonnet 5 over OpenAI-compatible wire, got %q", wire.ReasoningEffort) } wire, err = c.buildRequest(Request{ Model: "gpt-5.1", Reasoning: "maximum", Messages: []Message{{Role: RoleUser, Content: []Content{TextBlock{Text: "hi"}}}}, }) if err != nil { t.Fatal(err) } if wire.ReasoningEffort != "high" { t.Fatalf("want generic OpenAI-compatible maximum clamped to high, got %q", wire.ReasoningEffort) } } func TestOpenAIBuildRequestSkipsReasoningOnlyAssistantMessages(t *testing.T) { c := NewKimi("token", "").(*openaiClient) wire, err := c.buildRequest(Request{ Model: "kimi-for-coding", Messages: []Message{ {Role: RoleUser, Content: []Content{TextBlock{Text: "first"}}}, {Role: RoleAssistant, Content: []Content{ReasoningBlock{Summary: "thinking only"}}}, {Role: RoleUser, Content: []Content{TextBlock{Text: "second"}}}, }, }) if err != nil { t.Fatal(err) } for i, msg := range wire.Messages { if msg.Role == "assistant" && msg.Content == nil && len(msg.ToolCalls) == 0 { t.Fatalf("message %d is empty assistant: %+v", i, msg) } } if got := len(wire.Messages); got != 2 { t.Fatalf("messages=%d want 2 after skipping reasoning-only assistant", got) } } func TestOpenAIStreamHappyPath(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/event-stream") w.WriteHeader(http.StatusOK) fl, _ := w.(http.Flusher) write := func(s string) { _, _ = w.Write([]byte(s)) if fl != nil { fl.Flush() } } write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hel\"},\"finish_reason\":null}]}\n\n") write("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"},\"finish_reason\":\"stop\"}]}\n\n") write("data: {\"choices\":[],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":2}}\n\n") write("data: [DONE]\n\n") })) defer srv.Close() c := NewOpenAI("x", srv.URL) evs, err := c.Stream(context.Background(), Request{Model: "gpt-5"}) if err != nil { t.Fatal(err) } var gotText string var done EventDone for ev := range evs { switch e := ev.(type) { case EventTextDelta: gotText += e.Delta case EventDone: done = e } } if gotText != "hello" { t.Fatalf("text=%q", gotText) } if done.Stop != StopEnd { t.Fatalf("stop=%v", done.Stop) } } func TestDiscoverOpenRouter(t *testing.T) { const body = `{"data":[ {"id":"x/full","pricing":{"prompt":"0.000003","completion":"0.000015"}, "context_length":200000,"top_provider":{"max_completion_tokens":64000}, "supported_parameters":["reasoning"]}, {"id":"x/fallback","top_provider":{"context_length":128000}}, {"id":""}]}` srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte(body)) })) defer srv.Close() models, err := DiscoverOpenRouter(context.Background(), srv.URL) if err != nil { t.Fatal(err) } if len(models) != 2 { // empty id dropped t.Fatalf("want 2 models, got %d", len(models)) } // per-token USD -> per-1M; reasoning from supported_parameters. if m := models[0]; m.Provider != "openrouter" || m.ContextWindow != 200000 || m.MaxOutput != 64000 || !m.Reasoning || m.PriceInput != 3 || m.PriceOutput != 15 { t.Errorf("model[0]: %+v", m) } // context falls back to top_provider; no reasoning. if m := models[1]; m.ContextWindow != 128000 || m.MaxOutput != 0 || m.Reasoning { t.Errorf("model[1]: %+v", m) } } func TestGPT56AndGPT6CatalogEntries(t *testing.T) { cases := []struct { provider string id string context int priceInput float64 priceOut float64 cacheRead float64 cacheWrite float64 }{ {"openai", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25}, {"openai", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25}, {"openai", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125}, {"openai", "gpt-6-astra", 500000, 10, 50, 1, 12.5}, {"openai", "gpt-6.1-sol", 500000, 2, 10, 0.1, 2.5}, {"openai-codex", "gpt-5.6-luna", 500000, 1, 6, 0.1, 1.25}, {"openai-codex", "gpt-5.6-sol", 500000, 5, 30, 0.5, 6.25}, {"openai-codex", "gpt-5.6-terra", 500000, 2.5, 15, 0.25, 3.125}, {"openai-codex", "gpt-6-astra", 500000, 10, 50, 1, 12.5}, {"openai-codex", "gpt-6-sol", 500000, 2, 10, 0.2, 2.5}, {"openai-codex", "gpt-6.1-sol", 500000, 2, 10, 0.1, 2.5}, {"openai-codex", "gpt-6-luna", 500000, 0.1, 0.5, 0.01, 0.125}, {"azure-openai-responses", "gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25}, {"azure-openai-responses", "gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25}, {"azure-openai-responses", "gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125}, {"vercel-ai-gateway", "openai/gpt-5.6-luna", 1050000, 1, 6, 0.1, 1.25}, {"vercel-ai-gateway", "openai/gpt-5.6-sol", 1050000, 5, 30, 0.5, 6.25}, {"vercel-ai-gateway", "openai/gpt-5.6-terra", 1050000, 2.5, 15, 0.25, 3.125}, } for _, tc := range cases { m, err := FindModel(tc.provider, tc.id) if err != nil { t.Fatal(err) } if m.ContextWindow != tc.context || m.MaxOutput != 128000 || !m.Reasoning { t.Fatalf("%s/%s limits: %+v", tc.provider, tc.id, m) } if m.PriceInput != tc.priceInput || m.PriceOutput != tc.priceOut || m.PriceCacheRead != tc.cacheRead || m.PriceCacheWrite != tc.cacheWrite { t.Fatalf("%s/%s prices: %+v", tc.provider, tc.id, m) } if tc.provider == "openai-codex" && m.Speculative { t.Fatalf("%s/%s is speculative", tc.provider, tc.id) } } } // TestOpenAIOmitsZeroMaxTokens guards against sending max_tokens: 0 for // discovered models that don't advertise an output cap (MaxOutput == 0). func TestOpenAIOmitsZeroMaxTokens(t *testing.T) { SetLiveModels([]Model{ {Provider: "openrouter", ID: "vendor/no-cap", DisplayName: "No Cap"}, {Provider: "openrouter", ID: "vendor/reason-no-cap", DisplayName: "Reason No Cap", Reasoning: true}, {Provider: "openrouter", ID: "vendor/capped", DisplayName: "Capped", MaxOutput: 4096}, }) defer SetLiveModels(nil) c := NewOpenAI("x", "").(*openaiClient) got, err := c.buildRequest(Request{Model: "vendor/no-cap"}) if err != nil { t.Fatal(err) } if got.MaxTokens != nil { t.Errorf("expected max_tokens omitted, got %d", *got.MaxTokens) } got, err = c.buildRequest(Request{Model: "vendor/reason-no-cap"}) if err != nil { t.Fatal(err) } if got.MaxCompletionTok != nil { t.Errorf("expected max_completion_tokens omitted, got %d", *got.MaxCompletionTok) } got, err = c.buildRequest(Request{Model: "vendor/capped"}) if err != nil { t.Fatal(err) } if got.MaxTokens == nil || *got.MaxTokens != 4096 { t.Errorf("expected max_tokens 4096, got %v", got.MaxTokens) } }