Files
hfb_jysh/backend/internal/modules/order/pricing_cent_test.go
T

103 lines
2.9 KiB
Go

package order
import (
"testing"
"hfb_sys/backend/internal/model"
"gorm.io/datatypes"
)
func TestReadOrderSnapshotCentPrefersCentField(t *testing.T) {
raw := datatypes.JSON([]byte(`{
"asset_summary": {
"price_breakdown": {
"seller_total_price": 353.00,
"seller_total_price_cent": 35301
}
}
}`))
if got := readSnapshotCent(raw, "seller_total_price"); got != 35301 {
t.Fatalf("优先读取分字段失败: got %d, want 35301", got)
}
}
func TestReadOrderSnapshotCentFallsBackToLegacyYuan(t *testing.T) {
raw := datatypes.JSON([]byte(`{
"asset_summary": {
"price_breakdown": {
"consumable_price": 120.00
}
}
}`))
if got := readSnapshotCent(raw, "consumable_price"); got != 12000 {
t.Fatalf("旧元字段回退失败: got %d, want 12000", got)
}
}
func TestBuildOrderPricingUsesCentBreakdown(t *testing.T) {
account := model.GameAccount{
AssetSummary: datatypes.JSON([]byte(`{
"asset_summary": {
"price_breakdown": {
"seller_total_price_cent": 35301,
"platform_markup_amount_cent": 2999,
"buyer_coin_base_price_cent": 26315,
"consumable_price_cent": 12000,
"seller_coin_base_price_cent": 23315
}
}
}`)),
}
pricing := buildOrderPricing(model.RentalListing{PriceCent: 38300}, account)
if pricing.OwnerRentAmountCent != 35301 {
t.Fatalf("OwnerRentAmountCent = %d, want 35301", pricing.OwnerRentAmountCent)
}
if pricing.PlatformFeeCent != 2999 {
t.Fatalf("PlatformFeeCent = %d, want 2999", pricing.PlatformFeeCent)
}
if pricing.PureCoinAmountCent != 26315 {
t.Fatalf("PureCoinAmountCent = %d, want 26315", pricing.PureCoinAmountCent)
}
if pricing.ExtraItemAmountCent != 11985 {
t.Fatalf("ExtraItemAmountCent = %d, want 11985", pricing.ExtraItemAmountCent)
}
if pricing.OwnerPureCoinAmountCent != 23315 {
t.Fatalf("OwnerPureCoinAmountCent = %d, want 23315", pricing.OwnerPureCoinAmountCent)
}
}
func TestCalculateCheckoutSettlementPrefersRatioBPS(t *testing.T) {
// bps 必须优先于旧 float 比例:buyer 1:40、seller 1:45,消耗 180M。
order := model.RentalOrder{
RentAmountCent: 25000,
OwnerRentAmountCent: 22222,
DepositAmountCent: 50000,
AccountSnapshot: datatypes.JSON([]byte(`{
"haf_coin_amount": 100000000,
"asset_summary": {
"price_breakdown": {
"buyer_ratio": 1,
"seller_ratio": 1,
"buyer_ratio_bps": 400000,
"seller_ratio_bps": 450000,
"buyer_coin_base_price": 250,
"seller_coin_base_price": 222.22,
"consumable_price": 0
}
}
}`)),
}
settlement := calculateCheckoutSettlement(order, 0, 180, 0)
if settlement.ActualRentAmountCent != 45000 {
t.Fatalf("ActualRentAmountCent = %d, want 45000", settlement.ActualRentAmountCent)
}
if settlement.OwnerRentIncomeCent != 40000 {
t.Fatalf("OwnerRentIncomeCent = %d, want 40000", settlement.OwnerRentIncomeCent)
}
if settlement.PlatformFeeCent != 5000 {
t.Fatalf("PlatformFeeCent = %d, want 5000", settlement.PlatformFeeCent)
}
}