roam_derive_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // roam_derive_test.go — roams derivados de la atribución wireless (#1740).
  2. package adapters
  3. import (
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/db"
  6. )
  7. func countRoamEvents(t *testing.T, d *db.DB) int {
  8. t.Helper()
  9. var n int
  10. if err := d.QueryRow("SELECT COUNT(*) FROM roam_events").Scan(&n); err != nil {
  11. t.Fatalf("count roam_events: %v", err)
  12. }
  13. return n
  14. }
  15. func polledWithWireless(routerID, mac, band string) map[string]*routerPolled {
  16. return map[string]*routerPolled{
  17. routerID: {
  18. cfg: RouterConfig{ID: routerID, Name: routerID, Host: "192.168.1.2"},
  19. wireless: map[string]WirelessClient{mac: {SignalDbm: -55, Band: band}},
  20. },
  21. }
  22. }
  23. // TestBuildDevicesDerivesRoamBetweenRouters: la primera vez que se ve una MAC
  24. // solo se aprende la atribución. Cuando el ciclo siguiente la ve en otro AP se
  25. // emite un 'connected', y repetir el mismo AP no emite nada.
  26. func TestBuildDevicesDerivesRoamBetweenRouters(t *testing.T) {
  27. d := openLiveTestDB(t)
  28. l := NewLive(nil, d, nil, nil)
  29. const mac = "AA:BB:CC:DD:EE:FF"
  30. l.buildDevices(polledWithWireless("patio", mac, "5 GHz"), "")
  31. if n := countRoamEvents(t, d); n != 0 {
  32. t.Fatalf("el primer ciclo no debe emitir roams, got %d", n)
  33. }
  34. l.buildDevices(polledWithWireless("salon", mac, "5 GHz"), "")
  35. if n := countRoamEvents(t, d); n != 1 {
  36. t.Fatalf("un salto de AP debe emitir 1 evento, got %d", n)
  37. }
  38. var routerID, typ, detail string
  39. if err := d.QueryRow("SELECT router_id, type, detail FROM roam_events").Scan(&routerID, &typ, &detail); err != nil {
  40. t.Fatalf("leer roam_events: %v", err)
  41. }
  42. if routerID != "salon" || typ != "connected" {
  43. t.Fatalf("evento inesperado: router=%q type=%q detail=%q", routerID, typ, detail)
  44. }
  45. if detail != "roamed from patio to salon" {
  46. t.Fatalf("detail inesperado: %q", detail)
  47. }
  48. l.buildDevices(polledWithWireless("salon", mac, "5 GHz"), "")
  49. if n := countRoamEvents(t, d); n != 1 {
  50. t.Fatalf("sin cambio de AP no debe emitir, got %d", n)
  51. }
  52. }
  53. // TestBuildDevicesRoamBandChangeAndEcho: cambiar de banda en el mismo AP cuenta
  54. // como reasociación, y el rebote inmediato de vuelta no suma (anti-eco).
  55. func TestBuildDevicesRoamBandChangeAndEcho(t *testing.T) {
  56. d := openLiveTestDB(t)
  57. l := NewLive(nil, d, nil, nil)
  58. const mac = "AA:BB:CC:DD:EE:11"
  59. l.buildDevices(polledWithWireless("patio", mac, "5 GHz"), "")
  60. l.buildDevices(polledWithWireless("patio", mac, "2.4 GHz"), "")
  61. if n := countRoamEvents(t, d); n != 1 {
  62. t.Fatalf("el cambio de banda debe emitir 1 evento, got %d", n)
  63. }
  64. l.buildDevices(polledWithWireless("patio", mac, "5 GHz"), "")
  65. if n := countRoamEvents(t, d); n != 1 {
  66. t.Fatalf("el eco de banda no debe emitir, got %d", n)
  67. }
  68. }
  69. // TestBuildDevicesRoamAmbiguousAcrossRouters: si dos APs reportan la misma MAC
  70. // en el mismo ciclo, el orden de iteración del mapa decide la atribución, así
  71. // que no se emite nada.
  72. func TestBuildDevicesRoamAmbiguousAcrossRouters(t *testing.T) {
  73. d := openLiveTestDB(t)
  74. l := NewLive(nil, d, nil, nil)
  75. const mac = "AA:BB:CC:DD:EE:22"
  76. l.buildDevices(polledWithWireless("patio", mac, "5 GHz"), "")
  77. l.buildDevices(map[string]*routerPolled{
  78. "patio": {
  79. cfg: RouterConfig{ID: "patio", Name: "patio"},
  80. wireless: map[string]WirelessClient{mac: {SignalDbm: -60, Band: "5 GHz"}},
  81. },
  82. "salon": {
  83. cfg: RouterConfig{ID: "salon", Name: "salon"},
  84. wireless: map[string]WirelessClient{mac: {SignalDbm: -55, Band: "5 GHz"}},
  85. },
  86. }, "")
  87. if n := countRoamEvents(t, d); n != 0 {
  88. t.Fatalf("una MAC en dos APs en el mismo ciclo no debe emitir, got %d", n)
  89. }
  90. }