ObservabilityMenu.utils.test.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. generateObservabilityMenuItems,
  4. type ObservabilityMenuSection,
  5. } from './ObservabilityMenu.utils'
  6. const REF = 'test-project-ref'
  7. const QUERY_PARAMS = ''
  8. const sectionTitles = (sections: ObservabilityMenuSection[]) => sections.map((s) => s.title)
  9. const itemKeys = (section: ObservabilityMenuSection | undefined) =>
  10. section?.items.map((i) => i.key) ?? []
  11. const findSection = (sections: ObservabilityMenuSection[], title: string) =>
  12. sections.find((s) => s.title === title)
  13. describe('generateObservabilityMenuItems - PRODUCT section', () => {
  14. it('includes PRODUCT section on platform', () => {
  15. const menu = generateObservabilityMenuItems({
  16. ref: REF,
  17. preservedQueryParams: QUERY_PARAMS,
  18. showOverview: false,
  19. isSupamonitorEnabled: false,
  20. storageSupported: true,
  21. isPlatform: true,
  22. })
  23. expect(sectionTitles(menu)).toContain('PRODUCT')
  24. const productSection = findSection(menu, 'PRODUCT')
  25. expect(itemKeys(productSection)).toEqual([
  26. 'database',
  27. 'postgrest',
  28. 'auth',
  29. 'edge-functions',
  30. 'storage',
  31. 'realtime',
  32. ])
  33. })
  34. it('excludes PRODUCT section in self-hosted mode', () => {
  35. const menu = generateObservabilityMenuItems({
  36. ref: REF,
  37. preservedQueryParams: QUERY_PARAMS,
  38. showOverview: false,
  39. isSupamonitorEnabled: false,
  40. storageSupported: true,
  41. isPlatform: false,
  42. })
  43. expect(sectionTitles(menu)).not.toContain('PRODUCT')
  44. expect(menu.length).toBe(1) // Only GENERAL section
  45. })
  46. it('excludes Storage from PRODUCT when storageSupported is false', () => {
  47. const menu = generateObservabilityMenuItems({
  48. ref: REF,
  49. preservedQueryParams: QUERY_PARAMS,
  50. showOverview: false,
  51. isSupamonitorEnabled: false,
  52. storageSupported: false,
  53. isPlatform: true,
  54. })
  55. const productSection = findSection(menu, 'PRODUCT')
  56. expect(itemKeys(productSection)).not.toContain('storage')
  57. expect(itemKeys(productSection)).toEqual([
  58. 'database',
  59. 'postgrest',
  60. 'auth',
  61. 'edge-functions',
  62. 'realtime',
  63. ])
  64. })
  65. it('includes Storage in PRODUCT when storageSupported is true', () => {
  66. const menu = generateObservabilityMenuItems({
  67. ref: REF,
  68. preservedQueryParams: QUERY_PARAMS,
  69. showOverview: false,
  70. isSupamonitorEnabled: false,
  71. storageSupported: true,
  72. isPlatform: true,
  73. })
  74. const productSection = findSection(menu, 'PRODUCT')
  75. expect(itemKeys(productSection)).toContain('storage')
  76. })
  77. })
  78. describe('generateObservabilityMenuItems - GENERAL section', () => {
  79. it('always includes GENERAL section', () => {
  80. const menu = generateObservabilityMenuItems({
  81. ref: REF,
  82. preservedQueryParams: QUERY_PARAMS,
  83. showOverview: false,
  84. isSupamonitorEnabled: false,
  85. storageSupported: true,
  86. isPlatform: false,
  87. })
  88. expect(sectionTitles(menu)).toContain('GENERAL')
  89. })
  90. it('includes Query Performance when supamonitor is disabled', () => {
  91. const menu = generateObservabilityMenuItems({
  92. ref: REF,
  93. preservedQueryParams: QUERY_PARAMS,
  94. showOverview: false,
  95. isSupamonitorEnabled: false,
  96. storageSupported: true,
  97. isPlatform: false,
  98. })
  99. const generalSection = findSection(menu, 'GENERAL')
  100. expect(itemKeys(generalSection)).toContain('query-performance')
  101. expect(itemKeys(generalSection)).not.toContain('query-insights')
  102. })
  103. it('includes Query Insights when supamonitor is enabled', () => {
  104. const menu = generateObservabilityMenuItems({
  105. ref: REF,
  106. preservedQueryParams: QUERY_PARAMS,
  107. showOverview: false,
  108. isSupamonitorEnabled: true,
  109. storageSupported: true,
  110. isPlatform: false,
  111. })
  112. const generalSection = findSection(menu, 'GENERAL')
  113. expect(itemKeys(generalSection)).toContain('query-insights')
  114. expect(itemKeys(generalSection)).not.toContain('query-performance')
  115. })
  116. it('includes Overview when showOverview is true', () => {
  117. const menu = generateObservabilityMenuItems({
  118. ref: REF,
  119. preservedQueryParams: QUERY_PARAMS,
  120. showOverview: true,
  121. isSupamonitorEnabled: false,
  122. storageSupported: true,
  123. isPlatform: false,
  124. })
  125. const generalSection = findSection(menu, 'GENERAL')
  126. expect(itemKeys(generalSection)).toContain('observability')
  127. })
  128. it('excludes Overview when showOverview is false', () => {
  129. const menu = generateObservabilityMenuItems({
  130. ref: REF,
  131. preservedQueryParams: QUERY_PARAMS,
  132. showOverview: false,
  133. isSupamonitorEnabled: false,
  134. storageSupported: true,
  135. isPlatform: false,
  136. })
  137. const generalSection = findSection(menu, 'GENERAL')
  138. expect(itemKeys(generalSection)).not.toContain('observability')
  139. })
  140. it('includes API Gateway on platform', () => {
  141. const menu = generateObservabilityMenuItems({
  142. ref: REF,
  143. preservedQueryParams: QUERY_PARAMS,
  144. showOverview: false,
  145. isSupamonitorEnabled: false,
  146. storageSupported: true,
  147. isPlatform: true,
  148. })
  149. const generalSection = findSection(menu, 'GENERAL')
  150. expect(itemKeys(generalSection)).toContain('api-overview')
  151. })
  152. it('excludes API Gateway in self-hosted mode', () => {
  153. const menu = generateObservabilityMenuItems({
  154. ref: REF,
  155. preservedQueryParams: QUERY_PARAMS,
  156. showOverview: false,
  157. isSupamonitorEnabled: false,
  158. storageSupported: true,
  159. isPlatform: false,
  160. })
  161. const generalSection = findSection(menu, 'GENERAL')
  162. expect(itemKeys(generalSection)).not.toContain('api-overview')
  163. })
  164. })
  165. describe('generateObservabilityMenuItems - URL construction', () => {
  166. it('constructs correct URLs with preserved query params', () => {
  167. const params = '?its=2024-01-01&ite=2024-01-31'
  168. const menu = generateObservabilityMenuItems({
  169. ref: REF,
  170. preservedQueryParams: params,
  171. showOverview: false,
  172. isSupamonitorEnabled: false,
  173. storageSupported: true,
  174. isPlatform: true,
  175. })
  176. const generalSection = findSection(menu, 'GENERAL')
  177. const queryPerfItem = generalSection?.items.find((i) => i.key === 'query-performance')
  178. expect(queryPerfItem?.url).toBe(`/project/${REF}/observability/query-performance${params}`)
  179. const productSection = findSection(menu, 'PRODUCT')
  180. const databaseItem = productSection?.items.find((i) => i.key === 'database')
  181. expect(databaseItem?.url).toBe(`/project/${REF}/observability/database${params}`)
  182. })
  183. it('handles undefined ref', () => {
  184. const menu = generateObservabilityMenuItems({
  185. ref: undefined,
  186. preservedQueryParams: QUERY_PARAMS,
  187. showOverview: false,
  188. isSupamonitorEnabled: false,
  189. storageSupported: true,
  190. isPlatform: true,
  191. })
  192. const generalSection = findSection(menu, 'GENERAL')
  193. const queryPerfItem = generalSection?.items.find((i) => i.key === 'query-performance')
  194. expect(queryPerfItem?.url).toBe('/project/undefined/observability/query-performance')
  195. })
  196. })
  197. describe('generateObservabilityMenuItems - complete structure', () => {
  198. it('returns only GENERAL in self-hosted with all standard items', () => {
  199. const menu = generateObservabilityMenuItems({
  200. ref: REF,
  201. preservedQueryParams: QUERY_PARAMS,
  202. showOverview: true,
  203. isSupamonitorEnabled: false,
  204. storageSupported: true,
  205. isPlatform: false,
  206. })
  207. expect(menu.length).toBe(1)
  208. expect(menu[0].title).toBe('GENERAL')
  209. expect(itemKeys(menu[0])).toEqual([
  210. 'observability', // Overview
  211. 'query-performance',
  212. // API Gateway excluded
  213. ])
  214. })
  215. it('returns GENERAL and PRODUCT on platform with all items', () => {
  216. const menu = generateObservabilityMenuItems({
  217. ref: REF,
  218. preservedQueryParams: QUERY_PARAMS,
  219. showOverview: true,
  220. isSupamonitorEnabled: true,
  221. storageSupported: true,
  222. isPlatform: true,
  223. })
  224. expect(menu.length).toBe(2)
  225. expect(sectionTitles(menu)).toEqual(['GENERAL', 'PRODUCT'])
  226. const generalSection = findSection(menu, 'GENERAL')
  227. expect(itemKeys(generalSection)).toEqual([
  228. 'observability', // Overview
  229. 'query-insights', // Supamonitor enabled
  230. 'api-overview', // Platform only
  231. ])
  232. const productSection = findSection(menu, 'PRODUCT')
  233. expect(itemKeys(productSection)).toEqual([
  234. 'database',
  235. 'postgrest',
  236. 'auth',
  237. 'edge-functions',
  238. 'storage',
  239. 'realtime',
  240. ])
  241. })
  242. })