Files
felhom.eu/hub/internal/offsitekeys/ended_test.go
T
admin 42ef25c23b hub (unreleased): the notice lock — no notice, no deletion; the hub sends the household's notice itself (decision 315, R-1374)
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 in b8cf6c24).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 16:17:44 +01:00

354 lines
18 KiB
Go

package offsitekeys
import (
"context"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/notify"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
// D72 / D84 (`09` §3 decisions 292, 309; `07-backup-architecture.md` §8.2f): the hub agrees first, by its own clock,
// and the ended apps' allowance covers only ids it agreed to.
// RED-PROOFS: documentation/audits/build-release-4-2026-11-15/A/redproof-hub-ended.txt.
func eid(n int) string { return snapID(900 + n) }
func endedRefusedFor(refused []store.EndedRefusal, app string) string {
for _, r := range refused {
if r.App == app {
return r.Reason
}
}
return ""
}
// The pure decision, one wrong case per row. now is the hub's clock; the claim's own dates are the BOX's and every
// row gives them a value that would agree if the hub read them.
func TestD84_AgreeEnded_TheHubsOwnRecordsDecide(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) }
old := days(60) // an upload time well before every absence below
boxSaysLongAgo := func(app string, ids ...string) EndedClaim {
return EndedClaim{App: app, RemovedAt: days(400).Format(time.RFC3339), EndsAt: days(370).Format(time.RFC3339), SnapshotIDs: ids}
}
listing := store.OffsiteSnapshotListing{ListedAt: now.Add(-20 * time.Hour), Entries: map[string]time.Time{
eid(1): old, eid(2): old, eid(3): old, eid(4): days(10), eid(5): {}, eid(6): old, eid(7): days(37),
}}
cases := []struct {
name string
presence map[string]store.AppPresence
listing bool
claims []EndedClaim
wantIDs []string
refused map[string]string // app → a fragment of its reason
}{
{name: "absent 31 days by the hub's clock: agreed",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(31)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1), eid(2))}, wantIDs: []string{eid(1), eid(2)}},
{name: "a record with a date in the past, written by a box the hub does not agree with (absent 1 day)",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(1)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1))}, refused: map[string]string{"vikunja": "30 needed"}},
{name: "the box's clock is a month ahead: it claims on day 2",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(2)}}, listing: true,
claims: []EndedClaim{{App: "vikunja", RemovedAt: days(2).Format(time.RFC3339), EndsAt: now.Add(-time.Hour).Format(time.RFC3339), SnapshotIDs: []string{eid(1)}}},
refused: map[string]string{"vikunja": "2 full day(s)"}},
{name: "29 days and 23 hours is not 30 days",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: now.Add(-EndedAppAfter + time.Hour)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1))}, refused: map[string]string{"vikunja": "30 needed"}},
{name: "installed again the day before the window: the newest report lists it",
presence: map[string]store.AppPresence{"vikunja": {Present: true}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1))}, refused: map[string]string{"vikunja": "lists the app as installed"}},
{name: "an app the hub has no record of",
presence: map[string]store.AppPresence{}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("ghost", eid(1))}, refused: map[string]string{"ghost": "no record"}},
{name: "an absence whose date cannot be read is never long ago",
presence: map[string]store.AppPresence{"vikunja": {}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1))}, refused: map[string]string{"vikunja": "cannot be read"}},
{name: "the shares pseudo-app and the marker tag are never eligible, whatever the record says",
presence: map[string]store.AppPresence{"_shares": {AbsentSince: days(90)}, "felhom-offbox": {AbsentSince: days(90)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("_shares", eid(1)), boxSaysLongAgo("felhom-offbox", eid(2))},
refused: map[string]string{notAnAppName: "not an app", "felhom-offbox": "not an app"}},
{name: "the box's own infrastructure stacks are never eligible",
presence: map[string]store.AppPresence{"traefik": {AbsentSince: days(90)}, "Samba": {AbsentSince: days(90)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("traefik", eid(1)), boxSaysLongAgo("Samba", eid(2))},
refused: map[string]string{"traefik": "not an app", "Samba": "not an app"}},
{name: "a snapshot with two apps' tags stays until BOTH have ended",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}, "immich": {Present: true}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1), eid(2)), boxSaysLongAgo("immich", eid(2))},
wantIDs: []string{eid(1)}, refused: map[string]string{"immich": "installed", "": "listed under an app the hub refused"}},
{name: "two ended apps share a snapshot: it goes, and it must be older than BOTH absences",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}, "kimai": {AbsentSince: days(35)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1), eid(3)), boxSaysLongAgo("kimai", eid(3))},
wantIDs: []string{eid(1), eid(3)}},
{name: "two ended apps share a snapshot uploaded between their two ends: it is not older than both, so it stays",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}, "kimai": {AbsentSince: days(35)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("kimai", eid(7), eid(1)), boxSaysLongAgo("vikunja", eid(7))},
wantIDs: []string{eid(1)}, refused: map[string]string{"": "uploaded after"}},
{name: "an id the hub's own listing does not hold",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1), eid(99), "not-a-snapshot-id")},
wantIDs: []string{eid(1)}, refused: map[string]string{"": "not in the hub's own listing"}},
{name: "a file uploaded AFTER the hub saw the app leave is not that app's copy",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1), eid(4))},
wantIDs: []string{eid(1)}, refused: map[string]string{"": "uploaded after"}},
{name: "a file with no upload time in the hub's listing",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}}, listing: true,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(5), eid(6))},
wantIDs: []string{eid(6)}, refused: map[string]string{"": "no upload time"}},
{name: "no listing of the hub's own: no id is agreed",
presence: map[string]store.AppPresence{"vikunja": {AbsentSince: days(40)}}, listing: false,
claims: []EndedClaim{boxSaysLongAgo("vikunja", eid(1))}, refused: map[string]string{"": "no listing of its own"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// the notice lock is not this table's subject: every claimed app was told 31 days ago (TestNotice_* pin it)
notices := map[string]store.EndNotice{}
for _, cl := range c.claims {
notices[cl.App] = store.EndNotice{App: cl.App, ToldAt: days(31), ToldTo: store.EndNoticeHousehold}
}
ids, byApp, refused := agreeEnded(now, c.presence, notices, listing, c.listing, 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)
}
for app, frag := range c.refused {
if got := endedRefusedFor(refused, app); !strings.Contains(got, frag) {
t.Fatalf("refusal for %q = %q, want it to say %q (all: %+v)", app, got, frag, refused)
}
}
if len(c.refused) == 0 && len(refused) != 0 {
t.Fatalf("nothing should be refused: %+v", refused)
}
n := 0
for _, v := range byApp {
n += len(v)
}
if len(ids) == 0 && n != 0 {
t.Fatalf("no id agreed, yet the per-app record holds %v", byApp)
}
})
}
}
// endedFixture: the pinned household of watchFixture with a stored listing of 20 snapshot files (ids eid(0..19),
// uploaded 60 days ago) and an app record: vikunja absent for 31 days, immich installed.
func endedFixture(t *testing.T) (*Service, *[]evt) {
t.Helper()
s, _, events := watchFixture(t)
now := time.Now().UTC()
l := store.OffsiteSnapshotListing{ListedAt: now.Add(-20 * time.Hour), Entries: map[string]time.Time{}}
for i := 0; i < 20; i++ {
l.Entries[eid(i)] = now.Add(-60 * 24 * time.Hour)
}
if err := s.Store.SetOffsiteSnapshotListing("c1", l); err != nil {
t.Fatal(err)
}
if err := s.Store.NoteAppList("c1", []string{"vikunja", "immich"}, nil, now.Add(-90*24*time.Hour)); err != nil {
t.Fatal(err)
}
if err := s.Store.NoteAppList("c1", []string{"immich"}, nil, now.Add(-31*24*time.Hour)); err != nil {
t.Fatal(err)
}
toldLongAgo(t, s, "vikunja")
return s, events
}
// toldLongAgo writes the hub's own notice record for apps as told to the household 31 days ago (the notice lock is
// pinned by TestNotice_*; the D84 tests are about the other conditions).
func toldLongAgo(t *testing.T, s *Service, apps ...string) {
t.Helper()
at := time.Now().UTC().Add(-31 * 24 * time.Hour)
for _, app := range apps {
if err := s.Store.SetEndNoticeForTest("c1", app, "", at, at, store.EndNoticeHousehold); err != nil {
t.Fatal(err)
}
}
}
func openGranted(t *testing.T, s *Service, count int, ended ...EndedClaim) WindowGrant {
t.Helper()
if err := s.Store.GrantOffsiteWindowOnce("c1"); err != nil {
t.Fatal(err)
}
g, err := s.OpenWindowFor(context.Background(), "c1", count, ended...)
if err != nil || !g.Granted {
t.Fatalf("window not granted: %+v %v", g, err)
}
return g
}
// THE ALLOWANCE IS BESIDE THE CAP (D84). 20 snapshots, cap 10. The policy removes 10 (the whole cap) and the window
// also removes 6 snapshots of the ended app: a fall of 16 raises nothing — and the same fall WITHOUT the hub's
// agreement is the drop alarm it always was. The cap itself is not moved by the ended apps.
func TestD84_Window_TheEndedAllowanceIsBesideTheCap(t *testing.T) {
s, events := endedFixture(t)
ctx := context.Background()
ended := []string{eid(0), eid(1), eid(2), eid(3), eid(4), eid(5)}
g := openGranted(t, s, 20, EndedClaim{App: "vikunja", SnapshotIDs: ended}, EndedClaim{App: "immich", SnapshotIDs: []string{eid(10)}})
if g.MaxRemove != MaxRemove(20) {
t.Fatalf("the cap moved: %d, want %d — the ended apps must not touch it", g.MaxRemove, MaxRemove(20))
}
if strings.Join(g.EndedAllowed, ",") != strings.Join(ended, ",") {
t.Fatalf("agreed ids = %v, want the six of vikunja", shortIDs(g.EndedAllowed))
}
if r := endedRefusedFor(g.EndedRefused, "immich"); !strings.Contains(r, "installed") {
t.Fatalf("the window must say that immich was refused, and why: %+v", g.EndedRefused)
}
if w, _ := s.Store.GetOffsiteWindow(g.WindowID); len(w.EndedAgreed) != 6 || len(w.EndedRefused) == 0 {
t.Fatalf("the window's own row does not hold the agreement and the refusal: %+v", w)
}
if err := s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g.WindowID, CountBefore: 20, CountAfter: 4, Removed: 16, Outcome: "pruned", RemovedForEndedApps: ended}); err != nil {
t.Fatal(err)
}
if d := ofType(*events, EventWindowDrop); len(d) != 0 {
t.Fatalf("a fall of cap + agreed ended ids raised the drop alarm: %+v", d)
}
rm := ofType(*events, EventEndedAppRemoved)
if len(rm) != 1 || rm[0].sev != "info" || !strings.Contains(rm[0].msg, `"vikunja"`) || !strings.Contains(rm[0].msg, "6 snapshot") {
t.Fatalf("want one info record for vikunja with 6 snapshots: %+v", rm)
}
if w, _ := s.Store.GetOffsiteWindow(g.WindowID); len(w.EndedRemoved) != 6 {
t.Fatalf("the closed window does not hold removed_for_ended_apps: %+v", w)
}
// The same fall, and the box names ids the hub never agreed to (immich's, and invented ones): the drop alarm.
*events = nil
g2 := openGranted(t, s, 20, EndedClaim{App: "immich", SnapshotIDs: []string{eid(10), eid(11), eid(12), eid(13), eid(14), eid(15)}})
if len(g2.EndedAllowed) != 0 {
t.Fatalf("ids of an installed app were agreed: %v", shortIDs(g2.EndedAllowed))
}
if err := s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g2.WindowID, CountBefore: 20, CountAfter: 4, Removed: 16, Outcome: "pruned",
RemovedForEndedApps: []string{eid(10), eid(11), eid(12), eid(13), eid(14), eid(15)}}); err != nil {
t.Fatal(err)
}
if d := ofType(*events, EventWindowDrop); len(d) != 1 {
t.Fatalf("a fall beyond the cap with NO agreed id must raise the drop alarm, got %+v", *events)
}
if rm := ofType(*events, EventEndedAppRemoved); len(rm) != 0 {
t.Fatalf("a removal the hub did not agree to was recorded as an ended app's: %+v", rm)
}
}
// A window that is not granted agrees to nothing — the box's claim does not open a window and does not reach the row.
func TestD84_Window_NotGrantedAgreesNothing(t *testing.T) {
s, _ := endedFixture(t)
g, err := s.OpenWindowFor(context.Background(), "c1", 20, EndedClaim{App: "vikunja", SnapshotIDs: []string{eid(0)}})
if err != nil || g.Granted || len(g.EndedAllowed) != 0 {
t.Fatalf("weekly windows are off and there is no grant: %+v %v", g, err)
}
}
// The operator hears a CLEAR disagreement once per app, and nothing for a box that asks an hour early.
func TestD84_Window_ARefusalTellsTheOperatorOnlyWhenItIsClear(t *testing.T) {
s, events := endedFixture(t)
now := time.Now().UTC()
// kimai left 2 days ago (the month-ahead clock); nearly left 29 days 23 hours ago (asks an hour early)
_ = s.Store.NoteAppList("c1", []string{"immich", "kimai", "nearly"}, nil, now.Add(-80*24*time.Hour))
_ = s.Store.NoteAppList("c1", []string{"immich", "kimai"}, nil, now.Add(-EndedAppAfter+time.Hour))
_ = s.Store.NoteAppList("c1", []string{"immich"}, nil, now.Add(-2*24*time.Hour))
toldLongAgo(t, s, "kimai", "nearly", "immich")
g := openGranted(t, s, 20,
EndedClaim{App: "kimai", EndsAt: now.Add(-time.Hour).Format(time.RFC3339), SnapshotIDs: []string{eid(1)}},
EndedClaim{App: "nearly", SnapshotIDs: []string{eid(2)}},
EndedClaim{App: "immich", SnapshotIDs: []string{eid(3)}})
if len(g.EndedAllowed) != 0 {
t.Fatalf("agreed: %v", shortIDs(g.EndedAllowed))
}
ref := ofType(*events, EventEndedAppRefused)
var apps []string
for _, e := range ref {
if e.sev != "warning" {
t.Fatalf("severity %q is not in the hub's vocabulary for this event", e.sev)
}
for _, a := range []string{"kimai", "nearly", "immich"} {
if strings.Contains(e.msg, `"`+a+`"`) {
apps = append(apps, a)
}
}
}
if strings.Join(apps, ",") != "kimai,immich" {
t.Fatalf("the operator must hear kimai (2 days) and immich (installed), and not `nearly` (an hour early); heard %v", apps)
}
for _, typ := range []string{EventEndedAppRefused, EventEndedAppRemoved} {
if !notify.IsOperatorOnly(typ) {
t.Fatalf("%s must be operator-only", typ)
}
}
}
// THE 07:10 AUDIT (must-hold 6): the files a window removed for ended apps are explained by name and raise nothing —
// and a file that vanished for any other reason still raises, alone.
func TestD84_Audit_ExplainsEndedAppsAndStillRaisesForTheRest(t *testing.T) {
s, events := endedFixture(t)
ctx := context.Background()
now := time.Now().UTC()
ended := []string{eid(0), eid(1), eid(2)}
g := openGranted(t, s, 20, EndedClaim{App: "vikunja", SnapshotIDs: ended})
if err := s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g.WindowID, CountBefore: 20, CountAfter: 17, Removed: 3, Outcome: "pruned", RemovedForEndedApps: ended}); err != nil {
t.Fatal(err)
}
*events = nil
listingWithout := func(gone ...string) SnapshotListing {
l := SnapshotListing{Entries: map[string]time.Time{}}
skip := map[string]bool{}
for _, id := range gone {
skip[id] = true
}
for i := 0; i < 20; i++ {
if !skip[eid(i)] {
l.Entries[eid(i)] = now.Add(-60 * 24 * time.Hour)
}
}
return l
}
// exactly the three agreed ids are gone: nothing is raised (the window's cap part explains 0 here: 20-17-3)
if v, _ := s.CheckSnapshotListing("c1", listingWithout(ended...), now.Add(time.Minute)); v != 0 {
t.Fatalf("the ended apps' files were judged as vanished (%d)", v)
}
if e := ofType(*events, EventSnapshotsVanished); len(e) != 0 {
t.Fatalf("an alarm for files the hub itself agreed to remove: %+v", e)
}
// A second household state: the same window, but ONE more file is gone that no window explains.
s2, events2 := endedFixture(t)
g2 := openGranted(t, s2, 20, EndedClaim{App: "vikunja", SnapshotIDs: ended})
if err := s2.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g2.WindowID, CountBefore: 20, CountAfter: 17, Removed: 3, Outcome: "pruned", RemovedForEndedApps: ended}); err != nil {
t.Fatal(err)
}
*events2 = nil
if v, _ := s2.CheckSnapshotListing("c1", listingWithout(append([]string{eid(15)}, ended...)...), now.Add(time.Minute)); v != 1 {
t.Fatalf("one file vanished outside the agreement: want it judged alone, got %d", v)
}
e := ofType(*events2, EventSnapshotsVanished)
if len(e) != 1 || !strings.Contains(e[0].msg, shortID(eid(15))) || strings.Contains(e[0].msg, shortID(eid(0))) {
t.Fatalf("want one alarm naming only the unexplained file %s: %+v", shortID(eid(15)), e)
}
}
// A box that names an ended app's id it did NOT get agreed cannot have it explained by the audit either.
func TestD84_Audit_AnIdTheHubDidNotAgreeToIsNotExplained(t *testing.T) {
s, events := endedFixture(t)
ctx := context.Background()
now := time.Now().UTC()
g := openGranted(t, s, 20, EndedClaim{App: "immich", SnapshotIDs: []string{eid(7)}})
if err := s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g.WindowID, CountBefore: 20, CountAfter: 20, Removed: 0, Outcome: "nothing", RemovedForEndedApps: []string{eid(7)}}); err != nil {
t.Fatal(err)
}
*events = nil
l := SnapshotListing{Entries: map[string]time.Time{}}
for i := 0; i < 20; i++ {
if i != 7 {
l.Entries[eid(i)] = now.Add(-60 * 24 * time.Hour)
}
}
if v, _ := s.CheckSnapshotListing("c1", l, now.Add(time.Minute)); v != 1 {
t.Fatalf("the file of an installed app vanished and was not judged (%d)", v)
}
if e := ofType(*events, EventSnapshotsVanished); len(e) != 1 {
t.Fatalf("want the vanished alarm: %+v", *events)
}
}