devices.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Tests de paginación y filtros de /api/devices (paginación obligatoria, skill).
  3. */
  4. import { describe, it, before, after } from 'node:test'
  5. import assert from 'node:assert/strict'
  6. import { makeTestServer, loginCookie } from './helpers.js'
  7. describe('GET /api/devices', () => {
  8. let srv
  9. let cookie
  10. before(async () => {
  11. srv = await makeTestServer()
  12. ;({ cookie } = await loginCookie(srv.base))
  13. })
  14. after(async () => {
  15. await srv.close()
  16. })
  17. const get = (qs) =>
  18. fetch(`${srv.base}/api/devices?${qs}`, { headers: { cookie: `session=${cookie}` } }).then((r) => r.json())
  19. it('devuelve los 47 clientes paginados (page 1 de 10)', async () => {
  20. const body = await get('page=1&pageSize=10')
  21. assert.equal(body.total, 47)
  22. assert.equal(body.items.length, 10)
  23. assert.equal(body.page, 1)
  24. assert.equal(body.pageSize, 10)
  25. })
  26. it('última página con resto (page 5 de 10 → 7 items)', async () => {
  27. const body = await get('page=5&pageSize=10')
  28. assert.equal(body.total, 47)
  29. assert.equal(body.items.length, 7)
  30. // Sin solape con la página anterior
  31. const prev = await get('page=4&pageSize=10')
  32. const prevIds = new Set(prev.items.map((d) => d.id))
  33. assert.ok(body.items.every((d) => !prevIds.has(d.id)))
  34. })
  35. it('filtros: routerId, band, status, type y búsqueda q', async () => {
  36. const living = await get('routerId=living&pageSize=200')
  37. assert.equal(living.total, 18)
  38. assert.ok(living.items.every((d) => d.routerId === 'living'))
  39. const cable = await get('band=cable&pageSize=200')
  40. assert.ok(cable.items.every((d) => d.band === 'cable'))
  41. const offline = await get('status=offline&pageSize=200')
  42. assert.equal(offline.total, 8)
  43. assert.ok(offline.items.every((d) => !d.online))
  44. const moviles = await get('type=movil&pageSize=200')
  45. assert.ok(moviles.items.every((d) => d.type === 'movil' || d.group === 'moviles'))
  46. const q = await get('q=pixel&pageSize=200')
  47. assert.ok(q.total >= 2, 'pixel-8-pro y pixel-7')
  48. assert.ok(q.items.some((d) => d.id === 'pixel-8-pro'))
  49. })
  50. it('valida parámetros (band inválida → 400)', async () => {
  51. const res = await fetch(`${srv.base}/api/devices?band=wifi6`, { headers: { cookie: `session=${cookie}` } })
  52. assert.equal(res.status, 400)
  53. const body = await res.json()
  54. assert.equal(body.error, 'invalid_query')
  55. })
  56. it('los items llevan el shape Device (camelCase del contrato)', async () => {
  57. const body = await get('page=1&pageSize=1')
  58. const d = body.items[0]
  59. for (const key of ['id', 'name', 'type', 'manufacturer', 'ip', 'mac', 'routerId', 'band', 'signalDbm', 'trafficMbps', 'online', 'sparkline']) {
  60. assert.ok(key in d, `device sin ${key}`)
  61. }
  62. })
  63. })