#[cfg(test)]
mod tests {
use aex::connection::protocol::Protocol;
use serde_json;
#[test]
fn test_protocol_as_str_exhaustive() {
let cases = [
(Protocol::Tcp, "tcp"),
(Protocol::Udp, "udp"),
(Protocol::Http, "http"),
(Protocol::Ws, "ws"),
(Protocol::Custom("libp2p".to_string()), "libp2p"),
];
for (proto, expected) in cases {
assert_eq!(proto.as_str(), expected);
}
}
#[test]
fn test_protocol_from_str_logic() {
assert_eq!(Protocol::from("tcp"), Protocol::Tcp);
assert_eq!(Protocol::from("UDP"), Protocol::Udp); assert_eq!(Protocol::from("Http"), Protocol::Http);
assert_eq!(Protocol::from("ws"), Protocol::Ws);
let custom = Protocol::from("quic");
if let Protocol::Custom(s) = custom {
assert_eq!(s, "quic");
} else {
panic!("Should be Protocol::Custom");
}
}
#[test]
fn test_protocol_serde_roundtrip() {
let protocols = vec![Protocol::Tcp, Protocol::Custom("grpc".to_string())];
let serialized = serde_json::to_string(&protocols).unwrap();
assert!(serialized.contains("\"Tcp\""));
assert!(serialized.contains("{\"Custom\":\"grpc\"}"));
let deserialized: Vec<Protocol> = serde_json::from_str(&serialized).unwrap();
assert_eq!(protocols, deserialized);
}
#[test]
fn test_derived_traits() {
let p1 = Protocol::Custom("p2p".to_string());
let p2 = p1.clone();
assert_eq!(p1, p2);
assert!(format!("{:?}", p1).contains("Custom"));
}
}