package api
import (
"net/http"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
func trustReport(stanza string) string {
if stanza == "-" {
return validReportBody("h1")
}
b := validReportBody("h1")
return b[:strings.LastIndex(b, "}")] + `,"proxmox_trust":` + stanza + "}"
}
func trustSetup(t *testing.T) (*Handler, *store.Store) {
t.Helper()
h, st, _ := newTestHandler(t)
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "p"})
st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: "HKEY"})
return h, st
}
func trustCounts(t *testing.T, st *store.Store) (problems, oks []store.Event) {
t.Helper()
since := time.Now().Add(-time.Hour)
problems, _ = st.GetEventsByType("c1", eventProxmoxTrustProblem, since)
oks, _ = st.GetEventsByType("c1", eventProxmoxTrustOK, since)
return
}
// R-1396: a cause is told ONCE (the box repeats it at every report), a second cause is told too, and one recovery
// event follows when the box reports no problem. A healthy stanza and an agent without the stanza raise nothing.
// COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub.txt).
func TestProxmoxTrust_CauseOnceThenRecovery(t *testing.T) {
h, st := trustSetup(t)
post := func(stanza string) {
t.Helper()
if rr := do(h, http.MethodPost, "/host-report", "HKEY", trustReport(stanza)); rr.Code != 200 {
t.Fatalf("report: %d %s", rr.Code, rr.Body.String())
}
}
post(`{"mode":"ca"}`)
post("-")
if p, o := trustCounts(t, st); len(p) != 0 || len(o) != 0 {
t.Fatalf("a healthy box raised %d problem(s), %d ok", len(p), len(o))
}
bad := `{"mode":"fingerprint","problem":"ca_copy_does_not_verify","detail":"","ca_days_left":3560,"ca_not_after":"2036-08-17T15:44:42Z"}`
post(bad)
post(bad)
post(bad)
p, o := trustCounts(t, st)
if len(p) != 1 || len(o) != 0 {
t.Fatalf("problems=%d ok=%d, want ONE problem for three reports of the same cause", len(p), len(o))
}
if p[0].Severity != "error" || !strings.Contains(p[0].Message, "keeps its pinned certificate") || !strings.Contains(p[0].Message, "h1") {
t.Fatalf("event = %+v", p[0])
}
if strings.Contains(p[0].Message, "