|
| 1 | +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; |
| 2 | + |
| 3 | +import { resolveSourceIds } from '@/KubernetesDashboardPage'; |
| 4 | + |
| 5 | +describe('resolveSourceIds', () => { |
| 6 | + const mockLogSource: TSource = { |
| 7 | + id: 'log-1', |
| 8 | + name: 'Log Source 1', |
| 9 | + kind: SourceKind.Log, |
| 10 | + connection: 'connection-1', |
| 11 | + // Add minimal required fields for TSource |
| 12 | + } as TSource; |
| 13 | + |
| 14 | + const mockMetricSource: TSource = { |
| 15 | + id: 'metric-1', |
| 16 | + name: 'Metric Source 1', |
| 17 | + kind: SourceKind.Metric, |
| 18 | + connection: 'connection-1', |
| 19 | + // Add minimal required fields for TSource |
| 20 | + } as TSource; |
| 21 | + |
| 22 | + const mockLogSource2: TSource = { |
| 23 | + id: 'log-2', |
| 24 | + name: 'Log Source 2', |
| 25 | + kind: SourceKind.Log, |
| 26 | + connection: 'connection-2', |
| 27 | + } as TSource; |
| 28 | + |
| 29 | + const mockMetricSource2: TSource = { |
| 30 | + id: 'metric-2', |
| 31 | + name: 'Metric Source 2', |
| 32 | + kind: SourceKind.Metric, |
| 33 | + connection: 'connection-2', |
| 34 | + } as TSource; |
| 35 | + |
| 36 | + describe('when both source IDs are provided', () => { |
| 37 | + it('should return both source IDs as-is', () => { |
| 38 | + const result = resolveSourceIds('log-1', 'metric-1', [ |
| 39 | + mockLogSource, |
| 40 | + mockMetricSource, |
| 41 | + ]); |
| 42 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return both source IDs even if sources array is undefined', () => { |
| 46 | + const result = resolveSourceIds('log-1', 'metric-1', undefined); |
| 47 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return both source IDs even if they are not in the sources array', () => { |
| 51 | + const result = resolveSourceIds('log-999', 'metric-999', [ |
| 52 | + mockLogSource, |
| 53 | + mockMetricSource, |
| 54 | + ]); |
| 55 | + expect(result).toEqual(['log-999', 'metric-999']); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('when only log source ID is provided', () => { |
| 60 | + it('should find metric source from the same connection', () => { |
| 61 | + const sources = [ |
| 62 | + mockLogSource, |
| 63 | + mockMetricSource, |
| 64 | + mockMetricSource2, |
| 65 | + mockLogSource2, |
| 66 | + ]; |
| 67 | + const result = resolveSourceIds('log-2', null, sources); |
| 68 | + expect(result).toEqual(['log-2', 'metric-2']); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should return undefined for metric source if no matching connection', () => { |
| 72 | + const sources = [mockLogSource, mockMetricSource2]; |
| 73 | + const result = resolveSourceIds('log-1', null, sources); |
| 74 | + expect(result).toEqual(['log-1', undefined]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return undefined for metric source if log source not found', () => { |
| 78 | + const sources = [mockLogSource, mockMetricSource]; |
| 79 | + const result = resolveSourceIds('log-999', null, sources); |
| 80 | + expect(result).toEqual(['log-999', undefined]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return log source ID and undefined if sources array is undefined', () => { |
| 84 | + const result = resolveSourceIds('log-1', null, undefined); |
| 85 | + expect(result).toEqual(['log-1', null]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle undefined metric source ID', () => { |
| 89 | + const sources = [mockLogSource, mockMetricSource]; |
| 90 | + const result = resolveSourceIds('log-1', undefined, sources); |
| 91 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('when only metric source ID is provided', () => { |
| 96 | + it('should find log source from the same connection', () => { |
| 97 | + const sources = [mockLogSource, mockMetricSource]; |
| 98 | + const result = resolveSourceIds(null, 'metric-1', sources); |
| 99 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return undefined for log source if no matching connection', () => { |
| 103 | + const sources = [mockLogSource2, mockMetricSource]; |
| 104 | + const result = resolveSourceIds(null, 'metric-1', sources); |
| 105 | + expect(result).toEqual([undefined, 'metric-1']); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return undefined for log source if metric source not found', () => { |
| 109 | + const sources = [mockLogSource, mockMetricSource]; |
| 110 | + const result = resolveSourceIds(null, 'metric-999', sources); |
| 111 | + expect(result).toEqual([undefined, 'metric-999']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should return undefined and metric source ID if sources array is undefined', () => { |
| 115 | + const result = resolveSourceIds(null, 'metric-1', undefined); |
| 116 | + expect(result).toEqual([null, 'metric-1']); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle undefined log source ID', () => { |
| 120 | + const sources = [mockLogSource, mockMetricSource]; |
| 121 | + const result = resolveSourceIds(undefined, 'metric-1', sources); |
| 122 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('when neither source ID is provided', () => { |
| 127 | + it('should find log and metric sources from the same connection', () => { |
| 128 | + const sources = [ |
| 129 | + mockLogSource, |
| 130 | + mockMetricSource, |
| 131 | + mockLogSource2, |
| 132 | + mockMetricSource2, |
| 133 | + ]; |
| 134 | + const result = resolveSourceIds(null, null, sources); |
| 135 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return sources from the connection with both source kinds', () => { |
| 139 | + const sources = [mockLogSource2, mockLogSource, mockMetricSource]; |
| 140 | + const result = resolveSourceIds(null, null, sources); |
| 141 | + expect(result).toEqual(['log-1', 'metric-1']); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle connection with only one source kind', () => { |
| 145 | + const sources = [mockLogSource, mockMetricSource2]; |
| 146 | + const result = resolveSourceIds(null, null, sources); |
| 147 | + expect(result).toEqual([undefined, undefined]); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should return [null, null] if sources array is undefined', () => { |
| 151 | + const result = resolveSourceIds(null, null, undefined); |
| 152 | + expect(result).toEqual([null, null]); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return [undefined, undefined] if sources array is empty', () => { |
| 156 | + const result = resolveSourceIds(null, null, []); |
| 157 | + expect(result).toEqual([undefined, undefined]); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should return [undefined, undefined] if no connection has both kinds', () => { |
| 161 | + const sources = [mockLogSource]; |
| 162 | + const result = resolveSourceIds(null, null, sources); |
| 163 | + expect(result).toEqual([undefined, undefined]); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('edge cases', () => { |
| 168 | + it('should handle multiple sources on the same connection', () => { |
| 169 | + const logSource3: TSource = { |
| 170 | + id: 'log-3', |
| 171 | + name: 'Log Source 3', |
| 172 | + kind: SourceKind.Log, |
| 173 | + connection: 'connection-1', |
| 174 | + } as TSource; |
| 175 | + const metricSource3: TSource = { |
| 176 | + id: 'metric-3', |
| 177 | + name: 'Metric Source 3', |
| 178 | + kind: SourceKind.Metric, |
| 179 | + connection: 'connection-1', |
| 180 | + } as TSource; |
| 181 | + const sources = [ |
| 182 | + mockLogSource, |
| 183 | + logSource3, |
| 184 | + mockMetricSource, |
| 185 | + metricSource3, |
| 186 | + ]; |
| 187 | + |
| 188 | + // When log source is specified, should find first metric on same connection |
| 189 | + const result1 = resolveSourceIds('log-1', null, sources); |
| 190 | + expect(result1).toEqual(['log-1', 'metric-1']); |
| 191 | + |
| 192 | + // When metric source is specified, should find first log on same connection |
| 193 | + const result2 = resolveSourceIds(null, 'metric-3', sources); |
| 194 | + expect(result2).toEqual(['log-1', 'metric-3']); |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments