Files
junhong_cmp_fiber/internal/domain/package/terms_test.go
break 8dde55ea6d 修复 code review 问题:提取重复函数、补全缺失测试用例
- 提取 ResolveTermsFromTx 到 service/package,消除 order 和 auto_purchase 中的重复实现
- 补全 domain 测试:新增 from_activation 覆盖用例,现覆盖两种覆盖值
- 补全激活集成测试:新增 from_purchase 立即激活场景、历史记录兼容回退计数器验证
- 补全自动购包集成测试:新增购买后修改套餐配置不影响已有快照的验证

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-23 11:18:38 +09:00

42 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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) {
overridePurchase := constants.PackageExpiryBaseFromPurchase
overrideActivation := constants.PackageExpiryBaseFromActivation
tests := []struct {
name string
pkg model.Package
allocation *model.ShopPackageAllocation
wantErr bool
wantExpiryBase string
}{
{name: "自然月默认", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromActivation, CalendarType: constants.PackageCalendarTypeNaturalMonth, DurationMonths: 12}, wantExpiryBase: constants.PackageExpiryBaseFromActivation},
{name: "from_purchase 覆盖", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromActivation, CalendarType: constants.PackageCalendarTypeByDay, DurationDays: 30}, allocation: &model.ShopPackageAllocation{ExpiryBaseOverride: &overridePurchase}, wantExpiryBase: constants.PackageExpiryBaseFromPurchase},
{name: "from_activation 覆盖", pkg: model.Package{ExpiryBase: constants.PackageExpiryBaseFromPurchase, CalendarType: constants.PackageCalendarTypeByDay, DurationDays: 14}, allocation: &model.ShopPackageAllocation{ExpiryBaseOverride: &overrideActivation}, wantExpiryBase: constants.PackageExpiryBaseFromActivation},
{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)
}
if !test.wantErr && test.wantExpiryBase != "" && got.ExpiryBase != test.wantExpiryBase {
t.Fatalf("生效条件不符预期want=%s got=%s", test.wantExpiryBase, got.ExpiryBase)
}
})
}
}