Files
felhom-controller/controller/internal/api/d72_offsite_end_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

137 lines
4.6 KiB
Go

package api
import (
"encoding/json"
"go/ast"
"go/parser"
"go/token"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/backup"
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
"gitea.dooplex.hu/admin/felhom-controller/internal/i18n"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
)
// D72 / D84 (`09` §3 decisions 292, 309; `07-backup-architecture.md` §8.2f).
// RED-PROOFS: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-ctrl-press.txt.
func endsRouter(t *testing.T, transport string) (*Router, *settings.Settings) {
t.Helper()
r, _ := newR442Router(t, "deployed: true\nenv: {}\n")
lg := log.New(io.Discard, "", 0)
sett, err := settings.Load(filepath.Join(t.TempDir(), "settings.json"), lg)
if err != nil {
t.Fatal(err)
}
if err := sett.SetOffboxTarget(&settings.OffboxTarget{Enabled: true, Host: "h", User: "u", RepoPath: "/r", Transport: transport}); err != nil {
t.Fatal(err)
}
cfg := &config.Config{}
cfg.Paths.DataDir = t.TempDir()
r.backupMgr = backup.NewManager(cfg, sett, lg)
r.sett = sett
return r, sett
}
// THE DATE IS SHOWN BEFORE THE PRESS (D72): the dialog's data carries the day the off-site copies would be deleted —
// today + 30 days, in the household's date form — only on a box where a press starts the clock.
func TestD72_BackupDataCarriesTheEndDateBeforeThePress(t *testing.T) {
get := func(r *Router, lang string) map[string]any {
w := httptest.NewRecorder()
r.getStackBackupData(w, httptest.NewRequest(http.MethodGet, "/api/stacks/app/backup-data?lang="+lang, nil), "app")
var resp struct {
OK bool `json:"ok"`
Data map[string]any `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil || !resp.OK {
t.Fatalf("backup-data: %s (%v)", w.Body.String(), err)
}
return resp.Data
}
r, sett := endsRouter(t, settings.TransportRclonePinned)
want := time.Now().Add(settings.OffsiteEndAfter).Local()
for _, lang := range []string{"hu", "en"} {
got, _ := get(r, lang)["offsite_end_date"].(string)
if got != i18n.FormatDate(lang, want) && got != i18n.FormatDate(lang, want.Add(-time.Minute)) {
t.Errorf("[%s] offsite_end_date = %q, want %q (today + 30 days)", lang, got, i18n.FormatDate(lang, want))
}
}
// reading the dialog's data writes NOTHING: the record is the press's, not the look's
if n := len(sett.GetOffsiteEnds()); n != 0 {
t.Fatalf("opening the dialog wrote %d end record(s)", n)
}
// the household's own NAS (no hub window): the dialog says nothing about off-site copies
r2, _ := endsRouter(t, "")
if v, has := get(r2, "hu")["offsite_end_date"]; has {
t.Fatalf("a box without the hub's tier names an end date: %v", v)
}
}
// MUST-HOLD 1, at the handler: the end record is written ONLY when the household pressed „delete backup data”.
// removeStack's success path needs Docker, so this reads the handler's source (as R-486/R-491 do): every call of
// StartOffsiteEnd in removeStack sits inside an `if` whose condition names body.RemoveBackups — and there is one.
// What the call then does is pinned on the real code in backup (TestD84_OnePressOneMail and the window tests).
func TestD72_RemovalWritesTheEndRecordOnlyWithDeleteBackupData(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "router.go", nil, 0)
if err != nil {
t.Fatal(err)
}
calls, guarded := 0, 0
for _, d := range f.Decls {
fn, ok := d.(*ast.FuncDecl)
if !ok || fn.Name.Name != "removeStack" || fn.Body == nil {
continue
}
isCall := func(n ast.Node) bool {
s, ok := n.(*ast.SelectorExpr)
return ok && s.Sel.Name == "StartOffsiteEnd"
}
ast.Inspect(fn.Body, func(n ast.Node) bool {
if isCall(n) {
calls++
}
ifs, ok := n.(*ast.IfStmt)
if !ok {
return true
}
names := false
ast.Inspect(ifs.Cond, func(c ast.Node) bool {
if s, ok := c.(*ast.SelectorExpr); ok && s.Sel.Name == "RemoveBackups" {
names = true
}
return true
})
if names {
ast.Inspect(ifs.Body, func(b ast.Node) bool {
if isCall(b) {
guarded++
}
return true
})
}
return true
})
}
if calls != 1 || guarded != 1 {
t.Fatalf("removeStack: %d call(s) of StartOffsiteEnd, %d inside `if … body.RemoveBackups …` — want exactly one, guarded: "+
"a removal that KEEPS the backups must write no end record", calls, guarded)
}
raw, err := os.ReadFile("router.go")
if err != nil {
t.Fatal(err)
}
if n := strings.Count(string(raw), "StartOffsiteEnd("); n != 1 {
t.Fatalf("router.go calls StartOffsiteEnd %d time(s) — the removal is its only caller here", n)
}
}