Added tests and readme

This commit is contained in:
2026-05-12 06:20:51 +00:00
parent 7e85bb0fcc
commit 139c224a31
23 changed files with 1449 additions and 97 deletions
+52
View File
@@ -0,0 +1,52 @@
package alerts
import (
"strings"
"testing"
"github.com/jasper/quptime/internal/checks"
"github.com/jasper/quptime/internal/config"
)
func TestRenderDownTransition(t *testing.T) {
check := &config.Check{Name: "homepage", Target: "https://example.com", Type: config.CheckHTTP}
snap := checks.Snapshot{Reports: 3, OKCount: 0, NotOK: 3, Detail: "connection refused"}
msg := Render("master-node", check, checks.StateUp, checks.StateDown, snap)
if !strings.Contains(msg.Subject, "DOWN") {
t.Errorf("subject missing DOWN: %q", msg.Subject)
}
if !strings.Contains(msg.Subject, "homepage") {
t.Errorf("subject missing check name: %q", msg.Subject)
}
if !strings.Contains(msg.Body, "connection refused") {
t.Errorf("body missing detail: %q", msg.Body)
}
if !strings.Contains(msg.Body, "master-node") {
t.Errorf("body missing reporter: %q", msg.Body)
}
if !strings.Contains(msg.Body, "3 (ok=0, fail=3)") {
t.Errorf("body missing report count: %q", msg.Body)
}
}
func TestRenderRecoveryTransition(t *testing.T) {
check := &config.Check{Name: "api", Target: "https://api/", Type: config.CheckHTTP}
snap := checks.Snapshot{Reports: 3, OKCount: 3, NotOK: 0}
msg := Render("master", check, checks.StateDown, checks.StateUp, snap)
if !strings.Contains(msg.Subject, "RECOVERED") {
t.Errorf("subject missing RECOVERED: %q", msg.Subject)
}
}
func TestRenderUpInitialTransition(t *testing.T) {
check := &config.Check{Name: "api", Target: "https://api/"}
snap := checks.Snapshot{Reports: 1, OKCount: 1}
msg := Render("master", check, checks.StateUnknown, checks.StateUp, snap)
if !strings.Contains(msg.Subject, "UP") {
t.Errorf("subject missing UP: %q", msg.Subject)
}
if strings.Contains(msg.Subject, "RECOVERED") {
t.Error("first-time UP should not be tagged RECOVERED")
}
}