Files
TradarrV2/backend/internal/api/handlers/ai_connection_test.go

38 lines
1.2 KiB
Go

package handlers
import (
"context"
"errors"
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/ai"
"net/http/httptest"
"testing"
)
type fakeConnection struct {
err error
id string
}
func (f *fakeConnection) Test(_ context.Context, id string) error { f.id = id; return f.err }
func TestAIProviderConnectionHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
for _, tt := range []struct {
err error
code int
body string
}{
{nil, 200, `{"status":"success"}`}, {ai.ErrConnectionAuth, 200, `{"status":"auth_rejected"}`}, {ai.ErrConnectionUnavailable, 200, `{"status":"unavailable"}`}, {ai.ErrConnectionUnsupported, 200, `{"status":"auth_check_unsupported"}`}, {ai.ErrConnectionNotFound, 404, `{"error":"not found"}`}, {errors.New("secret endpoint raw error"), 500, `{"error":"internal server error"}`},
} {
f := &fakeConnection{err: tt.err}
h := &Handler{connectionTester: f}
r := gin.New()
r.POST("/api/admin/ai-providers/:id/test", h.TestAIProvider)
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest("POST", "/api/admin/ai-providers/saved-id/test", nil))
if w.Code != tt.code || w.Body.String() != tt.body || f.id != "saved-id" {
t.Fatalf("code=%d body=%s id=%s", w.Code, w.Body, f.id)
}
}
}