package hub import ( "context" "testing" ) type fakeMemBackups []Backup func (f fakeMemBackups) Backups(context.Context) []Backup { return f } type fakeKnownBackups []Backup func (f fakeKnownBackups) KnownBackupSuccesses() []Backup { return f } // The consequence, not the mechanism: after a restart (empty in-memory list) the host report still carries // the night's backup, so the hub's freshness check sees it. Measured 2026-10-09 on demo-felhom: without this // the report carried backups: [] and the hub alarmed „newest backup is 48h old" an hour after a good backup. // RED-PROOF: return before the saved-success loop in collectBackups → the first case reports nothing → fails. func TestCollectBackups_SavedSuccessSurvivesARestart(t *testing.T) { saved := Backup{TargetID: "felhom-backup", VMID: 9201, Success: true, StartedAt: "2026-10-09T02:40:16Z"} t.Run("restart: memory empty, the saved success is reported", func(t *testing.T) { c := &Collector{backups: fakeMemBackups(nil), knownBackups: fakeKnownBackups{saved}} got := c.collectBackups(context.Background()) if len(got) != 1 || got[0].StartedAt != saved.StartedAt || !got[0].Success || got[0].TargetID != "felhom-backup" { t.Fatalf("want the saved success, got %+v", got) } }) t.Run("memory holds the same or a newer success: no second entry", func(t *testing.T) { mem := Backup{TargetID: "felhom-backup", VMID: 9201, Success: true, StartedAt: "2026-10-09T02:40:16Z", Archive: "a"} c := &Collector{backups: fakeMemBackups{mem}, knownBackups: fakeKnownBackups{saved}} if got := c.collectBackups(context.Background()); len(got) != 1 || got[0].Archive != "a" { t.Fatalf("want only the in-memory record, got %+v", got) } }) t.Run("a newer FAILURE in memory never hides the saved success, and is kept", func(t *testing.T) { fail := Backup{TargetID: "felhom-backup", VMID: 9201, Success: false, StartedAt: "2026-10-10T02:40:00Z", Error: "x"} c := &Collector{backups: fakeMemBackups{fail}, knownBackups: fakeKnownBackups{saved}} got := c.collectBackups(context.Background()) if len(got) != 2 || got[0].Success || !got[1].Success { t.Fatalf("want the failure and the saved success, got %+v", got) } }) t.Run("another tier's success does not cover this tier", func(t *testing.T) { other := Backup{TargetID: "felhom-pbs", VMID: 9201, Success: true, StartedAt: "2026-10-10T00:00:00Z"} c := &Collector{backups: fakeMemBackups{other}, knownBackups: fakeKnownBackups{saved}} if got := c.collectBackups(context.Background()); len(got) != 2 { t.Fatalf("want both tiers, got %+v", got) } }) t.Run("no saved state wired: the in-memory list as before, never nil", func(t *testing.T) { c := &Collector{} if got := c.collectBackups(context.Background()); got == nil || len(got) != 0 { t.Fatalf("want an empty non-nil list, got %#v", got) } }) }