snapshot.test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Tests del snapshot demo (contrato):
  3. * - El overview contiene todas las claves del contrato con los shapes del mock.
  4. * - El primer snapshot es EXACTAMENTE el canon (antes del random walk).
  5. * - El random walk respeta los límites (cpu/ram/temp ±2, latencia 6–11 ms,
  6. * bytes WG crecientes).
  7. */
  8. import { describe, it } from 'node:test'
  9. import assert from 'node:assert/strict'
  10. import { createDemoAdapter } from '../src/adapters/demo.js'
  11. import { routers as canonRouters, wan as canonWan, adguard as canonAdguard } from '../src/demo/dataset.js'
  12. describe('snapshot demo', () => {
  13. it('contiene todas las claves del contrato', () => {
  14. const adapter = createDemoAdapter()
  15. const snap = adapter.getOverview()
  16. for (const key of [
  17. 'health', 'wan', 'traffic', 'adguard', 'wireguard',
  18. 'routers', 'deviceTotals', 'topDevices', 'alerts', 'unreadAlerts', 'ts',
  19. ]) {
  20. assert.ok(key in snap, `falta ${key} en el snapshot`)
  21. }
  22. assert.equal(snap.routers.length, 4)
  23. assert.equal(snap.topDevices.length, 5)
  24. assert.ok(Array.isArray(snap.alerts))
  25. for (const range of ['1h', '24h', '7d', '30d']) {
  26. assert.ok(Array.isArray(snap.traffic[range]), `falta traffic.${range}`)
  27. }
  28. // Shapes clave (camelCase del mock)
  29. const r0 = snap.routers[0]
  30. for (const key of ['id', 'name', 'model', 'modelShort', 'roleBadge', 'ip', 'status', 'health', 'cpu', 'ram', 'temp', 'uptime', 'clients', 'sparkline']) {
  31. assert.ok(key in r0, `router sin ${key}`)
  32. }
  33. for (const key of ['queries24h', 'blocked24h', 'blockedPct', 'topBlocked', 'filterLists', 'rules']) {
  34. assert.ok(key in snap.adguard, `adguard sin ${key}`)
  35. }
  36. for (const key of ['interface', 'subnet', 'status', 'peers']) {
  37. assert.ok(key in snap.wireguard, `wireguard sin ${key}`)
  38. }
  39. for (const key of ['id', 'name', 'type', 'tunnelIp', 'active', 'lastHandshake', 'rx', 'tx']) {
  40. assert.ok(key in snap.wireguard.peers[0], `peer sin ${key}`)
  41. }
  42. assert.equal(typeof snap.deviceTotals.total, 'number')
  43. assert.equal(typeof snap.unreadAlerts, 'number')
  44. assert.equal(typeof snap.ts, 'number')
  45. })
  46. it('el primer snapshot es exactamente el canon', () => {
  47. const adapter = createDemoAdapter()
  48. const snap = adapter.getOverview()
  49. const flint = snap.routers.find((r) => r.id === 'flint2')
  50. assert.equal(flint.cpu, canonRouters[0].cpu)
  51. assert.equal(flint.ram, canonRouters[0].ram)
  52. assert.equal(flint.temp, canonRouters[0].temp)
  53. assert.equal(snap.wan.downMbps, canonWan.downMbps)
  54. assert.equal(snap.wan.latencyMs, canonWan.latencyMs)
  55. assert.equal(snap.adguard.queries24h, canonAdguard.queries24h)
  56. const pixel = snap.wireguard.peers.find((p) => p.id === 'pixel-8-pro')
  57. assert.equal(pixel.rx, '1,2 GB')
  58. assert.equal(pixel.tx, '214 MB')
  59. const inactivo = snap.wireguard.peers.find((p) => p.id === 'casa-familia')
  60. assert.equal(inactivo.rx, '12 GB')
  61. assert.equal(snap.deviceTotals.total, 47)
  62. })
  63. it('random walk: cpu/ram/temp ±2 máx, latencia 6–11 ms, bytes WG crecen', () => {
  64. const adapter = createDemoAdapter()
  65. const before = adapter.getOverview()
  66. adapter.tick()
  67. const after = adapter.getOverview()
  68. for (let i = 0; i < 4; i++) {
  69. for (const key of ['cpu', 'ram', 'temp']) {
  70. const delta = Math.abs(after.routers[i][key] - before.routers[i][key])
  71. assert.ok(delta <= 2, `${key} de ${after.routers[i].id} saltó ${delta} (>2)`)
  72. }
  73. }
  74. assert.ok(after.wan.latencyMs >= 6 && after.wan.latencyMs <= 11, `latencia ${after.wan.latencyMs} fuera de 6–11`)
  75. // AdGuard crece
  76. assert.ok(after.adguard.queries24h >= before.adguard.queries24h)
  77. // WG: parseo de formato ES y comparación numérica
  78. const parse = (s) => {
  79. const m = /^([\d,]+) (GB|MB|KB)$/.exec(s)
  80. const mult = { GB: 1e9, MB: 1e6, KB: 1e3 }[m[2]]
  81. return parseFloat(m[1].replace(',', '.')) * mult
  82. }
  83. for (const id of ['pixel-8-pro', 'macbook-air']) {
  84. const b = before.wireguard.peers.find((p) => p.id === id)
  85. const a = after.wireguard.peers.find((p) => p.id === id)
  86. assert.ok(parse(a.rx) >= parse(b.rx), `rx de ${id} debe crecer`)
  87. assert.ok(parse(a.tx) >= parse(b.tx), `tx de ${id} debe crecer`)
  88. }
  89. // Peers inactivos: no cambian
  90. const bIn = before.wireguard.peers.find((p) => p.id === 'ipad-air')
  91. const aIn = after.wireguard.peers.find((p) => p.id === 'ipad-air')
  92. assert.equal(aIn.rx, bIn.rx)
  93. })
  94. it('detalle de router: extras, series y 404 en id desconocido', async () => {
  95. const adapter = createDemoAdapter()
  96. const detail = await adapter.getRouterDetail('flint2')
  97. assert.ok(detail.ports.length >= 5, 'flint2 tiene 5 bocas RJ45')
  98. assert.equal(detail.radios, null, 'flint2 no define radios en el canon')
  99. assert.equal(detail.backhaul, null)
  100. for (const range of ['1h', '24h', '7d']) {
  101. assert.ok(Array.isArray(detail.series[range]), `falta series.${range}`)
  102. const last = detail.series[range].at(-1)
  103. // La serie termina SIEMPRE en el valor actual del router
  104. assert.equal(last.cpu, detail.router.cpu)
  105. assert.equal(last.temp, detail.router.temp)
  106. }
  107. assert.ok(detail.adguard, 'gateway incluye adguard')
  108. assert.ok(detail.wireguard, 'gateway incluye wireguard')
  109. assert.ok(Array.isArray(detail.clients))
  110. const living = await adapter.getRouterDetail('living')
  111. assert.ok(Array.isArray(living.radios) && living.radios.length === 2)
  112. assert.equal(living.adguard, undefined, 'AP no incluye adguard')
  113. assert.equal(await adapter.getRouterDetail('no-existe'), null)
  114. })
  115. })