🐛 Reject week keys that are not real ISO weeks
WeekDayKeys accepted W01 to W53 without checking that the year has that week. Closing 2025-W53, a year with 52 weeks, closed the days of 2026-W01 under a second key and the balance counted them twice. The computed Monday must now map back to the requested week key.
This commit is contained in:
@@ -2,6 +2,7 @@ package tracking
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -333,6 +334,29 @@ func TestWeekDayKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeekDayKeysRejectsWeeksTheYearDoesNotHave(t *testing.T) {
|
||||
// 2025 has 52 weeks: its days around New Year belong to 2025-W01 and 2026-W01.
|
||||
keys, err := WeekDayKeys("2025-W53")
|
||||
if !errors.Is(err, ErrInvalidWeekKey) {
|
||||
t.Fatalf("2025-W53: got %v and %v, want ErrInvalidWeekKey", keys, err)
|
||||
}
|
||||
// 2026 has 53 weeks: the last one ends in 2027.
|
||||
keys, err = WeekDayKeys("2026-W53")
|
||||
if err != nil {
|
||||
t.Fatalf("2026-W53: %v", err)
|
||||
}
|
||||
if len(keys) != 7 || keys[0] != "2026-12-28" || keys[6] != "2027-01-03" {
|
||||
t.Errorf("2026-W53: got %v, want 2026-12-28 to 2027-01-03", keys)
|
||||
}
|
||||
// W53 is valid exactly in the long years.
|
||||
for year, valid := range map[int]bool{2020: true, 2021: false, 2022: false, 2023: false, 2024: false, 2025: false, 2026: true, 2027: false, 2028: false, 2029: false, 2030: false, 2032: true} {
|
||||
_, err := WeekDayKeys(fmt.Sprintf("%d-W53", year))
|
||||
if got := err == nil; got != valid {
|
||||
t.Errorf("%d-W53: valid is %t, want %t (%v)", year, got, valid, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthz(t *testing.T) {
|
||||
e := setup(t)
|
||||
(&tests.ApiScenario{
|
||||
|
||||
Reference in New Issue
Block a user