package systemconfig import ( "encoding/json" "strings" "testing" ) func TestDecodeCP1252Mojibake(t *testing.T) { original := `{"label":"6级子弹","price":"0.26元/发","punct":"(:,)"}` mojibake := encodeCP1252Mojibake(original) decoded, ok := decodeCP1252Mojibake(mojibake) if !ok { t.Fatal("期望识别出 CP1252 乱码") } if decoded != original { t.Fatalf("还原结果 = %q, want %q", decoded, original) } if _, ok := decodeCP1252Mojibake(original); ok { t.Fatal("正常中文不应被误判为乱码") } if _, ok := decodeCP1252Mojibake("plain ascii"); ok { t.Fatal("纯 ASCII 不应被误判为乱码") } } func TestRepairMojibakeJSONRepairsIndividualFields(t *testing.T) { raw := `{"normal":"正常字段","broken":"` + encodeCP1252Mojibake("6级子弹") + `"}` repaired := repairMojibakeJSON(raw) var payload map[string]string if err := json.Unmarshal([]byte(repaired), &payload); err != nil { t.Fatalf("还原后的 JSON 无法解析: %v", err) } if payload["normal"] != "正常字段" { t.Fatalf("正常字段被破坏: %q", payload["normal"]) } if payload["broken"] != "6级子弹" { t.Fatalf("乱码字段未还原: %q", payload["broken"]) } } func TestNormalizePublishOptionsValueMigratesLegacyQuantityItems(t *testing.T) { legacy := `{ "server_options": ["QQ"], "quantity_items": [ {"key": "kit5", "label": "5级全装套数量", "price": "5元/个"}, {"key": "level6Ammo", "label": "6级子弹", "price": "0.26元/发"} ] }` normalized, err := normalizePublishOptionsValue(legacy) if err != nil { t.Fatalf("规范化失败: %v", err) } var options PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &options); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if options.SchemaVersion != publishOptionsSchemaVersion { t.Fatalf("schema_version = %d, want %d", options.SchemaVersion, publishOptionsSchemaVersion) } for _, item := range options.QuantityItems { if item.Key == "kit5" { t.Fatal("旧版 kit5 选项应被迁移掉") } } if !hasQuantityItemKey(options.QuantityItems, "level6Ammo") { t.Fatal("迁移后的默认选项缺少 level6Ammo") } } func TestNormalizePublishOptionsValuePreservesCurrentSchema(t *testing.T) { options := DefaultPublishOptions() for index := range options.ScreenshotSlots { if options.ScreenshotSlots[index].Key == "tencentSecurity" { options.ScreenshotSlots[index].Required = true } } options.QuantityItems = append(options.QuantityItems, PublishQuantityItem{ Key: "kit5", Label: "自定义旧选项", Price: "9元/个", }) raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造当前版本配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !hasQuantityItemKey(got.QuantityItems, "kit5") { t.Fatal("当前版本配置中的自定义项不应被旧版迁移清掉") } for _, slot := range got.ScreenshotSlots { if slot.Key == "tencentSecurity" && !slot.Required { t.Fatal("当前版本配置中 tencentSecurity.required=true 不应被覆盖") } } } func TestNormalizePublishOptionsValueAddsSpecialSkinAndLevelRules(t *testing.T) { options := DefaultPublishOptions() options.SchemaVersion = 2 for index := range options.SkinGroups { options.SkinGroups[index].SpecialPriceOptions = nil options.SkinGroups[index].SpecialRatioAdjustment = 0 } options.RatioConfig.LevelRatioRules = nil raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造旧版配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if got.SchemaVersion != publishOptionsSchemaVersion { t.Fatalf("schema_version = %d, want %d", got.SchemaVersion, publishOptionsSchemaVersion) } if len(got.RatioConfig.LevelRatioRules) != 4 { t.Fatalf("level_ratio_rules = %#v, want 4 default rules", got.RatioConfig.LevelRatioRules) } var melee *PublishOptionGroup for index := range got.SkinGroups { if got.SkinGroups[index].Key == "melee" { melee = &got.SkinGroups[index] break } } if melee == nil || len(melee.SpecialPriceOptions) != 2 || melee.SpecialPriceOptions[0] != "龙牙" || melee.SpecialPriceOptions[1] != "赤霄" || melee.SpecialRatioAdjustment != -1 { t.Fatalf("melee special pricing = %#v", melee) } } func TestDefaultPublishOptionsMatchesCurrentProductionBaseline(t *testing.T) { options := DefaultPublishOptions() if len(options.ServerOptions) != 2 || options.ServerOptions[0] != "QQ" || options.ServerOptions[1] != "微信" { t.Fatalf("server_options = %#v", options.ServerOptions) } if len(options.LevelOptions) != 4 || options.LevelOptions[0] != "4级" || options.LevelOptions[3] != "7级" { t.Fatalf("level_options = %#v", options.LevelOptions) } awmPrice := "" for _, item := range options.QuantityItems { if item.Key == "awmAmmo" { awmPrice = item.Price } } if awmPrice != "0.7元/发" { t.Fatalf("awm price = %q, want 0.7元/发", awmPrice) } if options.DepositRecommend.BaseAmount != 200 { t.Fatalf("deposit base amount = %v, want 200", options.DepositRecommend.BaseAmount) } if len(options.RatioConfig.InsuranceBaseRatios) != 4 || options.RatioConfig.InsuranceBaseRatios[0].Ratio != 45 || options.RatioConfig.InsuranceBaseRatios[3].Ratio != 51 { t.Fatalf("insurance ratios = %#v", options.RatioConfig.InsuranceBaseRatios) } melee := options.SkinGroups[0] if len(melee.Options) != 14 || melee.Options[5] != "赤霄" || len(melee.SpecialPriceOptions) != 2 || melee.SpecialPriceOptions[0] != "龙牙" { t.Fatalf("melee production baseline = %#v", melee) } } func TestNormalizePublishOptionsValuePreservesExplicitEmptySpecialSkins(t *testing.T) { options := DefaultPublishOptions() options.SchemaVersion = publishOptionsSchemaVersion for index := range options.SkinGroups { if options.SkinGroups[index].Key == "melee" { options.SkinGroups[index].SpecialPriceOptions = []string{} options.SkinGroups[index].SpecialRatioAdjustment = 0 } } raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } for _, group := range got.SkinGroups { if group.Key != "melee" { continue } if len(group.SpecialPriceOptions) != 0 || group.SpecialRatioAdjustment != 0 { t.Fatalf("显式清空的特殊皮肤配置被覆盖: %#v", group) } return } t.Fatal("未找到 melee 皮肤分组") } func TestNormalizePublishOptionsValuePreservesProductionFieldsWhenAddingV3Defaults(t *testing.T) { raw := `{ "schema_version": 2, "unlock_demulan_enabled": true, "next_season_pass_enabled": false, "server_options": ["QQ", "微信"], "level_options": ["4级", "5级", "6级", "7级"], "skin_groups": [{ "key": "melee", "title": "近战皮肤", "options": ["北极星", "龙牙", "赤霄"] }], "quantity_items": [{"key": "awmAmmo", "label": "AWM子弹", "price": "0.7元/发"}], "deposit_recommend_config": { "base_amount": 999, "skin_group_rules": [{"group_key": "melee", "label": "刀皮", "amount_per_item": 321}] }, "ratio_config": { "premium_review_ratio_markup": 0, "insurance_base_ratios": [{"insurance": "3*3", "ratio": 123}], "config_items": [{"key": "melee", "label": "刀皮", "kind": "skin_group", "group_key": "melee", "missing_penalty": 1}] } }` normalized, err := normalizePublishOptionsValue(raw) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !got.UnlockDemulanEnabled || got.NextSeasonPassEnabled { t.Fatalf("生产布尔开关被覆盖: unlock=%v next=%v", got.UnlockDemulanEnabled, got.NextSeasonPassEnabled) } if len(got.ServerOptions) != 2 || got.ServerOptions[1] != "微信" { t.Fatalf("生产区服被覆盖: %#v", got.ServerOptions) } if len(got.QuantityItems) != 1 || got.QuantityItems[0].Price != "0.7元/发" { t.Fatalf("生产消耗品配置被覆盖: %#v", got.QuantityItems) } if got.DepositRecommend.BaseAmount != 999 || len(got.DepositRecommend.SkinGroupRules) != 1 || got.DepositRecommend.SkinGroupRules[0].AmountPerItem != 321 { t.Fatalf("生产推荐押金配置被覆盖: %#v", got.DepositRecommend) } if got.RatioConfig.PremiumReviewRatioMarkup != 0 || len(got.RatioConfig.InsuranceBaseRatios) != 1 || got.RatioConfig.InsuranceBaseRatios[0].Ratio != 123 { t.Fatalf("生产价格配置被覆盖: %#v", got.RatioConfig) } if len(got.RatioConfig.LevelRatioRules) != 4 { t.Fatalf("v3 等级规则未补齐: %#v", got.RatioConfig.LevelRatioRules) } if len(got.SkinGroups) != 1 || len(got.SkinGroups[0].SpecialPriceOptions) != 2 || got.SkinGroups[0].SpecialRatioAdjustment != -1 { t.Fatalf("v3 特殊皮肤配置未补齐或覆盖了生产皮肤名单: %#v", got.SkinGroups) } } func TestNormalizePublishOptionsValueDefaultsQualityOwnerRatioDiscount(t *testing.T) { legacy := `{ "schema_version": 2, "ratio_config": { "insurance_base_ratios": [{"insurance": "3*3", "ratio": 40}] } }` normalized, err := normalizePublishOptionsValue(legacy) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if got.RatioConfig.QualityOwnerRatioDiscount != 1 { t.Fatalf( "quality_owner_ratio_discount = %v, want 1", got.RatioConfig.QualityOwnerRatioDiscount, ) } if got.RatioConfig.PremiumReviewRatioMarkup != 0 { t.Fatalf( "premium_review_ratio_markup = %v, want 0", got.RatioConfig.PremiumReviewRatioMarkup, ) } } func TestNormalizePublishOptionsValueDefaultsDepositTooltip(t *testing.T) { normalized, err := normalizePublishOptionsValue(`{"schema_version":2}`) if err != nil { t.Fatalf("规范化失败: %v", err) } var options PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &options); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !strings.Contains(options.PriceConfig.DepositTooltip, "系统推荐押金(基础覆盖)") { t.Fatalf("押金说明默认文案缺失: %q", options.PriceConfig.DepositTooltip) } } func TestNormalizePublishOptionsValueDefaultsCompensationNote(t *testing.T) { normalized, err := normalizePublishOptionsValue(`{"schema_version":2}`) if err != nil { t.Fatalf("规范化失败: %v", err) } var options PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &options); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !strings.Contains(options.PriceConfig.CompensationNote, "普通赔付方案如下") { t.Fatalf("赔付方案说明默认文案缺失: %q", options.PriceConfig.CompensationNote) } } func TestNormalizePublishOptionsValueDefaultsPremiumZoneTooltip(t *testing.T) { normalized, err := normalizePublishOptionsValue(`{"schema_version":2}`) if err != nil { t.Fatalf("规范化失败: %v", err) } var options PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &options); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !strings.Contains(options.PriceConfig.PremiumZoneTooltip, "极品专区是面向优质号主") { t.Fatalf("极品专区说明默认文案缺失: %q", options.PriceConfig.PremiumZoneTooltip) } } func TestNormalizePublishOptionsValueMigratesInsuranceBillingBaseCopy(t *testing.T) { options := DefaultPublishOptions() options.PriceConfig.CompensationNote = "注:选择此方案,需额外抽取订单佣金的8.0%作为服务费:押金须达到推荐押金以上方可购买。" options.PriceConfig.PremiumZoneTooltip = "4. 保险服务:服务费率按当前配置执行,并从卖家实际到手租金中扣除。" raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !strings.Contains(got.PriceConfig.CompensationNote, "按实际纯币结算金额计算") { t.Fatalf("赔付说明未迁移纯币计费口径: %q", got.PriceConfig.CompensationNote) } if strings.Contains(got.PriceConfig.CompensationNote, "订单佣金") { t.Fatalf("赔付说明仍含旧计费口径: %q", got.PriceConfig.CompensationNote) } if !strings.Contains(got.PriceConfig.PremiumZoneTooltip, "额外物品不计入保险基数") { t.Fatalf("专区说明未迁移纯币计费口径: %q", got.PriceConfig.PremiumZoneTooltip) } } func TestNormalizePublishOptionsValuePreservesExplicitZeroPremiumReviewRatioMarkup(t *testing.T) { options := DefaultPublishOptions() options.RatioConfig.PremiumReviewRatioMarkup = 0 raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if got.RatioConfig.PremiumReviewRatioMarkup != 0 { t.Fatalf( "premium_review_ratio_markup = %v, want explicit 0", got.RatioConfig.PremiumReviewRatioMarkup, ) } } func TestNormalizePublishOptionsValuePreservesExplicitZeroQualityOwnerDiscount(t *testing.T) { options := DefaultPublishOptions() options.RatioConfig.QualityOwnerRatioDiscount = 0 raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if got.RatioConfig.QualityOwnerRatioDiscount != 0 { t.Fatalf( "quality_owner_ratio_discount = %v, want explicit 0", got.RatioConfig.QualityOwnerRatioDiscount, ) } } func TestNormalizePublishOptionsValueMigratesSchemaV1Switches(t *testing.T) { legacy := `{"schema_version":1,"next_season_pass_enabled":false,"server_options":["QQ"]}` normalized, err := normalizePublishOptionsValue(legacy) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if got.SchemaVersion != publishOptionsSchemaVersion { t.Fatalf("schema_version = %d, want %d", got.SchemaVersion, publishOptionsSchemaVersion) } if got.UnlockDemulanEnabled { t.Fatal("v1 升级后「是否解锁德穆兰」应默认关闭") } if !got.NextSeasonPassEnabled { t.Fatal("v1 升级后「下赛季是否购买通行证」应默认打开") } } func TestNormalizePublishOptionsValueKeepsSchemaV2Switches(t *testing.T) { options := DefaultPublishOptions() options.UnlockDemulanEnabled = true options.NextSeasonPassEnabled = false raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } normalized, err := normalizePublishOptionsValue(string(raw)) if err != nil { t.Fatalf("规范化失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !got.UnlockDemulanEnabled { t.Fatal("v2 配置中后台手动开启的德穆兰开关不应被覆盖") } if got.NextSeasonPassEnabled { t.Fatal("v2 配置中后台手动关闭的通行证开关不应被覆盖") } } func TestNormalizePublishOptionsValueRepairsMojibake(t *testing.T) { options := DefaultPublishOptions() raw, err := json.Marshal(options) if err != nil { t.Fatalf("构造配置失败: %v", err) } mojibake := encodeCP1252Mojibake(string(raw)) normalized, err := normalizePublishOptionsValue(mojibake) if err != nil { t.Fatalf("规范化乱码配置失败: %v", err) } var got PublishOptionsDTO if err := json.Unmarshal([]byte(normalized), &got); err != nil { t.Fatalf("解析规范化结果失败: %v", err) } if !containsString(got.ServerOptions, "微信") { t.Fatalf("乱码未恢复,server_options = %#v", got.ServerOptions) } if got.SchemaVersion != publishOptionsSchemaVersion { t.Fatalf("schema_version = %d, want %d", got.SchemaVersion, publishOptionsSchemaVersion) } } func encodeCP1252Mojibake(value string) string { var builder strings.Builder for _, item := range []byte(value) { if item >= 0x80 && item <= 0x9F { builder.WriteRune(cp1252HighRunes[item-0x80]) continue } builder.WriteRune(rune(item)) } return builder.String() } func hasQuantityItemKey(items []PublishQuantityItem, key string) bool { for _, item := range items { if item.Key == key { return true } } return false } func containsString(items []string, target string) bool { for _, item := range items { if item == target { return true } } return false }