Files
Pulse/internal/api/patrol_digest_email_test.go
rcourtman b633e54105 Count each finished Patrol investigation once in the weekly digest
The weekly digest counted investigation_outcome lifecycle rows and read
their outcome from a metadata["outcome"] key the findings store never
writes, so every one of them showed as "unknown". Those rows are later
steps of an earlier run's fix (approved, run or verified), not new
investigations, while the investigation_updated rows every run writes
were ignored apart from one fallback count per finding. Two runs on one
finding counted once, a fix approved after last week's run counted as
this week's investigation, and the email could print "No issues needed
a closer look." under a non-zero count.

The digest now counts one investigation per run that finished in the
window, from the rows the store already writes for every run, including
a run whose own fix was verified before it closed. A fix that moves on
afterwards relabels its run instead of counting again. A run that failed
with no outcome is "failed", the bucket telemetry already uses; the
email words it "ended with an error" and no longer denies a non-zero
count. The summary-field fallback is removed: every run since the v6
release writes lifecycle rows, and the fallback could count one run in
two adjacent weeks.

The tests drive the findings store's own writers rather than hand-built
rows, which is how the wrong metadata key went unnoticed.
2026-10-23 12:04:23 +01:00

120 lines
5.7 KiB
Go

package api
import (
"strings"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/ai"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
func TestRenderPatrolDigestEmailMirrorsTheCardInPlainLanguage(t *testing.T) {
last := time.Date(2026, 9, 1, 11, 0, 0, 0, time.UTC)
digest := ai.PatrolDigest{
Window: ai.PatrolDigestWindow{Days: 7, HistoryComplete: true},
Mode: config.PatrolAutonomyApproval,
Runs: ai.PatrolDigestRuns{Total: 38, Checks: 1520, ResourcesCovered: 40, Failed: 1, LastRunAt: &last},
Findings: ai.PatrolDigestFindings{
New: 12, OpenBySeverity: ai.PatrolDigestSeverityCounts{Critical: 1, Warning: 3}, Resolved: 9, AutoResolved: 7, Dismissed: 2,
},
Investigations: ai.PatrolDigestInvestigations{Total: 4, ByOutcome: map[string]int{"fix_verified": 2, "needs_attention": 1, "resolved": 1}},
Actions: ai.PatrolDigestActions{Executed: 2, Verified: 1, Pending: 1},
Alerts: ai.PatrolDigestAlerts{Reviewed: 5},
Spend: ai.PatrolDigestSpend{EstimatedUSD: 1.2345, PricingKnown: true, Calls: 40},
}
subject, htmlBody, textBody := renderPatrolDigestEmail(digest, "https://pulse.example.com/")
if subject != "Pulse: what Patrol did this week (38 runs, 12 new issues)" {
t.Fatalf("subject = %q", subject)
}
for _, want := range []string{
"Patrol runs: 38", "1520 checks across 40 resources.", "5 alerts looked into.", "1 run failed.",
"New issues: 12", "4 still open (1 critical, 3 warning).",
"Issues resolved: 9", "7 cleared by Patrol on its own.", "2 dismissed by you.",
"Investigated: 4", "1 need you, 2 fixed and verified.",
"Fixes run: 2", "1 of 2 verified afterwards.", "1 fix waiting for your approval.",
"Estimated spend: $1.23", "40 model calls.",
"Ask first. Patrol investigates and prepares fixes, but every change waits for your approval.",
"Open Patrol: https://pulse.example.com/patrol", "Review waiting fixes: https://pulse.example.com/actions",
} {
if !strings.Contains(textBody, want) {
t.Fatalf("text body missing %q:\n%s", want, textBody)
}
}
for _, want := range []string{`href="https://pulse.example.com/patrol"`, `href="https://pulse.example.com/actions"`, "<strong>New issues:</strong> 12"} {
if !strings.Contains(htmlBody, want) {
t.Fatalf("html body missing %q:\n%s", want, htmlBody)
}
}
for _, forbidden := range []string{"evidence class", "verdict", "agent_attested", "fix_verified"} {
if strings.Contains(strings.ToLower(textBody), forbidden) || strings.Contains(strings.ToLower(htmlBody), forbidden) {
t.Fatalf("email leaks forensic vocabulary %q", forbidden)
}
}
}
func TestRenderPatrolDigestEmailNamesHowInvestigationsEnded(t *testing.T) {
digest := ai.PatrolDigest{
Window: ai.PatrolDigestWindow{Days: 7, HistoryComplete: true},
Mode: config.PatrolAutonomyApproval,
Runs: ai.PatrolDigestRuns{Total: 6},
Investigations: ai.PatrolDigestInvestigations{Total: 3, ByOutcome: map[string]int{"interrupted": 1, "failed": 2}},
Spend: ai.PatrolDigestSpend{PricingKnown: true},
}
_, _, textBody := renderPatrolDigestEmail(digest, "")
if !strings.Contains(textBody, "Investigated: 3\n 2 ended with an error, 1 interrupted by a restart.\n") {
t.Fatalf("text body does not name how the runs ended:\n%s", textBody)
}
// A counted run whose outcome has no wording here must not turn the line
// into "nothing was investigated", in any mode.
for _, mode := range []string{config.PatrolAutonomyApproval, config.PatrolAutonomyMonitor} {
digest.Mode = mode
digest.Investigations = ai.PatrolDigestInvestigations{Total: 1, ByOutcome: map[string]int{"unknown": 1}}
_, _, textBody = renderPatrolDigestEmail(digest, "")
if !strings.Contains(textBody, "Investigated: 1\n\n") {
t.Fatalf("%s: text body = \n%s", mode, textBody)
}
for _, contradiction := range []string{"No issues needed a closer look.", "reports issues without investigating them"} {
if strings.Contains(textBody, contradiction) {
t.Fatalf("%s: one investigation reported as none (%q):\n%s", mode, contradiction, textBody)
}
}
}
}
func TestRenderPatrolDigestEmailHandlesQuietWatchOnlyWeeks(t *testing.T) {
since := time.Date(2026, 8, 29, 8, 0, 0, 0, time.UTC)
digest := ai.PatrolDigest{
Window: ai.PatrolDigestWindow{Days: 7, HistoryComplete: false, HistorySince: &since},
Mode: config.PatrolAutonomyMonitor,
Runs: ai.PatrolDigestRuns{Total: 3, Checks: 30, ResourcesCovered: 10},
Investigations: ai.PatrolDigestInvestigations{ByOutcome: map[string]int{}},
Spend: ai.PatrolDigestSpend{PricingKnown: false, Calls: 3, EstimatedUSD: 0.5},
}
subject, htmlBody, textBody := renderPatrolDigestEmail(digest, "<script>")
if subject != "Pulse: what Patrol did this week (3 runs, 0 new issues)" {
t.Fatalf("subject = %q", subject)
}
for _, want := range []string{
"Since 29 Aug (older runs are no longer kept)", "Nothing new was raised.", "No issues were resolved this period.",
"Patrol is watch only, so it reports issues without investigating them.", "Patrol is watch only, so no fixes were proposed.",
"Some calls used a model with no known price.", "Watch only. Patrol checks infrastructure and reports issues only.",
} {
if !strings.Contains(textBody, want) {
t.Fatalf("text body missing %q:\n%s", want, textBody)
}
}
if strings.Contains(htmlBody, "<script>") {
t.Fatal("public URL must be HTML-escaped")
}
empty := ai.PatrolDigest{Window: ai.PatrolDigestWindow{Days: 7, HistoryComplete: true}, Investigations: ai.PatrolDigestInvestigations{ByOutcome: map[string]int{}}, Spend: ai.PatrolDigestSpend{PricingKnown: true}}
subject, _, _ = renderPatrolDigestEmail(empty, "")
if subject != "Pulse: Patrol has not run in the last 7 days" {
t.Fatalf("empty subject = %q", subject)
}
}