4
0

data_reasons_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // data_reasons_test.go — #1703: EnrichOverview inyecta el motivo de caída de
  2. // las unidades empujadas por integraciones solo cuando están offline o
  3. // unreachable; las online no llevan motivo.
  4. package httpapi_test
  5. import (
  6. "testing"
  7. "github.com/gnacho/netpulse/server-go/internal/adapters"
  8. "github.com/gnacho/netpulse/server-go/internal/db"
  9. "github.com/gnacho/netpulse/server-go/internal/httpapi"
  10. )
  11. func TestEnrichOverviewAppliesPusherReasons(t *testing.T) {
  12. // EnrichOverview lee kv: necesita una base real (panic con nil).
  13. d, err := db.Open(t.TempDir())
  14. if err != nil {
  15. t.Fatalf("db.Open: %v", err)
  16. }
  17. t.Cleanup(func() { _ = d.Close() })
  18. handle := d.DB
  19. ov := &adapters.Overview{
  20. Routers: []adapters.Router{
  21. {ID: "hex", Type: "external", Status: "offline"},
  22. {ID: "hex-ok", Type: "external", Status: "online"},
  23. {ID: "gw", Type: "glinet", Status: "unreachable"},
  24. },
  25. }
  26. reasons := map[string]string{
  27. "hex": "pusher_auth_failed",
  28. "gw": "pusher_stopped",
  29. "hex-ok": "pusher_auth_failed", // unidad online: no debe aplicarse
  30. }
  31. httpapi.EnrichOverview(handle, nil, ov, reasons)
  32. if got := ov.Routers[0].StatusReason; got != "pusher_auth_failed" {
  33. t.Errorf("hex: StatusReason = %q, want pusher_auth_failed", got)
  34. }
  35. if got := ov.Routers[1].StatusReason; got != "" {
  36. t.Errorf("hex-ok (online): StatusReason = %q, want empty", got)
  37. }
  38. if got := ov.Routers[2].StatusReason; got != "pusher_stopped" {
  39. t.Errorf("gw (unreachable): StatusReason = %q, want pusher_stopped", got)
  40. }
  41. // Sin motivos (demo / sin integraciones): nada cambia.
  42. ov2 := &adapters.Overview{Routers: []adapters.Router{{ID: "hex", Type: "external", Status: "offline"}}}
  43. httpapi.EnrichOverview(handle, nil, ov2, nil)
  44. if ov2.Routers[0].StatusReason != "" {
  45. t.Errorf("nil reasons: StatusReason = %q, want empty", ov2.Routers[0].StatusReason)
  46. }
  47. }