use super::*;
fn argv(args: &[&str]) -> Vec<String> {
args.iter().map(|arg| arg.to_string()).collect()
}
#[test]
fn no_args_defaults_to_background() {
assert_eq!(parse(argv(&[])), Command::Background);
}
#[test]
fn interactive_flags_select_foreground() {
for flag in ["-i", "--interactive", "-f", "--foreground"] {
assert_eq!(parse(argv(&[flag])), Command::Foreground, "flag {flag}");
}
}
#[test]
fn background_flags_select_background() {
for flag in ["-b", "--background", "-d", "--detach", "--daemon"] {
assert_eq!(parse(argv(&[flag])), Command::Background, "flag {flag}");
}
}
#[test]
fn stop_and_status_commands() {
assert_eq!(parse(argv(&["stop"])), Command::Stop { json: false });
assert_eq!(parse(argv(&["status"])), Command::Status { json: false });
}
#[test]
fn cleanup_command() {
assert_eq!(parse(argv(&["cleanup"])), Command::Cleanup { json: false });
}
#[test]
fn json_flag_sets_machine_readable_output() {
assert_eq!(
parse(argv(&["status", "--json"])),
Command::Status { json: true }
);
assert_eq!(
parse(argv(&["cleanup", "--json"])),
Command::Cleanup { json: true }
);
assert_eq!(
parse(argv(&["stop", "--json"])),
Command::Stop { json: true }
);
}
#[test]
fn json_flag_only_applies_to_its_command() {
assert_eq!(parse(argv(&["--json"])), Command::Help);
assert_eq!(
parse(argv(&["status", "--verbose"])),
Command::Status { json: false }
);
}
#[test]
fn status_json_reports_running_pid_and_address() {
let value: serde_json::Value = serde_json::from_str(&status_json(true, Some(42))).unwrap();
assert_eq!(value["running"], serde_json::json!(true));
assert_eq!(value["pid"], serde_json::json!(42));
assert_eq!(value["address"], serde_json::json!(BIND_ADDR));
}
#[test]
fn status_json_null_pid_when_unknown_or_down() {
let value: serde_json::Value = serde_json::from_str(&status_json(false, None)).unwrap();
assert_eq!(value["running"], serde_json::json!(false));
assert!(value["pid"].is_null());
assert_eq!(value["address"], serde_json::json!(BIND_ADDR));
}
#[test]
fn cleanup_json_reports_removed_and_running() {
let value: serde_json::Value = serde_json::from_str(&cleanup_json(3, true)).unwrap();
assert_eq!(value["running"], serde_json::json!(true));
assert_eq!(value["removed"], serde_json::json!(3));
let down: serde_json::Value = serde_json::from_str(&cleanup_json(0, false)).unwrap();
assert_eq!(down["running"], serde_json::json!(false));
assert_eq!(down["removed"], serde_json::json!(0));
}
#[test]
fn stop_json_reports_running_and_pid() {
let up: serde_json::Value = serde_json::from_str(&stop_json(true, Some(42))).unwrap();
assert_eq!(up["running"], serde_json::json!(true));
assert_eq!(up["pid"], serde_json::json!(42));
let down: serde_json::Value = serde_json::from_str(&stop_json(false, None)).unwrap();
assert_eq!(down["running"], serde_json::json!(false));
assert!(down["pid"].is_null());
}
#[test]
fn liveness_exit_code_maps_running_to_codes() {
assert_eq!(liveness_exit_code(true), 0);
assert_eq!(liveness_exit_code(false), EXIT_NOT_RUNNING);
assert_eq!(EXIT_NOT_RUNNING, 3);
}
#[test]
fn restart_command() {
assert_eq!(parse(argv(&["restart"])), Command::Restart);
}
#[test]
fn restart_rotation_line_shows_old_and_new_pid() {
assert_eq!(
restart_rotation_line(Some(123), 456),
"restarted: pid 123 -> 456"
);
}
#[test]
fn restart_rotation_line_reads_none_when_nothing_was_running() {
assert_eq!(
restart_rotation_line(None, 456),
"restarted: pid none -> 456"
);
}
#[test]
fn help_and_version_flags() {
for flag in ["-h", "--help", "help"] {
assert_eq!(parse(argv(&[flag])), Command::Help, "flag {flag}");
}
for flag in ["-V", "--version", "version"] {
assert_eq!(parse(argv(&[flag])), Command::Version, "flag {flag}");
}
}
#[test]
fn unknown_arg_falls_back_to_help() {
assert_eq!(parse(argv(&["--nonsense"])), Command::Help);
}
#[test]
fn parses_http_status_code() {
assert_eq!(parse_status_code("HTTP/1.1 200 OK\r\n\r\n"), Some(200));
assert_eq!(
parse_status_code("HTTP/1.1 503 Service Unavailable"),
Some(503)
);
}
#[test]
fn rejects_malformed_status_line() {
assert_eq!(parse_status_code(""), None);
assert_eq!(parse_status_code("garbage"), None);
}
#[test]
fn extracts_body_after_headers() {
let resp = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"removed\":3}";
assert_eq!(parse_body(resp), "{\"removed\":3}");
}
#[test]
fn body_is_empty_without_header_separator() {
assert_eq!(parse_body("HTTP/1.1 200 OK"), "");
}
#[test]
fn parses_removed_count_from_cleanup_body() {
assert_eq!(parse_removed_count("{\"removed\":0}"), Some(0));
assert_eq!(parse_removed_count("{\"removed\":7}"), Some(7));
}
#[test]
fn rejects_non_cleanup_body() {
assert_eq!(parse_removed_count(""), None);
assert_eq!(parse_removed_count("not json"), None);
assert_eq!(parse_removed_count("{\"other\":1}"), None);
}
use std::net::TcpListener;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
const UNREACHABLE_ADDR: &str = "127.0.0.1:1";
struct EnvGuard {
name: &'static str,
previous: Option<std::ffi::OsString>,
}
impl EnvGuard {
fn set(name: &'static str, value: &str) -> EnvGuard {
let previous = std::env::var_os(name);
unsafe {
std::env::set_var(name, value);
}
EnvGuard { name, previous }
}
}
impl Drop for EnvGuard {
fn drop(&mut self) {
unsafe {
match self.previous.take() {
Some(value) => std::env::set_var(self.name, value),
None => std::env::remove_var(self.name),
}
}
}
}
fn temp_home(tag: &str) -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!("moadim-cli-{tag}-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).expect("create temp home");
dir
}
struct FakeServer {
addr: String,
alive: Arc<AtomicBool>,
stop: Arc<AtomicBool>,
handle: Option<std::thread::JoinHandle<()>>,
}
impl FakeServer {
fn start(status: u16, body: String) -> FakeServer {
let listener = TcpListener::bind("127.0.0.1:0").expect("bind ephemeral port");
let addr = listener.local_addr().expect("local addr").to_string();
listener.set_nonblocking(true).expect("set nonblocking");
let alive = Arc::new(AtomicBool::new(true));
let stop = Arc::new(AtomicBool::new(false));
let alive_loop = Arc::clone(&alive);
let stop_loop = Arc::clone(&stop);
let handle = std::thread::spawn(move || {
let response = format!(
"HTTP/1.1 {status} OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
while !stop_loop.load(Ordering::SeqCst) {
match listener.accept() {
Ok((mut stream, _)) => {
let mut buf = [0u8; 1024];
let _ = stream.read(&mut buf);
if alive_loop.load(Ordering::SeqCst) {
let _ = stream.write_all(response.as_bytes());
}
}
Err(ref err) if err.kind() == std::io::ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(2));
}
Err(_) => break,
}
}
});
FakeServer {
addr,
alive,
stop,
handle: Some(handle),
}
}
fn stop_after(&self, delay: Duration) {
let alive = Arc::clone(&self.alive);
std::thread::spawn(move || {
std::thread::sleep(delay);
alive.store(false, Ordering::SeqCst);
});
}
}
impl Drop for FakeServer {
fn drop(&mut self) {
self.stop.store(true, Ordering::SeqCst);
if let Some(handle) = self.handle.take() {
let _ = handle.join();
}
}
}
#[test]
fn bind_addr_uses_default_when_unset() {
let previous = std::env::var_os(BIND_ADDR_ENV);
unsafe {
std::env::remove_var(BIND_ADDR_ENV);
}
assert_eq!(bind_addr(), BIND_ADDR);
unsafe {
if let Some(value) = previous {
std::env::set_var(BIND_ADDR_ENV, value);
}
}
}
#[test]
fn bind_addr_honors_override() {
let _addr = EnvGuard::set(BIND_ADDR_ENV, "127.0.0.1:6000");
assert_eq!(bind_addr(), "127.0.0.1:6000");
}
#[test]
fn print_help_and_version_emit_without_panicking() {
print_help();
print_version();
}
#[test]
fn stop_reports_not_running_when_no_server() {
let home = temp_home("stop-down");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, UNREACHABLE_ADDR);
assert_eq!(stop(false).unwrap(), EXIT_NOT_RUNNING);
assert_eq!(stop(true).unwrap(), EXIT_NOT_RUNNING);
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn stop_signals_running_server() {
let server = FakeServer::start(200, String::new());
let home = temp_home("stop-up");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
assert_eq!(stop(false).unwrap(), 0);
assert_eq!(stop(true).unwrap(), 0);
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn stop_errors_on_unexpected_status() {
let server = FakeServer::start(500, String::new());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
assert!(stop(false).is_err());
}
#[test]
fn status_reports_down_when_no_server() {
let home = temp_home("status-down");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, UNREACHABLE_ADDR);
assert_eq!(status(false).unwrap(), EXIT_NOT_RUNNING);
assert_eq!(status(true).unwrap(), EXIT_NOT_RUNNING);
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn status_reports_running_with_pid() {
let server = FakeServer::start(200, String::new());
let home = temp_home("status-up");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
write_pid_file().unwrap();
assert_eq!(status(false).unwrap(), 0);
assert_eq!(status(true).unwrap(), 0);
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn cleanup_reports_removed_counts_when_running() {
let home = temp_home("cleanup-up");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
{
let server = FakeServer::start(200, "{\"removed\":1}".to_string());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
assert_eq!(cleanup(false).unwrap(), 0);
assert_eq!(cleanup(true).unwrap(), 0);
}
{
let server = FakeServer::start(200, "{\"removed\":2}".to_string());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
assert_eq!(cleanup(false).unwrap(), 0);
}
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn cleanup_reports_not_running_when_no_server() {
let _addr = EnvGuard::set(BIND_ADDR_ENV, UNREACHABLE_ADDR);
assert_eq!(cleanup(false).unwrap(), EXIT_NOT_RUNNING);
assert_eq!(cleanup(true).unwrap(), EXIT_NOT_RUNNING);
}
#[test]
fn cleanup_errors_on_unexpected_status() {
let server = FakeServer::start(500, String::new());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
assert!(cleanup(false).is_err());
}
#[test]
fn pid_file_write_read_clear_roundtrip() {
let home = temp_home("pidfile");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
write_pid_file().unwrap();
assert_eq!(read_pid_file(), Some(std::process::id()));
assert!(crate::paths::config_gitignore_path().exists());
write_pid_file().unwrap();
clear_pid_file();
assert!(read_pid_file().is_none());
std::fs::write(crate::paths::pid_file(), "not-a-pid").unwrap();
assert!(read_pid_file().is_none());
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn run_background_starts_when_none_running() {
let home = temp_home("runbg-fresh");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, UNREACHABLE_ADDR);
run_background().unwrap();
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn run_background_restarts_when_already_running() {
let server = FakeServer::start(200, String::new());
let home = temp_home("runbg-restart");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
let _timeout = EnvGuard::set("MOADIM_RESTART_TIMEOUT_MS", "2000");
let _poll = EnvGuard::set("MOADIM_RESTART_POLL_MS", "10");
write_pid_file().unwrap();
server.stop_after(Duration::from_millis(80));
run_background().unwrap();
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn restart_starts_fresh_when_none_running() {
let home = temp_home("restart-fresh");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, UNREACHABLE_ADDR);
restart().unwrap();
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn restart_replaces_running_server() {
let server = FakeServer::start(200, String::new());
let home = temp_home("restart-running");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
let _addr = EnvGuard::set(BIND_ADDR_ENV, &server.addr);
let _timeout = EnvGuard::set("MOADIM_RESTART_TIMEOUT_MS", "2000");
let _poll = EnvGuard::set("MOADIM_RESTART_POLL_MS", "10");
write_pid_file().unwrap();
server.stop_after(Duration::from_millis(80));
restart().unwrap();
let _ = std::fs::remove_dir_all(&home);
}