package store import ( "encoding/json" "os" "path/filepath" "strings" "sync" "testing" "time" "git.gsdistance.org/tyuXX/RUD/internal/config" ) func TestStoreRingBufferAndPersistence(t *testing.T) { tmpDir := t.TempDir() persistPath := filepath.Join(tmpDir, "state.json") cfg := config.DefaultConfig() cfg.Storage.HistorySize = 3 cfg.Storage.PersistenceFile = persistPath cfg.Monitors = []config.MonitorConfig{ {ID: "m1", Name: "Mon 1", Method: "http", URL: "http://localhost"}, {ID: "m2", Name: "Mon 2", Method: "tcp", Target: "localhost:80"}, } st := New(&cfg) t0 := time.Now().UTC().Add(-10 * time.Minute) st.Record("m1", CheckResult{Timestamp: t0, State: StateUp, LatencyMs: 10}) st.Record("m1", CheckResult{Timestamp: t0.Add(time.Minute), State: StateUp, LatencyMs: 20}) st.Record("m1", CheckResult{Timestamp: t0.Add(2 * time.Minute), State: StateDegraded, LatencyMs: 800}) st.Record("m1", CheckResult{Timestamp: t0.Add(3 * time.Minute), State: StateDown, LatencyMs: 0, Message: "timeout"}) snap := st.Snapshot() m1 := snap.MonitorsByID["m1"] if len(m1.History) != 3 { t.Fatalf("expected ring buffer length 3, got %d", len(m1.History)) } if m1.State != StateDown { t.Fatalf("expected m1 state down, got %s", m1.State) } if m1.UptimePct != 75.0 { t.Fatalf("expected 75%% uptime (3 of 4 checks up/degraded), got %.2f%%", m1.UptimePct) } if snap.OverallState != StateDown { t.Fatalf("expected overall state down, got %s", snap.OverallState) } if err := st.SaveToDisk(); err != nil { t.Fatalf("SaveToDisk failed: %v", err) } st2 := New(&cfg) if err := st2.LoadFromDisk(); err != nil { t.Fatalf("LoadFromDisk failed: %v", err) } snap2 := st2.Snapshot() if snap2.MonitorsByID["m1"].TotalChecks != 4 { t.Fatalf("expected restored TotalChecks=4, got %d", snap2.MonitorsByID["m1"].TotalChecks) } } func TestStoreNullHistoryAndConcurrentSaves(t *testing.T) { tmpDir := t.TempDir() persistPath := filepath.Join(tmpDir, "state.json") // Simulate persisted state with explicit null history raw := `{"saved_at":"2026-01-01T00:00:00Z","monitors":{"m1":{"id":"m1","name":"Mon 1","method":"http","state":"pending","history":null}}}` if err := os.WriteFile(persistPath, []byte(raw), 0644); err != nil { t.Fatalf("write seed state failed: %v", err) } cfg := config.DefaultConfig() cfg.Storage.HistorySize = 5 cfg.Storage.PersistenceFile = persistPath cfg.Monitors = []config.MonitorConfig{ {ID: "m1", Name: "Mon 1", Method: "http", URL: "http://localhost"}, } st := New(&cfg) if err := st.LoadFromDisk(); err != nil { t.Fatalf("LoadFromDisk failed: %v", err) } snap := st.Snapshot() if snap.MonitorsByID["m1"].History == nil { t.Fatalf("expected non-nil History slice after restoring null history") } b, err := json.Marshal(snap) if err != nil { t.Fatalf("json.Marshal snapshot failed: %v", err) } if strings.Contains(string(b), `"history":null`) { t.Fatalf("expected [] instead of null in JSON snapshot: %s", string(b)) } // Concurrent Record + SaveToDisk calls under -race var wg sync.WaitGroup for i := 0; i < 8; i++ { wg.Add(1) go func(idx int) { defer wg.Done() st.Record("m1", CheckResult{State: StateUp, LatencyMs: float64(idx)}) if err := st.SaveToDisk(); err != nil { t.Errorf("concurrent SaveToDisk failed: %v", err) } }(i) } wg.Wait() }