67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package signinreward
|
|
|
|
import "testing"
|
|
|
|
func TestNormalizeSaveDays(t *testing.T) {
|
|
validDays := []SignInRewardDayConfigInput{
|
|
{DayIndex: 7, GoldAmount: 70},
|
|
{DayIndex: 1, GoldAmount: 10},
|
|
{DayIndex: 3, GoldAmount: 30},
|
|
{DayIndex: 2, GoldAmount: 20},
|
|
{DayIndex: 4, GoldAmount: 40},
|
|
{DayIndex: 5, GoldAmount: 50},
|
|
{DayIndex: 6, GoldAmount: 60},
|
|
}
|
|
|
|
normalized, err := normalizeSaveDays(validDays, true)
|
|
if err != nil {
|
|
t.Fatalf("normalizeSaveDays() error = %v", err)
|
|
}
|
|
for index, item := range normalized {
|
|
expectedDay := index + 1
|
|
if item.DayIndex != expectedDay {
|
|
t.Fatalf("normalized[%d].DayIndex = %d, want %d", index, item.DayIndex, expectedDay)
|
|
}
|
|
}
|
|
|
|
t.Run("enabled config rejects empty reward day", func(t *testing.T) {
|
|
days := append([]SignInRewardDayConfigInput(nil), validDays...)
|
|
days[0].GoldAmount = 0
|
|
if _, err := normalizeSaveDays(days, true); err == nil {
|
|
t.Fatalf("normalizeSaveDays() error = nil, want error")
|
|
}
|
|
})
|
|
|
|
t.Run("disabled config allows empty reward day", func(t *testing.T) {
|
|
days := append([]SignInRewardDayConfigInput(nil), validDays...)
|
|
days[0].GoldAmount = 0
|
|
if _, err := normalizeSaveDays(days, false); err != nil {
|
|
t.Fatalf("normalizeSaveDays() error = %v, want nil", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSignInRewardCycleHelpers(t *testing.T) {
|
|
testCases := []struct {
|
|
streak int
|
|
nextDayIndex int
|
|
cycleProgress int
|
|
}{
|
|
{streak: 0, nextDayIndex: 1, cycleProgress: 0},
|
|
{streak: 1, nextDayIndex: 2, cycleProgress: 1},
|
|
{streak: 6, nextDayIndex: 7, cycleProgress: 6},
|
|
{streak: 7, nextDayIndex: 1, cycleProgress: 7},
|
|
{streak: 8, nextDayIndex: 2, cycleProgress: 1},
|
|
{streak: 14, nextDayIndex: 1, cycleProgress: 7},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
if got := nextSignInRewardDayIndex(testCase.streak); got != testCase.nextDayIndex {
|
|
t.Fatalf("nextSignInRewardDayIndex(%d) = %d, want %d", testCase.streak, got, testCase.nextDayIndex)
|
|
}
|
|
if got := streakToCycleProgress(testCase.streak); got != testCase.cycleProgress {
|
|
t.Fatalf("streakToCycleProgress(%d) = %d, want %d", testCase.streak, got, testCase.cycleProgress)
|
|
}
|
|
}
|
|
}
|