44 lines
2.3 KiB
Go
44 lines
2.3 KiB
Go
package notify
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Review of 2026-11-14: the checkers behind these events tell each THING once and keep a durable mark. With the
|
|
// plain `customer:type` cooldown key the second thing of the same kind inside the hour was suppressed — and never
|
|
// told again, because its mark was already set. Two tokens that end the same day must give TWO mails; the same
|
|
// token again inside the hour gives none; and a type that is not on the list keeps its old key, subject or not.
|
|
// COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/review/redproof-hub-review-fixes.txt).
|
|
func TestSubjectCooldown_TwoThingsTwoMails(t *testing.T) {
|
|
st := newDispStore(t)
|
|
d := NewDispatcher(st, "test-key", "from@felhom.eu", "op@felhom.eu", true, log.New(io.Discard, "", 0))
|
|
var bodies []string
|
|
d.sendEmailFn = func(_, _, body string, _ map[string]string) error { bodies = append(bodies, body); return nil }
|
|
d.processOperator("", "operator_token_ends_soon", "warning", "token A ends", `{"subject":"token:1:soon"}`, "hub")
|
|
d.processOperator("", "operator_token_ends_soon", "warning", "token B ends", `{"subject":"token:2:soon"}`, "hub")
|
|
d.processOperator("", "operator_token_ends_soon", "warning", "token A ends", `{"subject":"token:1:soon"}`, "hub")
|
|
if len(bodies) != 2 || !strings.Contains(bodies[0], "token A") || !strings.Contains(bodies[1], "token B") {
|
|
t.Fatalf("mails = %d, want two (one per token) and none for the repeat: %q", len(bodies), bodies)
|
|
}
|
|
// every type on the list has the suffix; a type that is not on it has none, also with a subject in its details
|
|
for typ := range perSubjectCooldownEvents {
|
|
if cooldownSubjectSuffix(typ, `{"subject":"x"}`) != ":x" {
|
|
t.Errorf("%s: no per-subject key", typ)
|
|
}
|
|
if !operatorOnlyEvents[typ] {
|
|
t.Errorf("%s is keyed per subject but is not operator-only", typ)
|
|
}
|
|
}
|
|
for _, typ := range []string{"backup_failed", "app_start_failed", "tunnel_down", "box_clock_wrong"} {
|
|
if s := cooldownSubjectSuffix(typ, `{"subject":"x"}`); s != "" {
|
|
t.Errorf("%s: its cooldown key changed (%q) — only the listed types may", typ, s)
|
|
}
|
|
}
|
|
if cooldownSubjectSuffix("signed_job_refused", `{"other":1}`) != "" || cooldownSubjectSuffix("signed_job_refused", `not json "subject"`) != "" {
|
|
t.Error("no subject, or details that are not JSON, must give no suffix")
|
|
}
|
|
}
|