Add available-models admin tab with live model list and latency probing
build / build (push) Successful in 2m29s
build / build (push) Successful in 2m29s
This commit is contained in:
@@ -271,6 +271,100 @@ func TestStats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminModels verifies the admin /api/models endpoint returns the live
|
||||
// model list advertised by the upstream gateway model-info endpoint.
|
||||
func TestAdminModels(t *testing.T) {
|
||||
upstream, proxy, st := setupTestServer(t)
|
||||
defer upstream.Close()
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
AccessKey: "AK", SecretKey: "SK", Token: "TOKEN",
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
if err := st.SaveCredentials(creds); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := http.Get(proxy.URL + "/api/models")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := data["ok"].(bool); !ok {
|
||||
t.Fatalf("models endpoint not ok: %v", data)
|
||||
}
|
||||
models, _ := data["models"].([]any)
|
||||
if len(models) != 2 {
|
||||
t.Fatalf("models = %v, want 2", models)
|
||||
}
|
||||
}
|
||||
|
||||
// TestModelTest verifies the admin /api/models/test endpoint probes a model and
|
||||
// reports availability plus latency against the mock streaming upstream.
|
||||
func TestModelTest(t *testing.T) {
|
||||
upstream, proxy, st := setupTestServer(t)
|
||||
defer upstream.Close()
|
||||
defer proxy.Close()
|
||||
|
||||
creds := auth.Credentials{
|
||||
AccessKey: "AK", SecretKey: "SK", Token: "TOKEN",
|
||||
APIKey: "sk-test-456", ModelBaseURL: upstream.URL, Email: "[email protected]",
|
||||
}
|
||||
if err := st.SaveCredentials(creds); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(map[string]string{"model": "GLM-4.7"})
|
||||
resp, err := http.Post(proxy.URL+"/api/models/test", "application/json", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := data["ok"].(bool); !ok {
|
||||
t.Fatalf("test endpoint not ok: %v", data)
|
||||
}
|
||||
if available, _ := data["available"].(bool); !available {
|
||||
t.Fatalf("model should be available: %v", data)
|
||||
}
|
||||
if _, ok := data["ttft_ms"]; !ok {
|
||||
t.Fatalf("ttft_ms should be present: %v", data)
|
||||
}
|
||||
}
|
||||
|
||||
// TestModelTestNoCredentials verifies the test endpoint reports unavailable
|
||||
// gracefully when no credentials are configured, instead of erroring.
|
||||
func TestModelTestNoCredentials(t *testing.T) {
|
||||
upstream, proxy, _ := setupTestServer(t)
|
||||
defer upstream.Close()
|
||||
defer proxy.Close()
|
||||
|
||||
body, _ := json.Marshal(map[string]string{"model": "GLM-4.7"})
|
||||
resp, err := http.Post(proxy.URL+"/api/models/test", "application/json", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var data map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := data["ok"].(bool); !ok {
|
||||
t.Fatalf("test endpoint should stay ok: %v", data)
|
||||
}
|
||||
if available, _ := data["available"].(bool); available {
|
||||
t.Fatalf("model should not be available without creds: %v", data)
|
||||
}
|
||||
}
|
||||
|
||||
// totNum extracts an int from a JSON-decoded numeric value (float64).
|
||||
func totNum(v any) int {
|
||||
f, _ := v.(float64)
|
||||
|
||||
Reference in New Issue
Block a user