62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package wallet_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"hyapp/pkg/xerr"
|
|
"hyapp/services/wallet-service/internal/domain/ledger"
|
|
walletservice "hyapp/services/wallet-service/internal/service/wallet"
|
|
"hyapp/services/wallet-service/internal/storage/memory"
|
|
)
|
|
|
|
// TestDebitGiftIsIdempotent 验证扣费命令重复提交时返回原始回执。
|
|
func TestDebitGiftIsIdempotent(t *testing.T) {
|
|
repository := memory.NewRepository()
|
|
repository.SetBalance(10001, 100)
|
|
svc := walletservice.New(walletservice.Config{Currency: "coin"}, repository)
|
|
command := ledger.DebitGiftCommand{
|
|
CommandID: "cmd-1",
|
|
RoomID: "room-1",
|
|
SenderUserID: 10001,
|
|
TargetUserID: 10002,
|
|
GiftID: "rose",
|
|
GiftCount: 2,
|
|
GiftUnitValue: 10,
|
|
}
|
|
|
|
first, err := svc.DebitGift(context.Background(), command)
|
|
if err != nil {
|
|
t.Fatalf("first debit failed: %v", err)
|
|
}
|
|
|
|
second, err := svc.DebitGift(context.Background(), command)
|
|
if err != nil {
|
|
t.Fatalf("second debit failed: %v", err)
|
|
}
|
|
|
|
if first.BillingReceiptID != second.BillingReceiptID || second.BalanceAfter != 80 {
|
|
t.Fatalf("idempotency mismatch: first=%+v second=%+v", first, second)
|
|
}
|
|
}
|
|
|
|
// TestDebitGiftInsufficientBalance 锁定余额不足的稳定 reason。
|
|
func TestDebitGiftInsufficientBalance(t *testing.T) {
|
|
repository := memory.NewRepository()
|
|
repository.SetBalance(10001, 1)
|
|
svc := walletservice.New(walletservice.Config{Currency: "coin"}, repository)
|
|
|
|
_, err := svc.DebitGift(context.Background(), ledger.DebitGiftCommand{
|
|
CommandID: "cmd-2",
|
|
RoomID: "room-1",
|
|
SenderUserID: 10001,
|
|
TargetUserID: 10002,
|
|
GiftID: "rose",
|
|
GiftCount: 1,
|
|
GiftUnitValue: 10,
|
|
})
|
|
if !xerr.IsCode(err, xerr.InsufficientBalance) {
|
|
t.Fatalf("expected INSUFFICIENT_BALANCE, got %v", err)
|
|
}
|
|
}
|