The hub agrees to remove a removed app's off-site copies only when its OWN record (offsite_end_notices) says the household was told at least 30 days ago, by the hub's clock — beside the 30 days of absence. The notice is sent from the box's report, at every report until the mail service accepts it; the box's event mails nobody. No address or a blocked household: the operator is mailed in its place and that mail starts the 30 days. A notice that cannot go out: no agreement, the window's answer names the reason, one line to the operator after 3 days. This commit also supplies the files that handler.go (c3fa43a4) already calls (store.NoteEndNotices, api.kickEndNotices) and the tests that replace TestD84_Receipt_* (their files left the tree inb8cf6c24). Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
150 lines
8.6 KiB
Go
150 lines
8.6 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
|
|
}
|