#![allow(
clippy::missing_docs_in_private_items,
reason = "test helpers and fixtures do not need doc comments"
)]
use super::*;
use crate::routines::model::{Repository, Routine};
fn make_routine(title: &str) -> Routine {
Routine {
model: None,
id: "cmd-test-id".to_string(),
schedule: "@daily".to_string(),
schedules: vec![],
title: title.to_string(),
agent: "claude".to_string(),
prompt: "do it".to_string(),
goal: None,
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,
snoozed_until: None,
skip_runs: None,
power_saving: false,
power_saving_exempt: false,
tags: vec![],
ttl_secs: None,
max_runtime_secs: None,
env: std::collections::HashMap::new(),
auto_disabled_reason: None,
consecutive_failures: 0,
failure_threshold: 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, TriggerSource::Scheduled);
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_extends_path_rather_than_replacing_it() {
let routine = make_routine("Path Extend 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, TriggerSource::Scheduled);
assert!(
cmd.contains("export PATH=$PATH:"),
"expected PATH to extend the profile's $PATH, not replace it, in: {cmd}"
);
}
#[test]
fn build_routine_command_writes_daemon_preamble_before_prompt_copy() {
let routine = make_routine("Cmd Preamble 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, TriggerSource::Scheduled);
let write = cmd.find(r#"> "$WB/CLAUDE.md" || {"#).unwrap();
assert!(
cmd.contains(
r#"> "$WB/CLAUDE.md" || { echo "moadim: failed to write agent instructions preamble; aborting launch" | tee -a "$WB/agent.log" >&2; exit 1; }"#
),
"expected the CLAUDE.md preamble write to fail-fast in: {cmd}"
);
let copy = cmd.find("/prompt.md\"").unwrap();
assert!(
write < copy,
"preamble-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 clone_repository_stmts_skips_repositories_with_auto_pull_disabled() {
let stmts = clone_repository_stmts(&[
Repository {
repository: "https://github.com/org/synced".to_string(),
branch: Some("main".to_string()),
auto_pull: true,
},
Repository {
repository: "https://github.com/org/pinned".to_string(),
branch: None,
auto_pull: false,
},
]);
let rendered = stmts.join("\n");
assert!(rendered.contains("https://github.com/org/synced"));
assert!(!rendered.contains("https://github.com/org/pinned"));
}
#[test]
fn compose_prompt_marks_repositories_with_auto_pull_disabled() {
let mut routine = make_routine("Auto Pull Opt Out");
routine.repositories = vec![Repository {
repository: "https://github.com/org/pinned".to_string(),
branch: None,
auto_pull: false,
}];
let prompt = compose_prompt(&routine);
assert!(prompt.contains("not auto-pulled"));
assert!(prompt.contains("https://github.com/org/pinned (auto-pull disabled)"));
}
include!("build_routine_command_workbench_base_tracks_moadim_home_override_tests.rs");