#![allow(clippy::missing_docs_in_private_items)]
use super::*;
use crate::routines::model::Routine;
fn make_routine(title: &str) -> Routine {
Routine {
id: "cmd-test-id".to_string(),
schedule: "@daily".to_string(),
title: title.to_string(),
agent: "claude".to_string(),
prompt: "do it".to_string(),
repositories: vec![],
machines: vec![crate::machine::current_machine()],
enabled: true,
source: "managed".to_string(),
created_at: 0,
updated_at: 0,
last_manual_trigger_at: None,
last_scheduled_trigger_at: None,
ttl_secs: None,
max_runtime_secs: None,
}
}
fn with_path(value: &std::path::Path, body: impl FnOnce()) {
let saved = std::env::var_os("PATH");
unsafe {
std::env::set_var("PATH", value);
}
body();
unsafe {
match saved {
Some(prev) => std::env::set_var("PATH", prev),
None => std::env::remove_var("PATH"),
}
}
}
#[test]
fn build_routine_command_resolves_bin_dir_when_tool_on_path() {
let dir = std::env::temp_dir().join(format!("moadim-cmd-path-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let tmux = dir.join("tmux");
std::fs::write(&tmux, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(&tmux, std::fs::Permissions::from_mode(0o755)).unwrap();
}
let dir_str = dir.to_string_lossy().into_owned();
with_path(&dir, || {
let routine = make_routine("Cmd Path Routine");
let agent = AgentCommand {
command: "claude".to_string(),
args: vec![],
instructions_file: "CLAUDE.md".to_string(),
setup: None,
};
let cmd = build_routine_command(&routine, &agent);
assert!(
cmd.contains(&dir_str),
"expected resolved tmux dir {dir_str} in: {cmd}"
);
});
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn build_routine_command_stamps_scheduled_trigger_sidecar() {
let routine = make_routine("Cmd Scheduled Stamp Routine");
let agent = AgentCommand {
command: "claude".to_string(),
args: vec![],
instructions_file: "CLAUDE.md".to_string(),
setup: None,
};
let cmd = build_routine_command(&routine, &agent);
let sidecar = crate::paths::routine_scheduled_state_path(&slugify(&routine.title))
.to_string_lossy()
.into_owned();
assert!(
cmd.contains(&format!(
r#"printf 'last_scheduled_trigger_at = %s\n' "$TS" > {} || true"#,
shell_quote(&sidecar)
)),
"expected scheduled-trigger sidecar stamp in: {cmd}"
);
let stamp = cmd.find("last_scheduled_trigger_at").unwrap();
let copy = cmd.find("/prompt.md\"").unwrap();
assert!(stamp < copy, "sidecar stamp must precede the prompt copy");
}
#[test]
fn build_routine_command_fail_fasts_when_disclosure_write_fails() {
let routine = make_routine("Cmd Disclosure Guard Routine");
let agent = AgentCommand {
command: "claude".to_string(),
args: vec![],
instructions_file: "CLAUDE.md".to_string(),
setup: None,
};
let cmd = build_routine_command(&routine, &agent);
let write = cmd.find(r#"> "$WB/CLAUDE.md" || {"#).unwrap();
assert!(
cmd.contains(
r#"> "$WB/CLAUDE.md" || { echo "moadim: failed to write agent instructions disclosure; aborting launch" | tee -a "$WB/agent.log" >&2; exit 1; }"#
),
"expected the CLAUDE.md disclosure write to fail-fast in: {cmd}"
);
let copy = cmd.find("/prompt.md\"").unwrap();
assert!(
write < copy,
"disclosure-write guard must precede the prompt copy"
);
assert!(
cmd.contains(r#">> "$WB/CLAUDE.md" || true"#),
"user-prompt append must remain best-effort in: {cmd}"
);
}
#[test]
fn cron_path_falls_back_to_root_home_when_home_unset() {
let saved = std::env::var_os("HOME");
unsafe {
std::env::remove_var("HOME");
}
let path = cron_path("definitely-not-a-real-binary-xyz");
assert!(
path.contains("/root/.local/bin"),
"expected /root-anchored fallback dirs in: {path}"
);
unsafe {
match saved {
Some(prev) => std::env::set_var("HOME", prev),
None => std::env::remove_var("HOME"),
}
}
}
#[test]
fn tmux_available_in_true_when_fake_tmux_present() {
let dir = std::env::temp_dir().join(format!("moadim-tmux-present-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let tmux = dir.join("tmux");
std::fs::write(&tmux, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(&tmux, std::fs::Permissions::from_mode(0o755)).unwrap();
}
assert!(tmux_available_in(&dir.to_string_lossy()));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn tmux_available_in_false_when_dir_has_no_tmux() {
let dir = std::env::temp_dir().join(format!("moadim-tmux-missing-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
assert!(!tmux_available_in(&dir.to_string_lossy()));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn tmux_available_reads_live_path_present() {
let dir = std::env::temp_dir().join(format!("moadim-tmux-live-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let tmux = dir.join("tmux");
std::fs::write(&tmux, "#!/bin/sh\n").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(&tmux, std::fs::Permissions::from_mode(0o755)).unwrap();
}
with_path(&dir, || assert!(tmux_available()));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn tmux_available_false_when_path_unset() {
let saved = std::env::var_os("PATH");
unsafe {
std::env::remove_var("PATH");
}
assert!(!tmux_available());
unsafe {
if let Some(prev) = saved {
std::env::set_var("PATH", prev);
}
}
}
#[test]
fn bin_dir_returns_none_when_path_unset() {
let saved = std::env::var_os("PATH");
unsafe {
std::env::remove_var("PATH");
}
assert!(bin_dir("definitely-not-a-real-binary-xyz").is_none());
unsafe {
if let Some(prev) = saved {
std::env::set_var("PATH", prev);
}
}
}