Files
felhom.eu/hub/internal/store/offsite_end_notice_test.go
T

265 lines
11 KiB
Go

package store
import (
"fmt"
"strings"
"testing"
"time"
)
// THE NOTICE LOCK, the hub's own record (`09` §3 decision 315; `07-backup-architecture.md` §8.2f).
// RED-PROOFS: documentation/audits/build-release-4b-2026-11-15/A/redproof-notice-store.txt.
func endNotice(t *testing.T, s *Store, cust, app string) (EndNotice, bool) {
t.Helper()
ns, err := s.EndNotices(cust)
if err != nil {
t.Fatal(err)
}
n, ok := ns[app]
return n, ok
}
// One press is one row, made at the first report that names it — by the HUB's clock — and nothing a later report
// says moves it. „Told” is written only by the hub, and only for the press it mailed about.
func TestNotice_Record_OnePressOneRow(t *testing.T) {
s := newTestStore(t)
t0 := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC)
press := "2026-11-15T09:59:00Z"
if err := s.SaveNotificationPrefs("c1", "customer@example.com", nil, 6); err != nil {
t.Fatal(err)
}
addr := NoticeAddressKey("customer@example.com")
note := func(at time.Time, deployed []string, tierOn bool, claims ...EndClaim) {
t.Helper()
if err := s.NoteEndNotices("c1", deployed, claims, tierOn, at); err != nil {
t.Fatal(err)
}
}
note(t0, []string{"immich"}, true, EndClaim{App: "vikunja", Press: press})
n, ok := endNotice(t, s, "c1", "vikunja")
if !ok || n.Told() || !n.FirstNamed.Equal(t0) || n.Press != press {
t.Fatalf("first report: %+v (found=%v) — want one row, not told, first named at the hub's time", n, ok)
}
// the same press in another time zone's writing, a day later: the same row
note(t0.Add(24*time.Hour), []string{"immich"}, true, EndClaim{App: "vikunja", Press: "2026-11-15T10:59:00+01:00"})
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.FirstNamed.Equal(t0) {
t.Fatalf("a later report of the same press moved the row: %+v", n)
}
// told: only with the press the hub mailed about
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "2026-11-01T00:00:00Z", EndNoticeHousehold, addr, t0); err != nil || ok {
t.Fatalf("a told mark for ANOTHER press was written (ok=%v err=%v)", ok, err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
t.Fatalf("told by a mark for another press: %+v", n)
}
if _, err := s.MarkEndNoticeTold("c1", "vikunja", press, "the box", addr, t0); err == nil {
t.Fatal("an unknown recipient was accepted")
}
told := t0.Add(time.Hour)
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", press, EndNoticeHousehold, addr, told); err != nil || !ok {
t.Fatalf("MarkEndNoticeTold: ok=%v err=%v", ok, err)
}
n, _ = endNotice(t, s, "c1", "vikunja")
if !n.Told() || !n.ToldAt.Equal(told) || n.ToldTo != EndNoticeHousehold {
t.Fatalf("told: %+v", n)
}
// a try that told nobody never touches the told time
if err := s.MarkEndNoticeTried("c1", "vikunja", press, "x", told.Add(time.Hour)); err != nil {
t.Fatal(err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.ToldAt.Equal(told) {
t.Fatalf("a failed try moved the told time: %+v", n)
}
}
// What voids a notice, and what does not.
func TestNotice_Record_WhatVoidsIt(t *testing.T) {
t0 := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC)
press := "2026-11-15T09:59:00Z"
cases := []struct {
name string
deployed []string
tierOn bool
claims []EndClaim
want string // "told" (kept), "gone", "fresh" (a new row, not told)
}{
{"the same press again", []string{"immich"}, true, []EndClaim{{App: "vikunja", Press: press}}, "told"},
{"a take-back: the tier is on and no record names the app", []string{"immich"}, true, nil, "gone"},
{"an install: the app is in the app list", []string{"immich", "vikunja"}, true, nil, "gone"},
{"an install that the box still claims (it lies, or it is slow)", []string{"immich", "vikunja"}, true, []EndClaim{{App: "vikunja", Press: press}}, "gone"},
{"another press", []string{"immich"}, true, []EndClaim{{App: "vikunja", Press: "2026-11-15T16:00:00Z"}}, "fresh"},
{"a press whose key is not a time", []string{"immich"}, true, []EndClaim{{App: "vikunja", Press: "yesterday"}}, "fresh"},
{"the report does not show the tier: its silence is not a take-back", []string{"immich"}, false, nil, "told"},
{"the report does not show the tier, and the app is installed", []string{"vikunja"}, false, nil, "gone"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
s := newTestStore(t)
if err := s.SetEndNoticeForTest("c1", "vikunja", press, t0, t0, EndNoticeHousehold); err != nil {
t.Fatal(err)
}
later := t0.Add(5 * 24 * time.Hour)
if err := s.NoteEndNotices("c1", c.deployed, c.claims, c.tierOn, later); err != nil {
t.Fatal(err)
}
n, ok := endNotice(t, s, "c1", "vikunja")
got := "gone"
switch {
case ok && n.Told() && n.ToldAt.Equal(t0):
got = "told"
case ok && !n.Told() && n.FirstNamed.Equal(later):
got = "fresh"
case ok:
got = fmt.Sprintf("?? %+v", n)
}
if got != c.want {
t.Fatalf("the notice is %s, want %s", got, c.want)
}
})
}
}
// A box's list is the box's word: only app names are stored, at most maxEndNoticeRows per household; a stored value
// the hub never writes is „not told”; a told time that cannot be read is „not told”.
func TestNotice_Record_IsBoundedAndFailsClosed(t *testing.T) {
s := newTestStore(t)
now := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC)
var claims []EndClaim
for _, bad := range []string{"", "_shares", "traefik", "felhom-offbox", "<b>x</b>", "pay now", strings.Repeat("a", 65)} {
claims = append(claims, EndClaim{App: bad})
}
for i := 0; i < 80; i++ {
claims = append(claims, EndClaim{App: fmt.Sprintf("app-%02d", i)})
}
if err := s.NoteEndNotices("c1", nil, claims, true, now); err != nil {
t.Fatal(err)
}
ns, _ := s.EndNotices("c1")
if len(ns) != maxEndNoticeRows {
t.Fatalf("rows = %d, want the limit %d", len(ns), maxEndNoticeRows)
}
for app := range ns {
if !strings.HasPrefix(app, "app-") {
t.Fatalf("a name that is not an app name was stored: %q", app)
}
}
// the same 80 again: still the limit, and the rows it holds are not replaced
if err := s.NoteEndNotices("c1", nil, claims, true, now.Add(time.Hour)); err != nil {
t.Fatal(err)
}
ns2, _ := s.EndNotices("c1")
if len(ns2) != maxEndNoticeRows || !ns2["app-00"].FirstNamed.Equal(now) {
t.Fatalf("a second report changed the bounded record: %d rows, app-00 %+v", len(ns2), ns2["app-00"])
}
// fail closed: a recipient the hub never writes, and a told time that is not a time
if _, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = '2020-01-01T00:00:00Z', told_to = 'box' WHERE app = 'app-00'`); err != nil {
t.Fatal(err)
}
if _, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = 'long ago', told_to = 'household' WHERE app = 'app-01'`); err != nil {
t.Fatal(err)
}
ns3, _ := s.EndNotices("c1")
if ns3["app-00"].Told() || ns3["app-01"].Told() {
t.Fatalf("an unreadable record counts as told: %+v %+v", ns3["app-00"], ns3["app-01"])
}
}
// The notice record goes with the household (D73's rule for every household-keyed table).
func TestNotice_Record_GoesWithTheHousehold(t *testing.T) {
s := newTestStore(t)
now := time.Now()
if err := s.SetEndNoticeForTest("gone", "vikunja", "", now, now, EndNoticeHousehold); err != nil {
t.Fatal(err)
}
if r, err := s.CustomerResidue("gone"); err != nil || r.OffsiteEndNotices != 1 || r.Total() < 1 {
t.Fatalf("the residue count does not see the notice record: %+v %v", r, err)
}
if err := s.PurgeCustomerResidue("gone"); err != nil {
t.Fatal(err)
}
if m, _ := s.EndNotices("gone"); len(m) != 0 {
t.Fatalf("the notice record outlived the household: %+v", m)
}
if err := s.SetEndNoticeForTest("ghost", "vikunja", "", now, now, EndNoticeHousehold); err != nil {
t.Fatal(err)
}
if n, err := s.PurgeOrphanRows(); err != nil || n["offsite_end_notices"] != 1 {
t.Fatalf("orphan sweep: %+v %v", n, err)
}
}
// Review finding 1 — „told” is told AT AN ADDRESS. The reader's rule alone: a row told to the household counts only
// while the household's address on record is the one the mail was accepted for — however the address came to differ
// (here: changed behind the writer's back). An operator-told row does not depend on the address.
func TestReview1_ToldCountsOnlyAtTheAddressOnRecord(t *testing.T) {
s := newTestStore(t)
at := time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)
if err := s.SaveNotificationPrefs("c1", "Real@Example.com", nil, 6); err != nil {
t.Fatal(err)
}
if err := s.NoteEndNotices("c1", nil, []EndClaim{{App: "vikunja"}, {App: "kimai"}}, true, at); err != nil {
t.Fatal(err)
}
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, NoticeAddressKey(" real@example.COM "), at); err != nil || !ok {
t.Fatalf("mark: %v %v", ok, err)
}
if ok, err := s.MarkEndNoticeTold("c1", "kimai", "", EndNoticeOperator, "", at); err != nil || !ok {
t.Fatalf("mark: %v %v", ok, err)
}
if _, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, "", at); err == nil {
t.Fatal("a household notice was marked told with no address key")
}
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
t.Fatalf("the same address in other capitals must be the same address: %+v", n)
}
for _, other := range []string{"intruder@example.net", ""} {
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = ? WHERE customer_id = 'c1'`, other); err != nil {
t.Fatal(err)
}
n, _ := endNotice(t, s, "c1", "vikunja")
if n.Told() || !n.ToldAt.IsZero() || !strings.Contains(n.LastReason, "address changed") {
t.Fatalf("the address on record is %q and the row still reads told: %+v", other, n)
}
if k, _ := endNotice(t, s, "c1", "kimai"); !k.Told() || k.ToldTo != EndNoticeOperator {
t.Fatalf("an operator-told row must not depend on the household's address: %+v", k)
}
}
// the address is put back by hand: the row reads told again — it WAS told at this address
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = 'real@example.com' WHERE customer_id = 'c1'`); err != nil {
t.Fatal(err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
t.Fatalf("told at the address the household has: %+v", n)
}
// … but through the WRITER a change is final: the row is un-told, and changing back does not bring it back
if err := s.SaveNotificationPrefs("c1", "intruder@example.net", nil, 6); err != nil {
t.Fatal(err)
}
if err := s.SaveNotificationPrefs("c1", "real@example.com", nil, 6); err != nil {
t.Fatal(err)
}
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
t.Fatalf("away and back through the save: the old „told” came back: %+v", n)
}
}
// Review finding 6: the test-only helpers that live in production files (other packages' tests need them) refuse to
// run outside a test binary — SetEndNoticeForTest could write „told” with any date.
func TestReview6_TheTestOnlyHelpersRefuseOutsideATestBinary(t *testing.T) {
s := newTestStore(t)
old := inTestBinary
inTestBinary = func() bool { return false } // as in the production binary
defer func() { inTestBinary = old }()
long := time.Now().Add(-90 * 24 * time.Hour)
if err := s.SetEndNoticeForTest("c1", "vikunja", "", long, long, EndNoticeOperator); err == nil {
t.Error("SetEndNoticeForTest ran outside a test binary")
}
if ns, _ := s.EndNotices("c1"); len(ns) != 0 {
t.Errorf("…and it wrote a row: %+v", ns)
}
if err := s.BackdateNotificationLogForTest("c1", time.Hour); err == nil {
t.Error("BackdateNotificationLogForTest ran outside a test binary")
}
}