package api import ( "encoding/json" "net/http" "os" "testing" "time" "gitea.dooplex.hu/admin/felhom-hub/internal/store" ) // R-1216 / D28 = A: the shared case table (byte-identical in felhom-controller, felhom.eu scripts/host_swap_rule_gate.py). // RED-PROOF: with the morning's rule (`guestMB > 0 && hostMB < guestMB`) the demo-hp, demo-felhom and 32 GB rows fail // (they read short) and the „no swap, no guest" row fails (not short) — seen failing 2026-10-22. func TestR1216_HostSwapRuleCases(t *testing.T) { b, err := os.ReadFile("testdata/host_swap_rule_cases.json") if err != nil { t.Fatal(err) } var f struct { Cases []struct { Name string `json:"name"` Host int64 `json:"host_swap_mb"` Guest int64 `json:"guest_swap_mb"` Short bool `json:"short"` } `json:"cases"` } if err := json.Unmarshal(b, &f); err != nil || len(f.Cases) < 10 { t.Fatalf("case table: %v (%d cases)", err, len(f.Cases)) } for _, c := range f.Cases { if got := hostSwapShort(c.Host, c.Guest); got != c.Short { t.Errorf("%s: host %d / guests %d → short=%t, want %t", c.Name, c.Host, c.Guest, got, c.Short) } } } // The consequence: a host that this morning's rule flagged (demo-felhom: 8191 vs 12288) gets ONE host_swap_ok at its // next report under the new rule, and no new alarm after. func TestR1216_TheMorningsAlarmClearsOnceAtTheNextReport(t *testing.T) { h, st, _ := newTestHandler(t) st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "p"}) st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: "HKEY"}) if _, err := st.SaveEvent("c1", eventHostSwapShort, "warning", "morning", `{"host_id":"h1","host_swap_mb":8191,"guest_swap_mb":12288}`, "hub"); err != nil { t.Fatal(err) } for i := 0; i < 3; i++ { if rr := do(h, http.MethodPost, "/host-report", "HKEY", swapReportBody("h1", 8191, 12288)); rr.Code != http.StatusOK { t.Fatalf("host-report = %d", rr.Code) } } ok, _ := st.GetEventsByType("c1", eventHostSwapOK, time.Time{}) short, _ := st.GetEventsByType("c1", eventHostSwapShort, time.Time{}) if len(ok) != 1 || len(short) != 1 { t.Fatalf("host_swap_ok = %d (want 1), host_swap_short = %d (want the morning's 1 only)", len(ok), len(short)) } }