|
1 | 1 | //! Shared test utilities and fixtures for McpMux integration tests. |
2 | 2 |
|
3 | | -pub use mcpmux_core::domain::{InstalledServer, Space, FeatureSet, FeatureSetType}; |
| 3 | +pub use mcpmux_core::domain::{InstalledServer, Space, FeatureSet, FeatureSetType, Client, Credential}; |
| 4 | +pub use mcpmux_core::{DomainEvent, ConnectionStatus, DiscoveredCapabilities, ServerFeature, FeatureType}; |
| 5 | + |
| 6 | +/// Mock repository implementations |
| 7 | +pub mod mocks; |
| 8 | +pub use mocks::MockRepositories; |
| 9 | + |
| 10 | +/// Service test helpers |
| 11 | +pub mod services; |
| 12 | +pub use services::ServerManagerTestHarness; |
| 13 | + |
| 14 | +/// Event testing utilities |
| 15 | +pub mod events { |
| 16 | + use mcpmux_core::DomainEvent; |
| 17 | + use tokio::sync::broadcast; |
| 18 | + use std::time::Duration; |
| 19 | + |
| 20 | + /// Create a test event channel with sufficient capacity |
| 21 | + pub fn test_event_channel() -> (broadcast::Sender<DomainEvent>, broadcast::Receiver<DomainEvent>) { |
| 22 | + broadcast::channel(100) |
| 23 | + } |
| 24 | + |
| 25 | + /// Collect events from a receiver with a timeout |
| 26 | + pub async fn collect_events( |
| 27 | + mut rx: broadcast::Receiver<DomainEvent>, |
| 28 | + timeout: Duration, |
| 29 | + ) -> Vec<DomainEvent> { |
| 30 | + let mut events = Vec::new(); |
| 31 | + let deadline = tokio::time::Instant::now() + timeout; |
| 32 | + |
| 33 | + loop { |
| 34 | + let remaining = deadline.saturating_duration_since(tokio::time::Instant::now()); |
| 35 | + if remaining.is_zero() { |
| 36 | + break; |
| 37 | + } |
| 38 | + |
| 39 | + match tokio::time::timeout(remaining, rx.recv()).await { |
| 40 | + Ok(Ok(event)) => events.push(event), |
| 41 | + Ok(Err(_)) => break, // Channel closed or lagged |
| 42 | + Err(_) => break, // Timeout |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + events |
| 47 | + } |
| 48 | + |
| 49 | + /// Wait for a specific event type |
| 50 | + pub async fn wait_for_event<F>( |
| 51 | + mut rx: broadcast::Receiver<DomainEvent>, |
| 52 | + timeout: Duration, |
| 53 | + predicate: F, |
| 54 | + ) -> Option<DomainEvent> |
| 55 | + where |
| 56 | + F: Fn(&DomainEvent) -> bool, |
| 57 | + { |
| 58 | + let deadline = tokio::time::Instant::now() + timeout; |
| 59 | + |
| 60 | + loop { |
| 61 | + let remaining = deadline.saturating_duration_since(tokio::time::Instant::now()); |
| 62 | + if remaining.is_zero() { |
| 63 | + return None; |
| 64 | + } |
| 65 | + |
| 66 | + match tokio::time::timeout(remaining, rx.recv()).await { |
| 67 | + Ok(Ok(event)) if predicate(&event) => return Some(event), |
| 68 | + Ok(Ok(_)) => continue, // Not the event we want |
| 69 | + Ok(Err(_)) => return None, // Channel closed |
| 70 | + Err(_) => return None, // Timeout |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Assert that a ServerStatusChanged event was emitted with expected status |
| 76 | + pub fn assert_status_changed( |
| 77 | + events: &[DomainEvent], |
| 78 | + expected_server_id: &str, |
| 79 | + expected_status: mcpmux_core::ConnectionStatus, |
| 80 | + ) -> bool { |
| 81 | + events.iter().any(|e| { |
| 82 | + if let DomainEvent::ServerStatusChanged { server_id, status, .. } = e { |
| 83 | + server_id == expected_server_id && *status == expected_status |
| 84 | + } else { |
| 85 | + false |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// Server feature fixtures for testing |
| 92 | +pub mod features { |
| 93 | + use mcpmux_core::ServerFeature; |
| 94 | + |
| 95 | + /// Create a test tool feature |
| 96 | + pub fn test_tool(space_id: &str, server_id: &str, name: &str) -> ServerFeature { |
| 97 | + ServerFeature::tool(space_id, server_id, name) |
| 98 | + .with_description(format!("Test tool: {}", name)) |
| 99 | + } |
| 100 | + |
| 101 | + /// Create a test prompt feature |
| 102 | + pub fn test_prompt(space_id: &str, server_id: &str, name: &str) -> ServerFeature { |
| 103 | + ServerFeature::prompt(space_id, server_id, name) |
| 104 | + .with_description(format!("Test prompt: {}", name)) |
| 105 | + } |
| 106 | + |
| 107 | + /// Create a test resource feature |
| 108 | + pub fn test_resource(space_id: &str, server_id: &str, uri: &str) -> ServerFeature { |
| 109 | + ServerFeature::resource(space_id, server_id, uri) |
| 110 | + .with_description(format!("Test resource: {}", uri)) |
| 111 | + } |
| 112 | + |
| 113 | + /// Create a set of test features for a server |
| 114 | + pub fn test_feature_set(space_id: &str, server_id: &str) -> Vec<ServerFeature> { |
| 115 | + vec![ |
| 116 | + test_tool(space_id, server_id, "read_file"), |
| 117 | + test_tool(space_id, server_id, "write_file"), |
| 118 | + test_prompt(space_id, server_id, "summarize"), |
| 119 | + test_resource(space_id, server_id, "file:///test"), |
| 120 | + ] |
| 121 | + } |
| 122 | +} |
4 | 123 |
|
5 | 124 | /// Test fixture utilities |
6 | 125 | pub mod fixtures { |
|
0 commit comments