mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
- Update TestBatchBindings_Status to use newTestHandlerWithRedis helper - Remove unused jsonID helper function from sync_bindings_spec_test.go - Add time import to models.go
141 lines
5.0 KiB
Go
141 lines
5.0 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/ez-api/ez-api/internal/model"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type bindingSnapshot struct {
|
|
Namespace string `json:"namespace"`
|
|
PublicModel string `json:"public_model"`
|
|
Candidates []struct {
|
|
RouteGroup string `json:"route_group"`
|
|
Error string `json:"error,omitempty"`
|
|
Upstreams map[string]string `json:"upstreams"`
|
|
} `json:"candidates"`
|
|
}
|
|
|
|
func TestSyncBindings_SelectorExact(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "m", Status: "active"}
|
|
if err := db.Create(&group).Error; err != nil {
|
|
t.Fatalf("create group: %v", err)
|
|
}
|
|
key := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
|
|
if err := db.Create(&key).Error; err != nil {
|
|
t.Fatalf("create api key: %v", err)
|
|
}
|
|
b := model.Binding{Namespace: "ns", PublicModel: "m", GroupID: group.ID, Weight: 1, SelectorType: "exact", Status: "active"}
|
|
if err := db.Create(&b).Error; err != nil {
|
|
t.Fatalf("create binding: %v", err)
|
|
}
|
|
|
|
mr := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
svc := NewSyncService(rdb)
|
|
if err := svc.SyncBindings(db); err != nil {
|
|
t.Fatalf("SyncBindings: %v", err)
|
|
}
|
|
|
|
raw := mr.HGet("config:bindings", "ns.m")
|
|
if raw == "" {
|
|
t.Fatalf("expected config:bindings entry")
|
|
}
|
|
var snap bindingSnapshot
|
|
if err := json.Unmarshal([]byte(raw), &snap); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if len(snap.Candidates) != 1 {
|
|
t.Fatalf("expected 1 candidate, got %+v", snap.Candidates)
|
|
}
|
|
if snap.Candidates[0].Upstreams == nil || snap.Candidates[0].Upstreams[jsonID(key.ID)] != "m" {
|
|
t.Fatalf("unexpected upstreams: %+v", snap.Candidates[0].Upstreams)
|
|
}
|
|
}
|
|
|
|
func TestSyncBindings_SelectorRegexAndNormalize(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.ProviderGroup{}, &model.APIKey{}, &model.Binding{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
group := model.ProviderGroup{Name: "rg", Type: "openai", BaseURL: "https://api.openai.com/v1", Models: "moonshot/kimi2,kimi2", Status: "active"}
|
|
if err := db.Create(&group).Error; err != nil {
|
|
t.Fatalf("create group: %v", err)
|
|
}
|
|
k1 := model.APIKey{GroupID: group.ID, APIKey: "k1", Status: "active"}
|
|
k2 := model.APIKey{GroupID: group.ID, APIKey: "k2", Status: "active"}
|
|
if err := db.Create(&k1).Error; err != nil {
|
|
t.Fatalf("create api key1: %v", err)
|
|
}
|
|
if err := db.Create(&k2).Error; err != nil {
|
|
t.Fatalf("create api key2: %v", err)
|
|
}
|
|
|
|
// Regex should match uniquely (moonshot/kimi2 only).
|
|
bRegex := model.Binding{Namespace: "ns", PublicModel: "kimi2", GroupID: group.ID, Weight: 1, SelectorType: "regex", SelectorValue: "^moonshot/kimi2$", Status: "active"}
|
|
if err := db.Create(&bRegex).Error; err != nil {
|
|
t.Fatalf("create binding regex: %v", err)
|
|
}
|
|
|
|
// Normalize_exact should match p2 (moonshot/kimi2) for "kimi2".
|
|
bNorm := model.Binding{Namespace: "ns", PublicModel: "kimi2-n", GroupID: group.ID, Weight: 1, SelectorType: "normalize_exact", SelectorValue: "kimi2", Status: "active"}
|
|
if err := db.Create(&bNorm).Error; err != nil {
|
|
t.Fatalf("create binding normalize: %v", err)
|
|
}
|
|
|
|
mr := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
svc := NewSyncService(rdb)
|
|
if err := svc.SyncBindings(db); err != nil {
|
|
t.Fatalf("SyncBindings: %v", err)
|
|
}
|
|
|
|
// Regex binding should include both providers mapped to moonshot/kimi2 (p1 also has it).
|
|
raw := mr.HGet("config:bindings", "ns.kimi2")
|
|
var snapRegex bindingSnapshot
|
|
if err := json.Unmarshal([]byte(raw), &snapRegex); err != nil {
|
|
t.Fatalf("unmarshal regex: %v", err)
|
|
}
|
|
if len(snapRegex.Candidates) != 1 {
|
|
t.Fatalf("expected 1 candidate, got %+v", snapRegex.Candidates)
|
|
}
|
|
upstreams := snapRegex.Candidates[0].Upstreams
|
|
if upstreams[jsonID(k1.ID)] != "moonshot/kimi2" || upstreams[jsonID(k2.ID)] != "moonshot/kimi2" {
|
|
t.Fatalf("unexpected regex upstreams: %+v", upstreams)
|
|
}
|
|
|
|
// Normalize_exact binding should include p2 but exclude p1 due to multi-match (moonshot/kimi2 + kimi2).
|
|
raw = mr.HGet("config:bindings", "ns.kimi2-n")
|
|
var snapNorm bindingSnapshot
|
|
if err := json.Unmarshal([]byte(raw), &snapNorm); err != nil {
|
|
t.Fatalf("unmarshal normalize: %v", err)
|
|
}
|
|
if len(snapNorm.Candidates) != 1 {
|
|
t.Fatalf("expected 1 candidate, got %+v", snapNorm.Candidates)
|
|
}
|
|
if len(snapNorm.Candidates[0].Upstreams) != 0 || snapNorm.Candidates[0].Error != "config_error" {
|
|
t.Fatalf("expected config_error with no upstreams, got %+v", snapNorm.Candidates[0])
|
|
}
|
|
}
|