#[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()));
let gitignore = crate::paths::config_gitignore_path();
assert!(gitignore.exists());
let content = std::fs::read_to_string(&gitignore).unwrap();
assert!(
content.contains("*.local.*"),
"gitignore must cover *.local.*"
);
assert!(
content.contains("schedule.compailed.cron"),
"gitignore must cover cron-union output"
);
assert!(
content.contains("cache/"),
"gitignore must cover repo cache"
);
assert!(
content.contains(".compailed.cron"),
"gitignore must keep covering legacy cron-union output"
);
std::fs::write(&gitignore, "*.pid\n*.log\n").unwrap();
write_pid_file().unwrap();
let content = std::fs::read_to_string(&gitignore).unwrap();
assert!(
content.contains("*.local.*"),
"missing pattern must be re-added"
);
assert!(
content.contains("schedule.compailed.cron"),
"missing cron-union pattern must be re-added"
);
assert!(
content.contains("cache/"),
"missing repo cache pattern must be re-added"
);
assert!(
content.contains(".compailed.cron"),
"missing legacy cron-union pattern must be re-added"
);
assert_eq!(
content.matches("*.pid").count(),
1,
"existing patterns must not duplicate"
);
std::fs::write(&gitignore, "*.pid\n*.log").unwrap();
write_pid_file().unwrap();
let content = std::fs::read_to_string(&gitignore).unwrap();
assert!(
content.contains("*.local.*"),
"must append after no-trailing-newline content"
);
write_pid_file().unwrap();
assert_eq!(
std::fs::read_to_string(&gitignore).unwrap(),
content,
"no-op write must not change file"
);
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());
std::fs::write(crate::paths::pid_file(), u32::MAX.to_string()).unwrap();
assert!(read_pid_file().is_none());
assert!(!crate::paths::pid_file().exists());
std::fs::write(crate::paths::pid_file(), std::process::id().to_string()).unwrap();
assert_eq!(read_pid_file(), Some(std::process::id()));
let _ = std::fs::remove_dir_all(&home);
}
#[test]
fn write_pid_file_seeds_readmes_without_clobbering_edits() {
let home = temp_home("pidfile-readme");
let _home = EnvGuard::set("MOADIM_HOME_OVERRIDE", home.to_str().unwrap());
write_pid_file().unwrap();
let config_readme = crate::paths::config_readme_path();
let routines_readme = crate::paths::routines_readme_path();
let agents_readme = crate::paths::agents_readme_path();
assert!(config_readme.exists());
assert!(routines_readme.exists());
assert!(agents_readme.exists());
assert!(std::fs::read_to_string(&config_readme)
.unwrap()
.contains("moadim config"));
assert!(std::fs::read_to_string(&routines_readme)
.unwrap()
.contains("moadim routines"));
assert!(std::fs::read_to_string(&agents_readme)
.unwrap()
.contains("moadim agents"));
std::fs::write(&config_readme, "custom notes").unwrap();
std::fs::write(&routines_readme, "custom notes").unwrap();
std::fs::write(&agents_readme, "custom notes").unwrap();
write_pid_file().unwrap();
assert_eq!(
std::fs::read_to_string(&config_readme).unwrap(),
"custom notes"
);
assert_eq!(
std::fs::read_to_string(&routines_readme).unwrap(),
"custom notes"
);
assert_eq!(
std::fs::read_to_string(&agents_readme).unwrap(),
"custom notes"
);
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);
}
#[allow(
clippy::missing_docs_in_private_items,
reason = "split-out module keeps the file under the linecheck limit"
)]
#[path = "run_background_restarts_when_already_running_tests.rs"]
mod run_background_restarts_when_already_running_tests;