package httpapi import ( "testing" "evobgp/internal/birdfmt" "evobgp/internal/nodedispatch" "evobgp/internal/store" ) func TestCountBGPSessions(t *testing.T) { total, est := countBGPSessions([]birdfmt.BGPSession{ {Name: "p1", State: "Established"}, {Name: "p2", State: "Idle"}, {Name: "p3", State: "established"}, }) if total != 3 || est != 2 { t.Fatalf("total=%d established=%d", total, est) } } func TestSpeakerLiveStatusJSON_masterUsesBirdPoll(t *testing.T) { sp := &store.Speaker{ID: "m1", Role: "master", Endpoint: "https://cp.example"} view := speakerBGPLive{ SpeakerID: "m1", Label: "CP ยท cp.example", Sessions: []birdfmt.BGPSession{ {Name: "evobgp_peer_x", State: "Established"}, }, } m := speakerLiveStatusJSON(sp, view, nil) if m["agent_ok"] != true || m["bgp_established"] != 1 || m["bgp_sessions_total"] != 1 { t.Fatalf("got %#v", m) } } func TestSpeakerLiveStatusJSON_replicaWithHealth(t *testing.T) { sp := &store.Speaker{ ID: "r1", Role: "replica", Endpoint: "https://node.example", MetaJSON: `{"agent_domain":"node.example","agent_secret":"s"}`, } view := speakerBGPLive{ SpeakerID: "r1", Label: "node.example", Sessions: []birdfmt.BGPSession{{Name: "p", State: "Idle"}}, } health := &nodedispatch.AgentHealthResult{ OK: true, LastSyncAt: "2026-05-21T12:00:00Z", LastAppliedRevisionID: "rev-1", } m := speakerLiveStatusJSON(sp, view, health) if m["agent_ok"] != true || m["agent_last_sync_at"] != "2026-05-21T12:00:00Z" { t.Fatalf("got %#v", m) } if m["bgp_established"] != 0 || m["bgp_poll_ok"] != true { t.Fatalf("bgp fields: %#v", m) } } func TestSpeakerLiveStatusJSON_pollError(t *testing.T) { sp := &store.Speaker{ID: "r1", Role: "replica", MetaJSON: `{"agent_domain":"x.example"}`} view := speakerBGPLive{SpeakerID: "r1", Label: "x.example", Error: "HTTP 503"} health := &nodedispatch.AgentHealthResult{OK: false, Error: "timeout"} m := speakerLiveStatusJSON(sp, view, health) if m["bgp_poll_ok"] != false || m["bgp_poll_error"] != "HTTP 503" { t.Fatalf("got %#v", m) } if m["agent_ok"] != false { t.Fatalf("agent_ok: %#v", m) } }