diff --git a/api/v1_challenges_info.go b/api/v1_challenges_info.go index 4b78a194..b5571ce2 100644 --- a/api/v1_challenges_info.go +++ b/api/v1_challenges_info.go @@ -1,7 +1,6 @@ package api import ( - "fmt" "time" "github.com/gofiber/fiber/v2" @@ -25,16 +24,25 @@ type ChallengeInfo struct { } func getWeeklyPoolWindowStart() time.Time { - now := time.Now().UTC() + return weeklyPoolWindowStartAt(time.Now()) +} - if now.Weekday() == time.Monday && now.Hour() < 16 { - year, month, day := now.AddDate(0, 0, -7).Date() - return time.Date(year, month, day, 16, 0, 0, 0, time.UTC) - } else { - // get monday of this week - year, month, day := now.AddDate(0, 0, -int(now.Weekday())+1).Date() - return time.Date(year, month, day, 16, 0, 0, 0, time.UTC) +// weeklyPoolWindowStartAt returns the most recent Monday 16:00 UTC at or +// before `now`: the instant the current weekly reward pool opened. +// +// Go numbers Sunday as 0, so the naive "subtract Weekday, add one" walk to +// Monday landed on the *following* Monday all day Sunday, putting the window +// start in the future and reporting the full pool as remaining every Sunday. +func weeklyPoolWindowStartAt(now time.Time) time.Time { + now = now.UTC() + daysSinceMonday := (int(now.Weekday()) + 6) % 7 // Monday=0 ... Sunday=6 + year, month, day := now.AddDate(0, 0, -daysSinceMonday).Date() + start := time.Date(year, month, day, 16, 0, 0, 0, time.UTC) + if now.Before(start) { + // Monday before 16:00: still last week's pool. + start = start.AddDate(0, 0, -7) } + return start } func (app *ApiServer) v1ChallengesInfo(c *fiber.Ctx) error { @@ -44,7 +52,6 @@ func (app *ApiServer) v1ChallengesInfo(c *fiber.Ctx) error { } weeklyPoolWindowStart := getWeeklyPoolWindowStart() - fmt.Println("weeklyPoolWindowStart", weeklyPoolWindowStart) sql := ` SELECT diff --git a/api/v1_challenges_info_test.go b/api/v1_challenges_info_test.go index 782846c5..d54895fa 100644 --- a/api/v1_challenges_info_test.go +++ b/api/v1_challenges_info_test.go @@ -72,3 +72,30 @@ func TestV1ChallengesInfoInvalidWeeklyPoolMinAmount(t *testing.T) { "error": "weekly_pool_min_amount is invalid", }) } + +func TestWeeklyPoolWindowStartAt(t *testing.T) { + utc := func(y int, m time.Month, d, h int) time.Time { + return time.Date(y, m, d, h, 0, 0, 0, time.UTC) + } + // 2026-09-07 is a Monday. + thisMonday := utc(2026, time.September, 7, 16) + lastMonday := utc(2026, time.August, 31, 16) + + cases := []struct { + name string + now time.Time + want time.Time + }{ + {"Monday before 16:00 is still last week", utc(2026, time.September, 7, 15), lastMonday}, + {"Monday at 16:00 opens the week", thisMonday, thisMonday}, + {"Wednesday", utc(2026, time.September, 9, 12), thisMonday}, + {"Saturday", utc(2026, time.September, 12, 23), thisMonday}, + {"Sunday (Go weekday 0) is the end of the week, not the start of the next", utc(2026, time.September, 13, 21), thisMonday}, + {"Sunday just before the next Monday", utc(2026, time.September, 13, 23), thisMonday}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, weeklyPoolWindowStartAt(tc.now)) + }) + } +}