use pretty_assertions::assert_eq;
use std::time::{Duration, Instant};
use super::{McpInFlightCalls, McpRouteError};
#[test]
fn registration_is_withdrawn_when_the_call_ends() {
let calls = McpInFlightCalls::new();
let (registration, _questions) = calls.register();
assert!(calls.sole_caller().is_ok());
drop(registration);
assert_eq!(
calls.sole_caller().err(),
Some(McpRouteError::NoCallInFlight)
);
}
#[test]
fn concurrent_calls_are_withdrawn_independently() {
let calls = McpInFlightCalls::new();
let (first, _first_questions) = calls.register();
let (second, _second_questions) = calls.register();
assert_eq!(
calls.sole_caller().err(),
Some(McpRouteError::AmbiguousCall { in_flight: 2 })
);
drop(first);
assert_eq!(
calls.sole_caller().err(),
Some(McpRouteError::AttributionUncertain)
);
drop(second);
assert_eq!(
calls.sole_caller().err(),
Some(McpRouteError::NoCallInFlight)
);
}
#[test]
fn sequential_calls_fail_closed_and_cancel_on_release() {
let calls = McpInFlightCalls::new();
let (first, _first_questions) = calls.register();
let first_caller = calls.sole_caller().unwrap();
assert!(!first_caller.cancellation().is_cancelled());
drop(first);
assert!(first_caller.cancellation().is_cancelled());
let (_second, _second_questions) = calls.register();
assert_eq!(
calls.sole_caller().err(),
Some(McpRouteError::AttributionUncertain)
);
}
#[test]
fn attribution_recovers_after_grace() {
let calls = McpInFlightCalls::new();
let (first, _first_questions) = calls.register();
drop(first);
let (_second, _second_questions) = calls.register();
calls.set_last_release_at_for_test(Instant::now() - Duration::from_secs(6));
assert!(calls.sole_caller().is_ok());
}