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

@@ -7,6 +7,7 @@ import (
"fmt"
"time"
"github.com/wotra/wotra/internal/clock"
"github.com/wotra/wotra/internal/domain"
"github.com/wotra/wotra/internal/store"
)
@@ -30,6 +31,7 @@ type WeekService struct {
}
rawDB *sql.DB
tz *time.Location
clock clock.Clock
}
func NewWeekService(
@@ -41,6 +43,7 @@ func NewWeekService(
syncStore *store.SyncStore,
rawDB *sql.DB,
tz *time.Location,
clk clock.Clock,
) *WeekService {
return &WeekService{
closedDays: closedDays,
@@ -51,6 +54,7 @@ func NewWeekService(
settings: settings,
rawDB: rawDB,
tz: tz,
clock: clk,
}
}
@@ -185,7 +189,7 @@ func (s *WeekService) CloseWeek(ctx context.Context, weekKey string) (*domain.Cl
// Get settings effective at close time (today), not necessarily at the
// start of the week. This ensures settings changes made mid-week are
// reflected when the week is closed.
set, err := s.settings.Current(ctx, time.Now().In(s.tz).Format("2006-01-02"))
set, err := s.settings.Current(ctx, s.clock.Now().In(s.tz).Format("2006-01-02"))
if err != nil {
return nil, ErrNoSettings
}
@@ -196,7 +200,7 @@ func (s *WeekService) CloseWeek(ctx context.Context, weekKey string) (*domain.Cl
// Verify all past workdays that have entries are closed; collect worked ms.
// Past workdays with no entries at all are skipped (they contribute 0h).
// Future workdays in the week are always skipped.
todayKey := time.Now().In(s.tz).Format("2006-01-02")
todayKey := s.clock.Now().In(s.tz).Format("2006-01-02")
var totalWorkedMs int64
for _, dk := range dayKeys {
t, _ := time.ParseInLocation("2006-01-02", dk, s.tz)
@@ -226,7 +230,7 @@ func (s *WeekService) CloseWeek(ctx context.Context, weekKey string) (*domain.Cl
// No entries, no closed record → untracked day, counts as 0h.
}
now := time.Now().UnixMilli()
now := s.clock.Now().UnixMilli()
cw := &domain.ClosedWeek{
WeekKey: weekKey,
ExpectedMs: expectedMs,