🐛 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:
@@ -26,7 +26,8 @@ func WeekKeyForDayKey(dayKey string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WeekDayKeys returns the day keys (YYYY-MM-DD) of Monday to Sunday of the ISO week
|
// WeekDayKeys returns the day keys (YYYY-MM-DD) of Monday to Sunday of the ISO week
|
||||||
// weekKey (YYYY-Www). It returns ErrInvalidWeekKey for a malformed week key.
|
// weekKey (YYYY-Www). It returns ErrInvalidWeekKey for a malformed week key and
|
||||||
|
// for a week the year does not have, for example 2025-W53.
|
||||||
func WeekDayKeys(weekKey string) ([]string, error) {
|
func WeekDayKeys(weekKey string) ([]string, error) {
|
||||||
m := weekKeyPattern.FindStringSubmatch(weekKey)
|
m := weekKeyPattern.FindStringSubmatch(weekKey)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
@@ -49,6 +50,11 @@ func WeekDayKeys(weekKey string) ([]string, error) {
|
|||||||
for i := range keys {
|
for i := range keys {
|
||||||
keys[i] = monday.AddDate(0, 0, i).Format(dayKeyLayout)
|
keys[i] = monday.AddDate(0, 0, i).Format(dayKeyLayout)
|
||||||
}
|
}
|
||||||
|
// Every year has 52 ISO weeks, but only some have a 53rd one. Without this
|
||||||
|
// check the days of the following W01 would be closed under a second key.
|
||||||
|
if WeekKeyForDayKey(keys[0]) != weekKey {
|
||||||
|
return nil, ErrInvalidWeekKey
|
||||||
|
}
|
||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package tracking
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestHealthz(t *testing.T) {
|
||||||
e := setup(t)
|
e := setup(t)
|
||||||
(&tests.ApiScenario{
|
(&tests.ApiScenario{
|
||||||
|
|||||||
@@ -277,11 +277,14 @@ func TestCloseWeekWithoutSettingsFails(t *testing.T) {
|
|||||||
|
|
||||||
func TestCloseWeekWithInvalidKeyFails(t *testing.T) {
|
func TestCloseWeekWithInvalidKeyFails(t *testing.T) {
|
||||||
e := setup(t)
|
e := setup(t)
|
||||||
for _, key := range []string{"", "2024-W3", "2024-w03", "2024-W54", "2024-03", "2024-W03x"} {
|
for _, key := range []string{"", "2024-W3", "2024-w03", "2024-W54", "2024-03", "2024-W03x", "2025-W53"} {
|
||||||
if _, err := CloseWeek(e.app, e.owner.Id, key, testNow); !errors.Is(err, ErrInvalidWeekKey) {
|
if _, err := CloseWeek(e.app, e.owner.Id, key, testNow); !errors.Is(err, ErrInvalidWeekKey) {
|
||||||
t.Errorf("%q: got %v, want ErrInvalidWeekKey", key, err)
|
t.Errorf("%q: got %v, want ErrInvalidWeekKey", key, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if rows := e.rows("closed_weeks", ""); len(rows) != 0 {
|
||||||
|
t.Errorf("got %d closed weeks, want 0", len(rows))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCloseWeekUsesSettingsEffectiveToday(t *testing.T) {
|
func TestCloseWeekUsesSettingsEffectiveToday(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user