You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
3.7 KiB

package user
import (
"testing"
"yunyan/comm"
"yunyan/pb"
)
// 开户礼 = VIP 天数 + 算力,两个数额由后台配置(comm.OpsParams)。
func TestApplyNewUserGift(t *testing.T) {
const now int64 = 1_700_000_000
p := comm.DefaultOpsParams()
u := &pb.DBUser{}
applyNewUserGift(u, now, p)
if got, want := u.Vipexptime, now+int64(p.NewUserGiftVipDays)*24*60*60; got != want {
t.Errorf("Vipexptime = %d, want %d(%d 天)", got, want, p.NewUserGiftVipDays)
}
if got, want := u.Computebalance, p.NewUserGiftCompute; got != want {
t.Errorf("Computebalance = %d, want %d", got, want)
}
// 旧三桶必须保持 0:它们已不是闸门,往里写值只会让老后台页面显示出「有余额」的假象
if u.Tradeintegral != 0 || u.Meetintegral != 0 || u.Aichatintegral != 0 {
t.Errorf("旧额度桶不应再发放: Trade=%d Meet=%d Ai=%d", u.Tradeintegral, u.Meetintegral, u.Aichatintegral)
}
}
// 后台把两项都配成 0 = 注册不送任何东西。这是合法配置,不能被当成「没配」而回落到默认值。
func TestApplyNewUserGiftZeroMeansNoGift(t *testing.T) {
const now int64 = 1_700_000_000
u := &pb.DBUser{}
applyNewUserGift(u, now, comm.OpsParams{NewUserGiftVipDays: 0, NewUserGiftCompute: 0})
if u.Vipexptime != 0 || u.Computebalance != 0 {
t.Errorf("配 0 应该什么都不送,得到 Vip=%d Compute=%d", u.Vipexptime, u.Computebalance)
}
}
// 只送算力、不送 VIP 也是合法配置。
func TestApplyNewUserGiftComputeOnly(t *testing.T) {
const now int64 = 1_700_000_000
u := &pb.DBUser{}
applyNewUserGift(u, now, comm.OpsParams{NewUserGiftVipDays: 0, NewUserGiftCompute: 500})
if u.Vipexptime != 0 {
t.Errorf("VIP 天数为 0 时不该设到期时间,得到 %d", u.Vipexptime)
}
if u.Computebalance != 500 {
t.Errorf("Computebalance = %d, want 500", u.Computebalance)
}
}
// 老用户不该被碰:applyNewUserGift 只在「新建用户」分支调用,
// 这里顺带守住它是直接赋值而非累加(新对象都是零值,累加与赋值等价;
// 万一将来有人挪去老用户路径,这个测试会提醒他先想清楚语义)。
func TestApplyTouristEntitlementFloor(t *testing.T) {
const now int64 = 1_700_000_000
floorExp := now + touristVipDays*24*60*60
fresh := &pb.DBUser{}
applyTouristEntitlement(fresh, now)
if fresh.Vipexptime != floorExp || fresh.Computebalance != touristCompute {
t.Fatalf("新游客应正好 1000 天 + 1000 算力,得到 vip=%d compute=%d", fresh.Vipexptime, fresh.Computebalance)
}
if fresh.Tradeintegral != 0 || fresh.Meetintegral != 0 || fresh.Aichatintegral != 0 {
t.Fatalf("游客权益不应再写旧额度桶")
}
rich := &pb.DBUser{Vipexptime: floorExp + 86400, Computebalance: touristCompute + 50}
applyTouristEntitlement(rich, now)
if rich.Vipexptime != floorExp+86400 || rich.Computebalance != touristCompute+50 {
t.Fatalf("已经更高的不应被削低,得到 vip=%d compute=%d", rich.Vipexptime, rich.Computebalance)
}
short := &pb.DBUser{Vipexptime: now + 10*86400, Computebalance: 20}
applyTouristEntitlement(short, now)
if short.Vipexptime != floorExp || short.Computebalance != touristCompute {
t.Fatalf("不足的应抬到游客底线,得到 vip=%d compute=%d", short.Vipexptime, short.Computebalance)
}
}
func TestApplyNewUserGiftIsAssignNotAccumulate(t *testing.T) {
const now int64 = 1_700_000_000
p := comm.DefaultOpsParams()
u := &pb.DBUser{Computebalance: 999, Vipexptime: now + 12345}
applyNewUserGift(u, now, p)
if u.Computebalance != p.NewUserGiftCompute {
t.Errorf("应为赋值语义,得到 Compute=%d", u.Computebalance)
}
if u.Vipexptime != now+int64(p.NewUserGiftVipDays)*24*60*60 {
t.Errorf("VIP 应为赋值语义,得到 %d", u.Vipexptime)
}
}