feat(ai): add provider connection test (#38)
This commit is contained in:
95
backend/internal/ai/connection_qa_test.go
Normal file
95
backend/internal/ai/connection_qa_test.go
Normal file
@ -0,0 +1,95 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/models"
|
||||
)
|
||||
|
||||
func TestConnectionClaudeCodeDoesNotClaimAuthSuccess(t *testing.T) {
|
||||
tester := NewConnectionTester(connectionRepo{p: &models.AIProvider{Name: "claudecode"}}, nil, func(string, string, string) (Provider, error) { return newClaudeCode(""), nil })
|
||||
err := tester.Test(context.Background(), "id")
|
||||
if err == nil || err.Error() != "auth_check_unsupported" {
|
||||
t.Fatalf("want unsupported auth check; got %v", err)
|
||||
}
|
||||
}
|
||||
func TestConnectionAnthropicChecksAuthentication(t *testing.T) {
|
||||
for _, code := range []int{200, 401, 403, 503} {
|
||||
t.Run(http.StatusText(code), func(t *testing.T) {
|
||||
calls := 0
|
||||
p := newAnthropic("saved-secret", "")
|
||||
p.client = &http.Client{Transport: connectionTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if r.Method != "GET" || r.URL.String() != "https://api.anthropic.com/v1/models" || r.Header.Get("x-api-key") != "saved-secret" || r.Header.Get("anthropic-version") == "" || r.Body != nil {
|
||||
t.Fatal("expected authenticated, bodyless model discovery")
|
||||
}
|
||||
body := `{"data":[{"id":"model-id"}]}`
|
||||
if code != 200 {
|
||||
body = `private body saved-secret https://private.invalid`
|
||||
}
|
||||
return &http.Response{StatusCode: code, Body: io.NopCloser(strings.NewReader(body)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
tester := NewConnectionTester(connectionRepo{p: &models.AIProvider{Name: "anthropic"}}, nil, func(string, string, string) (Provider, error) { return p, nil })
|
||||
err := tester.Test(context.Background(), "id")
|
||||
if calls != 1 {
|
||||
t.Fatalf("auth requests=%d", calls)
|
||||
}
|
||||
var want error
|
||||
if code == 401 || code == 403 {
|
||||
want = ErrConnectionAuth
|
||||
} else if code != 200 {
|
||||
want = ErrConnectionUnavailable
|
||||
}
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("got %v want %v", err, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectionAnthropicDiscoverySanitizesFailures(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
code int
|
||||
body string
|
||||
transportErr error
|
||||
}{
|
||||
{name: "auth", code: 401, body: "saved-secret https://private.invalid private body"},
|
||||
{name: "malformed", code: 200, body: "saved-secret https://private.invalid private body"},
|
||||
{name: "transport", transportErr: errors.New("saved-secret https://private.invalid private body")},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := newAnthropic("saved-secret", "")
|
||||
p.client = &http.Client{Transport: connectionTransport(func(*http.Request) (*http.Response, error) {
|
||||
if tc.transportErr != nil {
|
||||
return nil, tc.transportErr
|
||||
}
|
||||
return &http.Response{StatusCode: tc.code, Body: io.NopCloser(strings.NewReader(tc.body)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
_, err := p.ListModels(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected discovery error")
|
||||
}
|
||||
for _, s := range []string{"saved-secret", "https://", "private body"} {
|
||||
if strings.Contains(err.Error(), s) {
|
||||
t.Fatalf("discovery error leaked sensitive detail")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestConnectionAnthropicReturnsDiscoveredModels(t *testing.T) {
|
||||
p := newAnthropic("saved-secret", "")
|
||||
p.client = &http.Client{Transport: connectionTransport(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"data":[{"id":"discovered-model"}]}`)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
models, err := p.ListModels(context.Background())
|
||||
if err != nil || len(models) != 1 || models[0] != "discovered-model" {
|
||||
t.Fatal("expected discovered models")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user