diff --git a/internal/scorer/scorer_test.go b/internal/scorer/scorer_test.go index 4c5493d..5d1f6e8 100644 --- a/internal/scorer/scorer_test.go +++ b/internal/scorer/scorer_test.go @@ -33,7 +33,7 @@ func setupGitRepo(t *testing.T, years, months int) (string, string) { t.Fatalf("write file failed: %v", err) } - commitDate := time.Now().AddDate(years, months, 0).Format(time.RFC3339) + commitDate := gitAgeBonusAnchor(years, months) env := append(os.Environ(), "GIT_AUTHOR_DATE="+commitDate, "GIT_COMMITTER_DATE="+commitDate, @@ -396,10 +396,20 @@ func TestFromL5Conversion(t *testing.T) { } // Git age bonus helper edge cases. +// The anchor day exists in every month and is never later than today's day. +func gitAgeBonusAnchor(years, months int) string { + now := time.Now() + safeDay := now.Day() + if safeDay > 28 { + safeDay = 28 + } + anchor := time.Date(now.Year(), now.Month(), safeDay, 0, 0, 0, 0, now.Location()) + return anchor.AddDate(years, months, 0).Format(time.RFC3339) +} + func TestGitAgeBonusExact24Months(t *testing.T) { // Spec says >24 months for +10. Exactly 24 months qualifies for +5 (>12). - date := time.Now().AddDate(-2, 0, 0).Format(time.RFC3339) - bonus := gitAgeBonus(date) + bonus := gitAgeBonus(gitAgeBonusAnchor(-2, 0)) if bonus != 5 { t.Errorf("expected bonus 5 for exactly 24 months, got %d", bonus) } @@ -407,24 +417,21 @@ func TestGitAgeBonusExact24Months(t *testing.T) { func TestGitAgeBonusExact12Months(t *testing.T) { // Spec says >12 months for +5. Exactly 12 months gets 0. - date := time.Now().AddDate(-1, 0, 0).Format(time.RFC3339) - bonus := gitAgeBonus(date) + bonus := gitAgeBonus(gitAgeBonusAnchor(-1, 0)) if bonus != 0 { t.Errorf("expected bonus 0 for exactly 12 months, got %d", bonus) } } func TestGitAgeBonus11Months(t *testing.T) { - date := time.Now().AddDate(0, -11, 0).Format(time.RFC3339) - bonus := gitAgeBonus(date) + bonus := gitAgeBonus(gitAgeBonusAnchor(0, -11)) if bonus != 0 { t.Errorf("expected bonus 0 for 11 months, got %d", bonus) } } func TestGitAgeBonus13Months(t *testing.T) { - date := time.Now().AddDate(-1, -1, 0).Format(time.RFC3339) - bonus := gitAgeBonus(date) + bonus := gitAgeBonus(gitAgeBonusAnchor(-1, -1)) if bonus != 5 { t.Errorf("expected bonus 5 for 13 months, got %d", bonus) }