9933d34bdd
Closes HIF-3 Stage 1 骨架:7 张表迁移 + 门槛规则引擎 + 加权选奖 + 次数入账/消耗(幂等)+ 发奖 handler 抽象与注册表。 - 单测覆盖 75.5%(未覆盖行 = stub handler ErrNotImplemented,合理) - CI 全绿(构建/Vet/测试 + golangci-lint) - 骨架不接入真实业务,vpn_duration/commission handler 在 Dispatch 中返回 ErrNotImplemented;合并后线上零变更 架构师 review 通过,4 项决策已在 issue 上给出: 1. 邀请转化语义 = 首次付款激活 2. 佣金日志类型 = 新增 CommissionTypeLottery=339 3. 家庭组归属 = 穿透到 owner 4. 管理端 IP 白名单 = 不做(推到 nginx/ingress 层) 后续 PR B/C 补真实业务对接 + 用户 API + 后台 CRUD + 集成/并发/概率测试。
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package lottery
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestRegistry_GetMissing(t *testing.T) {
|
|
r := NewRegistry()
|
|
if _, ok := r.Get("nope"); ok {
|
|
t.Fatalf("empty registry should not return handler")
|
|
}
|
|
if _, err := r.MustGet("nope"); !errors.Is(err, ErrHandlerNotRegistered) {
|
|
t.Fatalf("MustGet on missing type should return ErrHandlerNotRegistered, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRegistry_RegisterAndGet(t *testing.T) {
|
|
r := NewRegistry()
|
|
r.Register(NewNoopHandler())
|
|
r.Register(NewVPNDurationStubHandler())
|
|
r.Register(NewCommissionStubHandler())
|
|
|
|
for _, want := range []string{PrizeTypeNone, PrizeTypeVPNDuration, PrizeTypeCommission} {
|
|
h, err := r.MustGet(want)
|
|
if err != nil {
|
|
t.Fatalf("MustGet(%q): %v", want, err)
|
|
}
|
|
if h.Type() != want {
|
|
t.Fatalf("Type mismatch: want %q got %q", want, h.Type())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNoopHandler_AlwaysAutoClaimed(t *testing.T) {
|
|
h := NewNoopHandler()
|
|
if !h.IsAuto() {
|
|
t.Fatal("noop must be auto")
|
|
}
|
|
res, err := h.Dispatch(context.Background(), (*gorm.DB)(nil), DispatchRequest{})
|
|
if err != nil {
|
|
t.Fatalf("noop dispatch err: %v", err)
|
|
}
|
|
if res.State != DispatchStateAutoClaimed {
|
|
t.Fatalf("noop should dispatch as auto_claimed, got %q", res.State)
|
|
}
|
|
}
|
|
|
|
func TestStubHandlers_ReturnNotImplemented(t *testing.T) {
|
|
for _, h := range []PrizeHandler{NewVPNDurationStubHandler(), NewCommissionStubHandler()} {
|
|
_, err := h.Dispatch(context.Background(), (*gorm.DB)(nil), DispatchRequest{})
|
|
if !errors.Is(err, ErrNotImplemented) {
|
|
t.Fatalf("%s: expected ErrNotImplemented, got %v", h.Type(), err)
|
|
}
|
|
}
|
|
}
|