Files
felhom-controller/controller/internal/backup/offbox_ended_test.go
T
admin f628cb5082
gates / gates (push) Successful in 41s
The off-site copies of a removed app end 30 days after „delete backup data” — the box's half (felhom.eu R-1374 part 1; D72, D84)
Built, not released; no box has run it. felhom.eu `07-backup-architecture.md` §8.2f.
It does nothing without a hub that knows it: an older hub agrees to no id.

- The record: settings `offsite_ends` {app, removed_at, ends_at}. One writer
  (backup.StartOffsiteEnd), called by a removal only with „delete backup data”
  and by „Törlés 30 nap múlva” on the restore page. A second press changes
  nothing. No record, no deletion. Only on the hub-provisioned tier.
- The date is on the remove dialog before the press; the restore page shows
  „Törlésre kijelölve …” with „Mégis megtartom”, and one history line per
  carried-out end. An install of the same app deletes the record.
- The weekly window: after the guard has passed the policy's plan under the cap
  as before, it adds the snapshots whose app tags are ALL due records, that the
  hub agreed to and the box named in the same request; the result names them
  apart (removed_for_ended_apps). `_shares`, the box's own stacks and an
  installed app are never due.
- The report carries the pending records; the household gets one mail per
  press (offsite_end_scheduled), in both languages.

