113 lines
2.9 KiB
Go
113 lines
2.9 KiB
Go
package dyzurbot
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"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)
|
|
}
|
|
}
|
|
|
|
func TestStoreLoadCorruptFileIsNotError(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "corrupt.json")
|
|
if err := os.WriteFile(path, []byte("{not valid json"), 0o644); err != nil {
|
|
t.Fatalf("WriteFile: %v", err)
|
|
}
|
|
|
|
s := NewStore(path)
|
|
if err := s.Load(); err != nil {
|
|
t.Fatalf("Load of corrupt 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 TestStoreConcurrentSnapshotAndReplace(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "rota.json")
|
|
s := NewStore(path)
|
|
var wg sync.WaitGroup
|
|
|
|
// Launch 2 goroutines repeatedly calling Replace
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 100; j++ {
|
|
_ = s.Replace(sampleRota(), time.Now())
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Launch 2 goroutines repeatedly calling Snapshot
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for j := 0; j < 100; j++ {
|
|
_, _ = s.Snapshot()
|
|
}
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|