Files
felhom.eu/hub/internal/store/d84_ended_test.go
T
admin 01a106b78c hub: the off-site copies of a removed app end after 30 days — the hub agrees first, the allowance is beside the cap, the household's mail (R-1374 part 1, D72, D84)
Built, not deployed. `07-backup-architecture.md` §8.2f parts 3-4 and decision 309.

- store: the hub's own record of which apps a box runs, by the hub's clock
  (offsite_app_presence, fed by every controller report); it goes with the household.
- offsitekeys: window-open takes the box's due end records and answers
  ended_allowed / ended_refused. An app is agreed only after 30 days of absence
  in the hub's record; an id only when the hub's own listing holds it, uploaded
  before that absence. The cap (max_remove) is unchanged; the close keeps only
  agreed ids from removed_for_ended_apps and allows them beside the cap.
- The 07:10 audit explains the ended apps' vanished files by name and still
  raises for every other vanished file.
- notify: offsite_end_scheduled is a household receipt — one mail per press to
  the household's own address, whatever its switches; the operator is not mailed.
- Two operator-only hub records: offsite_ended_app_removed (info),
  offsite_ended_app_refused (warning).

Tests TestD72_*, TestD84_*; 44 mutations seen red, 5 after a redo
(documentation/audits/build-release-4-2026-11-15/A/redproof-hub-*.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 13:26:13 +01:00

173 lines
7.7 KiB
Go

package store
import (
"testing"
"time"
)
// D72 (`09` §3 decision 292; `07` §8.2f part 4): the hub keeps ITS OWN record of when an app left a box's app list,
// by the hub's clock. RED-PROOFS: documentation/audits/build-release-4-2026-11-15/A/redproof-hub-store.txt.
func TestD72_NoteAppList_TheHubsOwnClock(t *testing.T) {
s := newTestStore(t)
t0 := time.Date(2026, 11, 1, 3, 0, 0, 0, time.UTC)
note := func(at time.Time, deployed, claimed []string) {
t.Helper()
if err := s.NoteAppList("c1", deployed, claimed, at); err != nil {
t.Fatal(err)
}
}
get := func() map[string]AppPresence {
t.Helper()
m, err := s.AppPresences("c1")
if err != nil {
t.Fatal(err)
}
return m
}
note(t0, []string{"vikunja", "immich"}, nil)
if m := get(); !m["vikunja"].Present || !m["immich"].Present || len(m) != 2 {
t.Fatalf("first report: %+v", m)
}
// vikunja leaves: absent since the FIRST report without it, and a later report does not move that time.
note(t0.Add(time.Hour), []string{"immich"}, nil)
note(t0.Add(5*time.Hour), []string{"immich"}, nil)
if p := get()["vikunja"]; p.Present || !p.AbsentSince.Equal(t0.Add(time.Hour)) {
t.Fatalf("vikunja must be absent since the first report without it (%s), got %+v", t0.Add(time.Hour), p)
}
if p := get()["immich"]; !p.Present || !p.AbsentSince.IsZero() {
t.Fatalf("immich is still listed: %+v", p)
}
// installed again: the absence is forgotten; removed again later: a NEW absence, never the old date.
note(t0.Add(48*time.Hour), []string{"immich", "vikunja"}, nil)
if p := get()["vikunja"]; !p.Present || !p.AbsentSince.IsZero() {
t.Fatalf("an install must end the absence: %+v", p)
}
note(t0.Add(72*time.Hour), []string{"immich"}, nil)
if p := get()["vikunja"]; p.Present || !p.AbsentSince.Equal(t0.Add(72*time.Hour)) {
t.Fatalf("the second removal must start a new absence at its own time: %+v", p)
}
// An app the hub never saw, named in a pending end record: the hub's clock starts when it first hears the claim.
note(t0.Add(80*time.Hour), []string{"immich"}, []string{"oldapp", "immich"})
note(t0.Add(90*time.Hour), []string{"immich"}, []string{"oldapp"})
m := get()
if p, ok := m["oldapp"]; !ok || p.Present || !p.AbsentSince.Equal(t0.Add(80*time.Hour)) {
t.Fatalf("a claimed, never-seen app must be absent since the first claim (%s): %+v ok=%v", t0.Add(80*time.Hour), p, ok)
}
if !m["immich"].Present {
t.Fatalf("a claim must never make a listed app absent: %+v", m["immich"])
}
// An app that is listed AND named in an end record in the very report that first shows it: it is installed.
note(t0.Add(91*time.Hour), []string{"immich", "fresh"}, []string{"oldapp", "fresh"})
if p, ok := get()["fresh"]; !ok || !p.Present {
t.Fatalf("a listed app named in an end record must be present: %+v ok=%v", p, ok)
}
// another household's box is not touched
if o, _ := s.AppPresences("c2"); len(o) != 0 {
t.Fatalf("c2 has rows: %+v", o)
}
}
// D84 (`09` §3 decision 309): a window's credit for ended apps is BESIDE its cap. The cap's part is what it was;
// the ended part is only ids the hub agreed to.
func TestD84_WindowCredits_TheEndedAllowanceIsBesideTheCap(t *testing.T) {
s := newTestStore(t)
at := func(ts string) time.Time { v, _ := time.Parse("2006-01-02 15:04:05", ts); return v }
from, to := at("2026-09-30 00:00:00"), at("2026-10-02 00:00:00")
ins := func(cust, closed string, before, after, maxRemove any, agreed, removed any) {
t.Helper()
var c any
if closed != "" {
c = closed
}
if _, err := s.db.Exec(`INSERT INTO offsite_windows (customer_id, opened_at, closes_by, closed_at, count_before, count_after, max_remove, ended_agreed, ended_removed) VALUES (?, '2026-10-01 03:00:00', '2026-10-01 23:00:00', ?, ?, ?, ?, ?, ?)`,
cust, c, before, after, maxRemove, agreed, removed); err != nil {
t.Fatal(err)
}
}
one := func(cust string) WindowCredit {
t.Helper()
cs, unk := s.WindowCreditsBetween(cust, from, to)
if unk || len(cs) != 1 {
t.Fatalf("%s: want one known credit, got %+v unknown=%v", cust, cs, unk)
}
return cs[0]
}
// closed, 40 -> 28: 3 ids for ended apps (named by the box, agreed by the hub) + 9 under a cap of 20
ins("a", "2026-10-01 03:10:00", 40, 28, 20, `["x1","x2","x3","x4"]`, `["x1","x2","x3"]`)
if c := one("a"); c.CapExplains != 9 || c.Explains != 12 || len(c.EndedIDs) != 3 {
t.Fatalf("a: want cap 9 + ended 3 = 12, got %+v", c)
}
// a lying box: count fell 40 -> 0; the cap still explains only 20, the ended part only the 3 named ids
ins("l", "2026-10-01 03:10:00", 40, 0, 20, `["x1","x2","x3","x4"]`, `["x1","x2","x3"]`)
if c := one("l"); c.CapExplains != 20 || c.Explains != 23 {
t.Fatalf("lying box: want cap 20 + ended 3, got %+v", c)
}
// no box result (still open): the cap, plus every id the hub AGREED to — never more
ins("o", "", 40, nil, 20, `["x1","x2","x3","x4"]`, nil)
if c := one("o"); c.CapExplains != 20 || c.Explains != 24 || len(c.EndedIDs) != 4 {
t.Fatalf("open window: want cap 20 + the 4 agreed ids, got %+v", c)
}
// only ended apps went (the policy removed nothing): the window still explains them
ins("e", "2026-10-01 03:10:00", 40, 38, 20, `["x1","x2"]`, `["x1","x2"]`)
if c := one("e"); c.CapExplains != 0 || c.Explains != 2 || len(c.EndedIDs) != 2 {
t.Fatalf("ended only: want cap 0 + ended 2, got %+v", c)
}
// a window with no ended app is exactly what it was
ins("p", "2026-10-01 03:10:00", 40, 31, 20, nil, nil)
if c := one("p"); c.CapExplains != 9 || c.Explains != 9 || len(c.EndedIDs) != 0 {
t.Fatalf("plain window changed: %+v", c)
}
}
// The box's result may name only ids the hub agreed to at the open: anything else is dropped before it is stored.
func TestD84_EndedRemoved_OnlyAgreedIdsAreStored(t *testing.T) {
s := newTestStore(t)
id, err := s.OpenOffsiteWindowRow("c1", time.Now().Add(20*time.Minute), 40, 20)
if err != nil {
t.Fatal(err)
}
if err := s.SetOffsiteWindowEnded(id, []string{"a1", "a2"}, map[string][]string{"vikunja": {"a1", "a2"}}, []EndedRefusal{{App: "immich", Reason: "listed"}}); err != nil {
t.Fatal(err)
}
kept, err := s.SetOffsiteWindowEndedRemoved(id, []string{"a2", "zz", "a2", "a1", "living"})
if err != nil || len(kept) != 2 || kept[0] != "a1" || kept[1] != "a2" {
t.Fatalf("kept = %v err=%v, want [a1 a2]", kept, err)
}
w, _ := s.GetOffsiteWindow(id)
if len(w.EndedRemoved) != 2 || len(w.EndedAgreed) != 2 || len(w.EndedRefused) != 1 || w.EndedRefused[0].App != "immich" || len(w.EndedApps["vikunja"]) != 2 {
t.Fatalf("stored row: %+v", w)
}
// a window that agreed to nothing stores nothing, whatever the box names
id2, _ := s.OpenOffsiteWindowRow("c1", time.Now().Add(20*time.Minute), 40, 20)
if kept, _ := s.SetOffsiteWindowEndedRemoved(id2, []string{"a1"}); len(kept) != 0 {
t.Fatalf("a window with no agreement stored %v", kept)
}
if w2, _ := s.GetOffsiteWindow(id2); len(w2.EndedRemoved) != 0 {
t.Fatalf("stored on a window with no agreement: %+v", w2)
}
}
// The hub's app record goes with the household (D73's rule for every household-keyed table).
func TestD72_AppRecordGoesWithTheHousehold(t *testing.T) {
s := newTestStore(t)
if err := s.NoteAppList("gone", []string{"vikunja"}, nil, time.Now()); err != nil {
t.Fatal(err)
}
if r, err := s.CustomerResidue("gone"); err != nil || r.OffsiteAppPresence != 1 || r.Total() < 1 {
t.Fatalf("the residue count does not see the app record: %+v %v", r, err)
}
if err := s.PurgeCustomerResidue("gone"); err != nil {
t.Fatal(err)
}
if m, _ := s.AppPresences("gone"); len(m) != 0 {
t.Fatalf("the app record outlived the household: %+v", m)
}
// and the daily orphan sweep removes what an earlier delete left
if err := s.NoteAppList("ghost", []string{"vikunja"}, nil, time.Now()); err != nil {
t.Fatal(err)
}
if n, err := s.PurgeOrphanRows(); err != nil || n["offsite_app_presence"] != 1 {
t.Fatalf("orphan sweep: %+v %v", n, err)
}
}