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
// SPDX-FileCopyrightText: 2026 aptu-coder contributors
// SPDX-License-Identifier: Apache-2.0
use serial_test::serial;
use std::env;
#[test]
#[serial]
fn test_init_otel_no_env_var_returns_none() {
// Arrange: ensure OTEL_EXPORTER_OTLP_ENDPOINT is not set
unsafe {
env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
}
// Act: call init_otel with no env var
let result = aptu_coder::otel::init_otel();
// Assert: should return None (graceful noop when env var unset)
assert!(
result.is_none(),
"init_otel should return None when OTEL_EXPORTER_OTLP_ENDPOINT is unset"
);
}
#[test]
#[serial]
fn test_init_otel_invalid_url_returns_none() {
// Arrange: set env var to an invalid/unreachable URL
unsafe {
env::set_var(
"OTEL_EXPORTER_OTLP_ENDPOINT",
"http://invalid-url-that-does-not-exist:9999",
);
}
// Act: call init_otel with invalid endpoint
let result = aptu_coder::otel::init_otel();
// Assert: opentelemetry-otlp 0.32+ uses lazy connection, so init_otel returns Some(provider)
// even with an invalid URL. The actual connection failure occurs at export time, not init time.
// This test verifies that init_otel doesn't panic and returns a provider.
assert!(
result.is_some(),
"init_otel should return Some(provider) with lazy connection (otel 0.32+)"
);
// Cleanup
unsafe {
env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
}
}
#[test]
#[serial]
fn test_noop_layer_composition_no_panic() {
// Arrange: ensure OTEL_EXPORTER_OTLP_ENDPOINT is not set
unsafe {
env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
}
// Act: call init_otel (returns None) and verify no panic on layer composition
let otel_provider = aptu_coder::otel::init_otel();
assert!(
otel_provider.is_none(),
"init_otel should return None when env var unset"
);
// Verify that composing a noop layer doesn't panic
// This is a compile-time check that the types work correctly
// The actual layer composition happens in main.rs, but we verify the provider
// can be used in the conditional logic without panicking
if let Some(_provider) = otel_provider {
panic!("Should not reach here");
}
// Assert: test passes if we get here without panic
}
#[test]
#[serial]
fn test_log_appender_no_env_var_returns_none() {
// Arrange: ensure OTEL_EXPORTER_OTLP_ENDPOINT is not set
unsafe {
env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
}
// Act: call init_log_appender with no env var
let result = aptu_coder::otel::init_log_appender();
// Assert: should return None (graceful noop when env var unset)
assert!(
result.is_none(),
"init_log_appender should return None when OTEL_EXPORTER_OTLP_ENDPOINT is unset"
);
}
#[test]
#[serial]
fn test_traceparent_malformed_no_panic() {
// Arrange: malformed traceparent (wrong format, not a valid W3C trace-context header)
let mut meta_map = serde_json::Map::new();
meta_map.insert(
"traceparent".to_string(),
serde_json::Value::String("not-a-valid-traceparent".to_string()),
);
let meta = rmcp::model::Meta(meta_map);
// Act: call the real extraction function -- must not panic regardless of input
aptu_coder::extract_and_set_trace_context(
Some(&meta),
aptu_coder::ClientMetadata {
session_id: None,
client_name: None,
client_version: None,
},
);
}
#[test]
#[serial]
fn test_traceparent_missing_meta_no_panic() {
// Act: None meta must be handled silently
aptu_coder::extract_and_set_trace_context(
None,
aptu_coder::ClientMetadata {
session_id: None,
client_name: None,
client_version: None,
},
);
}
#[test]
#[serial]
fn test_metrics_histogram_no_env_var() {
// Arrange: ensure OTEL_EXPORTER_OTLP_ENDPOINT is not set
unsafe {
env::remove_var("OTEL_EXPORTER_OTLP_ENDPOINT");
}
// Act: call init_meter with no env var
let result = aptu_coder::otel::init_meter();
// Assert: should return None (graceful noop when env var unset)
assert!(
result.is_none(),
"init_meter should return None when OTEL_EXPORTER_OTLP_ENDPOINT is unset"
);
}