MinAgent unchanged (0.158.1). 143 parity fixtures gained the dialog's one new
script line (patched, not regenerated); one new parity case.
Tests TestD72_*, TestD84_*; 56 mutations seen red
(felhom.eu documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-*.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-11-15 14:09:47 +01:00

571 lines
25 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package backup
import (
"context"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
)
// D72 / D84 (`09` §3 decisions 292 and 309; `07-backup-architecture.md` §8.2f): the off-site copies of a removed app
// end 30 days after „delete backup data”, in the weekly window, for ids the hub agreed to. restic is the package's
// fake runner (as in offbox_window_test.go); here it keeps state, so a forget really removes what it names.
// RED-PROOFS: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-window.txt.
// endedRunner: `snapshots` lists what is left, `forget --dry-run` gives the policy's plan, a real `forget` removes
// the ids it names (and is recorded).
type endedRunner struct {
snaps []guardSnap
plan []guardSnap
forgets [][]string
}
func (e *endedRunner) run(_ context.Context, _ []string, args ...string) ([]byte, error) {
switch {
case contains(args, "forget") && contains(args, "--dry-run"):
return planJSON(e.plan), nil
case contains(args, "forget"):
e.forgets = append(e.forgets, append([]string{}, args...))
gone := map[string]bool{}
for _, a := range args {
gone[a] = true
}
var left []guardSnap
for _, s := range e.snaps {
if !gone[s.ID] {
left = append(left, s)
}
}
e.snaps = left
return nil, nil
case contains(args, "snapshots"):
return snapJSON(e.snaps), nil
}
return nil, nil
}
// removedIDs are the snapshot ids every real forget named, sorted.
func (e *endedRunner) removedIDs() []string {
var out []string
for _, f := range e.forgets {
for _, a := range f {
if strings.HasSuffix(a, "-full") {
out = append(out, a)
}
}
}
sort.Strings(out)
return out
}
func tagged(id string, at time.Time, tags ...string) guardSnap {
s := snap(id, at)
s.Tags = append([]string{OffboxMarkerTag}, tags...)
return s
}
func ids(names ...string) []string {
out := make([]string, 0, len(names))
for _, n := range names {
out = append(out, n+"-full")
}
sort.Strings(out)
return out
}
// agreeingHub is the fake hub: it grants the window and agrees to exactly the ids in `allow` — or, with allowAll, to
// EVERY id in the repository (a hub that would agree to anything: the box must still remove only its own candidates).
type agreeingHub struct {
fakeWindow
allow []string
allowAll func() []string
refuse []OffsiteEndedRefusal
maxRm int
}
func (h *agreeingHub) Open(ctx context.Context, n int, ended []OffsiteEndedClaim) (OffsiteWindow, error) {
h.opened++
h.claims = ended
w := OffsiteWindow{Granted: true, ID: 7, NewestAllowed: time.Now(), MaxRemove: h.maxRm, EndedAllowed: h.allow, EndedRefused: h.refuse}
if h.allowAll != nil {
w.EndedAllowed = h.allowAll()
}
return w, nil
}
// endedBox: a pinned box whose living check says `living` apps are installed. The repository, 60 days old:
// vikunja ×3 (v1..v3), immich ×2 (i1,i2), one snapshot of BOTH (vi), the shares (s1), kimai ×1 (k1).
func endedBox(t *testing.T, living ...string) (*Manager, *settings.Settings, *endedRunner, *agreeingHub) {
t.Helper()
m, sett := newOffboxManager(t)
pinTarget(t, sett)
old := time.Now().Add(-60 * 24 * time.Hour)
er := &endedRunner{snaps: []guardSnap{
tagged("v1", old, "vikunja"), tagged("v2", old.Add(time.Hour), "vikunja"), tagged("v3", old.Add(2*time.Hour), "vikunja"),
tagged("i1", old, "immich"), tagged("i2", old.Add(time.Hour), "immich"),
tagged("vi", old, "vikunja", "immich"),
tagged("s1", old, SharesPseudoStack),
tagged("k1", old, "kimai"),
}}
m.SetOffboxRunner(er.run)
hub := &agreeingHub{maxRm: 5}
hub.allowAll = func() []string {
var all []string
for _, s := range er.snaps {
all = append(all, s.ID)
}
return all
}
m.SetOffsiteWindowClient(hub)
live := map[string]bool{}
for _, l := range living {
live[l] = true
}
m.SetOffsiteEndLiving(func(app string) bool { return live[app] })
return m, sett, er, hub
}
// pressed writes an end record as the household's press `daysAgo` days ago.
func pressed(t *testing.T, sett *settings.Settings, app string, daysAgo int) settings.OffsiteEnd {
t.Helper()
rec, created, err := sett.StartOffsiteEnd(app, time.Now().Add(-time.Duration(daysAgo)*24*time.Hour))
if err != nil || !created {
t.Fatalf("press for %s: created=%v err=%v", app, created, err)
}
return rec
}
// MUST-HOLD 1 — NO RECORD, NO DELETION, EVER. The hub here agrees to EVERY id in the repository, and still nothing is
// removed: a removal that kept the backups, a rebuilt guest and an app removed before this was built all look the
// same on the box — there is no record.
func TestD72_NoRecordNoDeletion(t *testing.T) {
for _, name := range []string{
"removed without „delete backup data”",
"a rebuilt guest (its settings are new)",
"an app removed before this was built",
} {
t.Run(name, func(t *testing.T) {
m, _, er, hub := endedBox(t, "immich")
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if hub.opened != 1 || len(hub.claims) != 0 {
t.Fatalf("opened=%d claims=%+v — with no record the box must claim nothing", hub.opened, hub.claims)
}
if got := er.removedIDs(); len(got) != 0 {
t.Fatalf("with NO end record the window removed %v", got)
}
if len(er.snaps) != 8 {
t.Fatalf("%d snapshots left of 8", len(er.snaps))
}
if len(hub.closed) != 1 || len(hub.closed[0].RemovedForEndedApps) != 0 {
t.Fatalf("the result names ended removals: %+v", hub.closed)
}
})
}
}
// MUST-HOLD 3 and 5 — the due app's own snapshots go, by id, in the window; the snapshot it shares with a living
// app, the living app's snapshots and the shares stay; the result names them apart; the record becomes history.
func TestD72_TheEndedAppsOwnSnapshotsGoAndNothingElse(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
rec := pressed(t, sett, "vikunja", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 1 || hub.claims[0].App != "vikunja" || strings.Join(hub.claims[0].SnapshotIDs, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
t.Fatalf("the claim sent to the hub = %+v, want vikunja with v1,v2,v3 only (not the snapshot shared with immich)", hub.claims)
}
if hub.claims[0].RemovedAt != rec.RemovedAt || hub.claims[0].EndsAt != rec.EndsAt {
t.Fatalf("the claim does not carry the record's dates: %+v vs %+v", hub.claims[0], rec)
}
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
t.Fatalf("removed %v, want exactly vikunja's own three", got)
}
left := map[string]bool{}
for _, s := range er.snaps {
left[s.ShortID] = true
}
for _, keep := range []string{"i1", "i2", "vi", "s1", "k1"} {
if !left[keep] {
t.Errorf("%s was removed — a living app's copy, the shares, or another app's", keep)
}
}
if len(er.forgets) != 1 || !contains(er.forgets[0], "--prune") {
t.Fatalf("want ONE forget … --prune (the window's own call), got %v", er.forgets)
}
if len(hub.closed) != 1 {
t.Fatalf("the window was not closed once: %+v", hub.closed)
}
r := hub.closed[0]
if r.Outcome != "pruned" || r.CountBefore != 8 || r.CountAfter != 5 || strings.Join(r.RemovedForEndedApps, ",") != strings.Join(ids("v1", "v2", "v3"), ",") {
t.Fatalf("result = %+v, want pruned 8 -> 5 with removed_for_ended_apps = v1,v2,v3", r)
}
// vikunja still has the shared snapshot in the store, so its record stays pending (asked again next week).
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
t.Fatal("the record was closed although a snapshot with vikunja's tag is still in the store")
}
if len(sett.GetOffsiteEndsDone()) != 0 {
t.Fatalf("a history line was written while a copy is left: %+v", sett.GetOffsiteEndsDone())
}
}
// A snapshot with two apps' tags goes only when BOTH have ended — and then the records become history.
func TestD72_ASharedSnapshotGoesWhenBothHaveEnded(t *testing.T) {
m, sett, er, hub := endedBox(t) // nothing is installed
pressed(t, sett, "vikunja", 40)
pressed(t, sett, "immich", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(ids("i1", "i2", "v1", "v2", "v3", "vi"), ",") {
t.Fatalf("removed %v, want both apps' snapshots and the shared one", got)
}
if len(er.snaps) != 2 {
t.Fatalf("left %d, want the shares and kimai", len(er.snaps))
}
if n := len(sett.GetOffsiteEnds()); n != 0 {
t.Fatalf("%d record(s) still pending after every copy went", n)
}
h := sett.GetOffsiteEndsDone()
if len(h) != 2 {
t.Fatalf("history = %+v, want one line per app", h)
}
got := map[string]int{}
for _, d := range h {
got[d.App] = d.Snapshots
}
if got["vikunja"] != 4 || got["immich"] != 3 {
t.Fatalf("history counts = %v, want vikunja 4 (three + the shared), immich 3", got)
}
_ = hub
}
// MUST-HOLD 3 — `_shares` and the box's own stacks are never eligible, even with a record that somehow exists.
func TestD72_SharesAndInfraAreNeverEligible(t *testing.T) {
now := time.Now()
old := now.Add(-60 * 24 * time.Hour)
rec := func(app string) settings.OffsiteEnd {
return settings.OffsiteEnd{App: app, RemovedAt: now.Add(-40 * 24 * time.Hour).Format(time.RFC3339), EndsAt: now.Add(-10 * 24 * time.Hour).Format(time.RFC3339)}
}
living := func(app string) bool { return app == "traefik" } // the box's own stacks answer true (main.go: IsProtectedStack)
due := dueOffsiteEnds([]settings.OffsiteEnd{rec(SharesPseudoStack), rec(OffboxMarkerTag), rec("traefik"), rec("vikunja")}, now, living)
if len(due) != 1 || due["vikunja"].App != "vikunja" {
t.Fatalf("due = %v, want vikunja only", due)
}
claims, cand := endedCandidates([]guardSnap{tagged("s1", old, SharesPseudoStack), tagged("t1", old, "traefik"), tagged("v1", old, "vikunja"),
{ID: "u1-full", ShortID: "u1", Time: old, Tags: []string{OffboxMarkerTag}}, {ID: "n1-full", ShortID: "n1", Time: old}}, due)
if len(claims) != 1 || len(cand) != 1 || !cand["v1-full"] {
t.Fatalf("claims=%+v candidates=%v — the shares, an infra stack, a marker-only and an untagged snapshot are never candidates", claims, cand)
}
// and no such record can be written by the press
m, sett, _, _ := endedBox(t, "traefik")
for _, app := range []string{SharesPseudoStack, OffboxMarkerTag, "traefik"} {
if _, created, err := m.StartOffsiteEnd(app); err == nil || created {
t.Errorf("%q got an end record", app)
}
}
if n := len(sett.GetOffsiteEnds()); n != 0 {
t.Fatalf("%d record(s) written", n)
}
}
// MUST-HOLD 2, the box's half — THE BOX REMOVES ONLY WHAT THE HUB AGREED TO. An older hub (no answer), a refusal, and
// a box whose clock is a month ahead (it asks on day 2; the hub, by its own clock, refuses): nothing of the app goes
// and the record stays for the next window.
func TestD84_TheBoxRemovesOnlyWhatTheHubAgreedTo(t *testing.T) {
for _, c := range []struct {
name string
allow []string
refuse []OffsiteEndedRefusal
want []string
}{
{"a hub that does not know ended apps answers nothing", nil, nil, nil},
{"the hub refuses the app (the box's clock is a month ahead: absent 2 days by the hub's clock)", nil,
[]OffsiteEndedRefusal{{App: "vikunja", Reason: "the hub has seen the app absent since … — 2 full day(s), 30 needed"}}, nil},
{"the hub agrees to two of the three ids", ids("v1", "v3"), nil, ids("v1", "v3")},
{"the hub „agrees” to ids the box never named (a living app's, the shares)", ids("i1", "i2", "s1", "vi", "v2"), nil, ids("v2")},
} {
t.Run(c.name, func(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
hub.allowAll, hub.allow, hub.refuse = nil, c.allow, c.refuse
pressed(t, sett, "vikunja", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(c.want, ",") {
t.Fatalf("removed %v, want %v", got, c.want)
}
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
t.Fatal("the record is gone although copies of the app are left — the next window could not finish the job")
}
if got := hub.closed[0].RemovedForEndedApps; strings.Join(got, ",") != strings.Join(c.want, ",") {
t.Fatalf("removed_for_ended_apps = %v, want %v", got, c.want)
}
})
}
}
// MUST-HOLD 5 — BEHIND THE SAME GUARD, AND THE CAP'S OWN RULE IS NOT TOUCHED. (a) the guard refuses (a future-dated
// snapshot): the ended apps wait with everything else. (b) the policy's plan is above the cap: refused as always —
// the ended apps do not push it through and do not go themselves. (c) the plan is exactly the cap: it goes, and the
// agreed ended ids go BESIDE it — more than the cap in one window.
func TestD84_OneGuard_AndTheAllowanceIsBesideTheCap(t *testing.T) {
old := time.Now().Add(-60 * 24 * time.Hour)
policy := func(n int) []guardSnap { // n old snapshots of the living app the policy wants to remove
var out []guardSnap
for i := 0; i < n; i++ {
out = append(out, tagged("p"+string(rune('a'+i)), old.Add(time.Duration(i)*time.Minute), "immich"))
}
return out
}
t.Run("the guard refuses: nothing goes, the ended apps wait", func(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 31)
er.snaps = append(er.snaps, tagged("fut", time.Now().Add(72*time.Hour), "immich"))
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(er.forgets) != 0 || hub.closed[0].Outcome != "guard-refused" || len(hub.closed[0].RemovedForEndedApps) != 0 {
t.Fatalf("forgets=%v result=%+v — a refused guard must delete nothing", er.forgets, hub.closed[0])
}
if _, ok := sett.GetOffsiteEnd("vikunja"); !ok {
t.Fatal("the record was lost at a refused window")
}
})
t.Run("the policy's plan is above the cap: refused, with or without ended apps", func(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 31)
er.plan = policy(6) // cap 5
er.snaps = append(er.snaps, er.plan...)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(er.forgets) != 0 || hub.closed[0].Outcome != "guard-refused" || !strings.Contains(hub.closed[0].Reason, "more than one week") {
t.Fatalf("forgets=%v result=%+v — the cap's own refusal must stand", er.forgets, hub.closed[0])
}
})
t.Run("the plan is exactly the cap: it goes, and the ended ids go beside it", func(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 31)
er.plan = policy(5) // cap 5
er.snaps = append(er.snaps, er.plan...)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
got := er.removedIDs()
if len(got) != 8 {
t.Fatalf("removed %d (%v), want the policy's 5 and vikunja's 3", len(got), got)
}
r := hub.closed[0]
if r.Outcome != "pruned" || r.CountBefore-r.CountAfter != 8 || len(r.RemovedForEndedApps) != 3 {
t.Fatalf("result = %+v, want a fall of 8 with 3 named for ended apps (so 5 are under the cap)", r)
}
})
t.Run("an ended id the policy already removes stays under the cap and is not named twice", func(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 31)
er.plan = []guardSnap{er.snaps[0]} // v1 is in the policy's own plan
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if got := hub.closed[0].RemovedForEndedApps; strings.Join(got, ",") != strings.Join(ids("v2", "v3"), ",") {
t.Fatalf("removed_for_ended_apps = %v, want v2,v3 (v1 went under the cap)", got)
}
if got := er.removedIDs(); len(got) != 3 {
t.Fatalf("removed %v", got)
}
})
}
// WRONG CASE — an ended app that is INSTALLED AGAIN the day before the window. The install deletes the record
// (stacks.DeployStack; TestD72_AnInstallDeletesTheEndRecord). And if a record were still there: an installed app is
// never due — nothing is claimed, nothing goes.
func TestD72_AnAppInstalledAgainIsNeverDue(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich", "vikunja") // vikunja is installed again
pressed(t, sett, "vikunja", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
t.Fatalf("claims=%+v removed=%v — an installed app's history must not be touched", hub.claims, er.removedIDs())
}
}
// A box that cannot tell what it runs (the seam is not wired) has nothing due and can write no record.
func TestD72_WithoutTheLivingCheckNothingIsDue(t *testing.T) {
m, sett, er, hub := endedBox(t)
m.SetOffsiteEndLiving(nil)
pressed(t, sett, "vikunja", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
t.Fatalf("claims=%+v removed=%v", hub.claims, er.removedIDs())
}
if _, created, err := m.StartOffsiteEnd("kimai"); err == nil || created {
t.Fatal("a press wrote a record on a box that cannot tell what is installed")
}
}
// A record that is not due yet (29 days) claims nothing; a snapshot made under the app's tag AFTER the removal is
// not the removed app's copy and is never a candidate.
func TestD72_NotDueYet_AndASnapshotYoungerThanTheRemoval(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 29)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
t.Fatalf("29 days: claims=%+v removed=%v", hub.claims, er.removedIDs())
}
m2, sett2, er2, hub2 := endedBox(t, "immich")
pressed(t, sett2, "kimai", 31)
er2.snaps = append(er2.snaps, tagged("k2", time.Now().Add(-20*24*time.Hour), "kimai")) // 11 days AFTER the removal
m2.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if got := er2.removedIDs(); strings.Join(got, ",") != strings.Join(ids("k1"), ",") {
t.Fatalf("removed %v, want k1 only — k2 was made after the removal", got)
}
if _, ok := sett2.GetOffsiteEnd("kimai"); !ok {
t.Fatal("the record closed although a snapshot with the tag is left")
}
_ = hub2
}
// An app that has NO copy in the store: its due record is closed with no history line and nothing is asked of the hub.
func TestD72_ARecordWithNoCopiesIsClosedQuietly(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "neverbackedup", 31)
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
t.Fatalf("claims=%+v removed=%v", hub.claims, er.removedIDs())
}
if _, ok := sett.GetOffsiteEnd("neverbackedup"); ok || len(sett.GetOffsiteEndsDone()) != 0 {
t.Fatalf("want the record closed with no history line; pending=%v history=%+v", ok, sett.GetOffsiteEndsDone())
}
}
// WRONG CASE — THE OFF-SITE TIER IS SWITCHED OFF AT THE DATE. No run, no window, no call to the store; the record
// stays and does nothing. And the household's own NAS target (no hub window) never removes an ended app's copies.
func TestD72_TierOffAtTheDate_TheRecordWaits(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
rec := pressed(t, sett, "vikunja", 31)
if err := sett.UpdateOffboxStatus(func(o *settings.OffboxTarget) { o.Enabled = false }); err != nil {
t.Fatal(err)
}
_ = m.RunOffboxBackup(context.Background())
if hub.opened != 0 || len(er.forgets) != 0 {
t.Fatalf("with the tier OFF the box opened %d window(s) and ran %d forget(s)", hub.opened, len(er.forgets))
}
if got, ok := sett.GetOffsiteEnd("vikunja"); !ok || got != rec {
t.Fatalf("the record changed while the tier was off: %+v ok=%v", got, ok)
}
// The household's own NAS: the box prunes by the policy alone; no id of an ended app is ever named.
m2, sett2 := newOffboxManager(t) // Transport "" = the NAS
if _, _, err := sett2.StartOffsiteEnd("vikunja", time.Now().Add(-31*24*time.Hour)); err != nil {
t.Fatal(err)
}
er2 := &endedRunner{snaps: []guardSnap{tagged("v1", time.Now().Add(-60*24*time.Hour), "vikunja")}}
m2.SetOffboxRunner(er2.run)
m2.SetOffsiteEndLiving(func(string) bool { return false })
m2.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if got := er2.removedIDs(); len(got) != 0 {
t.Fatalf("on the household's own NAS the box removed %v by id", got)
}
if _, created, err := m2.StartOffsiteEnd("kimai"); err != ErrOffsiteEndNotOffered || created {
t.Fatalf("a press on a box without the hub's tier: created=%v err=%v, want ErrOffsiteEndNotOffered and no record", created, err)
}
}
// MUST-HOLD 7 and the wrong case TWO PRESSES — one record, ONE mail; the date does not move. A press for an
// installed app writes nothing and mails nobody.
func TestD84_OnePressOneMail(t *testing.T) {
m, sett, _, _ := endedBox(t, "immich")
var mails []settings.OffsiteEnd
m.SetOffsiteEndNotify(func(rec settings.OffsiteEnd) { mails = append(mails, rec) })
first, created, err := m.StartOffsiteEnd("vikunja")
if err != nil || !created {
t.Fatalf("first press: created=%v err=%v", created, err)
}
if first.Ends().Sub(first.Removed()) != settings.OffsiteEndAfter {
t.Fatalf("the record's end is not 30 days after the press: %+v", first)
}
second, created2, err := m.StartOffsiteEnd("vikunja")
if err != nil || created2 || second != first {
t.Fatalf("second press: created=%v rec=%+v err=%v — want the first record, unchanged", created2, second, err)
}
if len(mails) != 1 || mails[0] != first {
t.Fatalf("two presses gave %d mail(s): %+v — want ONE, for the record", len(mails), mails)
}
if _, c, err := m.StartOffsiteEnd("immich"); err != ErrOffsiteEndLiving || c {
t.Fatalf("a press for an INSTALLED app: created=%v err=%v", c, err)
}
if len(mails) != 1 || len(sett.GetOffsiteEnds()) != 1 {
t.Fatalf("mails=%d records=%d after a refused press", len(mails), len(sett.GetOffsiteEnds()))
}
// MUST-HOLD 4 — the household takes it back: the record is gone, and the next window claims nothing.
if kept, err := m.KeepOffsiteCopies("vikunja"); err != nil || !kept {
t.Fatalf("keep: %v %v", kept, err)
}
if n := len(sett.GetOffsiteEnds()); n != 0 {
t.Fatalf("%d record(s) after the take-back", n)
}
if len(mails) != 1 {
t.Fatalf("the take-back sent a mail: %d", len(mails))
}
}
// The take-back before the window: with the record deleted, the window removes nothing of the app.
func TestD72_AfterTheTakeBackTheWindowRemovesNothing(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "vikunja", 31)
if kept, err := m.KeepOffsiteCopies("vikunja"); err != nil || !kept {
t.Fatalf("keep: %v %v", kept, err)
}
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(hub.claims) != 0 || len(er.removedIDs()) != 0 {
t.Fatalf("claims=%+v removed=%v after „keep it after all”", hub.claims, er.removedIDs())
}
}
// The hub report carries the pending records (so the hub can start its own clock for an app it never saw).
func TestD72_TheReportCarriesThePendingRecords(t *testing.T) {
m, sett, _, _ := endedBox(t, "immich")
if st := m.OffboxReportStatus(); st == nil || len(st.EndedApps) != 0 {
t.Fatalf("no record: %+v", st)
}
rec := pressed(t, sett, "vikunja", 3)
st := m.OffboxReportStatus()
if st == nil || len(st.EndedApps) != 1 || st.EndedApps[0].App != "vikunja" || st.EndedApps[0].RemovedAt != rec.RemovedAt || st.EndedApps[0].EndsAt != rec.EndsAt {
t.Fatalf("report = %+v, want the pending record", st)
}
}
// The living check and the mail are WIRED in main.go — without the first nothing is ever due, without the second no
// household is told. (A seam nobody wires is a feature that does not exist.)
func TestD72_MainWiresTheLivingCheck(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("..", "..", "cmd", "controller", "main.go"))
if err != nil {
t.Fatal(err)
}
src := string(raw)
for _, want := range []string{"backupMgr.SetOffsiteEndLiving(func(app string) bool {", "cfg.IsProtectedStack(app)", "st.Deployed || st.Deploying || st.Protected",
"backupMgr.SetOffsiteEndNotify(func(rec settings.OffsiteEnd) {", "notifier.NotifyOffsiteEndScheduled(rec.App, rec.RemovedAt, rec.EndsAt"} {
if !strings.Contains(src, want) {
t.Errorf("main.go lacks %q", want)
}
}
}
// The result names for the hub's audit only ids that are REALLY gone: a forget that removed nothing (the provider
// refused, the call failed) names none, and the record stays for the next window.
func TestD84_TheResultNamesOnlyIdsThatAreReallyGone(t *testing.T) {
m, sett, er, hub := endedBox(t, "immich")
pressed(t, sett, "kimai", 31)
base := er.run
m.SetOffboxRunner(func(ctx context.Context, env []string, args ...string) ([]byte, error) {
if contains(args, "forget") && !contains(args, "--dry-run") {
er.forgets = append(er.forgets, append([]string{}, args...))
return []byte("Fatal: unable to remove"), context.DeadlineExceeded // nothing is removed
}
return base(ctx, env, args...)
})
m.offsiteWindowRetention(context.Background(), nil, nil, "after-run")
if len(er.forgets) != 1 || len(hub.closed) != 1 {
t.Fatalf("forgets=%d closed=%d", len(er.forgets), len(hub.closed))
}
r := hub.closed[0]
if r.Outcome != "error" || len(r.RemovedForEndedApps) != 0 || r.CountAfter != r.CountBefore {
t.Fatalf("result = %+v — a failed forget removed nothing, so nothing may be named as removed for ended apps", r)
}
if _, ok := sett.GetOffsiteEnd("kimai"); !ok || len(sett.GetOffsiteEndsDone()) != 0 {
t.Fatal("the record was closed although its copy is still in the store")
}
}