Files
felhom.eu/hub/internal/notify/d84_receipt_test.go
T
admin be272b7cf4 hub: review fix 4 — the hub writes the household's receipt; a box cannot word it or flood it (D84)
offsite_end_scheduled bypasses the household's switches and went through the
ordinary customer leg: subject and body were the box's own sentence, and 50
invented subjects gave 50 mails.

- processReceipt: the details must name a checked app (app-name shape, not
  reserved) and two parsed dates; the mail is rendered only from the hub's
  bundle with that app and the parsed end date — never the box's message.
- The cooldown key is the checked app + the parsed time of the press.
- At most 5 receipts per household in 24 hours, counted in the hub's sent log;
  drops are said once a day; old receipt keys leave the cooldown map.
- Pinned: a blocked household and a household with no address get none.

Tests seen red before the fix; 12 mutations seen red, 3 after a redo
(documentation/audits/build-release-4-2026-11-15/A/redproof-review-4.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 14:46:27 +01:00

128 lines
5.9 KiB
Go

package notify
import (
"strings"
"testing"
)
// D84 (`09` §3 decision 309; `07-backup-architecture.md` §8.2f): the household gets ONE mail at the moment it presses
// „delete backup data”, naming the app, the end date and how to take it back.
// RED-PROOFS: documentation/audits/build-release-4-2026-11-15/A/redproof-hub-mail.txt.
type receiptMail struct{ to, subject, body string }
func receiptDispatcher(t *testing.T, prefsEmail string, enabled []string) (*Dispatcher, *[]receiptMail) {
t.Helper()
st := opOnlyStore(t)
if prefsEmail != "" || enabled != nil {
if err := st.SaveNotificationPrefs("c1", prefsEmail, enabled, 6); err != nil {
t.Fatal(err)
}
}
d := opOnlyDispatcher(t, st, &sentTo{})
var mails []receiptMail
d.sendEmailFn = func(to, subject, body string, _ map[string]string) error {
mails = append(mails, receiptMail{to, subject, body})
return nil
}
return d, &mails
}
const (
receiptHU = "vikunja: az alkalmazás távoli másolatai törlésre vannak kijelölve. Törlésük napja: 2026. 12. 15. Addig a Mentések oldalon a „Mégis megtartom” gombbal visszavonhatod."
receiptEN = "vikunja: the off-site copies of this app are marked for deletion. They are deleted on: 2026-12-15. Until then you can take this back on the Backups page with the “Keep it after all” button."
receiptDetails = `{"stack_name":"vikunja","removed_at":"2026-11-15T10:00:00Z","ends_at":"2026-12-15T10:00:00Z","subject":"vikunja|2026-11-15T10:00:00Z"}`
)
// The mail reaches the household's own address although EVERY group switch is off, names the app in the subject, and
// carries the box's sentence (the day, the way back). The operator is not mailed.
func TestD84_Receipt_ReachesTheHouseholdWhateverItsSwitches(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", []string{})
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", receiptHU, "", receiptDetails, "controller")
if len(*mails) != 1 {
t.Fatalf("mails = %d, want exactly one (to the household): %+v", len(*mails), *mails)
}
m := (*mails)[0]
if m.to != "customer@example.com" {
t.Fatalf("the receipt went to %q, not to the household's own address", m.to)
}
if !strings.Contains(m.subject, "vikunja: a távoli másolatok törlésre kijelölve") {
t.Fatalf("the subject does not name the app: %q", m.subject)
}
for _, want := range []string{"2026. 12. 15.", "Mégis megtartom", "Mentések oldalon"} {
if !strings.Contains(m.body, want) {
t.Fatalf("the body lacks %q:\n%s", want, m.body)
}
}
if strings.Contains(m.body, "removed_at") || strings.Contains(m.body, "{") {
t.Fatalf("the body carries the raw details:\n%s", m.body)
}
}
// An English household reads it in English (the box's own second sentence, R-558).
func TestD84_Receipt_FollowsTheHouseholdsLanguage(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", nil)
if err := d.store.SaveReport("c1", []byte(`{"customer_id":"c1","language":"en"}`)); err != nil {
t.Fatal(err)
}
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", receiptHU, receiptEN, receiptDetails, "controller")
if len(*mails) != 1 {
t.Fatalf("mails = %d: %+v", len(*mails), *mails)
}
m := (*mails)[0]
if !strings.Contains(m.subject, "vikunja: the off-site copies are marked for deletion") || !strings.Contains(m.body, "Keep it after all") || !strings.Contains(m.body, "2026-12-15") {
t.Fatalf("not the English receipt:\n%s\n%s", m.subject, m.body)
}
if strings.Contains(m.body, "Mégis megtartom") {
t.Fatalf("Hungarian in an English household's mail:\n%s", m.body)
}
}
// Once per press: the same press delivered twice is one mail; a second app, or the same app removed again later, is
// its own mail — also inside the household's cooldown.
func TestD84_Receipt_OncePerPress(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", nil)
send := func(app, removedAt string) {
det := `{"stack_name":"` + app + `","removed_at":"` + removedAt + `","ends_at":"2026-12-15T10:00:00Z","subject":"` + app + `|` + removedAt + `"}`
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", "x "+app, "", det, "controller")
}
send("vikunja", "2026-11-15T10:00:00Z")
send("vikunja", "2026-11-15T10:00:00Z") // the same press, delivered again
if len(*mails) != 1 {
t.Fatalf("one press gave %d mails", len(*mails))
}
send("immich", "2026-11-15T10:05:00Z") // another app, five minutes later
send("vikunja", "2026-11-15T11:00:00Z") // installed again, removed again
if len(*mails) != 3 {
t.Fatalf("three presses gave %d mails, want 3: %+v", len(*mails), *mails)
}
}
// A household with no address gets no mail (the page is all it has), and nobody else is mailed in its place.
func TestD84_Receipt_NoAddressNoMail(t *testing.T) {
d, mails := receiptDispatcher(t, "", nil)
d.ProcessBoxEvent("c1", "offsite_end_scheduled", "info", receiptHU, "", receiptDetails, "controller")
if len(*mails) != 0 {
t.Fatalf("a household with no address: %d mail(s) went out: %+v", len(*mails), *mails)
}
}
// The receipt register does not open any other type: an ordinary info event still mails nobody, and a type the
// household switched off still stays off.
func TestD84_Receipt_OpensNothingElse(t *testing.T) {
d, mails := receiptDispatcher(t, "customer@example.com", []string{})
d.ProcessBoxEvent("c1", "app_removed", "info", "Alkalmazás eltávolítva.", "", `{"stack_name":"vikunja"}`, "controller")
d.ProcessBoxEvent("c1", "app_stopped_unhealthy", "error", "x", "", `{"stack_name":"vikunja"}`, "controller")
for _, m := range *mails {
if m.to == "customer@example.com" {
t.Fatalf("the household was mailed a type it switched off / an info event: %+v", m)
}
}
if len(householdReceiptEvents) != 1 || !householdReceiptEvents["offsite_end_scheduled"] {
t.Fatalf("the receipt register changed: %v — every entry bypasses the household's switches and needs its own ruling", householdReceiptEvents)
}
if operatorOnlyEvents["offsite_end_scheduled"] {
t.Fatal("a receipt cannot be operator-only")
}
}