// file: crates/ksp-onchain-transport-lib/unit_tests/ws_protocol_session.rs // version: 2 use futures_util::StreamExt; // rust-rules: trait-import fn endpoint(url: &str, protocol: crate::WsProtocolKind) -> crate::WsEndpointSettings { return crate::WsEndpointSettings::new( "local_protocol_fixture", true, crate::WsProviderName::new("local-fixture"), crate::WsClusterName::new("local"), protocol, crate::WsEndpointUrl::parse(url).expect("local WebSocket URL must parse"), crate::WsSessionSettings::default(), ); } async fn bind_local_listener() -> (tokio::net::TcpListener, std::string::String) { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.expect("local listener must bind"); let address = listener.local_addr().expect("local listener must expose address"); return (listener, format!("ws://{address}")); } async fn accept_until_close(listener: tokio::net::TcpListener) { let (stream, _) = listener.accept().await.expect("local peer must accept connection"); let mut websocket = tokio_tungstenite::accept_async(stream).await.expect("local WebSocket handshake must succeed"); while let std::option::Option::Some(message) = websocket.next().await { let message = message.expect("local peer message must decode"); if message.is_close() { return; } } return; } #[tokio::test] async fn protocol_facades_share_the_existing_physical_session_path() { let (standard_listener, standard_url) = bind_local_listener().await; let standard_server = tokio::spawn(accept_until_close(standard_listener)); let standard = crate::SolanaStandardWsSession::connect(endpoint(standard_url.as_str(), crate::WsProtocolKind::SolanaStandard)) .await .expect("standard facade must connect"); assert_eq!(standard.snapshot().protocol(), crate::WsProtocolKind::SolanaStandard); standard.close().await.expect("standard facade must close"); standard_server.await.expect("standard peer task must finish"); let (helius_listener, helius_url) = bind_local_listener().await; let helius_server = tokio::spawn(accept_until_close(helius_listener)); let helius_url = format!("{helius_url}/?api-key=SECRET-CANARY"); let helius = crate::HeliusLaserStreamWsSession::connect(endpoint(helius_url.as_str(), crate::WsProtocolKind::HeliusLaserStream)) .await .expect("Helius facade must connect"); assert_eq!(helius.snapshot().protocol(), crate::WsProtocolKind::HeliusLaserStream); let rendered = format!("{helius:?}"); assert!(!rendered.contains("SECRET-CANARY")); assert!(!rendered.contains(helius_url.as_str())); helius.close().await.expect("Helius facade must close"); helius_server.await.expect("Helius peer task must finish"); } #[tokio::test] async fn historical_generic_constructor_remains_standard_only_before_network_io() { let endpoint = endpoint("ws://127.0.0.1:9", crate::WsProtocolKind::HeliusLaserStream); let error = crate::WsSession::connect(endpoint).await.expect_err("generic historical constructor must reject Helius protocol"); assert_eq!(error.code(), crate::ERROR_CODE_INVALID_SETTINGS); assert_eq!( error.context().iter().find(|entry| return entry.key() == "expected_protocol").map(|entry| return entry.value()), std::option::Option::Some("solana_standard") ); assert_eq!( error.context().iter().find(|entry| return entry.key() == "actual_protocol").map(|entry| return entry.value()), std::option::Option::Some("helius_laserstream") ); } #[tokio::test] async fn typed_facades_reject_protocol_mismatch_before_network_io() { let helius_error = crate::HeliusLaserStreamWsSession::connect(endpoint("ws://127.0.0.1:9", crate::WsProtocolKind::SolanaStandard)) .await .expect_err("Helius facade must reject standard endpoint"); assert_eq!(helius_error.code(), crate::ERROR_CODE_INVALID_SETTINGS); let standard_error = crate::SolanaStandardWsSession::connect(endpoint("ws://127.0.0.1:9", crate::WsProtocolKind::HeliusLaserStream)) .await .expect_err("standard facade must reject Helius endpoint"); assert_eq!(standard_error.code(), crate::ERROR_CODE_INVALID_SETTINGS); } #[test] fn protocol_facades_define_no_second_actor_socket_or_public_inner_escape_hatch() { let source = include_str!("../src/ws_protocol_session.rs"); assert!(!source.contains("tokio::spawn")); assert!(!source.contains("tokio_tungstenite")); assert!(!source.contains("WsSessionCommand")); assert!(!source.contains("pub fn inner(")); assert!(!source.contains("pub fn into_inner(")); }