test(host): harden gateway and acceptance validation

This commit is contained in:
phamnazage-jpg
2026-05-21 15:45:55 +08:00
parent 2649956b59
commit 7c6e18f94d
3 changed files with 261 additions and 11 deletions

View File

@@ -657,6 +657,108 @@ func TestCreateChannelWithMock(t *testing.T) {
}
}
func TestUpdateChannelWithMock(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Fatalf("method = %s, want PUT", r.Method)
}
if r.URL.Path != "/api/v1/admin/channels/201" {
t.Fatalf("path = %s, want /api/v1/admin/channels/201", r.URL.Path)
}
var req struct {
ModelMapping map[string]map[string]string `json:"model_mapping"`
ModelPricing []struct {
Platform string `json:"platform"`
Models []string `json:"models"`
BillingMode string `json:"billing_mode"`
} `json:"model_pricing"`
RestrictModels bool `json:"restrict_models"`
BillingModelSource string `json:"billing_model_source"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
t.Fatalf("decode request: %v", err)
}
if req.ModelMapping["openai"]["deepseek-v4-pro"] != "deepseek-v4-pro" {
t.Fatalf("model_mapping = %+v, want openai/deepseek-v4-pro passthrough", req.ModelMapping)
}
if len(req.ModelPricing) != 1 || req.ModelPricing[0].Platform != "openai" || req.ModelPricing[0].BillingMode != "token" {
t.Fatalf("model_pricing = %+v, want openai/token entry", req.ModelPricing)
}
if !req.RestrictModels {
t.Fatal("restrict_models = false, want true")
}
if req.BillingModelSource != "channel_mapped" {
t.Fatalf("billing_model_source = %q, want channel_mapped", req.BillingModelSource)
}
w.Write([]byte(`{"data":{"id":201,"name":"ch"}}`))
}))
defer srv.Close()
client, _ := NewClient(srv.URL, WithAPIKey("k"))
if err := client.UpdateChannel(context.Background(), "201", CreateChannelRequest{
Name: "ch",
GroupIDs: []string{"101"},
ModelMapping: map[string]string{"deepseek-v4-pro": "deepseek-v4-pro"},
ModelPricing: []ChannelModelPricing{{Platform: "openai", Models: []string{"deepseek-v4-pro"}, BillingMode: "token"}},
RestrictModels: true,
BillingModelSource: "channel_mapped",
}); err != nil {
t.Fatal(err)
}
}
func TestCreateChannelRequestMarshalJSONDefaultsPricingPlatform(t *testing.T) {
t.Run("request platform", func(t *testing.T) {
payload, err := json.Marshal(CreateChannelRequest{
Name: "ch",
GroupIDs: []string{"101"},
Platform: "openai",
ModelMapping: map[string]string{"deepseek-v4-pro": "deepseek-v4-pro"},
ModelPricing: []ChannelModelPricing{{Models: []string{"deepseek-v4-pro"}, BillingMode: "token"}},
})
if err != nil {
t.Fatalf("Marshal() error = %v", err)
}
var got struct {
ModelMapping map[string]map[string]string `json:"model_mapping"`
ModelPricing []struct {
Platform string `json:"platform"`
} `json:"model_pricing"`
}
if err := json.Unmarshal(payload, &got); err != nil {
t.Fatalf("Unmarshal() error = %v", err)
}
if got.ModelMapping["openai"]["deepseek-v4-pro"] != "deepseek-v4-pro" {
t.Fatalf("model_mapping = %+v, want openai/deepseek-v4-pro passthrough", got.ModelMapping)
}
if len(got.ModelPricing) != 1 || got.ModelPricing[0].Platform != "openai" {
t.Fatalf("model_pricing = %+v, want platform openai", got.ModelPricing)
}
})
t.Run("openai fallback", func(t *testing.T) {
payload, err := json.Marshal(CreateChannelRequest{
Name: "ch",
GroupIDs: []string{"101"},
ModelMapping: map[string]string{"deepseek-v4-pro": "deepseek-v4-pro"},
ModelPricing: []ChannelModelPricing{{Models: []string{"deepseek-v4-pro"}, BillingMode: "token"}},
})
if err != nil {
t.Fatalf("Marshal() error = %v", err)
}
var got struct {
ModelPricing []struct {
Platform string `json:"platform"`
} `json:"model_pricing"`
}
if err := json.Unmarshal(payload, &got); err != nil {
t.Fatalf("Unmarshal() error = %v", err)
}
if len(got.ModelPricing) != 1 || got.ModelPricing[0].Platform != "openai" {
t.Fatalf("model_pricing = %+v, want platform openai fallback", got.ModelPricing)
}
})
}
func TestCreatePlanWithMock(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req struct {