package events import ( "context" "io" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync" "testing" "time" "git.gsdistance.org/tyuXX/RUD/internal/config" "git.gsdistance.org/tyuXX/RUD/internal/store" ) func TestEventEngineBooleanAndSustainedDurationAndMetricTriggers(t *testing.T) { var webhookMu sync.Mutex var webhookBodies []string ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { b, _ := io.ReadAll(r.Body) webhookMu.Lock() webhookBodies = append(webhookBodies, string(b)) webhookMu.Unlock() w.WriteHeader(http.StatusOK) })) defer ts.Close() tmpFile := filepath.Join(t.TempDir(), "cmd_alert.txt") evCfg := []config.EventConfig{ { ID: "multi-or-sustained", Name: "Multi Monitor Down or 5h Outage", Cooldown: config.NewDuration(10 * time.Minute), TriggerOnRecovery: true, Condition: config.EventCondition{ Operator: "or", Conditions: []config.EventCondition{ // Monitors 1, 2, 3 are all down { Type: "state", Monitors: []string{"1", "2", "3"}, Match: "all", State: "down", }, // Any monitor down for >= 5 hours AND uptime_pct < 90 { Operator: "and", Conditions: []config.EventCondition{ { Type: "state", Monitors: []string{"*"}, Match: "any", State: "down", For: config.NewDuration(5 * time.Hour), }, { Type: "metric", Monitors: []string{"*"}, Match: "any", Metric: "uptime_pct", Comparator: "<", Value: 90.0, }, }, }, }, }, Actions: []config.EventAction{ { Type: "webhook", URL: ts.URL, BodyTemplate: `{"event":"{{.EventID}}","status":"{{.Status}}","monitors":"{{.TriggeredMonitors}}"}`, }, { Type: "command", Command: "printf '%s:%s' '{{.Status}}' '{{.TriggeredMonitors}}' > " + tmpFile, Shell: true, }, }, }, } eng := NewEngine(evCfg) now := time.Now().UTC() // Case 1: Only monitor 1 and 2 are down for 1 minute (should NOT trigger) snap1 := store.Snapshot{ OverallState: store.StateDown, UpdatedAt: now, Monitors: []store.MonitorState{ {ID: "1", State: store.StateDown, StateChangedAt: now.Add(-time.Minute), UptimePct: 95}, {ID: "2", State: store.StateDown, StateChangedAt: now.Add(-time.Minute), UptimePct: 95}, {ID: "3", State: store.StateUp, StateChangedAt: now.Add(-time.Hour), UptimePct: 100}, }, } snap1.MonitorsByID = map[string]store.MonitorState{ "1": snap1.Monitors[0], "2": snap1.Monitors[1], "3": snap1.Monitors[2], } eng.Evaluate(context.Background(), snap1) eng.Wait() webhookMu.Lock() if len(webhookBodies) != 0 { t.Fatalf("expected 0 webhook calls, got %d", len(webhookBodies)) } webhookMu.Unlock() // Case 2: Monitor 1 has been down for 5h10m and uptime_pct is 80% (SHOULD trigger!) snap2 := snap1 snap2.Monitors[0].StateChangedAt = now.Add(-5*time.Hour - 10*time.Minute) snap2.Monitors[0].UptimePct = 80.0 snap2.MonitorsByID["1"] = snap2.Monitors[0] eng.Evaluate(context.Background(), snap2) eng.Wait() webhookMu.Lock() if len(webhookBodies) != 1 { t.Fatalf("expected 1 webhook call on sustained 5h + metric trigger, got %d", len(webhookBodies)) } if !strings.Contains(webhookBodies[0], `"status":"TRIGGERED"`) { t.Fatalf("unexpected webhook body: %s", webhookBodies[0]) } webhookMu.Unlock() cmdData, err := os.ReadFile(tmpFile) if err != nil || !strings.HasPrefix(string(cmdData), "TRIGGERED:") { t.Fatalf("expected command action output TRIGGERED:..., got %q (err=%v)", string(cmdData), err) } // Case 3: All monitors recover -> should fire RESOLVED snap3 := snap1 for i := range snap3.Monitors { snap3.Monitors[i].State = store.StateUp snap3.Monitors[i].UptimePct = 99.9 snap3.MonitorsByID[snap3.Monitors[i].ID] = snap3.Monitors[i] } eng.Evaluate(context.Background(), snap3) eng.Wait() webhookMu.Lock() if len(webhookBodies) != 2 || !strings.Contains(webhookBodies[1], `"status":"RESOLVED"`) { t.Fatalf("expected recovery webhook call, got %v", webhookBodies) } webhookMu.Unlock() }