feat(ai): add provider connection test (#38)
This commit is contained in:
41
backend/internal/ai/connection_http_test.go
Normal file
41
backend/internal/ai/connection_http_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type connectionTransport func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f connectionTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
func TestConnectionDiscoveryHTTPStatus(t *testing.T) {
|
||||
for _, code := range []int{401, 403, 429, 500} {
|
||||
for _, name := range []string{"gemini", "ollama"} {
|
||||
t.Run(fmt.Sprintf("%s/%d", name, code), func(t *testing.T) {
|
||||
client := &http.Client{Transport: connectionTransport(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: code, Body: io.NopCloser(strings.NewReader(`{"secret":"private response"}`)), Header: make(http.Header)}, nil
|
||||
})}
|
||||
var p Provider
|
||||
if name == "gemini" {
|
||||
g := newGemini("secret", "")
|
||||
g.client = client
|
||||
p = g
|
||||
} else {
|
||||
o := newOllama("https://private.invalid", "")
|
||||
o.client = client
|
||||
p = o
|
||||
}
|
||||
_, err := p.ListModels(context.Background())
|
||||
var status *summaryHTTPError
|
||||
if !errors.As(err, &status) || status.status != code || strings.Contains(err.Error(), "private") {
|
||||
t.Fatalf("unsafe or unclassified error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user