From 8d2dd5f355210304390643d0ca3ed73cd4af479c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 8 Aug 2025 10:14:28 -0700 Subject: [PATCH] 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. --- internal/manager/sleep_scheduler/calculations.go | 8 +++++++- internal/manager/sleep_scheduler/sleep_scheduler_test.go | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/manager/sleep_scheduler/calculations.go b/internal/manager/sleep_scheduler/calculations.go index e8c36fa0..569bb402 100644 --- a/internal/manager/sleep_scheduler/calculations.go +++ b/internal/manager/sleep_scheduler/calculations.go @@ -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) } diff --git a/internal/manager/sleep_scheduler/sleep_scheduler_test.go b/internal/manager/sleep_scheduler/sleep_scheduler_test.go index 8a75c0c7..e951fcb5 100644 --- a/internal/manager/sleep_scheduler/sleep_scheduler_test.go +++ b/internal/manager/sleep_scheduler/sleep_scheduler_test.go @@ -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()) {