rack-catalog-check.mjs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // rack-catalog-check.mjs - verificación ejecutable del catálogo de faceplates
  2. // (gates A1-A3). Corre con Node >= 23 (type stripping), sin dependencias.
  3. const mod = await import('../src/lib/rackFaceplates.ts')
  4. const { FACEPLATES, seedPorts, suggestFaceplate, applyFaceplate, getFaceplate } = mod
  5. let failed = false
  6. const bad = (msg) => {
  7. failed = true
  8. console.error('FAIL:', msg)
  9. }
  10. // A1: >= 9 plantillas, bandas no solapadas, puertos dentro de 0..1.
  11. if (FACEPLATES.length < 9) bad(`solo ${FACEPLATES.length} plantillas (min 9)`)
  12. for (const p of FACEPLATES) {
  13. if (![3, 4, 6, 12].includes(p.colSpan)) bad(`${p.id}: colSpan ${p.colSpan} fuera de {3,4,6,12}`)
  14. if (p.uHeight < 1) bad(`${p.id}: uHeight ${p.uHeight}`)
  15. // r = semi-extensión vertical (fracción del alto): validamos el eje Y y
  16. // que el centro X quede a la izquierda de la labelBox (banda fija LED,
  17. // luego labelBox: nunca se tocan).
  18. if (p.led.y - p.led.r < 0 || p.led.y + p.led.r > 1) bad(`${p.id}: led fuera del eje Y`)
  19. if (p.led.x < 0 || p.led.x > 1) bad(`${p.id}: led fuera del eje X`)
  20. const lb = p.labelBox
  21. if (lb.x < 0 || lb.y < 0 || lb.x + lb.w > 1 || lb.y + lb.h > 1) bad(`${p.id}: labelBox fuera de 0..1`)
  22. if (p.led.x >= lb.x) bad(`${p.id}: led debe quedar a la izquierda de la labelBox`)
  23. for (const row of p.rows) {
  24. if (row.count < 1) bad(`${p.id}: fila vacía`)
  25. if (row.xStart < 0 || row.xEnd > 1 || row.y < 0 || row.y > 1) bad(`${p.id}: fila fuera de 0..1`)
  26. }
  27. }
  28. const ids = new Set(FACEPLATES.map((p) => p.id))
  29. if (ids.size !== FACEPLATES.length) bad('IDs de plantilla duplicados')
  30. console.log(failed ? '' : 'CATALOG OK')
  31. // A2: suggest para los tipos canónicos del clasificador.
  32. const expected = [
  33. ['router', 'router'],
  34. ['ap', 'router'],
  35. ['switch', 'switch'],
  36. ['servidor', 'server-1u'],
  37. ['ordenador', 'server-1u'],
  38. ['nas', 'nas-tower'],
  39. ['ups', 'ups-4u'],
  40. ['pdu', 'pdu-1u'],
  41. ['desconocido', undefined],
  42. ['iot', undefined],
  43. ['', undefined],
  44. ]
  45. for (const [typ, want] of expected) {
  46. const got = suggestFaceplate(typ)
  47. const gotId = got ? got.id : undefined
  48. if (gotId !== want) bad(`suggest("${typ}") = ${gotId}; want ${want}`)
  49. }
  50. console.log(failed ? '' : 'SUGGEST OK')
  51. // A3: seed estable y applyFaceplate.
  52. const sw = getFaceplate('switch')
  53. const p1 = seedPorts(sw)
  54. const p2 = seedPorts(sw)
  55. if (JSON.stringify(p1) !== JSON.stringify(p2)) bad('seedPorts no es estable')
  56. if (p1.length !== 28) bad(`switch: ${p1.length} puertos; want 28 (24 rj45 + 4 sfp+)`)
  57. const kinds = p1.reduce((m, p) => ((m[p.kind] = (m[p.kind] || 0) + 1), m), {})
  58. if (kinds.rj45 !== 24 || kinds['sfp+'] !== 4) bad(`switch kinds: ${JSON.stringify(kinds)}`)
  59. const idsStable = p1.map((p) => p.id).join(',')
  60. if (idsStable !== p1.map((_, i) => 'p' + String(i + 1).padStart(2, '0')).join(',')) bad('IDs de puerto inestables')
  61. for (const p of p1) {
  62. if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) bad(`puerto ${p.id} fuera de 0..1`)
  63. }
  64. const applied = applyFaceplate('switch')
  65. if (!applied || applied.plate.id !== 'switch' || applied.ports.length !== 28) bad('applyFaceplate(switch) roto')
  66. // A4b: layoutPhysicalPorts determinista y con ids reales preservados.
  67. const { layoutPhysicalPorts } = mod
  68. const phys = layoutPhysicalPorts([
  69. ...Array.from({ length: 5 }, (_, i) => ({ id: 'lan' + (i + 1), kind: 'rj45' })),
  70. { id: 'eth1', kind: 'sfp+' },
  71. ])
  72. if (phys.length !== 6) bad(`layoutPhysicalPorts: ${phys.length}; want 6`)
  73. if (phys[5].id !== 'eth1' || phys[5].x < 0.7) bad('sfp+ no queda a la derecha')
  74. if (phys[0].id !== 'lan1') bad('ids reales no preservados')
  75. for (const p of phys) if (p.x < 0 || p.x > 1 || p.y < 0 || p.y > 1) bad(`puerto físico ${p.id} fuera de 0..1`)
  76. console.log(failed ? '' : 'LAYOUT OK')
  77. const appliedBad = applyFaceplate('iot')
  78. if (appliedBad !== undefined) bad('applyFaceplate(iot) debería ser undefined')
  79. console.log(failed ? '' : 'SEED OK')
  80. console.log(failed ? 'CHECK FAILED' : 'ALL CHECKS PASS')
  81. process.exit(failed ? 1 : 0)