use crate::adapters::secondary::transport::connect::{ConnectError, Spoke, connect};
use crate::application::client::ClientConfig;
use crate::domain::{ProtocolVersion, TransportKind};
fn config() -> ClientConfig {
ClientConfig::new(ProtocolVersion::V2025_06_18, TransportKind::StreamableHttp)
}
#[tokio::test]
async fn a_url_that_is_not_a_url_is_refused_as_a_url() -> Result<(), TestError> {
let error = expect_err(connect("not a url at all", &config()).await)?;
let ConnectError::Http(_) = &error else {
return Err(TestError::WrongShape { got: error });
};
Ok(())
}
#[tokio::test]
async fn a_url_nothing_is_listening_on_is_refused_as_a_detection_failure() -> Result<(), TestError>
{
let error = expect_err(connect("http://127.0.0.1:0/mcp", &config()).await)?;
let ConnectError::Detect(_) = &error else {
return Err(TestError::WrongShape { got: error });
};
Ok(())
}
#[tokio::test]
async fn a_client_offering_no_revision_is_refused_before_anything_is_sent() -> Result<(), TestError>
{
let config = ClientConfig::speaking(
[ProtocolVersion::V2025_06_18],
TransportKind::StreamableHttp,
)?;
assert_eq!(config.versions().len(), 1);
let error = expect_err(connect("not a url at all", &config).await)?;
let ConnectError::Http(_) = &error else {
return Err(TestError::WrongShape { got: error });
};
Ok(())
}
#[test]
fn the_transport_names_are_stable() {
assert_eq!(Spoke::StreamableHttp.as_str(), "streamable-http");
assert_eq!(Spoke::LegacyHttpSse.as_str(), "legacy-http-sse");
}
#[test]
fn the_two_transports_are_distinguishable() {
assert_ne!(Spoke::StreamableHttp, Spoke::LegacyHttpSse);
}
fn expect_err<T>(result: Result<T, ConnectError>) -> Result<ConnectError, TestError> {
match result {
Ok(_) => Err(TestError::ExpectedAnError),
Err(error) => Ok(error),
}
}
#[derive(Debug, thiserror::Error)]
enum TestError {
#[error("the call was expected to fail and succeeded")]
ExpectedAnError,
#[error("the error was not the shape under test: {got:?}")]
WrongShape { got: ConnectError },
#[error(transparent)]
Client(#[from] crate::application::client::ClientError),
}