Files
felhom.eu/hub/internal/offsitekeys/ended_bound_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

226 lines
11 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"
)
// Review of 2026-11-15, finding 1: a box could invent an ended app (or stop listing a living one), wait 30 days, and
// have the hub agree to snapshots of the WHOLE store beyond the cap, with no operator signal. Three things now hold:
// the ended allowance of ONE window is at most the cap's own size (the rest waits for the next window); the operator
// is warned when a window is asked for more than half of the store; and (api, store) the operator is warned the
// moment a box first names an app the hub never saw installed — 30 days before anything can go.
// RED-PROOF: documentation/audits/build-release-4-2026-11-15/A/redproof-review-1.txt.
// ghostFixture: 20 snapshot files (40 days old) and a box that has only ever NAMED „ghost” in an end record, 31 days
// ago; immich is installed all along.
func ghostFixture(t *testing.T) (*Service, *[]evt, []string) {
t.Helper()
s, _, events := watchFixture(t)
now := time.Now().UTC()
l := store.OffsiteSnapshotListing{ListedAt: now.Add(-20 * time.Hour), Entries: map[string]time.Time{}}
var all []string
for i := 0; i < 20; i++ {
l.Entries[eid(i)] = now.Add(-time.Duration(40*24+i) * time.Hour) // eid(19) is the oldest
all = append(all, eid(i))
}
if err := s.Store.SetOffsiteSnapshotListing("c1", l); err != nil {
t.Fatal(err)
}
if err := s.Store.NoteAppList("c1", []string{"immich"}, []string{"ghost"}, now.Add(-31*24*time.Hour)); err != nil {
t.Fatal(err)
}
if err := s.Store.NoteAppList("c1", []string{"immich"}, []string{"ghost"}, now.Add(-time.Hour)); err != nil {
t.Fatal(err)
}
toldLongAgo(t, s, "ghost")
*events = nil
return s, events, all
}
// aWeekLater moves the hub's own first sight of the household's large requests eight days into the past.
func aWeekLater(t *testing.T, s *Service, _ ...int64) {
t.Helper()
if err := s.Store.BackdateLargeEndedRequestsForTest("c1", 8*24*time.Hour); err != nil {
t.Fatal(err)
}
}
func hasReason(refused []store.EndedRefusal, frag string) bool {
for _, r := range refused {
if strings.Contains(r.Reason, frag) {
return true
}
}
return false
}
// THE WORST CASE: the box names EVERY snapshot under the ghost. The first window agrees to NOTHING and the operator
// is mailed; if the box removes the store anyway, the drop alarm and the 07:10 audit both fire. A week later a window
// agrees — to at most the cap's own size, the oldest files first — and says that the rest waits.
func TestD84_ALargeRequestWaitsAWeekAndOneWindowAgreesAtMostTheCapsSize(t *testing.T) {
s, events, all := ghostFixture(t)
g := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: all})
if g.MaxRemove != MaxRemove(20) {
t.Fatalf("the cap's own rule moved: %d, want %d", g.MaxRemove, MaxRemove(20))
}
if len(g.EndedAllowed) != 0 {
t.Fatalf("the window that first saw a request for the whole store agreed to %d ids — it must agree to none and tell the operator first", len(g.EndedAllowed))
}
if !hasReason(g.EndedRefused, "waits one week") {
t.Fatalf("the window does not say that the request waits: %+v", g.EndedRefused)
}
big := ofType(*events, EventEndedLargeAgreement)
if len(big) != 1 || big[0].sev != "warning" || !strings.Contains(big[0].msg, "c1") || !strings.Contains(big[0].msg, "20 of 20") || !strings.Contains(big[0].msg, "ghost") {
t.Fatalf("want ONE operator warning naming the household, the app and „20 of 20”: %+v", *events)
}
// the box removes the WHOLE store anyway and names all 20: nothing was agreed, so both alarms fire
*events = nil
if err := s.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: g.WindowID, CountBefore: 20, CountAfter: 0, Removed: 20, Outcome: "pruned", RemovedForEndedApps: all}); err != nil {
t.Fatal(err)
}
if len(ofType(*events, EventWindowDrop)) != 1 {
t.Fatalf("no drop alarm for a store emptied with nothing agreed: %+v", *events)
}
if v, _ := s.CheckSnapshotListing("c1", SnapshotListing{Entries: map[string]time.Time{}}, time.Now().UTC().Add(time.Minute)); v == 0 {
t.Fatal("the audit explained a store emptied beyond what the hub agreed to")
}
// A second household state, where the box waits: two days later still nothing; a week later the cap's size.
s2, events2, all2 := ghostFixture(t)
g1 := openGranted(t, s2, 20, EndedClaim{App: "ghost", SnapshotIDs: all2})
_ = s2.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: g1.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
if err := s2.Store.BackdateLargeEndedRequestsForTest("c1", 2*24*time.Hour); err != nil {
t.Fatal(err)
}
gEarly := openGranted(t, s2, 20, EndedClaim{App: "ghost", SnapshotIDs: all2})
_ = s2.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: gEarly.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
if len(gEarly.EndedAllowed) != 0 {
t.Fatalf("two days after the warning a window agreed to %d ids — the operator must have a week", len(gEarly.EndedAllowed))
}
aWeekLater(t, s2, g1.WindowID, gEarly.WindowID)
g2 := openGranted(t, s2, 20, EndedClaim{App: "ghost", SnapshotIDs: all2})
if len(g2.EndedAllowed) != g2.MaxRemove || g2.MaxRemove != 10 {
t.Fatalf("a week later: %d ended ids agreed with a cap of %d — want exactly the cap's size", len(g2.EndedAllowed), g2.MaxRemove)
}
for _, id := range g2.EndedAllowed { // the OLDEST go first: eid(10)..eid(19)
ok := false
for i := 10; i < 20; i++ {
ok = ok || id == eid(i)
}
if !ok {
t.Fatalf("the allowance did not take the oldest files first: %s agreed", shortID(id))
}
}
if !hasReason(g2.EndedRefused, "wait for the next window") {
t.Fatalf("the window does not say that the rest waits: %+v", g2.EndedRefused)
}
if n := len(ofType(*events2, EventEndedLargeAgreement)); n != 1 {
t.Fatalf("the operator was told %d time(s) about one large request, want once", n)
}
if w, _ := s2.Store.GetOffsiteWindow(g2.WindowID); len(w.EndedAgreed) != 10 {
t.Fatalf("the window's row holds %d agreed ids", len(w.EndedAgreed))
}
}
// The week belongs to the apps that were told: a large request for ANOTHER app, a week after the first one's warning,
// waits its own week and gets its own mail.
func TestD84_ALargeRequestForOtherAppsWaitsItsOwnWeek(t *testing.T) {
s, events, all := ghostFixture(t)
now := time.Now().UTC()
_ = s.Store.NoteAppList("c1", []string{"immich"}, []string{"ghost", "second"}, now.Add(-31*24*time.Hour-time.Minute))
toldLongAgo(t, s, "second")
g0 := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: all})
_ = s.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: g0.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
aWeekLater(t, s, g0.WindowID)
g1 := openGranted(t, s, 20, EndedClaim{App: "second", SnapshotIDs: all})
if len(g1.EndedAllowed) != 0 || !hasReason(g1.EndedRefused, "waits one week") {
t.Fatalf("a large request for another app rode the first app's week: agreed %d, refused %+v", len(g1.EndedAllowed), g1.EndedRefused)
}
if n := len(ofType(*events, EventEndedLargeAgreement)); n != 2 {
t.Fatalf("two large requests for different apps gave %d operator warning(s), want 2", n)
}
}
// AN HONEST LARGE END IS NOT LOST: it waits a week, then goes — the cap's size per window, the rest in the next one.
func TestD84_TheRestGoesInTheNextWindow(t *testing.T) {
s, _, all := ghostFixture(t)
ctx := context.Background()
big := all[5:] // 15 ids of a big removed app
g0 := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: big})
if len(g0.EndedAllowed) != 0 {
t.Fatalf("the first window agreed to %d", len(g0.EndedAllowed))
}
_ = s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g0.WindowID, CountBefore: 20, CountAfter: 20, Outcome: "nothing"})
aWeekLater(t, s, g0.WindowID)
g1 := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: big})
if len(g1.EndedAllowed) != 10 {
t.Fatalf("a week later: %d agreed, want the cap's size 10", len(g1.EndedAllowed))
}
if err := s.CloseWindowFor(ctx, "c1", WindowResult{WindowID: g1.WindowID, CountBefore: 20, CountAfter: 10, Removed: 10, Outcome: "pruned", RemovedForEndedApps: g1.EndedAllowed}); err != nil {
t.Fatal(err)
}
gone := map[string]bool{}
for _, id := range g1.EndedAllowed {
gone[id] = true
}
var rest []string
for _, id := range big {
if !gone[id] {
rest = append(rest, id)
}
}
// the next day's audit: the hub's own listing now holds the 10 that are left, and raises nothing
left := SnapshotListing{Entries: map[string]time.Time{}}
for _, id := range all {
if !gone[id] {
left.Entries[id] = time.Now().UTC().Add(-40 * 24 * time.Hour)
}
}
if v, _ := s.CheckSnapshotListing("c1", left, time.Now().UTC().Add(time.Minute)); v != 0 {
t.Fatalf("the agreed removals were judged as vanished (%d)", v)
}
g2 := openGranted(t, s, 10, EndedClaim{App: "ghost", SnapshotIDs: rest})
if len(rest) != 5 || strings.Join(g2.EndedAllowed, ",") != strings.Join(rest, ",") {
t.Fatalf("the next window: agreed %v, want the 5 that waited %v (refused: %+v)", shortIDs(g2.EndedAllowed), shortIDs(rest), g2.EndedRefused)
}
}
// The bound uses the hub's OWN count of the store, not only the box's word: a box that says „1000 snapshots” gets a
// large cap for its policy, and still no larger ended allowance than half of what the hub's listing holds.
func TestD84_TheAllowanceIsBoundByTheHubsOwnCount(t *testing.T) {
s, _, all := ghostFixture(t)
g0 := openGranted(t, s, 1000, EndedClaim{App: "ghost", SnapshotIDs: all})
_ = s.CloseWindowFor(context.Background(), "c1", WindowResult{WindowID: g0.WindowID, CountBefore: 1000, CountAfter: 1000, Outcome: "nothing"})
aWeekLater(t, s, g0.WindowID)
g := openGranted(t, s, 1000, EndedClaim{App: "ghost", SnapshotIDs: all})
if g.MaxRemove != MaxRemove(1000) {
t.Fatalf("cap = %d", g.MaxRemove)
}
if len(g.EndedAllowed) != MaxRemove(20) {
t.Fatalf("a box that claims 1000 snapshots got %d ended ids agreed; the hub's own listing holds 20 → at most %d", len(g.EndedAllowed), MaxRemove(20))
}
}
// A normal end (3 of 20) raises no warning and waits for nothing.
func TestD84_ASmallEndIsQuiet(t *testing.T) {
s, events, all := ghostFixture(t)
g := openGranted(t, s, 20, EndedClaim{App: "ghost", SnapshotIDs: all[:3]})
if len(g.EndedAllowed) != 3 || len(g.EndedRefused) != 0 {
t.Fatalf("agreed=%d refused=%+v", len(g.EndedAllowed), g.EndedRefused)
}
if e := ofType(*events, EventEndedLargeAgreement); len(e) != 0 {
t.Fatalf("a small end raised the large-agreement warning: %+v", e)
}
for _, typ := range []string{EventEndedLargeAgreement, EventEndedClaimUnseen} {
if !notify.IsOperatorOnly(typ) {
t.Fatalf("%s must be operator-only", typ)
}
}
}