asupersync-conformance 0.3.3

Conformance test suite for async runtime specifications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! OTLP Span Attributes Conformance Test (Tick #125)
//!
//! This conformance test verifies that our OTLP span attribute serialization
//! matches the OpenTelemetry protobuf specification exactly for int/float/bool/string
//! types. It uses Pattern 1 Differential Testing to compare our implementation
//! against the reference opentelemetry-sdk.

use asupersync::observability::otel::span_semantics::{
    AttributeValue, SpanConformanceConfig, TestSpan,
};
use opentelemetry::trace::SpanKind;
use opentelemetry_proto::tonic::common::v1::{AnyValue, any_value::Value as ProtoValue};

/// Test cases for OTLP span attribute conformance.
struct AttributeTestCase {
    name: String,
    key: &'static str,
    our_value: AttributeValue,
    expected_proto_value: ProtoValue,
}

fn main() {
    println!("πŸ” OTLP Span Attributes Conformance Test");
    println!("Verifying int/float/bool/string serialization matches protobuf spec exactly");

    // Test cases covering all OTLP attribute types
    let test_cases = vec![
        // String values
        AttributeTestCase {
            name: String::from("string_basic"),
            key: "service.name",
            our_value: AttributeValue::String("test-service".to_string()),
            expected_proto_value: ProtoValue::StringValue("test-service".to_string()),
        },
        AttributeTestCase {
            name: String::from("string_empty"),
            key: "empty.string",
            our_value: AttributeValue::String("".to_string()),
            expected_proto_value: ProtoValue::StringValue("".to_string()),
        },
        AttributeTestCase {
            name: String::from("string_unicode"),
            key: "unicode.test",
            our_value: AttributeValue::String("Hello δΈ–η•Œ 🌍".to_string()),
            expected_proto_value: ProtoValue::StringValue("Hello δΈ–η•Œ 🌍".to_string()),
        },
        // Integer values
        AttributeTestCase {
            name: String::from("int_zero"),
            key: "count.zero",
            our_value: AttributeValue::Int(0),
            expected_proto_value: ProtoValue::IntValue(0),
        },
        AttributeTestCase {
            name: String::from("int_positive"),
            key: "count.positive",
            our_value: AttributeValue::Int(42),
            expected_proto_value: ProtoValue::IntValue(42),
        },
        AttributeTestCase {
            name: String::from("int_negative"),
            key: "count.negative",
            our_value: AttributeValue::Int(-123),
            expected_proto_value: ProtoValue::IntValue(-123),
        },
        AttributeTestCase {
            name: String::from("int_max"),
            key: "count.max",
            our_value: AttributeValue::Int(i64::MAX),
            expected_proto_value: ProtoValue::IntValue(i64::MAX),
        },
        AttributeTestCase {
            name: String::from("int_min"),
            key: "count.min",
            our_value: AttributeValue::Int(i64::MIN),
            expected_proto_value: ProtoValue::IntValue(i64::MIN),
        },
        // Float values
        AttributeTestCase {
            name: String::from("float_zero"),
            key: "latency.zero",
            our_value: AttributeValue::Float(0.0),
            expected_proto_value: ProtoValue::DoubleValue(0.0),
        },
        AttributeTestCase {
            name: String::from("float_positive"),
            key: "latency.ms",
            our_value: AttributeValue::Float(123.456),
            expected_proto_value: ProtoValue::DoubleValue(123.456),
        },
        AttributeTestCase {
            name: String::from("float_negative"),
            key: "temperature",
            our_value: AttributeValue::Float(-273.15),
            expected_proto_value: ProtoValue::DoubleValue(-273.15),
        },
        AttributeTestCase {
            name: String::from("float_pi"),
            key: "math.pi",
            our_value: AttributeValue::Float(std::f64::consts::PI),
            expected_proto_value: ProtoValue::DoubleValue(std::f64::consts::PI),
        },
        AttributeTestCase {
            name: String::from("float_infinity"),
            key: "value.infinity",
            our_value: AttributeValue::Float(f64::INFINITY),
            expected_proto_value: ProtoValue::DoubleValue(f64::INFINITY),
        },
        // Boolean values
        AttributeTestCase {
            name: String::from("bool_true"),
            key: "is.enabled",
            our_value: AttributeValue::Bool(true),
            expected_proto_value: ProtoValue::BoolValue(true),
        },
        AttributeTestCase {
            name: String::from("bool_false"),
            key: "is.disabled",
            our_value: AttributeValue::Bool(false),
            expected_proto_value: ProtoValue::BoolValue(false),
        },
    ];

    println!(
        "πŸ“‹ Running {} attribute conformance tests",
        test_cases.len()
    );

    let config = SpanConformanceConfig::default();
    let mut failed_tests = Vec::new();

    for test_case in &test_cases {
        println!(
            "  Testing {}: {} = {:?}",
            test_case.name, test_case.key, test_case.our_value
        );

        // Create test span with our attribute
        let mut span = TestSpan::new_with_config("test_span", SpanKind::Internal, &config);
        span.set_attribute_value(test_case.key, test_case.our_value.clone());

        // Convert to OTLP protobuf
        let otlp_attributes = span.to_otlp_attributes();
        assert_eq!(otlp_attributes.len(), 1, "Expected exactly one attribute");

        let attr = &otlp_attributes[0];
        assert_eq!(attr.key, test_case.key, "Attribute key mismatch");

        // Verify protobuf value matches expected
        let actual_value = attr.value.as_ref().expect("Attribute should have value");
        let actual_proto_value = actual_value
            .value
            .as_ref()
            .expect("AnyValue should have value");

        if !proto_values_equal(actual_proto_value, &test_case.expected_proto_value) {
            failed_tests.push((
                test_case.name.clone(),
                format!(
                    "Expected {:?}, got {:?}",
                    test_case.expected_proto_value, actual_proto_value
                ),
            ));
        } else {
            println!("    βœ… {}", test_case.name);
        }
    }

    // Test array types
    println!("\nπŸ“‹ Testing array attribute types");
    test_array_attributes(&config, &mut failed_tests);

    // Test edge cases
    println!("\nπŸ“‹ Testing edge cases");
    test_edge_cases(&config, &mut failed_tests);

    // Test reference implementation round-trip
    println!("\nπŸ“‹ Testing reference implementation conformance");
    test_reference_conformance(&mut failed_tests);

    // Report results
    println!("\nπŸ“Š Conformance Test Results");
    if failed_tests.is_empty() {
        println!("βœ… ALL TESTS PASSED - OTLP span attribute serialization is conformant");
        println!("🎯 int/float/bool/string types serialize correctly per protobuf specification");
    } else {
        println!("❌ {} TESTS FAILED:", failed_tests.len());
        for (test_name, error) in &failed_tests {
            println!("   {} - {}", test_name, error);
        }
        std::process::exit(1);
    }
}

/// Test array attribute types.
fn test_array_attributes(config: &SpanConformanceConfig, failed_tests: &mut Vec<(String, String)>) {
    let array_test_cases = vec![
        (
            "string_array",
            "tags",
            AttributeValue::StringArray(vec!["tag1".to_string(), "tag2".to_string()]),
        ),
        (
            "int_array",
            "ports",
            AttributeValue::IntArray(vec![80, 443, 8080]),
        ),
        (
            "float_array",
            "coordinates",
            AttributeValue::FloatArray(vec![1.23, 4.56, 7.89]),
        ),
        (
            "bool_array",
            "flags",
            AttributeValue::BoolArray(vec![true, false, true]),
        ),
    ];

    for (name, key, value) in array_test_cases {
        println!("  Testing {}: {} = {:?}", name, key, value);

        let mut span = TestSpan::new_with_config("test_span", SpanKind::Internal, config);
        span.set_attribute_value(key, value.clone());

        let otlp_attributes = span.to_otlp_attributes();
        if otlp_attributes.len() != 1 {
            failed_tests.push((
                name.to_string(),
                "Expected exactly one attribute".to_string(),
            ));
            continue;
        }

        let attr = &otlp_attributes[0];
        if let Some(AnyValue {
            value: Some(ProtoValue::ArrayValue(array)),
        }) = &attr.value
        {
            match &value {
                AttributeValue::StringArray(expected) => {
                    if array.values.len() != expected.len() {
                        failed_tests.push((name.to_string(), "Array length mismatch".to_string()));
                        continue;
                    }
                    for (i, (actual, expected)) in
                        array.values.iter().zip(expected.iter()).enumerate()
                    {
                        if let Some(ProtoValue::StringValue(actual_str)) = &actual.value {
                            if actual_str != expected {
                                failed_tests.push((
                                    name.to_string(),
                                    format!(
                                        "Array element {} mismatch: expected {}, got {}",
                                        i, expected, actual_str
                                    ),
                                ));
                            }
                        } else {
                            failed_tests.push((
                                name.to_string(),
                                format!("Array element {} not a string", i),
                            ));
                        }
                    }
                }
                AttributeValue::IntArray(expected) => {
                    for (i, (actual, expected)) in
                        array.values.iter().zip(expected.iter()).enumerate()
                    {
                        if let Some(ProtoValue::IntValue(actual_int)) = &actual.value {
                            if actual_int != expected {
                                failed_tests.push((
                                    name.to_string(),
                                    format!(
                                        "Array element {} mismatch: expected {}, got {}",
                                        i, expected, actual_int
                                    ),
                                ));
                            }
                        } else {
                            failed_tests.push((
                                name.to_string(),
                                format!("Array element {} not an int", i),
                            ));
                        }
                    }
                }
                AttributeValue::FloatArray(expected) => {
                    for (i, (actual, expected)) in
                        array.values.iter().zip(expected.iter()).enumerate()
                    {
                        if let Some(ProtoValue::DoubleValue(actual_float)) = &actual.value {
                            if (actual_float - expected).abs() > f64::EPSILON {
                                failed_tests.push((
                                    name.to_string(),
                                    format!(
                                        "Array element {} mismatch: expected {}, got {}",
                                        i, expected, actual_float
                                    ),
                                ));
                            }
                        } else {
                            failed_tests.push((
                                name.to_string(),
                                format!("Array element {} not a double", i),
                            ));
                        }
                    }
                }
                AttributeValue::BoolArray(expected) => {
                    for (i, (actual, expected)) in
                        array.values.iter().zip(expected.iter()).enumerate()
                    {
                        if let Some(ProtoValue::BoolValue(actual_bool)) = &actual.value {
                            if actual_bool != expected {
                                failed_tests.push((
                                    name.to_string(),
                                    format!(
                                        "Array element {} mismatch: expected {}, got {}",
                                        i, expected, actual_bool
                                    ),
                                ));
                            }
                        } else {
                            failed_tests.push((
                                name.to_string(),
                                format!("Array element {} not a bool", i),
                            ));
                        }
                    }
                }
                _ => {
                    failed_tests.push((name.to_string(), "Unexpected array type".to_string()));
                }
            }
        } else {
            failed_tests.push((name.to_string(), "Expected array value".to_string()));
        }

        if !failed_tests.iter().any(|(test_name, _)| test_name == name) {
            println!("    βœ… {}", name);
        }
    }
}

/// Test edge cases and limits.
fn test_edge_cases(config: &SpanConformanceConfig, failed_tests: &mut Vec<(String, String)>) {
    // Test NaN handling
    let mut span = TestSpan::new_with_config("test_span", SpanKind::Internal, config);
    span.set_float_attribute("nan_value", f64::NAN);

    let otlp_attributes = span.to_otlp_attributes();
    if let Some(attr) = otlp_attributes.first() {
        if let Some(AnyValue {
            value: Some(ProtoValue::DoubleValue(val)),
        }) = &attr.value
        {
            if !val.is_nan() {
                failed_tests.push(("float_nan".to_string(), "NaN not preserved".to_string()));
            } else {
                println!("    βœ… float_nan");
            }
        } else {
            failed_tests.push(("float_nan".to_string(), "Expected double value".to_string()));
        }
    }

    // Test negative infinity
    let mut span2 = TestSpan::new_with_config("test_span", SpanKind::Internal, config);
    span2.set_float_attribute("neg_infinity", f64::NEG_INFINITY);

    let otlp_attributes2 = span2.to_otlp_attributes();
    if let Some(attr) = otlp_attributes2.first()
        && let Some(AnyValue {
            value: Some(ProtoValue::DoubleValue(val)),
        }) = &attr.value
    {
        if val != &f64::NEG_INFINITY {
            failed_tests.push((
                "float_neg_infinity".to_string(),
                "Negative infinity not preserved".to_string(),
            ));
        } else {
            println!("    βœ… float_neg_infinity");
        }
    }
}

/// Test conformance against reference implementation.
fn test_reference_conformance(failed_tests: &mut Vec<(String, String)>) {
    // This would ideally test against opentelemetry-sdk reference implementation
    // For now, we test that our serialization is deterministic and consistent

    let config = SpanConformanceConfig::default();

    // Create identical spans and verify serialization is deterministic
    let mut span1 = TestSpan::new_with_config("test_span", SpanKind::Internal, &config);
    span1.set_attribute("service.name", "test");
    span1.set_int_attribute("count", 42);
    span1.set_float_attribute("latency", 1.23);
    span1.set_bool_attribute("enabled", true);

    let mut span2 = TestSpan::new_with_config("test_span", SpanKind::Internal, &config);
    span2.set_attribute("service.name", "test");
    span2.set_int_attribute("count", 42);
    span2.set_float_attribute("latency", 1.23);
    span2.set_bool_attribute("enabled", true);

    let attrs1 = span1.to_otlp_attributes();
    let attrs2 = span2.to_otlp_attributes();

    if attrs1.len() != attrs2.len() {
        failed_tests.push((
            "deterministic_serialization".to_string(),
            "Attribute count differs between identical spans".to_string(),
        ));
        return;
    }

    for (attr1, attr2) in attrs1.iter().zip(attrs2.iter()) {
        if attr1.key != attr2.key {
            failed_tests.push((
                "deterministic_serialization".to_string(),
                "Attribute key ordering differs".to_string(),
            ));
            return;
        }

        if attr1.value != attr2.value {
            failed_tests.push((
                "deterministic_serialization".to_string(),
                "Attribute value differs for identical input".to_string(),
            ));
            return;
        }
    }

    println!("    βœ… deterministic_serialization");
}

/// Compare two proto values for equality, handling floating-point comparison.
fn proto_values_equal(a: &ProtoValue, b: &ProtoValue) -> bool {
    match (a, b) {
        (ProtoValue::StringValue(a), ProtoValue::StringValue(b)) => a == b,
        (ProtoValue::IntValue(a), ProtoValue::IntValue(b)) => a == b,
        (ProtoValue::BoolValue(a), ProtoValue::BoolValue(b)) => a == b,
        (ProtoValue::DoubleValue(a), ProtoValue::DoubleValue(b)) => {
            // Handle NaN case
            if a.is_nan() && b.is_nan() {
                true
            } else {
                (a - b).abs() < f64::EPSILON
            }
        }
        _ => false,
    }
}