use std::collections::HashMap;
use allframe_core::otel::{
current_trace_id, extract_context, get_baggage, inject_context, set_baggage, start_trace,
traced, SpanContext,
};
#[tokio::test]
async fn test_context_propagation_through_di() {
#[traced]
async fn operation() -> Result<String, String> {
Ok(current_trace_id())
}
let result = operation().await.unwrap();
assert!(!result.is_empty());
}
#[tokio::test]
async fn test_trace_id_consistency() {
start_trace("test-trace-123");
#[traced]
async fn operation1() -> Result<String, String> {
Ok(current_trace_id())
}
#[traced]
async fn operation2() -> Result<String, String> {
Ok(current_trace_id())
}
let trace1 = operation1().await.unwrap();
let trace2 = operation2().await.unwrap();
assert_eq!(trace1, trace2);
}
#[tokio::test]
async fn test_baggage_propagation() {
#[traced]
async fn outer_operation() -> Result<Option<String>, String> {
set_baggage("user_id", "123");
inner_operation().await
}
#[traced]
async fn inner_operation() -> Result<Option<String>, String> {
Ok(get_baggage("user_id"))
}
let result = outer_operation().await.unwrap();
assert!(result.is_none());
}
#[tokio::test]
async fn test_distributed_tracing() {
let span_context = SpanContext::new("trace-123", "span-456");
let headers = inject_context(&span_context);
assert!(headers.contains_key("traceparent"));
let extracted = extract_context(&headers);
assert!(extracted.is_some());
let ctx = extracted.unwrap();
assert!(!ctx.trace_id.is_empty());
}
#[tokio::test]
async fn test_context_extraction_from_headers() {
let mut headers = HashMap::new();
headers.insert(
"traceparent".to_string(),
"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01".to_string(),
);
let context = extract_context(&headers);
assert!(context.is_some());
let ctx = context.unwrap();
assert!(!ctx.trace_id.is_empty());
assert!(!ctx.parent_span_id.is_empty());
}