#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
#[actix_rt::test]
async fn test_request_response() {
let router = Arc::new(MessageRouter::new());
let broker = RequestResponseBroker::new(router.clone());
let from = Uuid::new_v4();
let to = Uuid::new_v4();
let broker_clone = Arc::new(broker);
let broker_handle = broker_clone.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(10)).await;
let response = AgentMessage::new(to, from, "response")
.unwrap()
.with_correlation(Uuid::new_v4());
broker_handle.handle_response(response);
});
let result = broker_clone
.request(from, to, "test", Duration::from_millis(5))
.await;
assert!(matches!(result, Err(RequestError::Timeout)));
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
use super::*;
#[test]
fn test_broker_creation() {
let router = Arc::new(MessageRouter::new());
let broker = RequestResponseBroker::new(router);
assert!(broker.pending_requests.read().is_empty());
}
#[test]
fn test_handle_response_no_correlation_id() {
let router = Arc::new(MessageRouter::new());
let broker = RequestResponseBroker::new(router);
let msg = AgentMessage::new(Uuid::new_v4(), Uuid::new_v4(), "response").unwrap();
broker.handle_response(msg);
}
#[test]
fn test_handle_response_unknown_correlation_id() {
let router = Arc::new(MessageRouter::new());
let broker = RequestResponseBroker::new(router);
let unknown_correlation = Uuid::new_v4();
let msg = AgentMessage::new(Uuid::new_v4(), Uuid::new_v4(), "response")
.unwrap()
.with_correlation(unknown_correlation);
broker.handle_response(msg);
}
#[actix_rt::test]
async fn test_request_timeout_cleanup() {
let router = Arc::new(MessageRouter::new());
let broker = RequestResponseBroker::new(router);
let from = Uuid::new_v4();
let to = Uuid::new_v4();
let result = broker
.request(from, to, "test", Duration::from_millis(1))
.await;
assert!(matches!(result, Err(RequestError::Timeout)));
assert!(broker.pending_requests.read().is_empty());
}
#[test]
fn test_request_error_timeout() {
let err = RequestError::Timeout;
let display_str = format!("{}", err);
assert!(display_str.contains("timeout") || display_str.contains("Timeout"));
let debug_str = format!("{:?}", err);
assert!(debug_str.contains("Timeout"));
}
#[test]
fn test_request_error_cancelled() {
let err = RequestError::Cancelled;
let display_str = format!("{}", err);
assert!(display_str.contains("cancelled") || display_str.contains("Cancelled"));
let debug_str = format!("{:?}", err);
assert!(debug_str.contains("Cancelled"));
}
#[test]
fn test_request_error_from_router_error() {
let router_err = RouterError::Expired;
let request_err: RequestError = router_err.into();
let display_str = format!("{}", request_err);
assert!(display_str.contains("Router error") || display_str.contains("expired"));
}
#[test]
fn test_analyze_request_serialize() {
let req = AnalyzeRequest {
code: "fn main() {}".to_string(),
};
let json = serde_json::to_string(&req).unwrap();
assert!(json.contains("fn main()"));
let deserialized: AnalyzeRequest = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.code, req.code);
}
#[test]
fn test_analyze_response_serialize() {
let resp = AnalyzeResponse {
complexity: 5,
lines: 100,
};
let json = serde_json::to_string(&resp).unwrap();
let deserialized: AnalyzeResponse = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.complexity, resp.complexity);
assert_eq!(deserialized.lines, resp.lines);
}
#[test]
fn test_analyze_request_debug() {
let req = AnalyzeRequest {
code: "test code".to_string(),
};
let debug_str = format!("{:?}", req);
assert!(debug_str.contains("AnalyzeRequest"));
assert!(debug_str.contains("test code"));
}
#[test]
fn test_transform_request_serialize() {
let req = TransformRequest {
code: "let x = 1".to_string(),
transform_type: "minify".to_string(),
};
let json = serde_json::to_string(&req).unwrap();
let deserialized: TransformRequest = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.code, req.code);
assert_eq!(deserialized.transform_type, req.transform_type);
}
#[test]
fn test_transform_response_serialize() {
let resp = TransformResponse {
transformed_code: "let x=1".to_string(),
};
let json = serde_json::to_string(&resp).unwrap();
let deserialized: TransformResponse = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.transformed_code, resp.transformed_code);
}
#[test]
fn test_transform_request_debug() {
let req = TransformRequest {
code: "code".to_string(),
transform_type: "format".to_string(),
};
let debug_str = format!("{:?}", req);
assert!(debug_str.contains("TransformRequest"));
}
#[test]
fn test_transform_response_debug() {
let resp = TransformResponse {
transformed_code: "transformed".to_string(),
};
let debug_str = format!("{:?}", resp);
assert!(debug_str.contains("TransformResponse"));
}
#[test]
fn test_validate_request_serialize() {
let req = ValidateRequest {
code: "function foo() {}".to_string(),
rules: vec!["no-unused-vars".to_string(), "semi".to_string()],
};
let json = serde_json::to_string(&req).unwrap();
let deserialized: ValidateRequest = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.code, req.code);
assert_eq!(deserialized.rules, req.rules);
}
#[test]
fn test_validate_response_serialize() {
let resp = ValidateResponse {
valid: false,
errors: vec![
"Missing semicolon".to_string(),
"Unused variable".to_string(),
],
};
let json = serde_json::to_string(&resp).unwrap();
let deserialized: ValidateResponse = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.valid, resp.valid);
assert_eq!(deserialized.errors, resp.errors);
}
#[test]
fn test_validate_request_debug() {
let req = ValidateRequest {
code: "code".to_string(),
rules: vec!["rule1".to_string()],
};
let debug_str = format!("{:?}", req);
assert!(debug_str.contains("ValidateRequest"));
}
#[test]
fn test_validate_response_debug() {
let resp = ValidateResponse {
valid: true,
errors: vec![],
};
let debug_str = format!("{:?}", resp);
assert!(debug_str.contains("ValidateResponse"));
}
#[test]
fn test_analyze_request_default_priority() {
let req = AnalyzeRequest {
code: "test".to_string(),
};
assert_eq!(req.priority(), Priority::Normal);
}
#[test]
fn test_transform_request_default_priority() {
let req = TransformRequest {
code: "test".to_string(),
transform_type: "test".to_string(),
};
assert_eq!(req.priority(), Priority::Normal);
}
#[test]
fn test_validate_request_default_priority() {
let req = ValidateRequest {
code: "test".to_string(),
rules: vec![],
};
assert_eq!(req.priority(), Priority::Normal);
}
#[actix_rt::test]
async fn test_successful_request_response_flow() {
let router = Arc::new(MessageRouter::new());
let broker = Arc::new(RequestResponseBroker::new(router.clone()));
let from = Uuid::new_v4();
let to = Uuid::new_v4();
let broker_handle = broker.clone();
let _broker_for_request = broker.clone();
let correlation_id = Uuid::new_v4();
let (tx, rx) = tokio::sync::oneshot::channel();
broker_handle
.pending_requests
.write()
.insert(correlation_id, tx);
let response = AgentMessage::new(to, from, "response data")
.unwrap()
.with_correlation(correlation_id);
broker_handle.handle_response(response);
let result = rx.await;
assert!(result.is_ok());
let msg = result.unwrap();
assert_eq!(msg.header.correlation_id, Some(correlation_id));
}
#[test]
fn test_request_error_serialization() {
let bad_data = vec![0xFFu8; 16]; let bincode_result: Result<String, _> = bincode::deserialize(&bad_data);
if let Err(e) = bincode_result {
let request_err = RequestError::Serialization(e);
let display_str = format!("{}", request_err);
assert!(display_str.contains("Serialization error"));
}
}
#[test]
fn test_validate_request_empty_rules() {
let req = ValidateRequest {
code: "const x = 1;".to_string(),
rules: vec![],
};
let json = serde_json::to_string(&req).unwrap();
let deserialized: ValidateRequest = serde_json::from_str(&json).unwrap();
assert!(deserialized.rules.is_empty());
}
#[test]
fn test_validate_response_valid_no_errors() {
let resp = ValidateResponse {
valid: true,
errors: vec![],
};
assert!(resp.valid);
assert!(resp.errors.is_empty());
}
#[test]
fn test_analyze_response_debug() {
let resp = AnalyzeResponse {
complexity: 10,
lines: 50,
};
let debug_str = format!("{:?}", resp);
assert!(debug_str.contains("AnalyzeResponse"));
assert!(debug_str.contains("10"));
assert!(debug_str.contains("50"));
}
}