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