dyzur-bot/sync_test.go

45 lines
1.3 KiB
Go

package dyzurbot
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
)
func TestSyncSuccessReplacesCache(t *testing.T) {
store := NewStore(filepath.Join(t.TempDir(), "rota.json"))
fixed := time.Date(2026, 6, 17, 20, 0, 0, 0, time.UTC)
sy := NewSyncer(store, func(context.Context) (Rota, error) { return sampleRota(), nil })
sy.now = func() time.Time { return fixed }
if err := sy.Sync(context.Background()); err != nil {
t.Fatalf("Sync: %v", err)
}
rota, ts := store.Snapshot()
if len(rota.People) != 1 || !ts.Equal(fixed) {
t.Errorf("cache not updated: people=%d ts=%v", len(rota.People), ts)
}
}
func TestSyncFailurePreservesCache(t *testing.T) {
store := NewStore(filepath.Join(t.TempDir(), "rota.json"))
good := time.Date(2026, 6, 17, 20, 0, 0, 0, time.UTC)
if err := store.Replace(sampleRota(), good); err != nil {
t.Fatalf("seed Replace: %v", err)
}
wantErr := errors.New("sheet unreachable")
sy := NewSyncer(store, func(context.Context) (Rota, error) { return Rota{}, wantErr })
if err := sy.Sync(context.Background()); !errors.Is(err, wantErr) {
t.Fatalf("Sync error: got %v, want %v", err, wantErr)
}
rota, ts := store.Snapshot()
if len(rota.People) != 1 || !ts.Equal(good) {
t.Errorf("cache should be untouched: people=%d ts=%v", len(rota.People), ts)
}
}