use chrono::{DateTime, Utc};
pub fn validate_url(url: &str) -> Result<(), String> {
if url.is_empty() {
return Err("URL cannot be empty".to_string());
}
if !url.starts_with("http://") && !url.starts_with("https://") {
return Err("URL must start with http:// or https://".to_string());
}
Ok(())
}
pub fn validate_team_name(name: &str) -> Result<(), String> {
if name.is_empty() {
return Err("Team name cannot be empty".to_string());
}
if name.len() > 255 {
return Err("Team name must be 255 characters or fewer".to_string());
}
Ok(())
}
pub fn can_task_retry(retry_count: i32, max_retries: i32) -> bool {
retry_count < max_retries
}
pub fn is_task_expired(expires_at: Option<DateTime<Utc>>) -> bool {
expires_at.is_some_and(|expires_at| Utc::now() > expires_at)
}
pub fn is_task_locked(
lock_token: Option<crate::domain::id::Id>,
lock_expires_at: Option<DateTime<Utc>>,
) -> bool {
lock_token.is_some() && lock_expires_at.is_some_and(|expires_at| Utc::now() < expires_at)
}
pub fn has_sufficient_balance(balance: i64, amount: i64) -> bool {
balance >= amount
}
pub fn can_webhook_retry(attempt_count: i32, max_retries: i32) -> bool {
attempt_count < max_retries
}
pub fn is_crawl_finished(status: &str) -> bool {
matches!(status, "completed" | "failed" | "cancelled")
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Duration;
#[test]
fn test_validate_url_valid_http() {
assert!(validate_url("http://example.com").is_ok());
}
#[test]
fn test_validate_url_valid_https() {
assert!(validate_url("https://example.com/path?q=1").is_ok());
}
#[test]
fn test_validate_url_empty() {
assert!(validate_url("").is_err());
assert_eq!(validate_url("").unwrap_err(), "URL cannot be empty");
}
#[test]
fn test_validate_url_no_scheme() {
assert!(validate_url("example.com").is_err());
assert!(validate_url("example.com")
.unwrap_err()
.contains("http:// or https://"));
}
#[test]
fn test_validate_url_ftp_scheme_rejected() {
assert!(validate_url("ftp://example.com").is_err());
}
#[test]
fn test_validate_team_name_valid() {
assert!(validate_team_name("My Team").is_ok());
}
#[test]
fn test_validate_team_name_empty() {
assert!(validate_team_name("").is_err());
assert_eq!(
validate_team_name("").unwrap_err(),
"Team name cannot be empty"
);
}
#[test]
fn test_validate_team_name_too_long() {
let long_name = "a".repeat(256);
assert!(validate_team_name(&long_name).is_err());
}
#[test]
fn test_validate_team_name_at_max_length() {
let max_name = "a".repeat(255);
assert!(validate_team_name(&max_name).is_ok());
}
#[test]
fn test_can_task_retry_under_limit() {
assert!(can_task_retry(0, 3));
assert!(can_task_retry(2, 3));
}
#[test]
fn test_can_task_retry_at_limit() {
assert!(!can_task_retry(3, 3));
}
#[test]
fn test_can_task_retry_over_limit() {
assert!(!can_task_retry(5, 3));
}
#[test]
fn test_can_task_retry_zero_max() {
assert!(!can_task_retry(0, 0));
}
#[test]
fn test_is_task_expired_none() {
assert!(!is_task_expired(None));
}
#[test]
fn test_is_task_expired_future() {
let future = Some(Utc::now() + Duration::hours(1));
assert!(!is_task_expired(future));
}
#[test]
fn test_is_task_expired_past() {
let past = Some(Utc::now() - Duration::hours(1));
assert!(is_task_expired(past));
}
#[test]
fn test_is_task_locked_no_token() {
assert!(!is_task_locked(None, None));
assert!(!is_task_locked(None, Some(Utc::now() + Duration::hours(1))));
}
#[test]
fn test_is_task_locked_with_valid_token() {
let token = Some(crate::domain::id::Id::generate());
let expires = Some(Utc::now() + Duration::hours(1));
assert!(is_task_locked(token, expires));
}
#[test]
fn test_is_task_locked_with_expired_token() {
let token = Some(crate::domain::id::Id::generate());
let expires = Some(Utc::now() - Duration::hours(1));
assert!(!is_task_locked(token, expires));
}
#[test]
fn test_has_sufficient_balance_enough() {
assert!(has_sufficient_balance(100, 50));
assert!(has_sufficient_balance(100, 100));
}
#[test]
fn test_has_sufficient_balance_insufficient() {
assert!(!has_sufficient_balance(50, 100));
}
#[test]
fn test_has_sufficient_balance_zero() {
assert!(has_sufficient_balance(0, 0));
assert!(!has_sufficient_balance(0, 1));
}
#[test]
fn test_can_webhook_retry_under_limit() {
assert!(can_webhook_retry(0, 3));
assert!(can_webhook_retry(2, 3));
}
#[test]
fn test_can_webhook_retry_at_limit() {
assert!(!can_webhook_retry(3, 3));
}
#[test]
fn test_is_crawl_finished_completed() {
assert!(is_crawl_finished("completed"));
}
#[test]
fn test_is_crawl_finished_failed() {
assert!(is_crawl_finished("failed"));
}
#[test]
fn test_is_crawl_finished_cancelled() {
assert!(is_crawl_finished("cancelled"));
}
#[test]
fn test_is_crawl_finished_running() {
assert!(!is_crawl_finished("running"));
assert!(!is_crawl_finished("pending"));
assert!(!is_crawl_finished("queued"));
}
}