Files
felhom.eu/hub/internal/notify/dispatcher_test.go
T
admin 0ff1d3c883 hub v0.24.0: dispatcher routes critical severity (+ nil-prefs crash guard)
ProcessEvent routed only warning/error; a critical-severity event was silently dropped.
Now routes warning/error/critical, logs unrecognized severities, and guards a nil
GetNotificationPrefs (which would panic/crash the hub). host_disk_critical emits its
natural critical severity.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HxLA1mZurFq9kt8hneFeCs
2026-06-30 14:18:45 +02:00

100 lines
3.4 KiB
Go

package notify
import (
"bytes"
"io"
"log"
"path/filepath"
"strings"
"sync"
"testing"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
_ "modernc.org/sqlite"
)
func newDispStore(t *testing.T) *store.Store {
t.Helper()
st, err := store.New(filepath.Join(t.TempDir(), "d.db"), log.New(io.Discard, "", 0))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "k", RetrievalPassword: "p"})
return st
}
// TestSeverityNotifies is the Part-0 contract + companion red-proof: warning/error/critical notify; info
// and unknown values do not. The companion models the PRE-FIX predicate (warning||error) that silently
// dropped "critical" — the bug this fixes.
func TestSeverityNotifies(t *testing.T) {
for _, s := range []string{"warning", "error", "critical"} {
if !severityNotifies(s) {
t.Errorf("%q must notify", s)
}
}
for _, s := range []string{"info", "", "frobnicate", "debug"} {
if severityNotifies(s) {
t.Errorf("%q must NOT notify", s)
}
}
// COMPANION RED-PROOF: the pre-fix predicate dropped "critical". The fix routes it.
preFix := func(sev string) bool { return sev == "warning" || sev == "error" }
if preFix("critical") {
t.Fatal("control: the pre-fix predicate should not match critical")
}
if !severityNotifies("critical") {
t.Fatal("the fix MUST route critical where the old (warning||error) predicate dropped it")
}
}
// TestProcessEvent_CriticalRoutes: a critical-severity event reaches the operator channel (Scenario E).
func TestProcessEvent_CriticalRoutes(t *testing.T) {
st := newDispStore(t)
d := NewDispatcher(st, "test-key", "from@felhom.eu", "op@felhom.eu", true, log.New(io.Discard, "", 0))
var mu sync.Mutex
var sent []string
d.sendEmailFn = func(to, _, _ string) error { mu.Lock(); defer mu.Unlock(); sent = append(sent, to); return nil }
d.ProcessEvent("c1", "host_disk_critical", "critical", "root full", "{}", "hub")
if len(sent) != 1 || sent[0] != "op@felhom.eu" {
t.Fatalf("critical must route to the operator, sent=%v", sent)
}
}
// TestProcessEvent_UnknownSeverityLogged: an unrecognized severity is LOGGED (not silently dropped) and
// not routed.
func TestProcessEvent_UnknownSeverityLogged(t *testing.T) {
st := newDispStore(t)
var buf bytes.Buffer
d := NewDispatcher(st, "test-key", "from", "op@felhom.eu", true, log.New(&buf, "", 0))
sent := 0
d.sendEmailFn = func(_, _, _ string) error { sent++; return nil }
d.ProcessEvent("c1", "weird_event", "frobnicate", "msg", "{}", "hub")
if sent != 0 {
t.Fatal("an unknown severity must not route")
}
if !strings.Contains(buf.String(), "unrecognized severity") {
t.Fatalf("an unknown severity must be logged, got: %q", buf.String())
}
}
// TestProcessEvent_InfoSilent: "info" is an intentional non-notify — not routed AND not logged as
// unrecognized (it's a known, deliberate non-alert).
func TestProcessEvent_InfoSilent(t *testing.T) {
st := newDispStore(t)
var buf bytes.Buffer
d := NewDispatcher(st, "test-key", "from", "op@felhom.eu", true, log.New(&buf, "", 0))
sent := 0
d.sendEmailFn = func(_, _, _ string) error { sent++; return nil }
d.ProcessEvent("c1", "controller_started", "info", "msg", "{}", "hub")
if sent != 0 {
t.Fatal("info must not route")
}
if strings.Contains(buf.String(), "unrecognized") {
t.Fatal("info is an intentional non-notify — it must NOT be logged as unrecognized")
}
}