Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 15s
99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
package app
|
||
|
||
import (
|
||
"bindbox-game/internal/repository/mysql/model"
|
||
"testing"
|
||
)
|
||
|
||
// TestSelectRewardExact 测试对对碰选奖逻辑:精确匹配 TotalPairs == MinScore
|
||
func TestSelectRewardExact(t *testing.T) {
|
||
// 模拟奖品设置
|
||
rewards := []*model.ActivityRewardSettings{
|
||
{ID: 1, Name: "奖品A-10对", MinScore: 10, Quantity: 5},
|
||
{ID: 2, Name: "奖品B-20对", MinScore: 20, Quantity: 5},
|
||
{ID: 3, Name: "奖品C-30对", MinScore: 30, Quantity: 5},
|
||
{ID: 4, Name: "奖品D-40对", MinScore: 40, Quantity: 5},
|
||
{ID: 5, Name: "奖品E-45对", MinScore: 45, Quantity: 5},
|
||
}
|
||
|
||
testCases := []struct {
|
||
name string
|
||
totalPairs int64
|
||
expectReward *int64 // nil = 无匹配
|
||
expectName string
|
||
}{
|
||
{"精确匹配10对", 10, ptr(int64(1)), "奖品A-10对"},
|
||
{"精确匹配20对", 20, ptr(int64(2)), "奖品B-20对"},
|
||
{"精确匹配30对", 30, ptr(int64(3)), "奖品C-30对"},
|
||
{"精确匹配40对", 40, ptr(int64(4)), "奖品D-40对"},
|
||
{"精确匹配45对", 45, ptr(int64(5)), "奖品E-45对"},
|
||
{"15对-无匹配", 15, nil, ""},
|
||
{"25对-无匹配", 25, nil, ""},
|
||
{"35对-无匹配", 35, nil, ""},
|
||
{"50对-无匹配", 50, nil, ""},
|
||
{"0对-无匹配", 0, nil, ""},
|
||
}
|
||
|
||
for _, tc := range testCases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
candidate := selectRewardExact(rewards, tc.totalPairs)
|
||
|
||
if tc.expectReward == nil {
|
||
if candidate != nil {
|
||
t.Errorf("期望无匹配,但得到奖品: %s (ID=%d)", candidate.Name, candidate.ID)
|
||
}
|
||
} else {
|
||
if candidate == nil {
|
||
t.Errorf("期望匹配奖品ID=%d,但无匹配", *tc.expectReward)
|
||
} else if candidate.ID != *tc.expectReward {
|
||
t.Errorf("期望奖品ID=%d,实际=%d", *tc.expectReward, candidate.ID)
|
||
} else if candidate.Name != tc.expectName {
|
||
t.Errorf("期望奖品名=%s,实际=%s", tc.expectName, candidate.Name)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestSelectRewardWithZeroQuantity 测试库存为0时不匹配
|
||
func TestSelectRewardWithZeroQuantity(t *testing.T) {
|
||
rewards := []*model.ActivityRewardSettings{
|
||
{ID: 1, Name: "奖品A-10对", MinScore: 10, Quantity: 0}, // 库存为0
|
||
{ID: 2, Name: "奖品B-20对", MinScore: 20, Quantity: 5},
|
||
}
|
||
|
||
// 即使精确匹配,库存为0也不应匹配
|
||
candidate := selectRewardExact(rewards, 10)
|
||
if candidate != nil {
|
||
t.Errorf("库存为0时不应匹配,但得到: %s", candidate.Name)
|
||
}
|
||
|
||
// 库存>0应正常匹配
|
||
candidate = selectRewardExact(rewards, 20)
|
||
if candidate == nil {
|
||
t.Error("库存>0时应匹配,但无匹配")
|
||
} else if candidate.ID != 2 {
|
||
t.Errorf("期望ID=2,实际=%d", candidate.ID)
|
||
}
|
||
}
|
||
|
||
// selectRewardExact 精确匹配选奖逻辑(从matching_game_app.go提取)
|
||
// 这是 CheckMatchingGame 中实际使用的逻辑
|
||
func selectRewardExact(rewards []*model.ActivityRewardSettings, totalPairs int64) *model.ActivityRewardSettings {
|
||
for _, r := range rewards {
|
||
if r.Quantity <= 0 {
|
||
continue
|
||
}
|
||
// 精确匹配:用户消除的对子数 == 奖品设置的 MinScore
|
||
if totalPairs == r.MinScore {
|
||
return r
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// ptr 辅助函数:创建int64指针
|
||
func ptr(v int64) *int64 {
|
||
return &v
|
||
}
|