use mecha10_cli::services::docker::containers::*;
use std::path::PathBuf;
use tempfile::TempDir;
#[test]
fn test_compose_file_exists_with_file_present() {
let temp_dir = TempDir::new().unwrap();
let compose_path = temp_dir.path().join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
assert!(compose_file_exists(None, Some(temp_dir.path())));
}
#[test]
fn test_compose_file_exists_with_file_missing() {
let temp_dir = TempDir::new().unwrap();
assert!(!compose_file_exists(None, Some(temp_dir.path())));
}
#[test]
fn test_compose_file_exists_with_custom_file() {
let temp_dir = TempDir::new().unwrap();
let custom_path = temp_dir.path().join("custom-compose.yml");
std::fs::write(&custom_path, "version: '3'").unwrap();
let custom_file = custom_path.to_string_lossy().to_string();
assert!(compose_file_exists(Some(&custom_file), None));
}
#[test]
fn test_compose_file_exists_custom_file_missing() {
let temp_dir = TempDir::new().unwrap();
let custom_path = temp_dir.path().join("missing.yml");
let custom_file = custom_path.to_string_lossy().to_string();
assert!(!compose_file_exists(Some(&custom_file), None));
}
#[test]
fn test_compose_file_exists_default_location() {
let _ = compose_file_exists(None, None);
}
#[test]
fn test_compose_file_exists_with_absolute_path() {
let temp_dir = TempDir::new().unwrap();
let compose_path = temp_dir.path().join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
let abs_path = compose_path.to_string_lossy().to_string();
assert!(compose_file_exists(Some(&abs_path), None));
}
#[test]
fn test_compose_file_exists_nested_directory() {
let temp_dir = TempDir::new().unwrap();
let nested = temp_dir.path().join("config").join("docker");
std::fs::create_dir_all(&nested).unwrap();
let compose_path = nested.join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
assert!(compose_file_exists(None, Some(&nested)));
}
#[test]
fn test_compose_file_exists_with_relative_path() {
let temp_dir = TempDir::new().unwrap();
let compose_path = temp_dir.path().join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
let relative = PathBuf::from("docker-compose.yml");
let full_path = temp_dir.path().join(&relative);
assert!(full_path.exists());
}
#[test]
fn test_compose_file_exists_custom_file_takes_precedence() {
let temp_dir = TempDir::new().unwrap();
let default_compose = temp_dir.path().join("docker-compose.yml");
std::fs::write(&default_compose, "version: '3'").unwrap();
let custom_compose = temp_dir.path().join("custom.yml");
std::fs::write(&custom_compose, "version: '3'").unwrap();
let custom_file = custom_compose.to_string_lossy().to_string();
assert!(compose_file_exists(Some(&custom_file), Some(temp_dir.path())));
}
#[test]
fn test_compose_file_exists_empty_custom_file_path() {
let temp_dir = TempDir::new().unwrap();
let compose_path = temp_dir.path().join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
assert!(!compose_file_exists(Some(""), None));
}
#[tokio::test]
async fn test_is_container_running_method_signature() {
let _result: Result<bool> = is_container_running("nonexistent-container").await;
}
#[tokio::test]
async fn test_is_container_running_with_empty_name() {
let result = is_container_running("").await;
let _ = result;
}
#[tokio::test]
async fn test_is_container_running_with_special_characters() {
let result = is_container_running("container-name_123").await;
let _ = result;
}
#[tokio::test]
async fn test_list_containers_method_signature() {
let _result: Result<Vec<ContainerInfo>> = list_containers().await;
}
#[tokio::test]
async fn test_list_containers_returns_vec() {
if let Ok(containers) = list_containers().await {
let _len = containers.len();
}
}
#[tokio::test]
async fn test_wait_for_healthy_with_zero_timeout() {
let result = wait_for_healthy("nonexistent", Duration::from_secs(0)).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_wait_for_healthy_with_very_short_timeout() {
let start = std::time::Instant::now();
let result = wait_for_healthy("nonexistent", Duration::from_millis(100)).await;
let elapsed = start.elapsed();
assert!(result.is_err());
assert!(elapsed < Duration::from_secs(2));
}
#[tokio::test]
async fn test_wait_for_healthy_error_message() {
let result = wait_for_healthy("test-container", Duration::from_millis(10)).await;
if let Err(e) = result {
let err_msg = e.to_string();
assert!(err_msg.contains("Timeout"));
assert!(err_msg.contains("test-container"));
}
}
#[tokio::test]
async fn test_wait_for_healthy_with_empty_container_name() {
let result = wait_for_healthy("", Duration::from_millis(50)).await;
let _ = result;
}
#[tokio::test]
async fn test_wait_for_healthy_timeout_precision() {
let timeout = Duration::from_millis(100);
let start = std::time::Instant::now();
let _result = wait_for_healthy("nonexistent", timeout).await;
let elapsed = start.elapsed();
assert!(elapsed < timeout + Duration::from_millis(5000));
}
#[test]
fn test_compose_file_exists_with_directory_not_file() {
let temp_dir = TempDir::new().unwrap();
let dir_path = temp_dir.path().join("docker-compose.yml");
std::fs::create_dir(&dir_path).unwrap();
assert!(compose_file_exists(None, Some(temp_dir.path())));
}
#[test]
fn test_compose_file_exists_symlink() {
let temp_dir = TempDir::new().unwrap();
let target = temp_dir.path().join("target.yml");
std::fs::write(&target, "version: '3'").unwrap();
#[cfg(unix)]
{
let link = temp_dir.path().join("docker-compose.yml");
std::os::unix::fs::symlink(&target, &link).unwrap();
assert!(compose_file_exists(None, Some(temp_dir.path())));
}
}
#[test]
fn test_compose_file_exists_with_unicode_path() {
let temp_dir = TempDir::new().unwrap();
let unicode_dir = temp_dir.path().join("容器配置");
std::fs::create_dir(&unicode_dir).unwrap();
let compose_path = unicode_dir.join("docker-compose.yml");
std::fs::write(&compose_path, "version: '3'").unwrap();
assert!(compose_file_exists(None, Some(&unicode_dir)));
}
#[tokio::test]
async fn test_is_container_running_with_unicode_name() {
let result = is_container_running("容器-test").await;
let _ = result;
}