package backup import ( "context" "strings" "testing" "gitea.dooplex.hu/admin/felhom-controller/internal/settings" ) // Review of 2026-11-15, finding 2: the due records and the candidates were read ONCE, before the hub was asked, and // never again before the forget. A take-back („Mégis megtartom”) or an install in that interval — the hub's answer, // the dry run, the guard: seconds to minutes — still lost the copies, while the page had just said „the off-site // copies are kept”. The window now reads the records again IMMEDIATELY before the forget. // RED-PROOF: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-review-2.txt. // racingHub runs `during` while the hub answers — after the box has sent its claims, before it removes anything. type racingHub struct { *agreeingHub during func() } func (h *racingHub) Open(ctx context.Context, n int, ended []OffsiteEndedClaim) (OffsiteWindow, error) { w, err := h.agreeingHub.Open(ctx, n, ended) h.during() return w, err } func TestD72_ATakeBackDuringTheWindowKeepsTheCopies(t *testing.T) { m, sett, er, hub := endedBox(t, "immich") pressed(t, sett, "kimai", 31) pressed(t, sett, "vikunja", 31) m.SetOffsiteWindowClient(&racingHub{agreeingHub: hub, during: func() { if kept, err := m.KeepOffsiteCopies("kimai"); err != nil || !kept { t.Fatalf("keep: %v %v", kept, err) } }}) m.offsiteWindowRetention(context.Background(), nil, nil, "after-run") if len(hub.claims) != 2 { t.Fatalf("fixture: both apps must have been claimed before the take-back, got %+v", hub.claims) } if _, ok := sett.GetOffsiteEnd("kimai"); ok { t.Fatal("fixture: the take-back did not delete the record") } got := er.removedIDs() for _, id := range got { if id == "k1-full" { t.Fatalf("the page said „the off-site copies are kept”, no record exists, and the window removed kimai's copy (%v)", got) } } // the OTHER app's end is not disturbed by the take-back if strings.Join(got, ",") != strings.Join(ids("v1", "v2", "v3"), ",") { t.Fatalf("removed %v, want vikunja's three and nothing of kimai", got) } if r := hub.closed[0].RemovedForEndedApps; strings.Join(r, ",") != strings.Join(ids("v1", "v2", "v3"), ",") { t.Fatalf("removed_for_ended_apps = %v", r) } } // The same interval, an INSTALL: the record is deleted by the install (stacks) and the app is living again. func TestD72_AnInstallDuringTheWindowKeepsTheCopies(t *testing.T) { for _, how := range []string{"the install deleted the record", "the app is living again (the record is still there)", "removed again: a NEW record with a new time"} { t.Run(how, func(t *testing.T) { m, sett, er, hub := endedBox(t, "immich") pressed(t, sett, "kimai", 31) live := map[string]bool{"immich": true} m.SetOffsiteEndLiving(func(app string) bool { return live[app] }) m.SetOffsiteWindowClient(&racingHub{agreeingHub: hub, during: func() { switch how { case "the install deleted the record": if _, err := sett.CancelOffsiteEnd("kimai", "the app is installed again"); err != nil { t.Fatal(err) } live["kimai"] = true case "the app is living again (the record is still there)": live["kimai"] = true default: // installed, removed again with „delete backup data”: the 30 days start again if _, err := sett.CancelOffsiteEnd("kimai", "the app is installed again"); err != nil { t.Fatal(err) } if _, _, err := sett.StartOffsiteEnd("kimai", m.now()); err != nil { t.Fatal(err) } } }}) m.offsiteWindowRetention(context.Background(), nil, nil, "after-run") if len(hub.claims) != 1 { t.Fatalf("fixture: kimai must have been claimed first: %+v", hub.claims) } if got := er.removedIDs(); len(got) != 0 { t.Fatalf("the window removed %v of an app that was installed again while the hub answered", got) } if len(sett.GetOffsiteEndsDone()) != 0 { t.Fatalf("a history line for an end that did not happen: %+v", sett.GetOffsiteEndsDone()) } }) } } // A take-back that comes while the forget itself runs is TOO LATE, and the page must not say „kept”: the answer is // „nothing to keep”, the record stays for the window to close, and the copies go as the window had decided. func TestD72_ATakeBackDuringTheForgetIsNotAnsweredKept(t *testing.T) { m, sett, er, hub := endedBox(t, "immich") pressed(t, sett, "kimai", 31) base := er.run var keptAnswer, asked bool m.SetOffboxRunner(func(ctx context.Context, env []string, args ...string) ([]byte, error) { if contains(args, "forget") && !contains(args, "--dry-run") { k, err := m.KeepOffsiteCopies("kimai") if err != nil { t.Fatal(err) } keptAnswer, asked = k, true } return base(ctx, env, args...) }) m.offsiteWindowRetention(context.Background(), nil, nil, "after-run") if !asked || keptAnswer { t.Fatalf("asked=%v kept=%v — a take-back during the forget was answered „kept” while the copies were being removed", asked, keptAnswer) } if got := er.removedIDs(); strings.Join(got, ",") != strings.Join(ids("k1"), ",") { t.Fatalf("removed %v", got) } if _, ok := sett.GetOffsiteEnd("kimai"); ok || len(sett.GetOffsiteEndsDone()) != 1 { t.Fatalf("the end must be closed as carried out: pending=%v history=%+v", ok, sett.GetOffsiteEndsDone()) } // after the window the mark is gone: a later record can be taken back again pressed(t, sett, "vikunja", 1) if k, err := m.KeepOffsiteCopies("vikunja"); err != nil || !k { t.Fatalf("after the window a take-back must work again: %v %v", k, err) } _ = hub } var _ = settings.OffsiteEndAfter