Manager: fix sleep scheduler unit test

The unit test was using a mocked "now" in a hardcoded timezone (UTC+2),
while the code under test was actually using the local timezone of the
computer.

Also the schedule computations are now explicitly only in local time.
This commit is contained in:
Sybren A. Stüvel 2025-08-08 10:14:28 -07:00
parent 01a97862db
commit 8d2dd5f355
2 changed files with 8 additions and 3 deletions

View File

@ -18,6 +18,9 @@ func scheduledWorkerStatus(now time.Time, sched *persistence.SleepSchedule) api.
return api.WorkerStatusAwake
}
// This function should always work with localized time.
now = now.In(time.Local)
tod := time_of_day.MakeTimeOfDay(now)
if !sched.IsActive {
@ -58,10 +61,13 @@ func cleanupDaysOfWeek(daysOfWeek string) string {
// Return a timestamp when the next scheck for this schedule is due.
func calculateNextCheck(now time.Time, schedule persistence.SleepSchedule) time.Time {
// This function should always work with localized time.
now = now.In(time.Local)
// calcNext returns the given time of day on "today" if that hasn't passed
// yet, otherwise on "tomorrow".
calcNext := func(tod time_of_day.TimeOfDay) time.Time {
nextCheck := tod.OnDate(now).In(time.Local)
nextCheck := tod.OnDate(now)
if nextCheck.Before(now) {
nextCheck = nextCheck.AddDate(0, 0, 1)
}

View File

@ -280,8 +280,7 @@ func testFixtures(t *testing.T) (*SleepScheduler, TestMocks, context.Context) {
ctx := context.Background()
mockedClock := clock.NewMock()
mockedNow, err := time.Parse(time.RFC3339, "2022-06-07T11:14:47+02:00")
require.NoError(t, err)
mockedNow := time.Date(2022, 6, 7, 11, 14, 47, 0, time.Local)
mockedClock.Set(mockedNow)
if !assert.Equal(t, time.Tuesday.String(), mockedNow.Weekday().String()) {