Files
felhom.eu/hub/internal/offsitekeys/notice_lock_test.go
T

181 lines
10 KiB
Go

package offsitekeys
import (
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// THE NOTICE LOCK, the agreement's half (`09` §3 decision 315; `07-backup-architecture.md` §8.2f): the hub agrees to
// an app only when ITS OWN record says the household was told at least 30 days ago — beside the 30 days of absence.
// RED-PROOFS: documentation/audits/build-release-4b-2026-11-15/A/redproof-notice-agree.txt.
// The pure decision. In every row the app has been absent for 40 days and its file is 60 days old: without the
// notice lock every row would be agreed.
func TestNotice_AgreeEnded_NoNoticeNoDeletion(t *testing.T) {
now := time.Date(2026, 12, 20, 3, 0, 0, 0, time.UTC)
days := func(n int) time.Time { return now.Add(-time.Duration(n) * 24 * time.Hour) }
listing := store.OffsiteSnapshotListing{ListedAt: now.Add(-20 * time.Hour), Entries: map[string]time.Time{eid(1): days(60), eid(2): days(60)}}
absent40 := map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}, "kimai": {AbsentSince: days(40)}}
claim := []EndedClaim{{App: "vikunja", RemovedAt: days(400).Format(time.RFC3339), EndsAt: days(370).Format(time.RFC3339), SnapshotIDs: []string{eid(1)}}}
told := func(at time.Time, to string) map[string]store.EndNotice {
return map[string]store.EndNotice{"vikunja": {App: "vikunja", ToldAt: at, ToldTo: to}}
}
cases := []struct {
name string
presence map[string]store.AppPresence
notices map[string]store.EndNotice
claims []EndedClaim
wantIDs []string
refused string // a fragment of vikunja's reason
}{
{name: "told 30 days and a minute ago: agreed", presence: absent40, notices: told(now.Add(-EndedAppAfter-time.Minute), store.EndNoticeHousehold), claims: claim, wantIDs: []string{eid(1)}},
{name: "the operator was told in the household's place 31 days ago: agreed", presence: absent40, notices: told(days(31), store.EndNoticeOperator), claims: claim, wantIDs: []string{eid(1)}},
{name: "no notice record at all (the box names the app only in the window request)", presence: absent40, notices: nil, claims: claim, refused: "no record that the household was told"},
{name: "a record, and nobody told yet (the send failed)", presence: absent40,
notices: map[string]store.EndNotice{"vikunja": {App: "vikunja", LastReason: "the mail service did not accept the mail"}}, claims: claim,
refused: "has not been told yet, so nothing is removed: the mail service did not accept the mail"},
{name: "a record, and nobody told yet (no try so far)", presence: absent40,
notices: map[string]store.EndNotice{"vikunja": {App: "vikunja"}}, claims: claim, refused: "has not been told yet"},
{name: "told 29 days and 23 hours ago", presence: absent40, notices: told(now.Add(-EndedAppAfter+time.Hour), store.EndNoticeHousehold), claims: claim, refused: "29 full day(s), 30 needed"},
{name: "told yesterday (the mail waited for the daily limit)", presence: absent40, notices: told(days(1), store.EndNoticeHousehold), claims: claim, refused: "the household was told on"},
{name: "the operator was told 5 days ago", presence: absent40, notices: told(days(5), store.EndNoticeOperator), claims: claim, refused: "the operator (the household has no usable address) was told on"},
{name: "a told time and no recipient (a value the hub never writes)", presence: absent40, notices: told(days(90), ""), claims: claim, refused: "has not been told yet"},
{name: "BESIDE the absence, not in its place: told 40 days ago, absent for 10", presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(10)}},
notices: told(days(40), store.EndNoticeHousehold), claims: claim, refused: "10 full day(s), 30 needed"},
{name: "told, and installed again", presence: map[string]store.AppPresence{"vikunja": {Present: true}},
notices: told(days(40), store.EndNoticeHousehold), claims: claim, refused: "lists the app as installed"},
{name: "a snapshot listed under a told app AND an app nobody was told about stays", presence: absent40,
notices: told(days(40), store.EndNoticeHousehold),
claims: []EndedClaim{{App: "vikunja", SnapshotIDs: []string{eid(1), eid(2)}}, {App: "kimai", SnapshotIDs: []string{eid(2)}}},
wantIDs: []string{eid(1)}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ids, _, refused := agreeEnded(now, c.presence, c.notices, listing, true, c.claims)
if strings.Join(ids, ",") != strings.Join(c.wantIDs, ",") {
t.Fatalf("agreed ids = %v, want %v (refused: %+v)", shortIDs(ids), shortIDs(c.wantIDs), refused)
}
if got := endedRefusedFor(refused, "vikunja"); !strings.Contains(got, c.refused) || (c.refused == "" && got != "") {
t.Fatalf("vikunja's refusal = %q, want it to say %q", got, c.refused)
}
})
}
}
// Through the window: the hub's own app record says vikunja left 31 days ago and its files are old — and the answer
// depends on the notice record alone.
func TestNotice_Window_TheNoticeRecordDecides(t *testing.T) {
ids := []string{eid(0), eid(1)}
claim := EndedClaim{App: "vikunja", SnapshotIDs: ids}
now := time.Now().UTC()
// (a) the box names the app ONLY in the window request, never in a report: no record → nothing, and the operator
// hears it (an honest box names a pending record in every report).
s, events := endedFixtureNoNotice(t)
g := openGranted(t, s, 20, claim)
if len(g.EndedAllowed) != 0 || !hasReason(g.EndedRefused, "no record that the household was told") {
t.Fatalf("no notice record: agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
if w, _ := s.Store.GetOffsiteWindow(g.WindowID); len(w.EndedAgreed) != 0 || len(w.EndedRefused) == 0 {
t.Fatalf("the window's own row: %+v", w)
}
if e := ofType(*events, EventEndedAppRefused); len(e) != 1 || !strings.Contains(e[0].msg, "no record that the household was told") {
t.Fatalf("the operator must hear of a box that asks for an app it never named in a report: %+v", *events)
}
// (b) the notice could not go out (the mail hold): nothing, the answer names the reason, and this refusal is
// quiet (the operator's line for it is offsite_end_notice_stuck).
s, events = endedFixtureNoNotice(t)
first := now.Add(-31 * 24 * time.Hour)
if err := s.Store.SetEndNoticeForTest("c1", "vikunja", "", first, time.Time{}, ""); err != nil {
t.Fatal(err)
}
if err := s.Store.MarkEndNoticeTried("c1", "vikunja", "", "the hub is not sending mail (the MAIL-HOLD marker is present)", now); err != nil {
t.Fatal(err)
}
g = openGranted(t, s, 20, claim)
if len(g.EndedAllowed) != 0 || !hasReason(g.EndedRefused, "MAIL-HOLD") {
t.Fatalf("a notice that could not go out: agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
if e := ofType(*events, EventEndedAppRefused); len(e) != 0 {
t.Fatalf("a notice that is only not sent yet raised the disagreement warning: %+v", e)
}
// (c) told 29 days ago: nothing. (d) told 31 days ago: agreed.
s, _ = endedFixtureNoNotice(t)
at := now.Add(-29 * 24 * time.Hour)
if err := s.Store.SetEndNoticeForTest("c1", "vikunja", "", at, at, store.EndNoticeHousehold); err != nil {
t.Fatal(err)
}
if g := openGranted(t, s, 20, claim); len(g.EndedAllowed) != 0 || !hasReason(g.EndedRefused, "29 full day(s), 30 needed") {
t.Fatalf("told 29 days ago: agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
s, _ = endedFixtureNoNotice(t)
at = now.Add(-31 * 24 * time.Hour)
if err := s.Store.SetEndNoticeForTest("c1", "vikunja", "", at, at, store.EndNoticeHousehold); err != nil {
t.Fatal(err)
}
if g := openGranted(t, s, 20, claim); strings.Join(g.EndedAllowed, ",") != strings.Join(ids, ",") {
t.Fatalf("positive control — told 31 days ago, absent 31 days: agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
// (e) the household is deleted between the notice and the window: its record is gone, nothing is agreed.
s, _ = endedFixtureNoNotice(t)
if err := s.Store.SetEndNoticeForTest("c1", "vikunja", "", at, at, store.EndNoticeHousehold); err != nil {
t.Fatal(err)
}
if err := s.Store.PurgeCustomerResidue("c1"); err != nil {
t.Fatal(err)
}
if ids, _, refused := s.agreeEndedFor("c1", 1, 10, []EndedClaim{claim}); len(ids) != 0 || len(refused) == 0 {
t.Fatalf("a deleted household: agreed %v, refused %+v", shortIDs(ids), refused)
}
}
// endedFixtureNoNotice is endedFixture without the notice row it writes.
func endedFixtureNoNotice(t *testing.T) (*Service, *[]evt) {
t.Helper()
s, events := endedFixture(t)
if err := s.Store.NoteEndNotices("c1", []string{"vikunja"}, nil, true, time.Now()); err != nil { // an install voids it
t.Fatal(err)
}
if ns, _ := s.Store.EndNotices("c1"); len(ns) != 0 {
t.Fatalf("fixture: %+v", ns)
}
return s, events
}
// Review finding 3: a name that cannot be put into a household's mail (a dot, a capital) is never agreed — even
// with a row that says „told”, which the hub itself can no longer write for it.
func TestReview3_ANameThatCannotBeToldIsNeverAgreed(t *testing.T) {
now := time.Date(2026, 12, 20, 3, 0, 0, 0, time.UTC)
old := now.Add(-60 * 24 * time.Hour)
listing := store.OffsiteSnapshotListing{ListedAt: now, Entries: map[string]time.Time{eid(1): old}}
for _, app := range []string{"www.evil-bank.example.com", "Vikunja"} {
ids, _, refused := agreeEnded(now,
map[string]store.AppPresence{app: {AbsentSince: now.Add(-40 * 24 * time.Hour)}},
map[string]store.EndNotice{app: {App: app, ToldAt: now.Add(-40 * 24 * time.Hour), ToldTo: store.EndNoticeHousehold}},
listing, true, []EndedClaim{{App: app, SnapshotIDs: []string{eid(1)}}})
if len(ids) != 0 || !hasReason(refused, "not a plain app name") {
t.Fatalf("%q: agreed %v, refused %+v", app, shortIDs(ids), refused)
}
}
}
// Review finding 1, through the window: told 31 days ago at one address, and the household's address is another one
// today — nothing is agreed, and the answer says why.
func TestReview1_Window_ToldAtAnotherAddressIsNotTold(t *testing.T) {
s, _ := endedFixture(t) // vikunja: absent 31 days, told 31 days ago at the fixture's address
claim := EndedClaim{App: "vikunja", SnapshotIDs: []string{eid(0)}}
if err := s.Store.SaveNotificationPrefs("c1", "someone-else@example.net", nil, 6); err != nil {
t.Fatal(err)
}
g := openGranted(t, s, 20, claim)
if len(g.EndedAllowed) != 0 || !hasReason(g.EndedRefused, "address changed") {
t.Fatalf("agreed %v, refused %+v", shortIDs(g.EndedAllowed), g.EndedRefused)
}
}