Harden core packages and add interactive CLI config builder (rud config/validate/test)

This commit is contained in:
tyuXX 2026-11-19 13:05:26 +00:00
parent d37503c863
commit 99e24be6c9
29 changed files with 6763 additions and 165 deletions

View file

@ -1,7 +1,11 @@
package store
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
@ -56,3 +60,52 @@ func TestStoreRingBufferAndPersistence(t *testing.T) {
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()
}