package order import ( "context" "testing" "github.com/perfect-panel/server/internal/model/promo" "github.com/perfect-panel/server/internal/svc" "github.com/perfect-panel/server/internal/types" "gorm.io/gorm" ) type fakePromoModel struct { rules []*promo.RuleWithPrice lastSubscribeID int64 lastQuantity int64 requireQuantity int64 quantityMismatch []*promo.RuleWithPrice } func (m *fakePromoModel) QueryEligibleRules(_ context.Context, subscribeID int64, quantity int64) ([]*promo.RuleWithPrice, error) { m.lastSubscribeID = subscribeID m.lastQuantity = quantity if m.requireQuantity > 0 && quantity != m.requireQuantity { return m.quantityMismatch, nil } return m.rules, nil } func (m *fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error { return nil } func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) { svcCtx := &svc.ServiceContext{ DB: &gorm.DB{}, PromoModel: &fakePromoModel{rules: []*promo.RuleWithPrice{ { Rule: promo.Rule{ Id: 9, Name: "campaign", Type: promo.RuleTypeCampaign, Enabled: true, }, PromoPrice: 600, }, }}, } result, err := calculatePurchasePrice( context.Background(), svcCtx, 1, 2, 1000, 3, []types.SubscribeDiscount{{Quantity: 3, Discount: 50}}, true, true, ) if err != nil { t.Fatalf("calculatePurchasePrice returned error: %v", err) } if result.OriginalPrice != 3000 { t.Fatalf("OriginalPrice = %d, want 3000", result.OriginalPrice) } if result.PayableBase != 1800 { t.Fatalf("PayableBase = %d, want 1800", result.PayableBase) } if result.DiscountAmount != 0 { t.Fatalf("DiscountAmount = %d, want 0", result.DiscountAmount) } if result.PromoRuleId != 9 { t.Fatalf("PromoRuleId = %d, want 9", result.PromoRuleId) } if result.PromoDiscount != 1200 { t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount) } } func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) { svcCtx := &svc.ServiceContext{ DB: &gorm.DB{}, PromoModel: &fakePromoModel{rules: []*promo.RuleWithPrice{ { Rule: promo.Rule{ Id: 10, Name: "invalid campaign", Type: promo.RuleTypeCampaign, Enabled: true, }, PromoPrice: 1000, }, }}, } result, err := calculatePurchasePrice( context.Background(), svcCtx, 1, 2, 1000, 3, []types.SubscribeDiscount{{Quantity: 3, Discount: 50}}, true, true, ) if err != nil { t.Fatalf("calculatePurchasePrice returned error: %v", err) } if result.PayableBase != 1500 { t.Fatalf("PayableBase = %d, want 1500", result.PayableBase) } if result.DiscountAmount != 1500 { t.Fatalf("DiscountAmount = %d, want 1500", result.DiscountAmount) } if result.PromoRuleId != 0 || result.PromoDiscount != 0 { t.Fatalf("promo fields = (%d, %d), want (0, 0)", result.PromoRuleId, result.PromoDiscount) } } func TestCalculatePurchasePricePassesQuantityToPromoEvaluation(t *testing.T) { promoModel := &fakePromoModel{ requireQuantity: 6, rules: []*promo.RuleWithPrice{ { Rule: promo.Rule{ Id: 11, Name: "quantity campaign", Type: promo.RuleTypeCampaign, Enabled: true, }, PromoPrice: 500, }, }, } svcCtx := &svc.ServiceContext{ DB: &gorm.DB{}, PromoModel: promoModel, } result, err := calculatePurchasePrice( context.Background(), svcCtx, 1, 2, 1000, 6, []types.SubscribeDiscount{{Quantity: 6, Discount: 80}}, true, true, ) if err != nil { t.Fatalf("calculatePurchasePrice returned error: %v", err) } if promoModel.lastSubscribeID != 2 { t.Fatalf("lastSubscribeID = %d, want 2", promoModel.lastSubscribeID) } if promoModel.lastQuantity != 6 { t.Fatalf("lastQuantity = %d, want 6", promoModel.lastQuantity) } if result.PayableBase != 3000 { t.Fatalf("PayableBase = %d, want 3000", result.PayableBase) } if result.PromoRuleId != 11 { t.Fatalf("PromoRuleId = %d, want 11", result.PromoRuleId) } }