demo_canon_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package adapters
  2. import (
  3. "context"
  4. "testing"
  5. )
  6. // Tests del canon reconciliado (SPEC-CANON fase 6, D1/D3/D4/D5). Fijan la
  7. // enumeración del dataset: sin IDs duplicadas, totales derivados, el switch
  8. // gestionado como Device + distnode managed y los puertos del gateway.
  9. func deviceByID(t *testing.T, devices []Device, id string) Device {
  10. t.Helper()
  11. for _, d := range devices {
  12. if d.ID == id {
  13. return d
  14. }
  15. }
  16. t.Fatalf("device %q no existe en el dataset", id)
  17. return Device{}
  18. }
  19. // D3: una sola identidad por ID en el dataset reconciliado.
  20. func TestCanonDatasetSinIDsDuplicadas(t *testing.T) {
  21. devices := canonAllDevices()
  22. seen := map[string]int{}
  23. for _, d := range devices {
  24. seen[d.ID]++
  25. }
  26. for id, n := range seen {
  27. if n != 1 {
  28. t.Fatalf("ID duplicada %q: %d definiciones", id, n)
  29. }
  30. }
  31. // Las dos identidades reconciliadas, en su versión final:
  32. xbox := deviceByID(t, devices, "xbox-series-s")
  33. if !xbox.Online || xbox.Band != "cable" || xbox.AttachTo != "dist-living-lan3" {
  34. t.Fatalf("xbox-series-s debería ser la cableada al GS308E: %+v", xbox)
  35. }
  36. hp := deviceByID(t, devices, "impresora-hp")
  37. if !hp.Online || hp.Band != "cable" || hp.AttachTo != "dist-flint2-lan3" {
  38. t.Fatalf("impresora-hp debería ser la cableada al switch inferido: %+v", hp)
  39. }
  40. }
  41. // D5: los totales se derivan del dataset — la enumeración manda.
  42. func TestCanonTotalesDerivadosDelDataset(t *testing.T) {
  43. devices := canonAllDevices()
  44. totals := deviceTotalsOf(devices)
  45. // Enumeración del canon reconciliado: 65 IDs únicos, 59 online, 6
  46. // offline conocidos, 3 nuevos hoy.
  47. if totals != (DeviceTotals{Total: 65, Online: 59, KnownOffline: 6, NewToday: 3}) {
  48. t.Fatalf("enumeración del dataset: %+v", totals)
  49. }
  50. d := NewDemo()
  51. ov, err := d.GetOverview(context.Background())
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if ov.DeviceTotals != totals {
  56. t.Fatalf("overview.deviceTotals no derivado del dataset: %+v vs %+v", ov.DeviceTotals, totals)
  57. }
  58. if ov.Adguard.ClientsTotal != totals.Total {
  59. t.Fatalf("adguard.clientsTotal=%d, dataset=%d", ov.Adguard.ClientsTotal, totals.Total)
  60. }
  61. // router.clients = recuento real online por router; la suma = online.
  62. sum := 0
  63. porRouter := map[string]int{}
  64. for _, r := range ov.Routers {
  65. porRouter[r.ID] = r.Clients
  66. sum += r.Clients
  67. if want := onlineClientsOf(devices, r.ID); r.Clients != want {
  68. t.Fatalf("router %s clients=%d, enumeración=%d", r.ID, r.Clients, want)
  69. }
  70. }
  71. if sum != totals.Online {
  72. t.Fatalf("suma de clients por router=%d, online=%d", sum, totals.Online)
  73. }
  74. // Canon fijado: flint2/living/estudio/patio = 26/20/8/5.
  75. want := map[string]int{"flint2": 26, "living": 20, "estudio": 8, "patio": 5}
  76. for id, w := range want {
  77. if porRouter[id] != w {
  78. t.Fatalf("router %s clients=%d, canon=%d", id, porRouter[id], w)
  79. }
  80. }
  81. }
  82. // D1: el switch gestionado GS308E es Device (type switch, lldp, attachTo al
  83. // router, port de uplink) Y DistributionNode managed con Mac — y su Device
  84. // NO cuelga de su propio nodo (D2, paridad con inferTopology).
  85. func TestCanonSwitchGestionadoDeviceYNodo(t *testing.T) {
  86. devices := canonAllDevices()
  87. sw := deviceByID(t, devices, "switch-netgear")
  88. if sw.Type != "switch" || sw.IP != "192.168.8.13" || !sw.Online {
  89. t.Fatalf("GS308E device: %+v", sw)
  90. }
  91. if sw.RouterID != "living" || sw.AttachTo != "living" || sw.Port != "lan3" {
  92. t.Fatalf("GS308E attachTo/port de uplink: %+v", sw)
  93. }
  94. if sw.Lldp == nil || sw.Lldp.Chassis != "GS308E" || sw.Lldp.Mgmt != "192.168.8.13" {
  95. t.Fatalf("GS308E device sin lldp: %+v", sw.Lldp)
  96. }
  97. var managed *DistributionNode
  98. for i, dn := range canonDistributionNodes() {
  99. if dn.Kind == "managed" {
  100. if managed != nil {
  101. t.Fatalf("más de un distnode managed")
  102. }
  103. managed = &canonDistributionNodes()[i]
  104. }
  105. }
  106. if managed == nil {
  107. t.Fatal("no hay distnode managed")
  108. }
  109. if managed.ID != "dist-living-lan3" || managed.Mac == "" {
  110. t.Fatalf("distnode managed sin mac: %+v", managed)
  111. }
  112. if managed.Mac != sw.MAC {
  113. t.Fatalf("distnode.Mac=%q ≠ device.MAC=%q (la app cruza ambas para ocultar el chip)", managed.Mac, sw.MAC)
  114. }
  115. if sw.AttachTo == managed.ID {
  116. t.Fatalf("el switch NO debe colgar de su propio nodo: %+v", sw)
  117. }
  118. // Y llega por el adapter demo (overview + devices).
  119. d := NewDemo()
  120. ov, _ := d.GetOverview(context.Background())
  121. found := false
  122. for _, dn := range ov.DistributionNodes {
  123. if dn.ID == "dist-living-lan3" {
  124. found = true
  125. if dn.Mac != "28:C6:8E:1D:90:44" {
  126. t.Fatalf("overview distnode managed mac: %q", dn.Mac)
  127. }
  128. }
  129. }
  130. if !found {
  131. t.Fatal("overview sin dist-living-lan3")
  132. }
  133. deviceByID(t, d.GetDevices(context.Background()), "switch-netgear")
  134. }
  135. // D4: puertos del gateway y del Salón en devices y distnodes.
  136. func TestCanonPuertosD4(t *testing.T) {
  137. devices := canonAllDevices()
  138. if d := deviceByID(t, devices, "nas-synology"); d.Port != "lan4" {
  139. t.Fatalf("NAS port: %q (D4: lan4)", d.Port)
  140. }
  141. if d := deviceByID(t, devices, "pve"); d.Port != "lan5" {
  142. t.Fatalf("pve port: %q (D4: lan5)", d.Port)
  143. }
  144. if d := deviceByID(t, devices, "ps5"); d.Port != "lan1" {
  145. t.Fatalf("PS5 port: %q (D4: lan1)", d.Port)
  146. }
  147. for _, dn := range canonDistributionNodes() {
  148. if dn.ID == "dist-pve" && dn.Port != "lan5" {
  149. t.Fatalf("dist-pve port: %q (D4: lan5)", dn.Port)
  150. }
  151. if dn.ID == "dist-flint2-lan3" && dn.Port != "lan3" {
  152. t.Fatalf("dist-flint2-lan3 port: %q", dn.Port)
  153. }
  154. }
  155. // Panel ethPorts del gateway cuadra con la tabla D4.
  156. extras := canonRouterExtras()["flint2"]
  157. ports := map[string]EthPort{}
  158. for _, p := range extras.EthPorts {
  159. ports[p.ID] = p
  160. }
  161. if ports["lan3"].ConnectedTo != "Switch sin gestión" || !ports["lan3"].Up {
  162. t.Fatalf("flint2 lan3 (switch inferido): %+v", ports["lan3"])
  163. }
  164. if ports["lan4"].ConnectedTo != "NAS Synology" || !ports["lan4"].Up {
  165. t.Fatalf("flint2 lan4 (NAS): %+v", ports["lan4"])
  166. }
  167. if ports["lan5"].ConnectedTo != "Proxmox pve" || ports["lan5"].Speed != "2.5 Gbps" || !ports["lan5"].Up {
  168. t.Fatalf("flint2 lan5 (pve 2.5G): %+v", ports["lan5"])
  169. }
  170. // Salón: lan3 = GS308E UP (antes "libre/down").
  171. living := map[string]EthPort{}
  172. for _, p := range canonRouterExtras()["living"].EthPorts {
  173. living[p.ID] = p
  174. }
  175. if !living["lan3"].Up || living["lan3"].ConnectedTo != "GS308E" {
  176. t.Fatalf("living lan3 (GS308E up): %+v", living["lan3"])
  177. }
  178. }