Add goal notifications and collector health
Test and publish / test (push) Successful in 1m40s
Test and publish / publish (push) Successful in 3m45s

This commit is contained in:
2026-10-23 20:45:51 +01:00
parent 667091efb0
commit 453cef40af
8 changed files with 375 additions and 3 deletions
+70 -1
View File
@@ -13,6 +13,7 @@ import (
"ccp/internal/auth"
"ccp/internal/store"
"ccp/internal/summary"
"ccp/internal/testdb"
)
@@ -171,12 +172,80 @@ func TestSamplesReportsAndSettings(t *testing.T) {
if code != http.StatusOK || len(out["samples"].([]any)) != 1 {
t.Fatalf("range summary: %d %v", code, out)
}
code, out = do(t, "PUT", srv.URL+"/api/v1/notifications/config", "", `{"enabled":true,"ntfy_topic":"goals","retention_days":30}`)
ntfyBody := make(chan string, 1)
ntfy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
ntfyBody <- string(body)
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(ntfy.Close)
config := `{"enabled":true,"ntfy_server":"` + ntfy.URL + `","ntfy_topic":"goals","retention_days":30}`
code, out = do(t, "PUT", srv.URL+"/api/v1/notifications/config", "", config)
if code != http.StatusOK || out["ntfy_topic"] != "goals" {
t.Fatalf("notification config: %d %v", code, out)
}
code, out = do(t, "POST", srv.URL+"/api/v1/notifications/goals/check", "", "")
if code != http.StatusOK || out["sent"] != 1.0 || out["at_risk"] != 1.0 {
t.Fatalf("goal notification: %d %v", code, out)
}
select {
case body := <-ntfyBody:
if !strings.Contains(body, "Goal at risk") {
t.Fatalf("ntfy body = %q", body)
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for ntfy request")
}
code, out = do(t, "POST", srv.URL+"/api/v1/notifications/goals/check", "", "")
if code != http.StatusOK || out["sent"] != 0.0 || out["at_risk"] != 1.0 {
t.Fatalf("duplicate goal notification: %d %v", code, out)
}
code, out = do(t, "GET", srv.URL+"/api/v1/health/collectors", "", "")
collectors := out["collectors"].([]any)
if code != http.StatusOK || len(collectors) != 1 {
t.Fatalf("collector health: %d %v", code, out)
}
collector := collectors[0].(map[string]any)
if collector["status"] != "healthy" || collector["activity_count"] != 0.0 || collector["sample_count"] != 1.0 {
t.Fatalf("collector health entry: %v", collector)
}
code, out = do(t, "POST", srv.URL+"/api/v1/reports", "", `{"period_start":"`+ts.Format(time.RFC3339)+`","period_end":"`+ts.Add(time.Hour).Format(time.RFC3339)+`","model":"test","body":"summary"}`)
if code != http.StatusCreated || out["id"] == nil {
t.Fatalf("report create: %d %v", code, out)
}
}
func TestValidateNtfyServer(t *testing.T) {
for _, tc := range []struct {
name string
value string
valid bool
}{
{"https", "https://ntfy.example", true},
{"http", "http://127.0.0.1:8080/ntfy", true},
{"relative", "ntfy.example", false},
{"unsupported scheme", "ftp://ntfy.example", false},
{"missing host", "https:///topic", false},
{"malformed", "https://[::1", false},
{"userinfo", "https://user:[email protected]", false},
} {
t.Run(tc.name, func(t *testing.T) {
_, err := validateNtfyServer(tc.value)
if (err == nil) != tc.valid {
t.Fatalf("validateNtfyServer(%q) error = %v, valid = %v", tc.value, err, tc.valid)
}
})
}
}
func TestGoalAtRisk(t *testing.T) {
if !goalAtRisk(summary.GoalResult{Goal: store.Goal{Op: ">=", TargetS: 100}, ActualS: 49}, 0.5) {
t.Error("goal below elapsed-day pace should be at risk")
}
if goalAtRisk(summary.GoalResult{Goal: store.Goal{Op: ">=", TargetS: 100}, ActualS: 50}, 0.5) {
t.Error("goal on elapsed-day pace should not be at risk")
}
if !goalAtRisk(summary.GoalResult{Goal: store.Goal{Op: "<=", TargetS: 100}, ActualS: 101}, 0.1) {
t.Error("over-limit goal should be at risk")
}
}