refactor: introduce clock to make tests reproducible

This commit is contained in:
2026-05-13 20:24:56 +00:00
parent 7e6c47a50e
commit ca5f5f95e2
9 changed files with 140 additions and 84 deletions

View File

@@ -6,6 +6,7 @@ import (
"errors"
"time"
"github.com/wotra/wotra/internal/clock"
"github.com/wotra/wotra/internal/domain"
"github.com/wotra/wotra/internal/store"
)
@@ -24,6 +25,7 @@ type DayService struct {
settings *store.SettingsStore
syncStore *store.SyncStore
tz *time.Location
clock clock.Clock
}
func NewDayService(
@@ -33,6 +35,7 @@ func NewDayService(
settings *store.SettingsStore,
syncStore *store.SyncStore,
tz *time.Location,
clk clock.Clock,
) *DayService {
return &DayService{
entries: entries,
@@ -41,6 +44,7 @@ func NewDayService(
settings: settings,
syncStore: syncStore,
tz: tz,
clock: clk,
}
}
@@ -76,7 +80,7 @@ func (s *DayService) recomputeWeek(ctx context.Context, dayKey string) error {
}
cw.WorkedMs = totalWorkedMs
cw.DeltaMs = totalWorkedMs - cw.ExpectedMs
cw.UpdatedAt = time.Now().UnixMilli()
cw.UpdatedAt = s.clock.Now().UnixMilli()
if err := s.closedWeeks.Upsert(ctx, cw); err != nil {
return err
}
@@ -130,7 +134,7 @@ func (s *DayService) CloseDay(ctx context.Context, dayKey string) (*domain.Close
totalMs += e.DurationMs()
}
now := time.Now().UnixMilli()
now := s.clock.Now().UnixMilli()
cd := &domain.ClosedDay{
DayKey: dayKey,
StartTime: &minStart,
@@ -172,7 +176,7 @@ func (s *DayService) MarkDay(ctx context.Context, dayKey string, kind domain.Day
}
}
now := time.Now().UnixMilli()
now := s.clock.Now().UnixMilli()
cd := &domain.ClosedDay{
DayKey: dayKey,
WorkedMs: workedMs,