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

@@ -8,6 +8,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/wotra/wotra/internal/clock"
"github.com/wotra/wotra/internal/domain"
"github.com/wotra/wotra/internal/store"
)
@@ -24,15 +25,16 @@ var (
type SettingsService struct {
store *store.SettingsStore
syncStore *store.SyncStore
clock clock.Clock
}
func NewSettingsService(s *store.SettingsStore, syncStore *store.SyncStore) *SettingsService {
return &SettingsService{store: s, syncStore: syncStore}
func NewSettingsService(s *store.SettingsStore, syncStore *store.SyncStore, clk clock.Clock) *SettingsService {
return &SettingsService{store: s, syncStore: syncStore, clock: clk}
}
// Current returns settings effective as of today.
func (s *SettingsService) Current(ctx context.Context) (*domain.Settings, error) {
today := time.Now().UTC().Format("2006-01-02")
today := s.clock.Now().UTC().Format("2006-01-02")
set, err := s.store.Current(ctx, today)
if err != nil {
return nil, ErrNoSettings
@@ -80,7 +82,7 @@ func (s *SettingsService) Upsert(ctx context.Context, input UpsertSettingsInput)
return nil, fmt.Errorf("invalid effective_from: %w", err)
}
now := time.Now().UnixMilli()
now := s.clock.Now().UnixMilli()
set := &domain.Settings{
ID: uuid.New().String(),
EffectiveFrom: input.EffectiveFrom,
@@ -135,7 +137,7 @@ func (s *SettingsService) UpdateSettings(ctx context.Context, id string, input U
set.HoursPerWeek = input.HoursPerWeek
set.WorkdaysMask = input.WorkdaysMask
set.Timezone = input.Timezone
set.UpdatedAt = time.Now().UnixMilli()
set.UpdatedAt = s.clock.Now().UnixMilli()
if err := s.store.Update(ctx, set); err != nil {
return nil, err