// 管理后台模块单元测试,验证模型计费、兑换码批次和审计搜索的纯业务规则。 package admin import ( "slices" "testing" ) // TestNormalizeModelPrices 验证按 Token 计费时价格键、单位和金额标准化。 func TestNormalizeModelPrices(t *testing.T) { input := ModelInput{ ModelType: "text", TextBillingMode: "per_token", Prices: []ModelPriceInput{ {PriceKey: " INPUT ", Price: "1.236"}, {PriceKey: "output", Price: 2}, }, } if err := normalizeModelPrices(&input); err != nil { t.Fatalf("normalize model prices: %v", err) } if input.Prices[0].PriceKey != "input" || input.Prices[0].Unit != "M Token" || input.Prices[0].Price != 1.24 { t.Fatalf("unexpected normalized input price: %#v", input.Prices[0]) } } // TestNormalizeModelPricesRejectsIncompleteConfig 验证文本模型缺少价格项时拒绝保存。 func TestNormalizeModelPricesRejectsIncompleteConfig(t *testing.T) { input := ModelInput{ModelType: "text", TextBillingMode: "per_token", Prices: []ModelPriceInput{{PriceKey: "input", Price: 1}}} if err := normalizeModelPrices(&input); err == nil { t.Fatal("expected incomplete token prices to be rejected") } } // TestValidateRedemptionBatchLimits 验证兑换码单码积分与批次数量边界。 func TestValidateRedemptionBatchLimits(t *testing.T) { if points, err := ValidateRedemptionBatchLimits("10.5", 10); err != nil || points != "10.5" { t.Fatalf("expected valid redemption limits, points=%q err=%v", points, err) } for _, test := range []struct { points any quantity int }{{0, 1}, {101, 1}, {10, 0}, {10, 11}} { if _, err := ValidateRedemptionBatchLimits(test.points, test.quantity); err == nil { t.Fatalf("expected limits to be rejected: %#v", test) } } } // TestMatchingAuditResourceTypes 验证中文资源名称可转换为审计记录使用的内部代码。 func TestMatchingAuditResourceTypes(t *testing.T) { for _, test := range []struct { keyword string want string }{ {keyword: "渠道", want: "channels"}, {keyword: "兑换", want: "redemption-codes"}, {keyword: "管理员账号", want: "admin-auth"}, } { if got := matchingAuditResourceTypes(test.keyword); !slices.Contains(got, test.want) { t.Fatalf("keyword %q matched %v, want %q", test.keyword, got, test.want) } } if got := matchingAuditResourceTypes("不存在的资源"); len(got) != 0 { t.Fatalf("unexpected resource matches: %v", got) } }