实现套餐生效条件覆盖与购买快照

This commit is contained in:
2026-07-22 20:51:12 +09:00
parent c7f8b4c702
commit 9818537239
30 changed files with 1092 additions and 149 deletions

View File

@@ -0,0 +1,35 @@
package packagedomain
import (
"testing"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// TestResolveTermsSnapshot 验证默认、覆盖及两种周期规则。
func TestResolveTermsSnapshot(t *testing.T) {
override := constants.PackageExpiryBaseFromPurchase
tests := []struct {
name string
pkg model.Package
allocation *model.ShopPackageAllocation
wantErr bool
}{
{name: "自然月默认", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromActivation, CalendarType: constants.PackageCalendarTypeNaturalMonth, DurationMonths: 12}},
{name: "按天覆盖", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromActivation, CalendarType: constants.PackageCalendarTypeByDay, DurationDays: 30}, allocation: &model.ShopPackageAllocation{ExpiryBaseOverride: &override}},
{name: "非法生效条件", pkg: model.Package{ExpiryBase: "invalid", CalendarType: constants.PackageCalendarTypeByDay, DurationDays: 30}, wantErr: true},
{name: "非法按天时长", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromPurchase, CalendarType: constants.PackageCalendarTypeByDay}, wantErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := ResolveTermsSnapshot(&test.pkg, test.allocation)
if (err != nil) != test.wantErr {
t.Fatalf("错误状态不符合预期:%v", err)
}
if !test.wantErr && !got.IsValid() {
t.Fatalf("快照应有效:%+v", got)
}
})
}
}