use netscli_core::trace::trace_route;
#[tokio::test]
async fn a_trace_returns_a_result_or_a_clean_error_never_a_panic() {
let outcome = trace_route("127.0.0.1", 3, false, None).await;
match outcome {
Ok(result) => {
assert_eq!(result.host, "127.0.0.1", "the result must name its target");
assert!(
!result.tool.is_empty(),
"the result must say what produced it"
);
}
Err(error) => {
let message = error.to_string();
assert!(!message.is_empty(), "an error must explain itself");
}
}
}
#[tokio::test]
async fn trace_output_carries_no_terminal_control_sequences() {
let Ok(result) = trace_route("127.0.0.1", 3, false, None).await else {
return;
};
for line in &result.lines {
assert!(
!line.chars().any(|c| c.is_control() && c != '\t'),
"control character in trace output: {line:?}"
);
}
}
#[tokio::test]
async fn a_trace_result_serializes_with_the_fields_downstream_reads() {
let Ok(result) = trace_route("127.0.0.1", 2, false, None).await else {
return;
};
let value = serde_json::to_value(&result).expect("TraceResult serializes");
for key in ["host", "tool", "lines"] {
assert!(value.get(key).is_some(), "missing {key}: {value}");
}
assert!(value["lines"].is_array(), "lines must stay an array");
}
#[tokio::test]
async fn an_unresolvable_host_fails_instead_of_hanging() {
let started = std::time::Instant::now();
let _ = trace_route("no-such-host.invalid", 2, false, None).await;
assert!(
started.elapsed().as_secs() < 60,
"an unresolvable target should not hang: {:?}",
started.elapsed()
);
}