bindbox-game/internal/api/activity/matching_game_app_test.go

99 lines
3.0 KiB
Go
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"bindbox-game/internal/repository/mysql/model"
"testing"
)
// TestSelectRewardExact 测试对对碰选奖逻辑:精确匹配 TotalPairs == MinScore
func TestSelectRewardExact(t *testing.T) {
// 模拟奖品设置 (使用 Level 作为标识,因为 ActivityRewardSettings 没有 Name 字段)
rewards := []*model.ActivityRewardSettings{
{ID: 1, Level: 1, MinScore: 10, Quantity: 5},
{ID: 2, Level: 2, MinScore: 20, Quantity: 5},
{ID: 3, Level: 3, MinScore: 30, Quantity: 5},
{ID: 4, Level: 4, MinScore: 40, Quantity: 5},
{ID: 5, Level: 5, MinScore: 45, Quantity: 5},
}
testCases := []struct {
name string
totalPairs int64
expectReward *int64 // nil = 无匹配
expectLevel int32
}{
{"精确匹配10对", 10, ptr(int64(1)), 1},
{"精确匹配20对", 20, ptr(int64(2)), 2},
{"精确匹配30对", 30, ptr(int64(3)), 3},
{"精确匹配40对", 40, ptr(int64(4)), 4},
{"精确匹配45对", 45, ptr(int64(5)), 5},
{"15对-无匹配", 15, nil, 0},
{"25对-无匹配", 25, nil, 0},
{"35对-无匹配", 35, nil, 0},
{"50对-无匹配", 50, nil, 0},
{"0对-无匹配", 0, nil, 0},
}
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("期望无匹配,但得到奖品: Level=%d (ID=%d)", candidate.Level, 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.Level != tc.expectLevel {
t.Errorf("期望奖品Level=%d实际=%d", tc.expectLevel, candidate.Level)
}
}
})
}
}
// TestSelectRewardWithZeroQuantity 测试库存为0时不匹配
func TestSelectRewardWithZeroQuantity(t *testing.T) {
rewards := []*model.ActivityRewardSettings{
{ID: 1, Level: 1, MinScore: 10, Quantity: 0}, // 库存为0
{ID: 2, Level: 2, MinScore: 20, Quantity: 5},
}
// 即使精确匹配库存为0也不应匹配
candidate := selectRewardExact(rewards, 10)
if candidate != nil {
t.Errorf("库存为0时不应匹配但得到: Level=%d", candidate.Level)
}
// 库存>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
}