package api import ( "net/http" "net/http/httptest" "strings" "testing" "time" "gitea.dooplex.hu/admin/felhom-hub/internal/notify" "gitea.dooplex.hu/admin/felhom-hub/internal/store" ) // D72 / D84 (`07-backup-architecture.md` ยง8.2f): the wire. RED-PROOFS: // documentation/audits/build-release-4-2026-11-15/A/redproof-hub-api.txt. // A controller report feeds the hub's OWN app record: the app list, and the apps named in pending end records. The // box's dates in `ended_apps` never become the hub's date. func TestD72_ReportFeedsTheHubsOwnAppRecord(t *testing.T) { h, st, _ := newTestHandler(t) st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "pp"}) post := func(body string) { t.Helper() req := httptest.NewRequest(http.MethodPost, "/api/v1/report", strings.NewReader(body)) req.Header.Set("Authorization", "Bearer ckey") rr := httptest.NewRecorder() h.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("report โ†’ %d (%s)", rr.Code, rr.Body.String()) } } post(`{"customer_id":"c1","stacks":{"deployed":["vikunja","immich"],"available":[]}}`) before := time.Now().Add(-time.Minute) // vikunja is gone; the box also holds an end record for an app the hub never saw, dated two years ago. post(`{"customer_id":"c1","stacks":{"deployed":["immich"],"available":[]},"offsite":{"enabled":true,"ended_apps":[{"app":"oldapp","removed_at":"2024-01-01T00:00:00Z","ends_at":"2024-01-31T00:00:00Z"}]}}`) m, err := st.AppPresences("c1") if err != nil { t.Fatal(err) } if !m["immich"].Present { t.Fatalf("immich is listed: %+v", m) } for _, app := range []string{"vikunja", "oldapp"} { p, ok := m[app] if !ok || p.Present || p.AbsentSince.Before(before) || p.AbsentSince.After(time.Now().Add(time.Minute)) { t.Fatalf("%s must be absent since NOW by the hub's clock (the box's 2024 date is not read): %+v ok=%v", app, p, ok) } } // A report with no `stacks` object is not an empty app list: nothing becomes absent. post(`{"customer_id":"c1"}`) if m2, _ := st.AppPresences("c1"); !m2["immich"].Present { t.Fatalf("a report without an app list made immich absent: %+v", m2) } } // The window-open request carries the box's due end records to the service, whole. func TestD84_WindowOpenCarriesTheEndedClaims(t *testing.T) { h, st, _ := newTestHandler(t) st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "pp"}) f := &fakeKeySvc{} h.SetOffsiteKeyService(f) id := strings.Repeat("ab", 32) req := httptest.NewRequest(http.MethodPost, "/api/v1/offsite/window-open/c1", strings.NewReader(`{"count_before":20,"ended":[{"app":"vikunja","removed_at":"2026-10-01T00:00:00Z","ends_at":"2026-10-31T00:00:00Z","snapshot_ids":["`+id+`"]}]}`)) req.Header.Set("Authorization", "Bearer ckey") rr := httptest.NewRecorder() h.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("window-open โ†’ %d (%s)", rr.Code, rr.Body.String()) } if len(f.gotEnded) != 1 || f.gotEnded[0].App != "vikunja" || len(f.gotEnded[0].SnapshotIDs) != 1 || f.gotEnded[0].SnapshotIDs[0] != id { t.Fatalf("the service did not receive the claim: %+v", f.gotEnded) } } // The household's receipt is a type a box may send; the two hub-made records are not โ€” no box can mint one. func TestD84_EventTypes(t *testing.T) { if !allowedEventTypes["offsite_end_scheduled"] { t.Fatal("offsite_end_scheduled must be accepted from a box (POST /event would answer 400)") } if notify.IsOperatorOnly("offsite_end_scheduled") { t.Fatal("the household's receipt must not be operator-only") } for _, et := range []string{"offsite_ended_app_removed", "offsite_ended_app_refused"} { if allowedEventTypes[et] { t.Fatalf("%s is hub-made: a box must not be able to POST it", et) } if !notify.IsOperatorOnly(et) { t.Fatalf("%s must be operator-only", et) } } }