hostbasket/sigsocket/tests/service_tests.rs
2025-05-19 14:48:40 +03:00

83 lines
2.9 KiB
Rust

use sigsocket::service::SigSocketService;
use sigsocket::registry::ConnectionRegistry;
use sigsocket::error::SigSocketError;
use std::sync::{Arc, RwLock};
#[tokio::test]
async fn test_service_send_to_sign() {
// Create a shared registry
let registry = Arc::new(RwLock::new(ConnectionRegistry::new()));
// Create the service
let service = SigSocketService::new(registry.clone());
// Test data
let public_key = "test_public_key";
let message = b"Test message to sign";
// Test send_to_sign (with simulated response)
let result = service.send_to_sign(public_key, message).await;
// Our implementation should return either ConnectionNotFound or InvalidPublicKey error
match result {
Err(SigSocketError::ConnectionNotFound) => {
// This is an expected error, since we're testing with a client that doesn't exist
println!("Got expected ConnectionNotFound error");
},
Err(SigSocketError::InvalidPublicKey) => {
// This is also an expected error since our test public key isn't valid
println!("Got expected InvalidPublicKey error");
},
Ok((response_message, signature)) => {
// For implementations that might simulate a response
// Verify response message matches the original
assert_eq!(response_message, message);
// Verify we got a signature (in this case, our dummy implementation returns a fixed signature)
assert_eq!(signature.len(), 4);
assert_eq!(signature, vec![1, 2, 3, 4]);
},
Err(e) => {
panic!("Unexpected error: {:?}", e);
}
}
}
#[tokio::test]
async fn test_service_connection_status() {
// Create a shared registry
let registry = Arc::new(RwLock::new(ConnectionRegistry::new()));
// Create the service
let service = SigSocketService::new(registry.clone());
// Check initial connection count
let count_result = service.connection_count();
assert!(count_result.is_ok());
assert_eq!(count_result.unwrap(), 0);
// Check if a connection exists (it shouldn't)
let connected_result = service.is_connected("some_key");
assert!(connected_result.is_ok());
assert!(!connected_result.unwrap());
// Note: We can't directly register a connection in the tests because the registry only accepts
// SigSocketManager addresses which require WebsocketContext, so we'll just test the API
// without manipulating the registry
}
#[tokio::test]
async fn test_create_websocket_handler() {
// Create a shared registry
let registry = Arc::new(RwLock::new(ConnectionRegistry::new()));
// Create the service
let service = SigSocketService::new(registry.clone());
// Create a websocket handler
let handler = service.create_websocket_handler();
// Verify the handler is properly initialized
assert!(handler.public_key.is_none());
}