From 3e1e2b77de1512178f39716cb8e4c888abbe4719 Mon Sep 17 00:00:00 2001 From: echowxsy Date: Sat, 28 Feb 2026 13:35:56 +0800 Subject: [PATCH] test: add test cases for anthropic protocol with API key - Verify anthropic protocol returns *ClaudeProvider (not *HTTPProvider) - Test custom api_base for Anthropic-compatible endpoints - Test missing API key error message Co-Authored-By: Claude Opus 4.6 --- pkg/providers/factory_provider_test.go | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pkg/providers/factory_provider_test.go b/pkg/providers/factory_provider_test.go index e0c0eddef..881cc6106 100644 --- a/pkg/providers/factory_provider_test.go +++ b/pkg/providers/factory_provider_test.go @@ -152,6 +152,48 @@ func TestCreateProviderFromConfig_Anthropic(t *testing.T) { if modelID != "claude-sonnet-4.6" { t.Errorf("modelID = %q, want %q", modelID, "claude-sonnet-4.6") } + // Should use ClaudeProvider (Anthropic SDK), not HTTPProvider + if _, ok := provider.(*ClaudeProvider); !ok { + t.Errorf("expected *ClaudeProvider, got %T", provider) + } +} + +func TestCreateProviderFromConfig_AnthropicWithCustomAPIBase(t *testing.T) { + cfg := &config.ModelConfig{ + ModelName: "test-anthropic-custom", + Model: "anthropic/glm-4.7", + APIKey: "test-key", + APIBase: "https://api.z.ai/api/anthropic/v1", + } + + provider, modelID, err := CreateProviderFromConfig(cfg) + if err != nil { + t.Fatalf("CreateProviderFromConfig() error = %v", err) + } + if provider == nil { + t.Fatal("CreateProviderFromConfig() returned nil provider") + } + if modelID != "glm-4.7" { + t.Errorf("modelID = %q, want %q", modelID, "glm-4.7") + } + if _, ok := provider.(*ClaudeProvider); !ok { + t.Errorf("expected *ClaudeProvider, got %T", provider) + } +} + +func TestCreateProviderFromConfig_AnthropicMissingAPIKey(t *testing.T) { + cfg := &config.ModelConfig{ + ModelName: "test-anthropic-no-key", + Model: "anthropic/claude-sonnet-4.6", + } + + _, _, err := CreateProviderFromConfig(cfg) + if err == nil { + t.Fatal("CreateProviderFromConfig() expected error for missing API key") + } + if !strings.Contains(err.Error(), "api_key is required") { + t.Errorf("error = %q, want message containing %q", err.Error(), "api_key is required") + } } func TestCreateProviderFromConfig_Antigravity(t *testing.T) {