use std::time::Duration;
use super::support::stdio;
use crate::mcp::REMOTE_UNSUPPORTED;
use crate::mcp::spawn::{HANDSHAKE_TIMEOUT, connect};
#[test]
fn handshake_timeout_is_ten_seconds() {
assert_eq!(HANDSHAKE_TIMEOUT, Duration::from_secs(10));
}
#[tokio::test]
async fn a_remote_transport_is_refused_with_a_reason() {
let mut server = stdio("remote", "unused", &[]);
server.transport = trusty_mcp::config::McpTransport::Http {
url: "https://example.invalid/mcp".to_string(),
headers: std::collections::BTreeMap::new(),
};
let reason = connect(&server).await.err().expect("must refuse");
assert_eq!(reason, REMOTE_UNSUPPORTED);
}
#[tokio::test]
async fn a_missing_binary_is_refused_with_a_reason() {
let server = stdio("ghost", "/nonexistent/mcp/binary/xyzzy-5428", &[]);
let reason = connect(&server).await.err().expect("must refuse");
assert!(
reason.contains("xyzzy-5428"),
"the reason names the command: {reason}",
);
}
#[tokio::test]
#[cfg(unix)]
async fn a_server_that_speaks_no_mcp_is_refused_with_a_reason() {
let server = stdio("parrot", "/bin/cat", &[]);
let reason = connect(&server).await.err().expect("must refuse");
assert!(
reason.contains("handshake"),
"the reason says what stage failed: {reason}",
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_wrapper_grandchild_dies_with_the_client() {
let dir = tempfile::tempdir().expect("tempdir");
let pidfile = dir.path().join("grandchild.pid");
let script = format!(
"sleep 300 & echo $! > '{}'; exec cat > /dev/null",
pidfile.display()
);
let server = stdio("wrapper", "/bin/sh", &["-c", &script]);
let outcome = crate::mcp::spawn::connect_within(&server, Duration::from_secs(1)).await;
assert!(outcome.is_err(), "the fixture never answers the handshake");
let pid: libc::pid_t = std::fs::read_to_string(&pidfile)
.expect("the wrapper recorded its grandchild")
.trim()
.parse()
.expect("a pid");
let alive = || unsafe { libc::kill(pid, 0) } == 0;
let deadline = std::time::Instant::now() + Duration::from_secs(5);
while alive() && std::time::Instant::now() < deadline {
tokio::time::sleep(Duration::from_millis(50)).await;
}
if alive() {
unsafe { libc::kill(pid, libc::SIGKILL) };
panic!("the grandchild {pid} outlived the client");
}
}