guard/tests/protocol_registry.rs
loki5512344 18a0ffa53d
style: switch rustfmt to hard tabs; document tests-separate rule in TODO
- rustfmt.toml: hard_tabs = true; whole tree reformatted (no logic changes)
- TODO §0.7: tests live in tests/, never inline #[cfg(test)] in code files
  (legacy inline tests to be migrated, see Backlog)
- TODO: add backlog item for the inline-test migration
2026-11-15 22:34:29 +01:00

38 lines
1.1 KiB
Rust

use std::future::Future;
use std::pin::Pin;
use rampart::protocol::{HandshakeResult, ProtocolHandler, ProtocolRegistry, Upstream};
use tokio::net::TcpStream;
struct NoopHandler;
impl ProtocolHandler for NoopHandler {
fn name(&self) -> &'static str {
"noop"
}
fn handshake<'a>(
&'a self,
_stream: &'a mut TcpStream,
) -> Pin<Box<dyn Future<Output = anyhow::Result<HandshakeResult>> + Send + 'a>> {
Box::pin(async { Ok(HandshakeResult::new(Upstream::new("127.0.0.1:9000"))) })
}
}
#[tokio::test]
async fn empty_registry_reports_missing_protocols() {
let registry = ProtocolRegistry::new();
assert!(registry.is_empty());
let err = registry.primary().err().expect("empty registry must fail fast");
assert!(err.to_string().contains("no protocol plugins compiled"));
}
#[tokio::test]
async fn registered_handler_resolves_by_name() {
let mut registry = ProtocolRegistry::new();
registry.register(Box::new(NoopHandler));
assert_eq!(registry.names(), vec!["noop"]);
let handler = registry.primary().expect("registered handler must resolve");
assert_eq!(handler.name(), "noop");
}