use mockforge_core::protocol_abstraction::Protocol;
use mockforge_core::routing::{HttpMethod, Route, RouteRegistry};
use serde_json::json;
#[cfg(test)]
mod protocol_state_consistency {
use super::*;
#[test]
fn test_route_registry_across_protocols() {
let mut registry = RouteRegistry::new();
let http_route = Route::new(HttpMethod::GET, "/api/users".to_string());
registry.add_http_route(http_route).unwrap();
let ws_route = Route::new(HttpMethod::GET, "/ws/chat".to_string());
registry.add_ws_route(ws_route).unwrap();
let http_routes = registry.find_http_routes(&HttpMethod::GET, "/api/users");
assert!(!http_routes.is_empty());
let ws_routes = registry.find_ws_routes("/ws/chat");
assert!(!ws_routes.is_empty());
}
#[test]
fn test_protocol_enum_consistency() {
let protocols = vec![
Protocol::Http,
Protocol::WebSocket,
Protocol::Grpc,
Protocol::GraphQL,
];
for protocol in protocols {
match protocol {
Protocol::Http => assert!(matches!(protocol, Protocol::Http)),
Protocol::WebSocket => assert!(matches!(protocol, Protocol::WebSocket)),
Protocol::Grpc => assert!(matches!(protocol, Protocol::Grpc)),
Protocol::GraphQL => assert!(matches!(protocol, Protocol::GraphQL)),
_ => {} }
}
}
#[test]
fn test_route_metadata_across_protocols() {
let mut registry = RouteRegistry::new();
let mut http_route = Route::new(HttpMethod::GET, "/api/users".to_string());
http_route = http_route.with_metadata("protocol".to_string(), json!("http"));
registry.add_http_route(http_route).unwrap();
let mut ws_route = Route::new(HttpMethod::GET, "/ws/chat".to_string());
ws_route = ws_route.with_metadata("protocol".to_string(), json!("websocket"));
registry.add_ws_route(ws_route).unwrap();
let http_routes = registry.find_http_routes(&HttpMethod::GET, "/api/users");
assert_eq!(http_routes[0].metadata.get("protocol"), Some(&json!("http")));
let ws_routes = registry.find_ws_routes("/ws/chat");
assert_eq!(ws_routes[0].metadata.get("protocol"), Some(&json!("websocket")));
}
}
#[cfg(test)]
mod protocol_bridge_tests {
use super::*;
#[test]
fn test_route_pattern_matching_across_protocols() {
let mut registry = RouteRegistry::new();
let http_route = Route::new(HttpMethod::GET, "/api/users/*".to_string());
registry.add_http_route(http_route).unwrap();
let ws_route = Route::new(HttpMethod::GET, "/ws/users/*".to_string());
registry.add_ws_route(ws_route).unwrap();
let http_matches = registry.find_http_routes(&HttpMethod::GET, "/api/users/123");
assert!(!http_matches.is_empty());
let ws_matches = registry.find_ws_routes("/ws/users/123");
assert!(!ws_matches.is_empty());
}
#[test]
fn test_route_priority_across_protocols() {
let mut registry = RouteRegistry::new();
let low_priority = Route::new(HttpMethod::GET, "/api/*".to_string()).with_priority(1);
let high_priority = Route::new(HttpMethod::GET, "/api/users".to_string()).with_priority(10);
registry.add_http_route(low_priority).unwrap();
registry.add_http_route(high_priority).unwrap();
let matches = registry.find_http_routes(&HttpMethod::GET, "/api/users");
assert!(!matches.is_empty());
}
#[test]
fn test_protocol_route_isolation() {
let mut registry = RouteRegistry::new();
let http_route = Route::new(HttpMethod::GET, "/api/test".to_string());
let ws_route = Route::new(HttpMethod::GET, "/api/test".to_string());
registry.add_http_route(http_route).unwrap();
registry.add_ws_route(ws_route).unwrap();
let http_routes = registry.find_http_routes(&HttpMethod::GET, "/api/test");
let ws_routes = registry.find_ws_routes("/api/test");
assert!(!http_routes.is_empty());
assert!(!ws_routes.is_empty());
}
}
#[cfg(test)]
mod protocol_data_consistency {
use super::*;
#[test]
fn test_route_metadata_serialization() {
let mut route = Route::new(HttpMethod::GET, "/api/test".to_string());
route = route.with_metadata("key1".to_string(), json!("value1"));
route = route.with_metadata("key2".to_string(), json!({"nested": "value"}));
let serialized = serde_json::to_string(&route).unwrap();
assert!(serialized.contains("key1"));
assert!(serialized.contains("value1"));
let deserialized: Route = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.metadata.get("key1"), Some(&json!("value1")));
assert_eq!(deserialized.metadata.get("key2"), Some(&json!({"nested": "value"})));
}
#[test]
fn test_route_registry_serialization() {
let mut registry = RouteRegistry::new();
let route1 = Route::new(HttpMethod::GET, "/api/users".to_string());
let route2 = Route::new(HttpMethod::POST, "/api/users".to_string());
registry.add_http_route(route1).unwrap();
registry.add_http_route(route2).unwrap();
let cloned = registry.clone();
let routes1 = registry.find_http_routes(&HttpMethod::GET, "/api/users");
let routes2 = cloned.find_http_routes(&HttpMethod::GET, "/api/users");
assert_eq!(routes1.len(), routes2.len());
}
}