a46fb83054
Closes HIF-3 (阶段 PR B) PR B:handler 真实业务对接 + 邀请钩子(迭代含 R1 修复) - 迁移 02157_lottery_grant_ledger:external_ref UNIQUE 作为发奖幂等键 - log.CommissionTypeLottery=339(架构师批准的新常量) - DispatchRequest.IdempotencyKey(架构师 review 建议第 2 条) - GrantLedger + LedgerService.Reserve:INSERT ON CONFLICT DO NOTHING 幂等 upsert - VPNDurationHandler:ResolveEffectiveUser 归位家庭 owner + UpdateSubscribe,ExpireTime 三分支对齐 grantGiftDays - CommissionHandler:UpdateCommission + WriteCommissionLog(339),发给中奖者本人,不做家庭组归位 - Handler 从 model 层迁到 logic 层(避免 model → logic 反向依赖);noop 保留在 model 层 - InviteHook:fire-and-forget 独立 goroutine + 10s timeout,扫 running 活动的 invite_success 源 - ServiceContext 新增 LotteryChance / LotteryLedger / LotteryInviteHook - activateOrderLogic.handleCommission 两条分支通过 invokeInviteHookIfEligible 助手触发,助手内统一 gate IsNew(架构师 R1 打回后的修复) 架构师 R1 打回:branch B 未按"首次付款激活"gate,续费也会给 referer 发抽奖机会 → 已通过助手函数集中收拢,避免 branch A/B 判断漂移。 测试覆盖率:model 76.5% / handler 73.2% / hook 91.7%;补 4 个回归测试覆盖 IsNew 门槛(含关键的 DoesNotFireOnRenewal)。 线上仍零可见变更:无对外路由,钩子仅在活动 status=running 时生效,Stage 1 全流程无活动记录时 loadRunningActivities 返回空。
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package lottery
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestLedgerService_Reserve_FirstInsertNotExisted(t *testing.T) {
|
|
db, mock, cleanup := newLotteryTestDB(t)
|
|
defer cleanup()
|
|
|
|
svc := NewLedgerService()
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec("INSERT INTO `lottery_grant_ledger`").
|
|
WillReturnResult(sqlmock.NewResult(7, 1))
|
|
mock.ExpectQuery("FROM `lottery_grant_ledger`").
|
|
WithArgs("lottery:100:200", 1).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "external_ref", "handler_type", "user_id", "activity_id", "draw_id", "amount"}).
|
|
AddRow(int64(7), "lottery:100:200", "vpn_duration", int64(42), int64(100), int64(200), int64(3)))
|
|
mock.ExpectCommit()
|
|
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
row, existed, e := svc.Reserve(context.Background(), tx, GrantLedger{
|
|
ExternalRef: "lottery:100:200",
|
|
HandlerType: "vpn_duration",
|
|
UserId: 42,
|
|
ActivityId: 100,
|
|
DrawId: 200,
|
|
Amount: 3,
|
|
})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if existed {
|
|
t.Fatalf("expected not existed")
|
|
}
|
|
if row.Id != 7 {
|
|
t.Fatalf("expected reloaded id=7, got %d", row.Id)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("tx: %v", err)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("expectations: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLedgerService_Reserve_DuplicateExisted(t *testing.T) {
|
|
db, mock, cleanup := newLotteryTestDB(t)
|
|
defer cleanup()
|
|
|
|
svc := NewLedgerService()
|
|
|
|
mock.ExpectBegin()
|
|
mock.ExpectExec("INSERT INTO `lottery_grant_ledger`").
|
|
WillReturnResult(sqlmock.NewResult(0, 0)) // conflict, 0 rows affected
|
|
mock.ExpectQuery("FROM `lottery_grant_ledger`").
|
|
WithArgs("lottery:100:200", 1).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "external_ref", "handler_type", "user_id", "activity_id", "draw_id", "amount"}).
|
|
AddRow(int64(9), "lottery:100:200", "vpn_duration", int64(42), int64(100), int64(200), int64(3)))
|
|
mock.ExpectCommit()
|
|
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
row, existed, e := svc.Reserve(context.Background(), tx, GrantLedger{
|
|
ExternalRef: "lottery:100:200",
|
|
HandlerType: "vpn_duration",
|
|
})
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if !existed {
|
|
t.Fatalf("expected existed=true when INSERT returns 0 rows affected")
|
|
}
|
|
if row.Id != 9 {
|
|
t.Fatalf("expected stored id=9, got %d", row.Id)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("tx: %v", err)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("expectations: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLedgerService_Reserve_RequiresTx(t *testing.T) {
|
|
svc := NewLedgerService()
|
|
if _, _, err := svc.Reserve(context.Background(), nil, GrantLedger{ExternalRef: "x"}); err == nil {
|
|
t.Fatalf("expected error when tx is nil")
|
|
}
|
|
}
|
|
|
|
func TestLedgerService_Reserve_RequiresExternalRef(t *testing.T) {
|
|
db, _, cleanup := newLotteryTestDB(t)
|
|
defer cleanup()
|
|
|
|
svc := NewLedgerService()
|
|
err := db.Transaction(func(tx *gorm.DB) error {
|
|
_, _, e := svc.Reserve(context.Background(), tx, GrantLedger{})
|
|
return e
|
|
})
|
|
if err == nil {
|
|
t.Fatalf("expected error on empty ExternalRef")
|
|
}
|
|
}
|