mod common; use std::process::Command; #[test] fn test_missing_config_file() { let output = Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg("nonexistent.toml") .output() .unwrap(); assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr).unwrap(); assert!(stderr.contains("Config file 'nonexistent.toml' not found")); assert!(stderr.contains("Create the config file with required fields")); } #[test] fn test_missing_hostname() { let temp_dir = common::setup_test_environment(); let config_path = temp_dir.path().join("config.toml"); let config_content = format!(r#" root = "{}" cert = "{}" key = "{}" bind_host = "127.0.0.1" "#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display()); std::fs::write(&config_path, config_content).unwrap(); let output = Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg(&config_path) .output() .unwrap(); assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr).unwrap(); assert!(stderr.contains("'hostname' field is required")); assert!(stderr.contains("Add: hostname = \"your.domain.com\"")); } #[test] fn test_nonexistent_root_directory() { let temp_dir = common::setup_test_environment(); let config_path = temp_dir.path().join("config.toml"); let config_content = format!(r#" root = "/definitely/does/not/exist" cert = "{}" key = "{}" hostname = "example.com" bind_host = "127.0.0.1" "#, temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display()); std::fs::write(&config_path, config_content).unwrap(); let output = Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg(&config_path) .output() .unwrap(); assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr).unwrap(); assert!(stderr.contains("Error: Root directory '/definitely/does/not/exist' does not exist")); assert!(stderr.contains("Create the directory and add your Gemini files (.gmi, .txt, images)")); } #[test] fn test_missing_certificate_file() { let temp_dir = common::setup_test_environment(); let config_path = temp_dir.path().join("config.toml"); let config_content = format!(r#" root = "{}" cert = "/nonexistent/cert.pem" key = "{}" hostname = "example.com" bind_host = "127.0.0.1" "#, temp_dir.path().join("content").display(), temp_dir.path().join("key.pem").display()); std::fs::write(&config_path, config_content).unwrap(); let output = Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg(&config_path) .output() .unwrap(); assert!(!output.status.success()); let stderr = String::from_utf8(output.stderr).unwrap(); assert!(stderr.contains("Error: Certificate file '/nonexistent/cert.pem' does not exist")); assert!(stderr.contains("Generate or obtain TLS certificates for your domain")); } #[test] fn test_valid_config_startup() { let temp_dir = common::setup_test_environment(); let port = 1967 + (std::process::id() % 1000) as u16; let config_path = temp_dir.path().join("config.toml"); let config_content = format!(r#" root = "{}" cert = "{}" key = "{}" hostname = "localhost" bind_host = "127.0.0.1" port = {} "#, temp_dir.path().join("content").display(), temp_dir.path().join("cert.pem").display(), temp_dir.path().join("key.pem").display(), port); std::fs::write(&config_path, config_content).unwrap(); let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux")) .arg("--config") .arg(&config_path) .spawn() .unwrap(); // Wait for server to start std::thread::sleep(std::time::Duration::from_millis(500)); // Check server is still running (didn't exit with error) assert!(server_process.try_wait().unwrap().is_none(), "Server should still be running with valid config"); // Kill server server_process.kill().unwrap(); }