searchIndex.test.mjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. MAX_SEARCH_HINT_ACTIONS,
  5. MAX_SEARCH_HINT_DASHBOARDS,
  6. MAX_SEARCH_HINT_ENTITIES_PER_TYPE,
  7. clearSearchIndex,
  8. dashboardRoutePath,
  9. indexRootDashboardEntries,
  10. indexSearchHints,
  11. indexSystemNavigation,
  12. searchIndexItems
  13. } from '../stores/searchIndex.js'
  14. function resetIndex () {
  15. clearSearchIndex()
  16. }
  17. test('dashboardRoutePath maps Actions to root', () => {
  18. assert.equal(dashboardRoutePath('Actions'), '/')
  19. })
  20. test('dashboardRoutePath builds dashboard and entity paths', () => {
  21. assert.equal(dashboardRoutePath('Servers'), '/dashboards/Servers')
  22. assert.equal(
  23. dashboardRoutePath('Servers', 'host', 'web01'),
  24. '/dashboards/Servers/host/web01'
  25. )
  26. assert.equal(
  27. dashboardRoutePath('Ops/Prod', 'host/type', 'key?1'),
  28. '/dashboards/Ops%2FProd/host%2Ftype/key%3F1'
  29. )
  30. assert.equal(
  31. dashboardRoutePath('Servers', 'host', 'key#frag'),
  32. '/dashboards/Servers/host/key%23frag'
  33. )
  34. })
  35. test('indexSearchHints indexes entities and actions', () => {
  36. resetIndex()
  37. indexSearchHints({
  38. entities: [
  39. { title: 'web01', type: 'host', uniqueKey: '0' },
  40. { title: '', type: 'host', uniqueKey: '1' },
  41. { title: 'skip', type: '', uniqueKey: 'x' },
  42. { title: 'slash host', type: 'host/type', uniqueKey: 'key?1' },
  43. { title: 'hash host', type: 'host', uniqueKey: 'key#frag' }
  44. ],
  45. actions: [
  46. { title: 'Ping Host', bindingId: 'bind-ping' },
  47. { title: '', bindingId: 'bind-empty-title' },
  48. { title: 'Ignored', bindingId: '' },
  49. { title: 'Slash Action', bindingId: 'bind/slash' },
  50. { title: 'Query Action', bindingId: 'bind?query' },
  51. { title: 'Hash Action', bindingId: 'bind#hash' }
  52. ]
  53. })
  54. const byId = Object.fromEntries(searchIndexItems.value.map((item) => [item.id, item]))
  55. assert.equal(byId['entity:host:0'].title, 'web01')
  56. assert.equal(byId['entity:host:0'].path, '/entity-details/host/0')
  57. assert.equal(byId['entity:host:0'].description, 'host')
  58. assert.equal(byId['entity:host:1'].title, '1')
  59. assert.equal(
  60. byId['entity:host/type:key?1'].path,
  61. '/entity-details/host%2Ftype/key%3F1'
  62. )
  63. assert.equal(
  64. byId['entity:host:key#frag'].path,
  65. '/entity-details/host/key%23frag'
  66. )
  67. assert.equal(byId['action:bind-ping'].title, 'Ping Host')
  68. assert.equal(byId['action:bind-ping'].path, '/action/bind-ping')
  69. assert.equal(byId['action:bind/slash'].path, '/action/bind%2Fslash')
  70. assert.equal(byId['action:bind?query'].path, '/action/bind%3Fquery')
  71. assert.equal(byId['action:bind#hash'].path, '/action/bind%23hash')
  72. assert.equal(byId['action:bind-ping'].category, 'Actions')
  73. assert.equal(byId['action:bind-empty-title'].title, 'bind-empty-title')
  74. assert.equal(Object.keys(byId).includes('entity:host:x'), false)
  75. assert.equal(Object.keys(byId).includes('action:'), false)
  76. })
  77. test('indexRootDashboardEntries indexes ACL-filtered dashboards', () => {
  78. resetIndex()
  79. indexRootDashboardEntries([
  80. { title: 'Actions', category: '' },
  81. { title: 'My Server', category: 'Infrastructure' },
  82. { title: '', category: 'Ignored' }
  83. ])
  84. const byId = Object.fromEntries(searchIndexItems.value.map((item) => [item.id, item]))
  85. assert.equal(byId['dashboard:Actions'].path, '/')
  86. assert.equal(byId['dashboard:My Server'].path, '/dashboards/My%20Server')
  87. assert.equal(byId['dashboard:My Server'].description, 'Infrastructure')
  88. assert.equal(byId['dashboard:My Server'].category, 'Dashboards')
  89. })
  90. test('indexSearchHints replaces prior entity and action snapshots', () => {
  91. resetIndex()
  92. indexSearchHints({
  93. entities: [{ title: 'old', type: 'host', uniqueKey: 'old' }],
  94. actions: [{ title: 'Old Action', bindingId: 'old-action' }]
  95. })
  96. indexRootDashboardEntries([{ title: 'Old Board', category: '' }])
  97. indexSearchHints({
  98. entities: [{ title: 'new', type: 'host', uniqueKey: 'new' }],
  99. actions: [{ title: 'New Action', bindingId: 'new-action' }]
  100. })
  101. indexRootDashboardEntries([{ title: 'New Board', category: 'Ops' }])
  102. const byId = Object.fromEntries(searchIndexItems.value.map((item) => [item.id, item]))
  103. assert.equal(byId['entity:host:old'], undefined)
  104. assert.equal(byId['action:old-action'], undefined)
  105. assert.equal(byId['dashboard:Old Board'], undefined)
  106. assert.equal(byId['entity:host:new'].title, 'new')
  107. assert.equal(byId['action:new-action'].title, 'New Action')
  108. assert.equal(byId['dashboard:New Board'].description, 'Ops')
  109. })
  110. test('caps actions globally, entities per type, and dashboards from root entries', () => {
  111. resetIndex()
  112. const actions = []
  113. for (let i = 0; i < MAX_SEARCH_HINT_ACTIONS + 20; i++) {
  114. actions.push({ title: `Action ${i}`, bindingId: `a-${i}` })
  115. }
  116. const entities = []
  117. for (let i = 0; i < MAX_SEARCH_HINT_ENTITIES_PER_TYPE + 20; i++) {
  118. entities.push({ title: `Host ${i}`, type: 'host', uniqueKey: `${i}` })
  119. entities.push({ title: `Container ${i}`, type: 'container', uniqueKey: `${i}` })
  120. }
  121. const dashboards = []
  122. for (let i = 0; i < MAX_SEARCH_HINT_DASHBOARDS + 20; i++) {
  123. dashboards.push({ title: `Board ${i}`, category: '' })
  124. }
  125. indexSearchHints({ actions, entities })
  126. indexRootDashboardEntries(dashboards)
  127. const items = searchIndexItems.value
  128. assert.equal(items.filter((item) => item.category === 'Actions').length, MAX_SEARCH_HINT_ACTIONS)
  129. assert.equal(items.filter((item) => item.category === 'Dashboards').length, MAX_SEARCH_HINT_DASHBOARDS)
  130. assert.equal(
  131. items.filter((item) => item.category === 'Entities' && item.description === 'host').length,
  132. MAX_SEARCH_HINT_ENTITIES_PER_TYPE
  133. )
  134. assert.equal(
  135. items.filter((item) => item.category === 'Entities' && item.description === 'container').length,
  136. MAX_SEARCH_HINT_ENTITIES_PER_TYPE
  137. )
  138. })
  139. test('indexSystemNavigation always includes entities and respects visibility flags', () => {
  140. resetIndex()
  141. indexSystemNavigation({
  142. showLogs: true,
  143. showDiagnostics: false,
  144. entitiesTitle: 'Entities',
  145. logsTitle: 'Logs',
  146. diagnosticsTitle: 'Diagnostics'
  147. })
  148. let byId = Object.fromEntries(searchIndexItems.value.map((item) => [item.id, item]))
  149. assert.equal(byId['nav:entities'].path, '/entities')
  150. assert.equal(byId['nav:logs'].path, '/logs')
  151. assert.equal(byId['nav:diagnostics'], undefined)
  152. indexSystemNavigation({
  153. showLogs: false,
  154. showDiagnostics: true
  155. })
  156. byId = Object.fromEntries(searchIndexItems.value.map((item) => [item.id, item]))
  157. assert.equal(byId['nav:entities'].path, '/entities')
  158. assert.equal(byId['nav:logs'], undefined)
  159. assert.equal(byId['nav:diagnostics'].path, '/diagnostics')
  160. })
  161. test('entities appear before actions in searchIndexItems', () => {
  162. resetIndex()
  163. indexSearchHints({
  164. entities: [{ title: 'server1', type: 'host', uniqueKey: '0' }],
  165. actions: [{ title: 'server1', bindingId: 'bind-server1' }]
  166. })
  167. const titlesByCategory = searchIndexItems.value.map((item) => [item.category, item.title])
  168. const entityIndex = titlesByCategory.findIndex(([category]) => category === 'Entities')
  169. const actionIndex = titlesByCategory.findIndex(([category]) => category === 'Actions')
  170. assert.ok(entityIndex >= 0)
  171. assert.ok(actionIndex >= 0)
  172. assert.ok(entityIndex < actionIndex)
  173. })
  174. test('search index has no Logs category', () => {
  175. resetIndex()
  176. indexSearchHints({
  177. entities: [{ title: 'web01', type: 'host', uniqueKey: '0' }],
  178. actions: [{ title: 'Backup', bindingId: 'bind-backup' }]
  179. })
  180. indexRootDashboardEntries([{ title: 'Ops', category: '' }])
  181. indexSystemNavigation({ showLogs: true, showDiagnostics: true })
  182. assert.equal(
  183. searchIndexItems.value.some((item) => item.category === 'Logs'),
  184. false
  185. )
  186. })