|
| 1 | +import { JSDataType } from '@hyperdx/common-utils/dist/clickhouse'; |
| 2 | +import { Field } from '@hyperdx/common-utils/dist/metadata'; |
| 3 | +import { renderHook } from '@testing-library/react'; |
| 4 | + |
| 5 | +import { LuceneLanguageFormatter } from '../../SearchInputV2'; |
| 6 | +import { useAutoCompleteOptions } from '../useAutoCompleteOptions'; |
| 7 | +import { useAllFields, useGetKeyValues } from '../useMetadata'; |
| 8 | + |
| 9 | +if (!globalThis.structuredClone) { |
| 10 | + globalThis.structuredClone = (obj: any) => { |
| 11 | + return JSON.parse(JSON.stringify(obj)); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +// Mock dependencies |
| 16 | +jest.mock('../useMetadata', () => ({ |
| 17 | + ...jest.requireActual('../useMetadata.tsx'), |
| 18 | + useAllFields: jest.fn(), |
| 19 | + useGetKeyValues: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +const luceneFormatter = new LuceneLanguageFormatter(); |
| 23 | + |
| 24 | +const mockFields: Field[] = [ |
| 25 | + { |
| 26 | + path: ['ResourceAttributes'], |
| 27 | + jsType: JSDataType.Map, |
| 28 | + type: 'map', |
| 29 | + }, |
| 30 | + { |
| 31 | + path: ['ResourceAttributes', 'service.name'], |
| 32 | + jsType: JSDataType.String, |
| 33 | + type: 'string', |
| 34 | + }, |
| 35 | + { |
| 36 | + path: ['TraceAttributes', 'trace.id'], |
| 37 | + jsType: JSDataType.String, |
| 38 | + type: 'string', |
| 39 | + }, |
| 40 | +]; |
| 41 | + |
| 42 | +const mockTableConnections = [ |
| 43 | + { |
| 44 | + databaseName: 'test_db', |
| 45 | + tableName: 'traces', |
| 46 | + connectionId: 'conn1', |
| 47 | + }, |
| 48 | +]; |
| 49 | + |
| 50 | +describe('useAutoCompleteOptions', () => { |
| 51 | + beforeEach(() => { |
| 52 | + // Reset mocks before each test |
| 53 | + jest.clearAllMocks(); |
| 54 | + |
| 55 | + // Setup default mock implementations |
| 56 | + (useAllFields as jest.Mock).mockReturnValue({ |
| 57 | + data: mockFields, |
| 58 | + }); |
| 59 | + |
| 60 | + (useGetKeyValues as jest.Mock).mockReturnValue({ |
| 61 | + data: null, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return field options with correct lucene formatting', () => { |
| 66 | + const { result } = renderHook(() => |
| 67 | + useAutoCompleteOptions(luceneFormatter, 'ResourceAttributes', { |
| 68 | + tableConnections: mockTableConnections, |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + expect(result.current).toEqual([ |
| 73 | + { |
| 74 | + value: 'ResourceAttributes', |
| 75 | + label: 'ResourceAttributes (map)', |
| 76 | + }, |
| 77 | + { |
| 78 | + value: 'ResourceAttributes.service.name', |
| 79 | + label: 'ResourceAttributes.service.name (string)', |
| 80 | + }, |
| 81 | + { |
| 82 | + value: 'TraceAttributes.trace.id', |
| 83 | + label: 'TraceAttributes.trace.id (string)', |
| 84 | + }, |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return key value options with correct lucene formatting', () => { |
| 89 | + const mockKeyValues = [ |
| 90 | + { |
| 91 | + key: 'ResourceAttributes.service.name', |
| 92 | + value: ['frontend', 'backend'], |
| 93 | + }, |
| 94 | + ]; |
| 95 | + |
| 96 | + (useGetKeyValues as jest.Mock).mockReturnValue({ |
| 97 | + data: mockKeyValues, |
| 98 | + }); |
| 99 | + |
| 100 | + const { result } = renderHook(() => |
| 101 | + useAutoCompleteOptions( |
| 102 | + luceneFormatter, |
| 103 | + 'ResourceAttributes.service.name', |
| 104 | + { |
| 105 | + tableConnections: mockTableConnections, |
| 106 | + }, |
| 107 | + ), |
| 108 | + ); |
| 109 | + |
| 110 | + expect(result.current).toEqual([ |
| 111 | + { |
| 112 | + value: 'ResourceAttributes', |
| 113 | + label: 'ResourceAttributes (map)', |
| 114 | + }, |
| 115 | + { |
| 116 | + value: 'ResourceAttributes.service.name', |
| 117 | + label: 'ResourceAttributes.service.name (string)', |
| 118 | + }, |
| 119 | + { |
| 120 | + value: 'TraceAttributes.trace.id', |
| 121 | + label: 'TraceAttributes.trace.id (string)', |
| 122 | + }, |
| 123 | + { |
| 124 | + value: 'ResourceAttributes.service.name:"frontend"', |
| 125 | + label: 'ResourceAttributes.service.name:"frontend"', |
| 126 | + }, |
| 127 | + { |
| 128 | + value: 'ResourceAttributes.service.name:"backend"', |
| 129 | + label: 'ResourceAttributes.service.name:"backend"', |
| 130 | + }, |
| 131 | + ]); |
| 132 | + }); |
| 133 | + |
| 134 | + // TODO: Does this test case need to be removed after HDX-1548? |
| 135 | + it('should handle nested key value options', () => { |
| 136 | + const mockKeyValues = [ |
| 137 | + { |
| 138 | + key: 'ResourceAttributes', |
| 139 | + value: [ |
| 140 | + { |
| 141 | + 'service.name': 'frontend', |
| 142 | + 'deployment.environment': 'production', |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + ]; |
| 147 | + |
| 148 | + (useGetKeyValues as jest.Mock).mockReturnValue({ |
| 149 | + data: mockKeyValues, |
| 150 | + }); |
| 151 | + |
| 152 | + const { result } = renderHook(() => |
| 153 | + useAutoCompleteOptions(luceneFormatter, 'ResourceAttributes', { |
| 154 | + tableConnections: mockTableConnections, |
| 155 | + }), |
| 156 | + ); |
| 157 | + |
| 158 | + //console.log(result.current); |
| 159 | + expect(result.current).toEqual([ |
| 160 | + { |
| 161 | + value: 'ResourceAttributes', |
| 162 | + label: 'ResourceAttributes (map)', |
| 163 | + }, |
| 164 | + { |
| 165 | + value: 'ResourceAttributes.service.name', |
| 166 | + label: 'ResourceAttributes.service.name (string)', |
| 167 | + }, |
| 168 | + { |
| 169 | + value: 'TraceAttributes.trace.id', |
| 170 | + label: 'TraceAttributes.trace.id (string)', |
| 171 | + }, |
| 172 | + { |
| 173 | + value: 'ResourceAttributes.service.name:"frontend"', |
| 174 | + label: 'ResourceAttributes.service.name:"frontend"', |
| 175 | + }, |
| 176 | + { |
| 177 | + value: 'ResourceAttributes.deployment.environment:"production"', |
| 178 | + label: 'ResourceAttributes.deployment.environment:"production"', |
| 179 | + }, |
| 180 | + ]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should handle additional suggestions', () => { |
| 184 | + const { result } = renderHook(() => |
| 185 | + useAutoCompleteOptions(luceneFormatter, 'ResourceAttributes', { |
| 186 | + tableConnections: mockTableConnections, |
| 187 | + additionalSuggestions: ['custom.field'], |
| 188 | + }), |
| 189 | + ); |
| 190 | + |
| 191 | + expect(result.current).toEqual([ |
| 192 | + { |
| 193 | + value: 'ResourceAttributes', |
| 194 | + label: 'ResourceAttributes (map)', |
| 195 | + }, |
| 196 | + { |
| 197 | + value: 'ResourceAttributes.service.name', |
| 198 | + label: 'ResourceAttributes.service.name (string)', |
| 199 | + }, |
| 200 | + { |
| 201 | + value: 'TraceAttributes.trace.id', |
| 202 | + label: 'TraceAttributes.trace.id (string)', |
| 203 | + }, |
| 204 | + { |
| 205 | + value: 'custom.field', |
| 206 | + label: 'custom.field', |
| 207 | + }, |
| 208 | + ]); |
| 209 | + }); |
| 210 | +}); |
0 commit comments