fix: weekDayKeys formula breaks when Jan 4 falls on Sunday

The naive formula used (jan4.Weekday() - time.Monday) produces -1
when Jan 4 is a Sunday (Weekday()==0), shifting the computed Monday
one week forward. 2026 is affected: Jan 4 is a Sunday, so every
week key in 2026 was mapped to the wrong 7-day range, causing
CloseWeek to look for closed_days on the wrong dates and finding
nothing — resulting in worked_ms=0 and a full -Nh delta.

Fix: use (weekday+6)%7 to get days-since-Monday (Mon=0…Sun=6),
which is always non-negative.

Adds table-driven TestWeekDayKeys covering 2024 (Thu), 2026 (Sun),
and 2023 (Wed) to prevent regression.
This commit is contained in:
2026-04-30 18:20:37 +02:00
parent 185bb0c6a9
commit e37458f513
2 changed files with 27 additions and 12 deletions

View File

@@ -34,18 +34,30 @@ func newFullServices(t *testing.T) (*service.EntryService, *service.DayService,
func TestWeekDayKeys(t *testing.T) {
tz, _ := time.LoadLocation("UTC")
keys, err := service.WeekDayKeysExported("2024-W03", tz)
if err != nil {
t.Fatal(err)
cases := []struct {
weekKey string
wantMon string
wantSun string
}{
{"2024-W03", "2024-01-15", "2024-01-21"}, // Jan 4 = Thursday
{"2026-W18", "2026-04-27", "2026-05-03"}, // Jan 4 = Sunday — was broken
{"2023-W01", "2023-01-02", "2023-01-08"}, // Jan 4 = Wednesday
}
if len(keys) != 7 {
t.Fatalf("expected 7 keys, got %d", len(keys))
}
if keys[0] != "2024-01-15" {
t.Errorf("expected Monday 2024-01-15, got %s", keys[0])
}
if keys[6] != "2024-01-21" {
t.Errorf("expected Sunday 2024-01-21, got %s", keys[6])
for _, tc := range cases {
keys, err := service.WeekDayKeysExported(tc.weekKey, tz)
if err != nil {
t.Fatalf("%s: %v", tc.weekKey, err)
}
if len(keys) != 7 {
t.Fatalf("%s: expected 7 keys, got %d", tc.weekKey, len(keys))
}
if keys[0] != tc.wantMon {
t.Errorf("%s: Monday want %s, got %s", tc.weekKey, tc.wantMon, keys[0])
}
if keys[6] != tc.wantSun {
t.Errorf("%s: Sunday want %s, got %s", tc.weekKey, tc.wantSun, keys[6])
}
}
}