package packagepkg import ( "testing" "github.com/break/junhong_cmp_fiber/internal/model" "github.com/break/junhong_cmp_fiber/pkg/constants" ) // TestValidateExpiryBaseOverride 验证覆盖字段必须显式提交且仅接受既定枚举。 func TestValidateExpiryBaseOverride(t *testing.T) { fromPurchase := constants.PackageExpiryBaseFromPurchase invalid := "from_realname" tests := []struct { name string value *string submitted bool wantValue *string wantErr bool }{ {name: "字段缺失", wantErr: true}, {name: "跟随默认", submitted: true}, {name: "购买即生效", value: &fromPurchase, submitted: true, wantValue: &fromPurchase}, {name: "非法枚举", value: &invalid, submitted: true, wantErr: true}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { got, err := ValidateExpiryBaseOverride(test.value, test.submitted) if (err != nil) != test.wantErr { t.Fatalf("错误状态不符合预期:%v", err) } if test.wantValue != nil && (got == nil || *got != *test.wantValue) { t.Fatalf("覆盖值不符合预期:%v", got) } if test.wantValue == nil && !test.wantErr && got != nil { t.Fatalf("期望跟随默认,实际为:%v", *got) } }) } } // TestEffectiveExpiryBase 验证覆盖优先于套餐默认值。 func TestEffectiveExpiryBase(t *testing.T) { pkg := &model.Package{ExpiryBase: constants.PackageExpiryBaseFromActivation} if got := EffectiveExpiryBase(pkg, nil); got != constants.PackageExpiryBaseFromActivation { t.Fatalf("无覆盖时应使用套餐默认值,实际为 %s", got) } override := constants.PackageExpiryBaseFromPurchase allocation := &model.ShopPackageAllocation{ExpiryBaseOverride: &override} if got := EffectiveExpiryBase(pkg, allocation); got != constants.PackageExpiryBaseFromPurchase { t.Fatalf("有覆盖时应使用覆盖值,实际为 %s", got) } pkg.ExpiryBase = constants.PackageExpiryBaseFromPurchase if got := EffectiveExpiryBase(pkg, allocation); got != constants.PackageExpiryBaseFromPurchase { t.Fatalf("套餐默认值变化不应改变覆盖结果,实际为 %s", got) } }