alerts_i18n_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // alerts_i18n_test.go — tipado de alertas para traducción client-side (#671).
  2. package adapters
  3. import (
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/alerts"
  6. )
  7. // TestUnknownDeviceAlertCarriesType: la alerta de dispositivo desconocido
  8. // lleva el slug del tipo y las vars (mac, router) para que el frontend
  9. // traduzca título/descripción/hint en el idioma del usuario.
  10. func TestUnknownDeviceAlertCarriesType(t *testing.T) {
  11. l := NewLive(nil, nil, []RouterConfig{
  12. {ID: "gateway", Name: "Flint 2", IsGateway: true},
  13. }, nil)
  14. l.emitUnknownDevice(Device{
  15. ID: "aa-bb-cc-dd-ee-ff", MAC: "AA:BB:CC:DD:EE:FF", Name: "AA:BB:CC:DD:EE:FF",
  16. RouterID: "gateway", Online: true,
  17. }, nil, nil)
  18. list := l.engine.List()
  19. if len(list) != 1 {
  20. t.Fatalf("esperado 1 alerta, got %d", len(list))
  21. }
  22. ev := list[0]
  23. if ev.Type != alerts.HintUnknownDevice {
  24. t.Fatalf("Type=%q (want %q)", ev.Type, alerts.HintUnknownDevice)
  25. }
  26. if ev.Vars["mac"] != "AA:BB:CC:DD:EE:FF" {
  27. t.Fatalf("Vars mac=%q", ev.Vars["mac"])
  28. }
  29. if ev.Vars["router"] != "Flint 2" {
  30. t.Fatalf("Vars router=%q (want nombre visible, no el id)", ev.Vars["router"])
  31. }
  32. // Los literales siguen viajando como fallback.
  33. if ev.Title == "" || ev.Description == "" || ev.Hint == "" {
  34. t.Fatalf("los literales de fallback no deben vaciarse: %+v", ev)
  35. }
  36. }
  37. // TestUnknownDeviceAlertCarriesLocation: the alert has to say where the
  38. // device is, not just its MAC — the IP, the box it hangs off and the port on
  39. // that box, so it can be found without hunting through the client list.
  40. func TestUnknownDeviceAlertCarriesLocation(t *testing.T) {
  41. l := NewLive(nil, nil, []RouterConfig{
  42. {ID: "gateway", Name: "Gateway", IsGateway: true},
  43. }, nil)
  44. d := Device{
  45. ID: "02-00-00-00-00-31", MAC: "02:00:00:00:00:31", Name: "02:00:00:00:00:31",
  46. IP: "192.0.2.31", RouterID: "gateway", Online: true, Band: "cable",
  47. AttachTo: "sw-desk", Port: "7", PortLabel: "Desk 7",
  48. }
  49. // The switch hangs off router port 3; the client is learnt on the switch's
  50. // own port 7, so that one is the client's.
  51. dists := []DistributionNode{{ID: "sw-desk", Kind: "managed", Name: "Desk switch", RouterID: "gateway", Port: "3"}}
  52. l.emitUnknownDevice(d, []Device{d}, dists)
  53. ev := l.engine.List()[0]
  54. for k, want := range map[string]string{
  55. "ip": "192.0.2.31",
  56. "where": "Desk switch",
  57. "port": "Desk 7", // the friendly label wins over the raw port number
  58. "band": "cable",
  59. } {
  60. if ev.Vars[k] != want {
  61. t.Fatalf("Vars[%q]=%q (want %q)", k, ev.Vars[k], want)
  62. }
  63. }
  64. }
  65. // TestUnknownDeviceLocationFallsBackToRouter: a wireless client with nothing
  66. // between it and the router still gets a "where" — the router's visible name.
  67. func TestUnknownDeviceLocationFallsBackToRouter(t *testing.T) {
  68. l := NewLive(nil, nil, []RouterConfig{
  69. {ID: "gateway", Name: "Gateway", IsGateway: true},
  70. }, nil)
  71. d := Device{
  72. MAC: "02:00:00:00:00:32", Name: "02:00:00:00:00:32",
  73. RouterID: "gateway", Online: true, Band: "5 GHz",
  74. }
  75. l.emitUnknownDevice(d, []Device{d}, nil)
  76. ev := l.engine.List()[0]
  77. if ev.Vars["where"] != "Gateway" {
  78. t.Fatalf("Vars[where]=%q (want the router name)", ev.Vars["where"])
  79. }
  80. if _, ok := ev.Vars["ip"]; ok {
  81. t.Fatalf("an unknown IP must be absent, not empty: %+v", ev.Vars)
  82. }
  83. }
  84. // TestUnknownDeviceDropsUplinkPort: a client behind a switch is learnt on the
  85. // router port the switch hangs off. That port is the uplink, not the socket
  86. // the client is in, so the alert must not name it.
  87. func TestUnknownDeviceDropsUplinkPort(t *testing.T) {
  88. l := NewLive(nil, nil, []RouterConfig{
  89. {ID: "gateway", Name: "Gateway", IsGateway: true},
  90. }, nil)
  91. d := Device{
  92. MAC: "02:00:00:00:00:33", Name: "02:00:00:00:00:33", IP: "192.0.2.33",
  93. RouterID: "gateway", Online: true, Band: "cable",
  94. AttachTo: "sw-desk", Port: "3",
  95. }
  96. dists := []DistributionNode{{ID: "sw-desk", Kind: "managed", Name: "Desk switch", RouterID: "gateway", Port: "3"}}
  97. l.emitUnknownDevice(d, []Device{d}, dists)
  98. ev := l.engine.List()[0]
  99. if ev.Vars["where"] != "Desk switch" {
  100. t.Fatalf("Vars[where]=%q", ev.Vars["where"])
  101. }
  102. if p, ok := ev.Vars["port"]; ok {
  103. t.Fatalf("the uplink port must not be shown as the client's: %q", p)
  104. }
  105. }