Unify integration test environment and add valid config validation

- Create shared tests/common.rs with TestEnvironment setup
- Simplify gemini_test_client.py to single-request client
- Refactor config validation tests to use common setup
- Add test_valid_config_startup for complete server validation
- Fix clippy warning in main.rs
- Remove unused code and consolidate test infrastructure
This commit is contained in:
Jeena 2026-01-16 23:59:54 +00:00
parent 3e490d85ef
commit 01bcda10d0
5 changed files with 219 additions and 295 deletions

View file

@ -1,5 +1,6 @@
mod common;
use std::process::Command;
use std::env;
#[test]
fn test_missing_config_file() {
@ -17,83 +18,100 @@ fn test_missing_config_file() {
#[test]
fn test_missing_hostname() {
let temp_dir = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id()));
std::fs::create_dir_all(&temp_dir).unwrap();
let config_path = temp_dir.join("config.toml");
std::fs::write(&config_path, r#"
root = "/tmp"
cert = "cert.pem"
key = "key.pem"
bind_host = "0.0.0.0"
"#).unwrap();
let env = common::setup_test_environment();
let config_content = format!(r#"
root = "{}"
cert = "{}"
key = "{}"
bind_host = "127.0.0.1"
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display());
std::fs::write(&env.config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg(&env.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("hostname = \"your.domain.com\""));
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir);
assert!(stderr.contains("Add: hostname = \"your.domain.com\""));
}
#[test]
fn test_nonexistent_root_directory() {
let temp_dir = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id()));
std::fs::create_dir_all(&temp_dir).unwrap();
let config_path = temp_dir.join("config.toml");
std::fs::write(&config_path, r#"
let env = common::setup_test_environment();
let config_content = format!(r#"
root = "/definitely/does/not/exist"
cert = "cert.pem"
key = "key.pem"
cert = "{}"
key = "{}"
hostname = "example.com"
bind_host = "0.0.0.0"
"#).unwrap();
bind_host = "127.0.0.1"
"#, env.cert_path.display(), env.key_path.display());
std::fs::write(&env.config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(config_path)
.arg(&env.config_path)
.output()
.unwrap();
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir);
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Root directory '/definitely/does/not/exist' does not exist"));
assert!(stderr.contains("Create the directory and add your Gemini files"));
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 = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id()));
std::fs::create_dir_all(&temp_dir).unwrap();
let config_path = temp_dir.join("config.toml");
std::fs::write(&config_path, r#"
root = "/tmp"
let env = common::setup_test_environment();
let config_content = format!(r#"
root = "{}"
cert = "/nonexistent/cert.pem"
key = "key.pem"
key = "{}"
hostname = "example.com"
bind_host = "0.0.0.0"
"#).unwrap();
bind_host = "127.0.0.1"
"#, env.content_path.display(), env.key_path.display());
std::fs::write(&env.config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg(&env.config_path)
.output()
.unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Certificate file '/nonexistent/cert.pem' does not exist"));
assert!(stderr.contains("Generate or obtain TLS certificates"));
assert!(stderr.contains("Error: Certificate file '/nonexistent/cert.pem' does not exist"));
assert!(stderr.contains("Generate or obtain TLS certificates for your domain"));
}
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir);
#[test]
fn test_valid_config_startup() {
let env = common::setup_test_environment();
let config_content = format!(r#"
root = "{}"
cert = "{}"
key = "{}"
hostname = "localhost"
bind_host = "127.0.0.1"
port = {}
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port);
std::fs::write(&env.config_path, config_content).unwrap();
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&env.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();
}