#![allow(
clippy::missing_docs_in_private_items,
reason = "test helpers and fixtures do not need doc comments"
)]
use super::*;
use crate::routines::{slugify, Repository, Routine};
fn scratch_dir(tag: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!("moadim-rs-{tag}-{}", uuid::Uuid::new_v4()))
}
fn with_override_home(body: impl FnOnce(&std::path::Path)) {
let home = scratch_dir("override-home-more");
std::fs::create_dir_all(&home).unwrap();
let previous = std::env::var_os("MOADIM_HOME_OVERRIDE");
unsafe {
std::env::set_var("MOADIM_HOME_OVERRIDE", &home);
}
body(&home);
unsafe {
match previous {
Some(value) => std::env::set_var("MOADIM_HOME_OVERRIDE", value),
None => std::env::remove_var("MOADIM_HOME_OVERRIDE"),
}
}
let _ = std::fs::remove_dir_all(&home);
}
fn make_routine(id: &str, title: &str) -> Routine {
Routine {
model: None,
id: id.to_string(),
schedule: "@daily".to_string(),
title: title.to_string(),
agent: "claude".to_string(),
prompt: "task".to_string(),
goal: None,
repositories: vec![Repository {
repository: "https://example.com/r.git".to_string(),
branch: Some("main".to_string()),
}],
machines: vec![crate::machine::current_machine()],
enabled: true,
source: "managed".to_string(),
created_at: 5,
updated_at: 6,
last_manual_trigger_at: None,
last_scheduled_trigger_at: None,
snoozed_until: None,
skip_runs: None,
power_saving: false,
tags: vec![],
ttl_secs: None,
max_runtime_secs: None,
env: std::collections::HashMap::new(),
}
}
#[test]
fn write_routine_leaves_no_tmp_residue() {
with_override_home(|_home| {
let id = "rs-no-residue-id";
let title = "Rs No Residue Routine";
let slug = slugify(title);
write_routine(&make_routine(id, title)).unwrap();
let residue = std::fs::read_dir(crate::paths::routine_dir(&slug))
.unwrap()
.filter_map(Result::ok)
.filter(|entry| entry.file_name().to_string_lossy().contains(".tmp"))
.count();
assert_eq!(residue, 0, "atomic_write must leave no .tmp files behind");
});
}
#[test]
fn write_routine_errors_when_schedule_cron_is_a_directory() {
with_override_home(|_home| {
let title = "Rs Cron Dir Failure";
let slug = slugify(title);
std::fs::create_dir_all(crate::paths::routine_cron_path(&slug)).unwrap();
let err = write_routine(&make_routine("rs-cron-dir-failure", title)).unwrap_err();
assert!(err.to_string().contains("directory") || err.to_string().contains("exists"));
});
}