feat(ai): add provider connection test (#38)
This commit is contained in:
28
backend/internal/api/ai_connection_ticket38_test.go
Normal file
28
backend/internal/api/ai_connection_ticket38_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"github.com/tradarr/backend/internal/api"
|
||||
"github.com/tradarr/backend/internal/api/handlers"
|
||||
"github.com/tradarr/backend/internal/config"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAIProviderConnectionRequiresAuthentication(t *testing.T) {
|
||||
h := handlers.New(nil, &config.Config{}, nil, nil, nil, nil)
|
||||
r := api.SetupRouter(h, "test-secret")
|
||||
found := false
|
||||
for _, route := range r.Routes() {
|
||||
if route.Method == "POST" && route.Path == "/api/admin/ai-providers/:id/test" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("test route missing")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, httptest.NewRequest("POST", "/api/admin/ai-providers/saved/test", nil))
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("unauthenticated status=%d", w.Code)
|
||||
}
|
||||
}
|
||||
@ -551,3 +551,22 @@ func (h *Handler) CreateAdminUser(c *gin.Context) {
|
||||
}
|
||||
httputil.Created(c, user)
|
||||
}
|
||||
|
||||
// TestAIProvider returns only bounded outcomes from saved configuration.
|
||||
func (h *Handler) TestAIProvider(c *gin.Context) {
|
||||
err := h.connectionTester.Test(c.Request.Context(), c.Param("id"))
|
||||
switch {
|
||||
case err == nil:
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
case errors.Is(err, ai.ErrConnectionAuth):
|
||||
c.JSON(200, gin.H{"status": "auth_rejected"})
|
||||
case errors.Is(err, ai.ErrConnectionUnavailable):
|
||||
c.JSON(200, gin.H{"status": "unavailable"})
|
||||
case errors.Is(err, ai.ErrConnectionUnsupported):
|
||||
c.JSON(200, gin.H{"status": "auth_check_unsupported"})
|
||||
case errors.Is(err, ai.ErrConnectionNotFound):
|
||||
c.JSON(404, gin.H{"error": "not found"})
|
||||
default:
|
||||
c.JSON(500, gin.H{"error": "internal server error"})
|
||||
}
|
||||
}
|
||||
|
||||
37
backend/internal/api/handlers/ai_connection_test.go
Normal file
37
backend/internal/api/handlers/ai_connection_test.go
Normal file
@ -0,0 +1,37 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,6 +16,9 @@ import (
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
connectionTester interface {
|
||||
Test(context.Context, string) error
|
||||
}
|
||||
companyCatalog CompanyCatalog
|
||||
indexSearcher indices.IndexSearcher
|
||||
generationJobs interface {
|
||||
@ -44,13 +47,16 @@ func New(
|
||||
}
|
||||
return &Handler{
|
||||
companyCatalog: catalog,
|
||||
repo: repo,
|
||||
cfg: cfg,
|
||||
enc: enc,
|
||||
registry: registry,
|
||||
pipeline: pipeline,
|
||||
scheduler: sched,
|
||||
reportManager: ai.NewReportManager(),
|
||||
connectionTester: ai.NewConnectionTester(repo, enc, func(name, key, endpoint string) (ai.Provider, error) {
|
||||
return ai.NewProvider(name, key, "", endpoint)
|
||||
}),
|
||||
repo: repo,
|
||||
cfg: cfg,
|
||||
enc: enc,
|
||||
registry: registry,
|
||||
pipeline: pipeline,
|
||||
scheduler: sched,
|
||||
reportManager: ai.NewReportManager(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -73,6 +73,7 @@ func SetupRouter(h *handlers.Handler, jwtSecret string) *gin.Engine {
|
||||
admin.POST("/ai-providers/:id/activate", h.SetActiveAIProvider)
|
||||
admin.DELETE("/ai-providers/:id", h.DeleteAIProvider)
|
||||
admin.GET("/ai-providers/:id/models", h.ListAIModels)
|
||||
admin.POST("/ai-providers/:id/test", h.TestAIProvider)
|
||||
admin.POST("/ai-providers/probe-models", h.ProbeAIModels)
|
||||
|
||||
admin.GET("/sources", h.ListSources)
|
||||
|
||||
Reference in New Issue
Block a user