|
| 1 | +// Copyright (c) 2025 coze-dev Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package tracehub |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/coze-dev/coze-loop/backend/modules/observability/domain/trace/entity/loop_span" |
| 10 | + "github.com/coze-dev/coze-loop/backend/pkg/lang/ptr" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestProcessSpecificFilter_StatusFilter(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + filter *loop_span.FilterField |
| 18 | + expectError bool |
| 19 | + validate func(t *testing.T, result *loop_span.FilterField) |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "status filter with success and error - should convert to always true", |
| 23 | + filter: &loop_span.FilterField{ |
| 24 | + FieldName: loop_span.SpanFieldStatus, |
| 25 | + FieldType: loop_span.FieldTypeString, |
| 26 | + QueryType: ptr.Of(loop_span.QueryTypeEnumIn), |
| 27 | + Values: []string{loop_span.SpanStatusSuccess, loop_span.SpanStatusError}, |
| 28 | + }, |
| 29 | + expectError: false, |
| 30 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 31 | + assert.Equal(t, loop_span.SpanFieldStatusCode, result.FieldName) |
| 32 | + assert.Equal(t, loop_span.FieldTypeLong, result.FieldType) |
| 33 | + assert.Equal(t, loop_span.QueryTypeEnumAlwaysTrue, *result.QueryType) |
| 34 | + assert.Nil(t, result.Values) |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "status filter with only success - should convert to value 0", |
| 39 | + filter: &loop_span.FilterField{ |
| 40 | + FieldName: loop_span.SpanFieldStatus, |
| 41 | + FieldType: loop_span.FieldTypeString, |
| 42 | + QueryType: ptr.Of(loop_span.QueryTypeEnumIn), |
| 43 | + Values: []string{loop_span.SpanStatusSuccess}, |
| 44 | + }, |
| 45 | + expectError: false, |
| 46 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 47 | + assert.Equal(t, loop_span.SpanFieldStatusCode, result.FieldName) |
| 48 | + assert.Equal(t, loop_span.FieldTypeLong, result.FieldType) |
| 49 | + assert.Equal(t, loop_span.QueryTypeEnumIn, *result.QueryType) |
| 50 | + assert.Equal(t, []string{"0"}, result.Values) |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "status filter with only error - should convert to not in value 0", |
| 55 | + filter: &loop_span.FilterField{ |
| 56 | + FieldName: loop_span.SpanFieldStatus, |
| 57 | + FieldType: loop_span.FieldTypeString, |
| 58 | + QueryType: ptr.Of(loop_span.QueryTypeEnumIn), |
| 59 | + Values: []string{loop_span.SpanStatusError}, |
| 60 | + }, |
| 61 | + expectError: false, |
| 62 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 63 | + assert.Equal(t, loop_span.SpanFieldStatusCode, result.FieldName) |
| 64 | + assert.Equal(t, loop_span.FieldTypeLong, result.FieldType) |
| 65 | + assert.Equal(t, loop_span.QueryTypeEnumNotIn, *result.QueryType) |
| 66 | + assert.Equal(t, []string{"0"}, result.Values) |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "status filter without in operator - should return error", |
| 71 | + filter: &loop_span.FilterField{ |
| 72 | + FieldName: loop_span.SpanFieldStatus, |
| 73 | + FieldType: loop_span.FieldTypeString, |
| 74 | + QueryType: ptr.Of(loop_span.QueryTypeEnumEq), |
| 75 | + Values: []string{loop_span.SpanStatusSuccess}, |
| 76 | + }, |
| 77 | + expectError: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "status filter with invalid status value - should return error", |
| 81 | + filter: &loop_span.FilterField{ |
| 82 | + FieldName: loop_span.SpanFieldStatus, |
| 83 | + FieldType: loop_span.FieldTypeString, |
| 84 | + QueryType: ptr.Of(loop_span.QueryTypeEnumIn), |
| 85 | + Values: []string{"invalid_status"}, |
| 86 | + }, |
| 87 | + expectError: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "status filter with empty values - should return error", |
| 91 | + filter: &loop_span.FilterField{ |
| 92 | + FieldName: loop_span.SpanFieldStatus, |
| 93 | + FieldType: loop_span.FieldTypeString, |
| 94 | + QueryType: ptr.Of(loop_span.QueryTypeEnumIn), |
| 95 | + Values: []string{}, |
| 96 | + }, |
| 97 | + expectError: true, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + // Make a copy to avoid modifying the original |
| 104 | + filterCopy := *tt.filter |
| 105 | + err := processSpecificFilter(&filterCopy) |
| 106 | + |
| 107 | + if tt.expectError { |
| 108 | + assert.Error(t, err) |
| 109 | + } else { |
| 110 | + assert.NoError(t, err) |
| 111 | + if tt.validate != nil { |
| 112 | + tt.validate(t, &filterCopy) |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestProcessSpecificFilter_LatencyFilter(t *testing.T) { |
| 120 | + tests := []struct { |
| 121 | + name string |
| 122 | + filter *loop_span.FilterField |
| 123 | + expectError bool |
| 124 | + validate func(t *testing.T, result *loop_span.FilterField) |
| 125 | + }{ |
| 126 | + { |
| 127 | + name: "duration filter - should convert ms to us", |
| 128 | + filter: &loop_span.FilterField{ |
| 129 | + FieldName: loop_span.SpanFieldDuration, |
| 130 | + FieldType: loop_span.FieldTypeLong, |
| 131 | + QueryType: ptr.Of(loop_span.QueryTypeEnumGte), |
| 132 | + Values: []string{"100", "200"}, |
| 133 | + }, |
| 134 | + expectError: false, |
| 135 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 136 | + assert.Equal(t, loop_span.SpanFieldDuration, result.FieldName) |
| 137 | + assert.Equal(t, loop_span.FieldTypeLong, result.FieldType) |
| 138 | + assert.Equal(t, loop_span.QueryTypeEnumGte, *result.QueryType) |
| 139 | + assert.Equal(t, []string{"100000", "200000"}, result.Values) // 100ms -> 100000us |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "latency_first_resp filter - should convert ms to us", |
| 144 | + filter: &loop_span.FilterField{ |
| 145 | + FieldName: loop_span.SpanFieldLatencyFirstResp, |
| 146 | + FieldType: loop_span.FieldTypeLong, |
| 147 | + QueryType: ptr.Of(loop_span.QueryTypeEnumLte), |
| 148 | + Values: []string{"50"}, |
| 149 | + }, |
| 150 | + expectError: false, |
| 151 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 152 | + assert.Equal(t, loop_span.SpanFieldLatencyFirstResp, result.FieldName) |
| 153 | + assert.Equal(t, loop_span.FieldTypeLong, result.FieldType) |
| 154 | + assert.Equal(t, loop_span.QueryTypeEnumLte, *result.QueryType) |
| 155 | + assert.Equal(t, []string{"50000"}, result.Values) // 50ms -> 50000us |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + name: "start_time_first_resp filter - should convert ms to us", |
| 160 | + filter: &loop_span.FilterField{ |
| 161 | + FieldName: loop_span.SpanFieldStartTimeFirstResp, |
| 162 | + FieldType: loop_span.FieldTypeLong, |
| 163 | + QueryType: ptr.Of(loop_span.QueryTypeEnumEq), |
| 164 | + Values: []string{"1000"}, |
| 165 | + }, |
| 166 | + expectError: false, |
| 167 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 168 | + assert.Equal(t, loop_span.SpanFieldStartTimeFirstResp, result.FieldName) |
| 169 | + assert.Equal(t, []string{"1000000"}, result.Values) // 1000ms -> 1000000us |
| 170 | + }, |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "start_time_first_token_resp filter - should convert ms to us", |
| 174 | + filter: &loop_span.FilterField{ |
| 175 | + FieldName: loop_span.SpanFieldStartTimeFirstTokenResp, |
| 176 | + FieldType: loop_span.FieldTypeLong, |
| 177 | + QueryType: ptr.Of(loop_span.QueryTypeEnumGt), |
| 178 | + Values: []string{"10"}, |
| 179 | + }, |
| 180 | + expectError: false, |
| 181 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 182 | + assert.Equal(t, []string{"10000"}, result.Values) // 10ms -> 10000us |
| 183 | + }, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "latency_first_token_resp filter - should convert ms to us", |
| 187 | + filter: &loop_span.FilterField{ |
| 188 | + FieldName: loop_span.SpanFieldLatencyFirstTokenResp, |
| 189 | + FieldType: loop_span.FieldTypeLong, |
| 190 | + QueryType: ptr.Of(loop_span.QueryTypeEnumLt), |
| 191 | + Values: []string{"5"}, |
| 192 | + }, |
| 193 | + expectError: false, |
| 194 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 195 | + assert.Equal(t, []string{"5000"}, result.Values) // 5ms -> 5000us |
| 196 | + }, |
| 197 | + }, |
| 198 | + { |
| 199 | + name: "reasoning_duration filter - should convert ms to us", |
| 200 | + filter: &loop_span.FilterField{ |
| 201 | + FieldName: loop_span.SpanFieldReasoningDuration, |
| 202 | + FieldType: loop_span.FieldTypeLong, |
| 203 | + QueryType: ptr.Of(loop_span.QueryTypeEnumGte), |
| 204 | + Values: []string{"30"}, |
| 205 | + }, |
| 206 | + expectError: false, |
| 207 | + validate: func(t *testing.T, result *loop_span.FilterField) { |
| 208 | + assert.Equal(t, []string{"30000"}, result.Values) // 30ms -> 30000us |
| 209 | + }, |
| 210 | + }, |
| 211 | + { |
| 212 | + name: "latency filter with wrong field type - should return error", |
| 213 | + filter: &loop_span.FilterField{ |
| 214 | + FieldName: loop_span.SpanFieldDuration, |
| 215 | + FieldType: loop_span.FieldTypeString, |
| 216 | + QueryType: ptr.Of(loop_span.QueryTypeEnumGte), |
| 217 | + Values: []string{"100"}, |
| 218 | + }, |
| 219 | + expectError: true, |
| 220 | + }, |
| 221 | + { |
| 222 | + name: "latency filter with invalid value - should return error", |
| 223 | + filter: &loop_span.FilterField{ |
| 224 | + FieldName: loop_span.SpanFieldDuration, |
| 225 | + FieldType: loop_span.FieldTypeLong, |
| 226 | + QueryType: ptr.Of(loop_span.QueryTypeEnumGte), |
| 227 | + Values: []string{"invalid"}, |
| 228 | + }, |
| 229 | + expectError: true, |
| 230 | + }, |
| 231 | + } |
| 232 | + |
| 233 | + for _, tt := range tests { |
| 234 | + t.Run(tt.name, func(t *testing.T) { |
| 235 | + // Make a copy to avoid modifying the original |
| 236 | + filterCopy := *tt.filter |
| 237 | + err := processSpecificFilter(&filterCopy) |
| 238 | + |
| 239 | + if tt.expectError { |
| 240 | + assert.Error(t, err) |
| 241 | + } else { |
| 242 | + assert.NoError(t, err) |
| 243 | + if tt.validate != nil { |
| 244 | + tt.validate(t, &filterCopy) |
| 245 | + } |
| 246 | + } |
| 247 | + }) |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +func TestProcessSpecificFilter_UnknownField(t *testing.T) { |
| 252 | + // Test with unknown field name - should not modify the filter |
| 253 | + filter := &loop_span.FilterField{ |
| 254 | + FieldName: "unknown_field", |
| 255 | + FieldType: loop_span.FieldTypeString, |
| 256 | + QueryType: ptr.Of(loop_span.QueryTypeEnumEq), |
| 257 | + Values: []string{"test"}, |
| 258 | + } |
| 259 | + |
| 260 | + original := *filter |
| 261 | + err := processSpecificFilter(filter) |
| 262 | + |
| 263 | + assert.NoError(t, err) |
| 264 | + assert.Equal(t, original.FieldName, filter.FieldName) |
| 265 | + assert.Equal(t, original.FieldType, filter.FieldType) |
| 266 | + assert.Equal(t, original.QueryType, filter.QueryType) |
| 267 | + assert.Equal(t, original.Values, filter.Values) |
| 268 | +} |
| 269 | + |
| 270 | +func TestProcessSpecificFilter_NilFilter(t *testing.T) { |
| 271 | + // Test with nil filter - should not panic |
| 272 | + err := processSpecificFilter(nil) |
| 273 | + assert.NoError(t, err) |
| 274 | +} |
0 commit comments