mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
test(ops): cover status test and model fetch
This commit is contained in:
42
internal/api/health_handler_test.go
Normal file
42
internal/api/health_handler_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/ez-api/ez-api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestHealthHandler_TestDeps(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
||||
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
|
||||
mr := miniredis.RunT(t)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
||||
|
||||
svc := service.NewHealthCheckService(db, rdb)
|
||||
h := NewHealthHandler(svc)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/api/status/test", h.TestDeps)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/status/test", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
@@ -58,3 +58,53 @@ func TestAdmin_TestProvider_OpenAICompatible(t *testing.T) {
|
||||
t.Fatalf("expected ok=true, got %v body=%s", payload["ok"], rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdmin_FetchProviderModels_OpenAICompatible(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer k" {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"object":"list","data":[{"id":"gpt-4o-mini"},{"id":"gpt-4o"}]}`))
|
||||
}))
|
||||
defer upstream.Close()
|
||||
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "openai",
|
||||
BaseURL: upstream.URL + "/v1",
|
||||
APIKey: "k",
|
||||
Group: "default",
|
||||
Models: "old-model",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(p).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/:id/fetch-models", h.FetchProviderModels)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/1/fetch-models", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
var updated model.Provider
|
||||
if err := db.First(&updated, p.ID).Error; err != nil {
|
||||
t.Fatalf("reload provider: %v", err)
|
||||
}
|
||||
if updated.Models != "gpt-4o,gpt-4o-mini" {
|
||||
t.Fatalf("expected models to update, got %q", updated.Models)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user