use crate::domain::models::CrawlStatus;
pub fn validate_crawl_config(max_depth: u32, max_concurrency: Option<u32>) -> Result<(), String> {
if max_depth > crate::application::dto::crawl_request::MAX_CRAWL_DEPTH {
return Err("max_depth must be between 0 and 5".to_string());
}
if let Some(concurrency) = max_concurrency {
if concurrency > crate::application::dto::crawl_request::MAX_CONCURRENCY {
return Err("max_concurrency must be between 1 and 50".to_string());
}
}
Ok(())
}
pub fn can_cancel(status: &CrawlStatus) -> bool {
!matches!(
status,
CrawlStatus::Completed | CrawlStatus::Failed | CrawlStatus::Cancelled
)
}
pub fn is_terminal(status: &CrawlStatus) -> bool {
matches!(
status,
CrawlStatus::Completed | CrawlStatus::Failed | CrawlStatus::Cancelled
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_crawl_config_valid() {
assert!(validate_crawl_config(0, None).is_ok());
assert!(validate_crawl_config(5, None).is_ok());
assert!(validate_crawl_config(3, Some(50)).is_ok());
assert!(validate_crawl_config(0, Some(1)).is_ok());
assert!(validate_crawl_config(0, Some(50)).is_ok());
}
#[test]
fn test_validate_crawl_config_depth_too_large() {
let result = validate_crawl_config(6, None);
assert!(result.is_err());
assert!(result.unwrap_err().contains("max_depth"));
}
#[test]
fn test_validate_crawl_config_concurrency_too_large() {
let result = validate_crawl_config(3, Some(101));
assert!(result.is_err());
assert!(result.unwrap_err().contains("max_concurrency"));
}
#[test]
fn test_can_cancel_queued() {
assert!(can_cancel(&CrawlStatus::Queued));
}
#[test]
fn test_can_cancel_running() {
assert!(!can_cancel(&CrawlStatus::Completed));
assert!(!can_cancel(&CrawlStatus::Failed));
assert!(!can_cancel(&CrawlStatus::Cancelled));
}
#[test]
fn test_is_terminal() {
assert!(is_terminal(&CrawlStatus::Completed));
assert!(is_terminal(&CrawlStatus::Failed));
assert!(is_terminal(&CrawlStatus::Cancelled));
assert!(!is_terminal(&CrawlStatus::Queued));
}
}