|
| 1 | +package edenai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "gomodel/internal/core" |
| 10 | + "gomodel/internal/llmclient" |
| 11 | + "gomodel/internal/providers" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRegistration(t *testing.T) { |
| 15 | + if Registration.Type != "edenai" { |
| 16 | + t.Fatalf("Registration.Type = %q, want edenai", Registration.Type) |
| 17 | + } |
| 18 | + if Registration.Discovery.DefaultBaseURL != defaultBaseURL { |
| 19 | + t.Fatalf("DefaultBaseURL = %q, want %q", Registration.Discovery.DefaultBaseURL, defaultBaseURL) |
| 20 | + } |
| 21 | + if p := New(providers.ProviderConfig{APIKey: "eden-key"}, providers.ProviderOptions{}); p == nil { |
| 22 | + t.Fatal("New() returned nil") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestChatCompletion_UsesBearerAuthAndChatEndpoint(t *testing.T) { |
| 27 | + var gotPath string |
| 28 | + var gotAuth string |
| 29 | + |
| 30 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + gotPath = r.URL.Path |
| 32 | + gotAuth = r.Header.Get("Authorization") |
| 33 | + w.Header().Set("Content-Type", "application/json") |
| 34 | + _, _ = w.Write([]byte(`{ |
| 35 | + "id":"chatcmpl-edenai", |
| 36 | + "created":1677652288, |
| 37 | + "model":"anthropic/claude-sonnet-4-5", |
| 38 | + "choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}], |
| 39 | + "usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4} |
| 40 | + }`)) |
| 41 | + })) |
| 42 | + defer server.Close() |
| 43 | + |
| 44 | + provider := NewWithHTTPClient("eden-key", server.URL, server.Client(), llmclient.Hooks{}) |
| 45 | + |
| 46 | + resp, err := provider.ChatCompletion(context.Background(), &core.ChatRequest{ |
| 47 | + Model: "anthropic/claude-sonnet-4-5", |
| 48 | + Messages: []core.Message{ |
| 49 | + {Role: "user", Content: "hi"}, |
| 50 | + }, |
| 51 | + }) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("ChatCompletion() error = %v", err) |
| 54 | + } |
| 55 | + if resp.Model != "anthropic/claude-sonnet-4-5" { |
| 56 | + t.Fatalf("resp.Model = %q, want anthropic/claude-sonnet-4-5", resp.Model) |
| 57 | + } |
| 58 | + if resp.Usage.TotalTokens != 4 { |
| 59 | + t.Fatalf("resp.Usage = %+v, want total_tokens=4", resp.Usage) |
| 60 | + } |
| 61 | + if gotPath != "/chat/completions" { |
| 62 | + t.Fatalf("path = %q, want /chat/completions", gotPath) |
| 63 | + } |
| 64 | + if gotAuth != "Bearer eden-key" { |
| 65 | + t.Fatalf("Authorization = %q, want Bearer eden-key", gotAuth) |
| 66 | + } |
| 67 | +} |
0 commit comments