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) } }