dyzur-bot/store_test.go

65 lines
1.8 KiB
Go

package dyzurbot
import (
"path/filepath"
"testing"
"time"
)
func sampleRota() Rota {
return Rota{
Weeks: []DutyWeek{{WeekStart: time.Date(2026, 1, 5, 0, 0, 0, 0, time.UTC), Person1: "Jan Kowalski", Person2: "Anna Nowak"}},
People: []Person{{Name: "Piotr Wiśniewski", DutyCount: 1}},
}
}
func TestStoreReplaceThenLoadRoundTrips(t *testing.T) {
path := filepath.Join(t.TempDir(), "rota.json")
synced := time.Date(2026, 6, 17, 20, 0, 0, 0, time.UTC)
s1 := NewStore(path)
if err := s1.Replace(sampleRota(), synced); err != nil {
t.Fatalf("Replace: %v", err)
}
s2 := NewStore(path)
if err := s2.Load(); err != nil {
t.Fatalf("Load: %v", err)
}
rota, ts := s2.Snapshot()
if !ts.Equal(synced) {
t.Errorf("lastSynced: got %v, want %v", ts, synced)
}
if len(rota.Weeks) != 1 || rota.Weeks[0].Person1 != "Jan Kowalski" {
t.Errorf("weeks not restored: %+v", rota.Weeks)
}
if len(rota.People) != 1 || rota.People[0].DutyCount != 1 {
t.Errorf("people not restored: %+v", rota.People)
}
}
func TestStoreLoadMissingFileIsNotError(t *testing.T) {
s := NewStore(filepath.Join(t.TempDir(), "does-not-exist.json"))
if err := s.Load(); err != nil {
t.Fatalf("Load of missing file should be nil, got %v", err)
}
rota, ts := s.Snapshot()
if len(rota.Weeks) != 0 || len(rota.People) != 0 || !ts.IsZero() {
t.Errorf("empty store expected, got weeks=%d people=%d ts=%v", len(rota.Weeks), len(rota.People), ts)
}
}
func TestStoreSnapshotIsACopy(t *testing.T) {
s := NewStore(filepath.Join(t.TempDir(), "rota.json"))
if err := s.Replace(sampleRota(), time.Now()); err != nil {
t.Fatalf("Replace: %v", err)
}
rota, _ := s.Snapshot()
rota.Weeks[0].Person1 = "MUTATED"
again, _ := s.Snapshot()
if again.Weeks[0].Person1 != "Jan Kowalski" {
t.Errorf("snapshot mutation leaked into store: %q", again.Weeks[0].Person1)
}
}