#![allow(clippy::missing_docs_in_private_items)]
use super::*;
use crate::routines::{slugify, Repository, Routine};
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,
tags: vec![],
ttl_secs: None,
max_runtime_secs: None,
}
}
#[test]
fn load_store_from_dir_inserts_written_routines() {
with_override_home(|_home| {
write_routine(&make_routine("rs-loadstore-id", "Rs Loadstore Routine")).unwrap();
std::fs::write(crate::paths::routines_dir().join("stray.txt"), b"x").unwrap();
let store = load_store_from_dir(&crate::paths::routines_dir());
assert!(store
.lock()
.unwrap()
.values()
.any(|routine| routine.id == "rs-loadstore-id"));
});
}
#[test]
fn write_then_load_round_trips() {
with_override_home(|_home| {
let id = "rs-roundtrip-id";
let title = "Rs Roundtrip Routine";
let slug = slugify(title);
let routine = make_routine(id, title);
write_routine(&routine).unwrap();
assert!(crate::paths::routine_toml_path(&slug).exists());
assert!(crate::paths::routine_pure_prompt_path(&slug).exists());
assert!(crate::paths::routine_compiled_prompt_path(&slug).exists());
assert!(crate::paths::routine_gitignore_path(&slug).exists());
let toml_text = std::fs::read_to_string(crate::paths::routine_toml_path(&slug)).unwrap();
assert!(
!toml_text.contains("prompt"),
"routine.toml must not carry the prompt: {toml_text}"
);
assert_eq!(
std::fs::read_to_string(crate::paths::routine_pure_prompt_path(&slug)).unwrap(),
"task"
);
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.id, id);
assert_eq!(loaded.schedule, "@daily");
assert_eq!(loaded.title, title);
assert_eq!(loaded.agent, "claude");
assert_eq!(loaded.prompt, "task");
assert_eq!(loaded.repositories.len(), 1);
assert_eq!(loaded.repositories[0].branch.as_deref(), Some("main"));
assert!(loaded.enabled);
remove_routine_dir(&slug).unwrap();
assert!(!crate::paths::routine_dir(&slug).exists());
});
}
#[test]
fn tags_round_trip_through_routine_toml() {
let title = "Rs Tags Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-tags-id", title);
routine.tags = vec!["triage".to_string(), "nightly".to_string()];
write_routine(&routine).unwrap();
let toml_text = std::fs::read_to_string(crate::paths::routine_toml_path(&slug)).unwrap();
assert!(toml_text.contains("tags"), "routine.toml should carry tags");
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(
loaded.tags,
vec!["triage".to_string(), "nightly".to_string()]
);
remove_routine_dir(&slug).unwrap();
}
#[test]
fn prompt_file_contains_composed_prompt() {
with_override_home(|_home| {
let title = "Rs Prompt Routine";
let slug = slugify(title);
write_routine(&make_routine("rs-prompt-id", title)).unwrap();
let prompt =
std::fs::read_to_string(crate::paths::routine_compiled_prompt_path(&slug)).unwrap();
assert!(prompt.contains("# Workbench"));
assert!(prompt.contains("https://example.com/r.git (branch main)"));
assert!(prompt.contains("task"));
});
}
#[test]
fn write_routine_persists_composed_prompt_sidecar_with_repos() {
with_override_home(|_home| {
let id = "rs-prompt-sidecar-id";
let title = "Rs Prompt Sidecar Routine";
let slug = slugify(title);
let mut routine = make_routine(id, title);
routine.prompt = "line one\nline two".to_string();
routine.repositories = vec![
Repository {
repository: "https://example.com/a.git".to_string(),
branch: Some("dev".to_string()),
},
Repository {
repository: "https://example.com/b.git".to_string(),
branch: None,
},
];
write_routine(&routine).unwrap();
let written =
std::fs::read_to_string(crate::paths::routine_compiled_prompt_path(&slug)).unwrap();
assert_eq!(written, compose_prompt(&routine));
assert!(written.contains("https://example.com/a.git (branch dev)"));
assert!(written.contains("https://example.com/b.git\n"));
assert!(written.contains("line one\nline two"));
let pure = std::fs::read_to_string(crate::paths::routine_pure_prompt_path(&slug)).unwrap();
assert_eq!(pure, "line one\nline two");
});
}
#[test]
fn write_routine_errors_when_prompt_sidecar_write_fails() {
with_override_home(|_home| {
let id = "rs-prompt-write-fail-id";
let title = "Rs Prompt Write Fail Routine";
let slug = slugify(title);
let dir = crate::paths::routine_dir(&slug);
std::fs::create_dir_all(&dir).unwrap();
let prompt_dir = crate::paths::routine_pure_prompt_path(&slug);
std::fs::create_dir_all(&prompt_dir).unwrap();
std::fs::write(prompt_dir.join("occupant"), "keep me non-empty").unwrap();
let err = write_routine(&make_routine(id, title)).unwrap_err();
let _ = err;
assert!(crate::paths::routine_toml_path(&slug).exists());
assert!(
prompt_dir.is_dir(),
"the blocking prompt dir is left in place"
);
});
}
#[test]
fn write_routine_errors_when_compiled_prompt_sidecar_write_fails() {
with_override_home(|_home| {
let id = "rs-compiled-prompt-write-fail-id";
let title = "Rs Compiled Prompt Write Fail Routine";
let slug = slugify(title);
let dir = crate::paths::routine_dir(&slug);
std::fs::create_dir_all(&dir).unwrap();
let prompt_dir = crate::paths::routine_compiled_prompt_path(&slug);
std::fs::create_dir_all(&prompt_dir).unwrap();
std::fs::write(prompt_dir.join("occupant"), "keep me non-empty").unwrap();
let err = write_routine(&make_routine(id, title)).unwrap_err();
let _ = err;
assert!(crate::paths::routine_toml_path(&slug).exists());
assert!(crate::paths::routine_pure_prompt_path(&slug).exists());
assert!(
prompt_dir.is_dir(),
"the blocking prompt dir is left in place"
);
});
}
#[test]
fn load_routine_from_dir_applies_defaults_for_absent_optional_fields() {
with_override_home(|_home| {
let slug = "rs-defaults-routine";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"schedule = \"@daily\"\ntitle = \"Rs Defaults Routine\"\nagent = \"claude\"\n",
)
.unwrap();
let loaded = load_routine_from_dir(slug).unwrap();
assert_eq!(loaded.id, slug, "absent id falls back to the dir name");
assert_eq!(loaded.prompt, "", "absent prompt defaults to empty");
assert!(loaded.enabled, "absent enabled defaults to true");
assert_eq!(loaded.created_at, 0);
assert_eq!(loaded.updated_at, 0);
assert!(loaded.repositories.is_empty());
});
}
#[test]
fn load_routine_from_dir_missing_returns_none() {
with_override_home(|_home| {
assert!(load_routine_from_dir("rs-does-not-exist-zzz").is_none());
});
}
#[test]
fn last_manual_trigger_at_persists_to_log_not_routine_toml() {
with_override_home(|_home| {
let title = "Rs Sidecar Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-sidecar-id", title);
routine.last_manual_trigger_at = Some(12345);
write_routine(&routine).unwrap();
crate::routine_storage::append_manual_trigger_log(&slug, 12345);
let toml_text = std::fs::read_to_string(crate::paths::routine_toml_path(&slug)).unwrap();
assert!(
!toml_text.contains("last_manual_trigger_at"),
"routine.toml must not carry runtime trigger state: {toml_text}"
);
assert!(crate::paths::routine_manual_log_path(&slug).exists());
let log_text =
std::fs::read_to_string(crate::paths::routine_manual_log_path(&slug)).unwrap();
assert!(
log_text.trim() == "12345",
"manual.log must contain the timestamp: {log_text}"
);
assert_eq!(
load_routine_from_dir(&slug).unwrap().last_manual_trigger_at,
Some(12345)
);
});
}
#[test]
fn write_routine_clears_stale_sidecar_when_untriggered() {
with_override_home(|_home| {
let title = "Rs Clear Sidecar Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-clear-id", title);
write_routine(&routine).unwrap();
assert!(
!crate::paths::routine_state_path(&slug).exists(),
"state.local.toml must not be written when there is no snooze/skip-runs state"
);
routine.snoozed_until = Some(9999);
write_routine(&routine).unwrap();
assert!(crate::paths::routine_state_path(&slug).exists());
routine.snoozed_until = None;
write_routine(&routine).unwrap();
assert!(
!crate::paths::routine_state_path(&slug).exists(),
"sidecar should be removed when there is no snooze/skip-runs state"
);
assert_eq!(
load_routine_from_dir(&slug).unwrap().last_manual_trigger_at,
None
);
});
}
#[test]
fn load_routine_falls_back_to_legacy_last_triggered_in_routine_toml() {
with_override_home(|_home| {
let slug = "rs-legacy-trigger-routine";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"schedule = \"@daily\"\ntitle = \"Rs Legacy Trigger\"\nagent = \"claude\"\nlast_triggered_at = 777\n",
)
.unwrap();
assert!(!crate::paths::routine_state_path(slug).exists());
assert_eq!(
load_routine_from_dir(slug).unwrap().last_manual_trigger_at,
Some(777)
);
});
}
#[test]
fn load_routine_ignores_unparsable_sidecar() {
with_override_home(|_home| {
let slug = "rs-bad-sidecar-routine";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"schedule = \"@daily\"\ntitle = \"Rs Bad Sidecar\"\nagent = \"claude\"\n",
)
.unwrap();
std::fs::write(crate::paths::routine_state_path(slug), "= not valid toml =").unwrap();
assert_eq!(
load_routine_from_dir(slug).unwrap().last_manual_trigger_at,
None
);
});
}
#[test]
fn load_routine_reads_scheduled_trigger_from_log() {
with_override_home(|_home| {
let title = "Rs Scheduled Sidecar Routine";
let slug = slugify(title);
write_routine(&make_routine("rs-scheduled-id", title)).unwrap();
std::fs::write(
crate::paths::routine_scheduled_log_path(&slug),
"1000\n4242\n",
)
.unwrap();
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.last_scheduled_trigger_at, Some(4242));
assert_eq!(loaded.last_manual_trigger_at, None);
});
}
#[test]
fn load_routine_ignores_unparsable_scheduled_log() {
with_override_home(|_home| {
let slug = "rs-bad-scheduled-sidecar-routine";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"schedule = \"@daily\"\ntitle = \"Rs Bad Scheduled Sidecar\"\nagent = \"claude\"\n",
)
.unwrap();
std::fs::write(
crate::paths::routine_scheduled_log_path(slug),
"not a timestamp\n",
)
.unwrap();
assert_eq!(
load_routine_from_dir(slug)
.unwrap()
.last_scheduled_trigger_at,
None
);
});
}
#[test]
fn write_routine_preserves_scheduler_written_scheduled_log() {
with_override_home(|_home| {
let title = "Rs Preserve Scheduled Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-preserve-scheduled-id", title);
write_routine(&routine).unwrap();
std::fs::write(crate::paths::routine_scheduled_log_path(&slug), "55\n").unwrap();
routine.last_manual_trigger_at = Some(7);
write_routine(&routine).unwrap();
crate::routine_storage::append_manual_trigger_log(&slug, 7);
assert!(
crate::paths::routine_scheduled_log_path(&slug).exists(),
"daemon write must not remove the scheduler-owned log"
);
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.last_scheduled_trigger_at, Some(55));
assert_eq!(loaded.last_manual_trigger_at, Some(7));
});
}
#[test]
fn torn_routine_toml_loads_as_none() {
with_override_home(|_home| {
let slug = "rs-torn-toml-routine";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(crate::paths::routine_toml_path(slug), "id = \"x\"\nschedu").unwrap();
assert!(load_routine_from_dir(slug).is_none());
});
}
#[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 load_store_includes_written_routine() {
with_override_home(|_home| {
let id = "rs-loadstore-id";
let title = "Rs Loadstore Routine";
write_routine(&make_routine(id, title)).unwrap();
let store = load_store();
assert!(store.lock().unwrap().contains_key(id));
});
}
#[test]
fn load_store_from_dir_skips_unloadable_dirs() {
with_override_home(|_home| {
write_routine(&make_routine("rs-valid-id", "Rs Valid Routine")).unwrap();
let bad_dir = crate::paths::routine_dir("rs-bad-toml");
std::fs::create_dir_all(&bad_dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path("rs-bad-toml"),
"id = \"x\"\nschedu",
)
.unwrap();
let empty_dir = crate::paths::routine_dir("rs-no-toml");
std::fs::create_dir_all(&empty_dir).unwrap();
let store = load_store_from_dir(&crate::paths::routines_dir());
let guard = store.lock().unwrap();
assert!(guard.values().any(|routine| routine.id == "rs-valid-id"));
assert!(!guard.contains_key("rs-bad-toml"));
assert!(!guard.contains_key("rs-no-toml"));
});
}
#[test]
fn load_store_from_dir_missing_dir_empty() {
let store = load_store_from_dir(std::path::Path::new("/nonexistent-routines-dir-99999"));
assert!(store.lock().unwrap().is_empty());
}
#[test]
fn remove_routine_dir_noop_when_absent() {
with_override_home(|_home| {
remove_routine_dir("rs-never-created-zzz").unwrap();
});
}
#[test]
fn migrate_routine_dirs_moves_legacy_uuid_dir_to_slug() {
with_override_home(|_home| {
let id = "rs-legacy-uuid-1234";
let title = "Rs Legacy Migrate Routine";
let slug = slugify(title);
let legacy_dir = crate::paths::routine_dir(id);
std::fs::create_dir_all(&legacy_dir).unwrap();
let toml = format!(
"id = \"{id}\"\nschedule = \"@daily\"\ntitle = \"{title}\"\nagent = \"claude\"\nprompt = \"task\"\nenabled = true\n"
);
std::fs::write(legacy_dir.join("routine.toml"), toml).unwrap();
std::fs::write(legacy_dir.join("prompt.md"), "legacy prompt").unwrap();
migrate_routine_dirs();
assert!(!legacy_dir.exists(), "legacy UUID dir should be removed");
assert!(crate::paths::routine_toml_path(&slug).exists());
assert!(crate::paths::routine_compiled_prompt_path(&slug).exists());
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.id, id, "UUID id preserved across the dir migration");
assert_eq!(loaded.prompt, "task");
});
}
#[test]
fn repersist_routines_recreates_missing_prompt_sidecar() {
with_override_home(|_home| {
let id = "rs-repersist-id";
let title = "Rs Repersist Routine";
let slug = slugify(title);
write_routine(&make_routine(id, title)).unwrap();
std::fs::remove_file(crate::paths::routine_compiled_prompt_path(&slug)).unwrap();
assert!(!crate::paths::routine_compiled_prompt_path(&slug).exists());
let mut map = HashMap::new();
map.insert(id.to_string(), make_routine(id, title));
let store = Arc::new(Mutex::new(map));
repersist_routines(&store);
assert!(
crate::paths::routine_compiled_prompt_path(&slug).exists(),
"repersist should recreate the prompt sidecar"
);
});
}
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");
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);
}
#[test]
fn migrate_prompt_files_from_dir_missing_dir_returns() {
let missing = scratch_dir("prompt-missing");
migrate_prompt_files_from_dir(&missing);
assert!(!missing.exists());
}
#[test]
fn migrate_prompt_files_from_dir_renames_txt_and_skips_non_dirs_and_existing() {
let dir = scratch_dir("prompt-rename");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("loose.txt"), "ignore me").unwrap();
let renameable = dir.join("renameable");
std::fs::create_dir_all(&renameable).unwrap();
std::fs::write(renameable.join("prompt.txt"), "old body").unwrap();
let already = dir.join("already");
std::fs::create_dir_all(&already).unwrap();
std::fs::write(already.join("prompt.txt"), "stale").unwrap();
std::fs::write(already.join("prompt.md"), "current").unwrap();
migrate_prompt_files_from_dir(&dir);
assert!(!renameable.join("prompt.txt").exists());
assert_eq!(
std::fs::read_to_string(renameable.join("prompt.md")).unwrap(),
"old body"
);
assert!(already.join("prompt.txt").exists());
assert_eq!(
std::fs::read_to_string(already.join("prompt.md")).unwrap(),
"current"
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn migrate_prompt_files_from_dir_logs_on_rename_failure() {
use std::os::unix::fs::PermissionsExt;
let dir = scratch_dir("prompt-rename-fail");
std::fs::create_dir_all(&dir).unwrap();
let locked = dir.join("locked");
std::fs::create_dir_all(&locked).unwrap();
std::fs::write(locked.join("prompt.txt"), "body").unwrap();
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_prompt_files_from_dir(&dir);
std::fs::set_permissions(&locked, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(locked.join("prompt.txt").exists());
assert!(!locked.join("prompt.md").exists());
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn migrate_prompt_files_public_wrapper_runs() {
with_override_home(|_home| {
migrate_prompt_files();
});
}
#[test]
fn migrate_prompts_to_subfolder_from_dir_missing_dir_returns() {
let missing = scratch_dir("prompts-subfolder-missing");
migrate_prompts_to_subfolder_from_dir(&missing);
assert!(!missing.exists());
}
#[test]
fn migrate_prompts_to_subfolder_from_dir_migrates_legacy_layout() {
let dir = scratch_dir("prompts-subfolder-migrate");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("loose.txt"), "ignore me").unwrap();
let legacy = dir.join("legacy-routine");
std::fs::create_dir_all(&legacy).unwrap();
std::fs::write(legacy.join("prompt.md"), "old composed body").unwrap();
std::fs::write(
legacy.join("routine.toml"),
"title = \"Legacy\"\nschedule = \"@daily\"\nagent = \"claude\"\nprompt = \"raw prompt\"\n",
)
.unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
assert!(
!legacy.join("prompt.md").exists(),
"top-level prompt.md should be moved"
);
assert_eq!(
std::fs::read_to_string(legacy.join("prompts").join("prompt.compiled.md")).unwrap(),
"old composed body"
);
assert_eq!(
std::fs::read_to_string(legacy.join("prompts").join("prompt.pure.md")).unwrap(),
"raw prompt"
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn migrate_prompts_to_subfolder_from_dir_skips_already_migrated() {
let dir = scratch_dir("prompts-subfolder-skip");
std::fs::create_dir_all(&dir).unwrap();
let routine = dir.join("already-migrated");
let prompts = routine.join("prompts");
std::fs::create_dir_all(&prompts).unwrap();
std::fs::write(prompts.join("prompt.compiled.md"), "compiled").unwrap();
std::fs::write(prompts.join("prompt.pure.md"), "pure").unwrap();
std::fs::write(
routine.join("routine.toml"),
"title = \"Already\"\nschedule = \"@daily\"\nagent = \"claude\"\n",
)
.unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
assert_eq!(
std::fs::read_to_string(prompts.join("prompt.compiled.md")).unwrap(),
"compiled"
);
assert_eq!(
std::fs::read_to_string(prompts.join("prompt.pure.md")).unwrap(),
"pure"
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn migrate_prompts_to_subfolder_from_dir_defaults_missing_legacy_prompt_to_empty() {
let dir = scratch_dir("prompts-subfolder-no-legacy");
std::fs::create_dir_all(&dir).unwrap();
let routine = dir.join("no-legacy-prompt");
std::fs::create_dir_all(&routine).unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
assert_eq!(
std::fs::read_to_string(routine.join("prompts").join("prompt.pure.md")).unwrap(),
""
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn migrate_prompts_to_subfolder_from_dir_logs_on_create_dir_failure() {
let dir = scratch_dir("prompts-subfolder-create-fail");
std::fs::create_dir_all(&dir).unwrap();
let routine = dir.join("blocked-routine");
std::fs::create_dir_all(&routine).unwrap();
std::fs::write(routine.join("prompts"), "i block the prompts dir").unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
assert!(
routine.join("prompts").is_file(),
"the blocking file is left in place"
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn migrate_prompts_to_subfolder_from_dir_logs_on_rename_failure() {
use std::os::unix::fs::PermissionsExt;
let dir = scratch_dir("prompts-subfolder-rename-fail");
std::fs::create_dir_all(&dir).unwrap();
let routine = dir.join("rename-fail-routine");
std::fs::create_dir_all(routine.join("prompts")).unwrap();
std::fs::write(routine.join("prompt.md"), "old composed body").unwrap();
std::fs::write(routine.join("prompts").join("prompt.pure.md"), "pure").unwrap();
std::fs::set_permissions(&routine, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
std::fs::set_permissions(&routine, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
routine.join("prompt.md").exists(),
"the rename could not happen, so the old file remains"
);
assert!(!routine.join("prompts").join("prompt.compiled.md").exists());
std::fs::remove_dir_all(&dir).unwrap();
}
#[cfg(unix)]
#[test]
fn migrate_prompts_to_subfolder_from_dir_logs_on_pure_write_failure() {
use std::os::unix::fs::PermissionsExt;
let dir = scratch_dir("prompts-subfolder-write-fail");
std::fs::create_dir_all(&dir).unwrap();
let routine = dir.join("write-fail-routine");
let prompts = routine.join("prompts");
std::fs::create_dir_all(&prompts).unwrap();
std::fs::write(
routine.join("routine.toml"),
"title = \"Write Fail\"\nschedule = \"@daily\"\nagent = \"claude\"\nprompt = \"raw\"\n",
)
.unwrap();
std::fs::set_permissions(&prompts, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_prompts_to_subfolder_from_dir(&dir);
std::fs::set_permissions(&prompts, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
!prompts.join("prompt.pure.md").exists(),
"the write could not happen"
);
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn migrate_prompts_to_subfolder_public_wrapper_runs() {
with_override_home(|_home| {
migrate_prompts_to_subfolder();
});
}
#[test]
fn migrate_routine_dirs_from_dir_missing_dir_returns() {
let missing = scratch_dir("migrate-missing");
migrate_routine_dirs_from_dir(&missing);
assert!(!missing.exists());
}
#[test]
fn migrate_routine_dirs_from_dir_skips_non_dir_and_unparsable() {
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
std::fs::write(routines.join("stray.txt"), "ignore me").unwrap();
let garbage = routines.join("garbage-dir");
std::fs::create_dir_all(&garbage).unwrap();
std::fs::write(garbage.join("routine.toml"), "id = \"x\"\nschedu").unwrap();
migrate_routine_dirs_from_dir(&routines);
assert!(routines.join("stray.txt").exists());
assert!(garbage.join("routine.toml").exists());
});
}
#[test]
fn migrate_routine_dirs_from_dir_skips_already_canonical_dir() {
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
let title = "Rs Canonical Routine";
let slug = slugify(title);
write_routine(&make_routine("rs-canonical-id", title)).unwrap();
assert!(routines.join(&slug).is_dir());
migrate_routine_dirs_from_dir(&routines);
assert!(crate::paths::routine_toml_path(&slug).exists());
assert_eq!(load_routine_from_dir(&slug).unwrap().id, "rs-canonical-id");
});
}
#[test]
fn migrate_routine_dirs_from_dir_migrates_legacy_dir() {
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
let id = "rs-inner-legacy-uuid";
let title = "Rs Inner Legacy Routine";
let slug = slugify(title);
let legacy_dir = crate::paths::routine_dir(id);
std::fs::create_dir_all(&legacy_dir).unwrap();
let toml = format!(
"id = \"{id}\"\nschedule = \"@daily\"\ntitle = \"{title}\"\nagent = \"claude\"\nprompt = \"task\"\nenabled = true\n"
);
std::fs::write(legacy_dir.join("routine.toml"), toml).unwrap();
std::fs::write(legacy_dir.join("prompt.md"), "legacy prompt").unwrap();
migrate_routine_dirs_from_dir(&routines);
assert!(!legacy_dir.exists(), "legacy UUID dir should be removed");
assert!(crate::paths::routine_toml_path(&slug).exists());
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.id, id, "UUID id preserved across the dir migration");
});
}
#[cfg(unix)]
#[test]
fn migrate_routine_dirs_from_dir_logs_on_remove_failure() {
use std::os::unix::fs::PermissionsExt;
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
let id = "rs-remove-fail-uuid";
let title = "Rs Remove Fail Routine";
let slug = slugify(title);
let legacy_dir = crate::paths::routine_dir(id);
std::fs::create_dir_all(&legacy_dir).unwrap();
let toml = format!(
"id = \"{id}\"\nschedule = \"@daily\"\ntitle = \"{title}\"\nagent = \"claude\"\nprompt = \"task\"\nenabled = true\n"
);
std::fs::write(legacy_dir.join("routine.toml"), &toml).unwrap();
std::fs::write(legacy_dir.join("prompt.md"), "legacy").unwrap();
std::fs::set_permissions(&legacy_dir, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_routine_dirs_from_dir(&routines);
assert!(crate::paths::routine_toml_path(&slug).exists());
std::fs::set_permissions(&legacy_dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(legacy_dir.exists(), "legacy dir survives a failed removal");
});
}
#[test]
fn migrate_routine_dirs_from_dir_logs_on_write_failure() {
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
let id = "rs-write-fail-uuid";
let title = "Rs Write Fail Routine";
let slug = slugify(title);
let legacy_dir = crate::paths::routine_dir(id);
std::fs::create_dir_all(&legacy_dir).unwrap();
let toml = format!(
"id = \"{id}\"\nschedule = \"@daily\"\ntitle = \"{title}\"\nagent = \"claude\"\nprompt = \"task\"\nenabled = true\n"
);
std::fs::write(legacy_dir.join("routine.toml"), toml).unwrap();
std::fs::write(legacy_dir.join("prompt.md"), "legacy").unwrap();
std::fs::write(routines.join(&slug), "i block the slug dir").unwrap();
migrate_routine_dirs_from_dir(&routines);
assert!(legacy_dir.exists(), "legacy dir is left when write fails");
assert!(routines.join(&slug).is_file());
});
}
#[test]
fn migrate_routine_dirs_public_wrapper_runs() {
with_override_home(|_home| {
migrate_routine_dirs();
});
}
#[test]
fn repersist_routines_logs_on_write_failure() {
with_override_home(|_home| {
let routines = crate::paths::routines_dir();
std::fs::create_dir_all(&routines).unwrap();
let id = "rs-repersist-fail-id";
let title = "Rs Repersist Fail Routine";
let slug = slugify(title);
std::fs::write(routines.join(&slug), "block").unwrap();
let mut map = HashMap::new();
map.insert(id.to_string(), make_routine(id, title));
let store = Arc::new(Mutex::new(map));
repersist_routines(&store);
assert!(routines.join(&slug).is_file());
});
}
#[test]
fn load_routine_from_dir_missing_title_returns_none() {
with_override_home(|_home| {
let slug = "rs-no-title-zzz";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"schedule = \"@daily\"\nagent = \"claude\"\n",
)
.unwrap();
assert!(load_routine_from_dir(slug).is_none());
});
}
#[test]
fn load_routine_from_dir_missing_schedule_returns_none() {
with_override_home(|_home| {
let slug = "rs-no-schedule-zzz";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"title = \"Rs No Schedule\"\nagent = \"claude\"\n",
)
.unwrap();
assert!(load_routine_from_dir(slug).is_none());
});
}
#[test]
fn load_routine_from_dir_missing_agent_returns_none() {
with_override_home(|_home| {
let slug = "rs-no-agent-zzz";
let dir = crate::paths::routine_dir(slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_toml_path(slug),
"title = \"Rs No Agent\"\nschedule = \"@daily\"\n",
)
.unwrap();
assert!(load_routine_from_dir(slug).is_none());
});
}
#[cfg(unix)]
#[test]
fn write_routine_fails_on_gitignore_write_error() {
use std::os::unix::fs::PermissionsExt as _;
with_override_home(|_home| {
let title = "Rs Gitignore Fail Routine";
let slug = slugify(title);
let dir = crate::paths::routine_dir(&slug);
std::fs::create_dir_all(crate::paths::routine_prompts_dir(&slug)).unwrap();
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap();
let result = write_routine(&make_routine("rs-gitignore-fail-id", title));
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
result.is_err(),
"write_routine should fail when .gitignore cannot be written"
);
});
}
#[cfg(unix)]
#[test]
fn write_routine_fails_on_routine_toml_write_error() {
use std::os::unix::fs::PermissionsExt as _;
with_override_home(|_home| {
let title = "Rs Toml Write Fail Routine";
let slug = slugify(title);
let dir = crate::paths::routine_dir(&slug);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
crate::paths::routine_gitignore_path(&slug),
"*.local.*\n*.log\nrun.sh\n",
)
.unwrap();
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap();
let result = write_routine(&make_routine("rs-toml-write-fail-id", title));
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
result.is_err(),
"write_routine should fail when routine.toml cannot be written"
);
});
}
#[test]
fn write_routine_fails_on_runtime_state_write_error() {
with_override_home(|_home| {
let title = "Rs Runtime State Write Fail Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-runtime-state-write-fail-id", title);
routine.last_manual_trigger_at = Some(12345);
let state_path = crate::paths::routine_state_path(&slug);
std::fs::create_dir_all(&state_path).unwrap();
std::fs::write(state_path.join("occupant"), "block").unwrap();
let result = write_routine(&routine);
std::fs::remove_dir_all(&state_path).unwrap();
assert!(
result.is_err(),
"write_routine should fail when state sidecar cannot be written"
);
});
}
#[test]
fn write_runtime_state_fails_when_state_file_is_a_directory() {
with_override_home(|_home| {
let title = "Rs Remove State Dir Fail Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-remove-state-dir-id", title);
routine.last_manual_trigger_at = None;
write_routine(&routine).unwrap();
let state_path = crate::paths::routine_state_path(&slug);
std::fs::create_dir_all(&state_path).unwrap();
let result = write_routine(&routine);
std::fs::remove_dir_all(&state_path).unwrap();
assert!(
result.is_err(),
"write_routine should fail when state.local.toml is a directory"
);
});
}
#[test]
fn snooze_fields_round_trip_through_sidecar_not_routine_toml() {
with_override_home(|_home| {
let title = "Rs Snooze Sidecar Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-snooze-sidecar-id", title);
routine.snoozed_until = Some(999_999);
write_routine(&routine).unwrap();
let toml_text = std::fs::read_to_string(crate::paths::routine_toml_path(&slug)).unwrap();
assert!(
!toml_text.contains("snoozed_until"),
"routine.toml must not carry snooze state: {toml_text}"
);
let state_text = std::fs::read_to_string(crate::paths::routine_state_path(&slug)).unwrap();
assert!(state_text.contains("snoozed_until"));
let loaded = load_routine_from_dir(&slug).unwrap();
assert_eq!(loaded.snoozed_until, Some(999_999));
assert_eq!(loaded.skip_runs, None);
});
}
#[test]
fn skip_runs_round_trips_and_clearing_both_removes_sidecar() {
with_override_home(|_home| {
let title = "Rs Skip Runs Sidecar Routine";
let slug = slugify(title);
let mut routine = make_routine("rs-skip-runs-sidecar-id", title);
routine.skip_runs = Some(3);
write_routine(&routine).unwrap();
assert!(crate::paths::routine_state_path(&slug).exists());
assert_eq!(load_routine_from_dir(&slug).unwrap().skip_runs, Some(3));
routine.skip_runs = None;
write_routine(&routine).unwrap();
assert!(
!crate::paths::routine_state_path(&slug).exists(),
"sidecar should be removed once no runtime state (trigger or snooze) remains"
);
assert_eq!(load_routine_from_dir(&slug).unwrap().skip_runs, None);
});
}
#[test]
fn append_manual_trigger_log_creates_and_appends() {
with_override_home(|_home| {
let title = "Rs Manual Log Append Routine";
let slug = slugify(title);
write_routine(&make_routine("rs-manual-log-id", title)).unwrap();
append_manual_trigger_log(&slug, 100);
append_manual_trigger_log(&slug, 200);
append_manual_trigger_log(&slug, 300);
let log_path = crate::paths::routine_manual_log_path(&slug);
let text = std::fs::read_to_string(&log_path).unwrap();
assert_eq!(text, "100\n200\n300\n");
assert_eq!(
load_routine_from_dir(&slug).unwrap().last_manual_trigger_at,
Some(300)
);
});
}
#[test]
fn append_manual_trigger_log_warns_on_write_failure() {
let dir = scratch_dir("manual-log-fail");
std::fs::create_dir_all(&dir).unwrap();
let slug_dir = dir.join("rs-manual-log-fail-routine");
std::fs::create_dir_all(&slug_dir).unwrap();
let blocker = slug_dir.join("manual.log");
std::fs::create_dir_all(&blocker).unwrap();
let previous = std::env::var_os("MOADIM_HOME_OVERRIDE");
unsafe {
std::env::set_var("MOADIM_HOME_OVERRIDE", &dir);
}
append_manual_trigger_log("rs-manual-log-fail-routine", 42);
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(&dir);
}
#[test]
fn migrate_trigger_logs_from_dir_missing_dir_returns() {
let missing = scratch_dir("trigger-logs-missing");
migrate_trigger_logs_from_dir(&missing);
assert!(!missing.exists());
}
#[test]
fn migrate_trigger_logs_from_dir_migrates_scheduled_and_manual() {
let dir = scratch_dir("trigger-logs-migrate");
std::fs::create_dir_all(&dir).unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
std::fs::write(
routine_dir.join("scheduled.local.toml"),
"last_scheduled_trigger_at = 1111\n",
)
.unwrap();
std::fs::write(
routine_dir.join("state.local.toml"),
"last_manual_trigger_at = 2222\n",
)
.unwrap();
migrate_trigger_logs_from_dir(&dir);
assert!(
!routine_dir.join("scheduled.local.toml").exists(),
"legacy toml should be removed"
);
let sched_text = std::fs::read_to_string(routine_dir.join("scheduled.log")).unwrap();
assert_eq!(sched_text, "1111\n");
let manual_text = std::fs::read_to_string(routine_dir.join("manual.log")).unwrap();
assert_eq!(manual_text, "2222\n");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn migrate_trigger_logs_from_dir_skips_when_logs_already_exist() {
let dir = scratch_dir("trigger-logs-skip");
std::fs::create_dir_all(&dir).unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
std::fs::write(
routine_dir.join("scheduled.local.toml"),
"last_scheduled_trigger_at = 5555\n",
)
.unwrap();
std::fs::write(routine_dir.join("scheduled.log"), "9999\n").unwrap();
std::fs::write(routine_dir.join("manual.log"), "8888\n").unwrap();
std::fs::write(
routine_dir.join("state.local.toml"),
"last_manual_trigger_at = 7777\n",
)
.unwrap();
migrate_trigger_logs_from_dir(&dir);
assert_eq!(
std::fs::read_to_string(routine_dir.join("scheduled.log")).unwrap(),
"9999\n"
);
assert_eq!(
std::fs::read_to_string(routine_dir.join("manual.log")).unwrap(),
"8888\n"
);
assert!(routine_dir.join("scheduled.local.toml").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn migrate_trigger_logs_from_dir_skips_non_dirs_and_unparsable() {
let dir = scratch_dir("trigger-logs-nondir");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("loose.txt"), "ignore me").unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
migrate_trigger_logs_from_dir(&dir);
assert!(!routine_dir.join("scheduled.log").exists());
assert!(!routine_dir.join("manual.log").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn migrate_trigger_logs_from_dir_removes_scheduled_toml_when_no_timestamp() {
let dir = scratch_dir("trigger-logs-no-ts");
std::fs::create_dir_all(&dir).unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
std::fs::write(routine_dir.join("scheduled.local.toml"), "").unwrap();
migrate_trigger_logs_from_dir(&dir);
assert!(!routine_dir.join("scheduled.log").exists());
assert!(!routine_dir.join("scheduled.local.toml").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
#[cfg(unix)]
fn migrate_trigger_logs_from_dir_logs_on_scheduled_write_failure() {
use std::os::unix::fs::PermissionsExt;
let dir = scratch_dir("trigger-logs-sched-fail");
std::fs::create_dir_all(&dir).unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
std::fs::write(
routine_dir.join("scheduled.local.toml"),
"last_scheduled_trigger_at = 42\n",
)
.unwrap();
std::fs::set_permissions(&routine_dir, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_trigger_logs_from_dir(&dir);
std::fs::set_permissions(&routine_dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(routine_dir.join("scheduled.local.toml").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
#[cfg(unix)]
fn migrate_trigger_logs_from_dir_logs_on_manual_write_failure() {
use std::os::unix::fs::PermissionsExt;
let dir = scratch_dir("trigger-logs-manual-fail");
std::fs::create_dir_all(&dir).unwrap();
let routine_dir = dir.join("my-routine");
std::fs::create_dir_all(&routine_dir).unwrap();
std::fs::write(
routine_dir.join("state.local.toml"),
"last_manual_trigger_at = 77\n",
)
.unwrap();
std::fs::set_permissions(&routine_dir, std::fs::Permissions::from_mode(0o555)).unwrap();
migrate_trigger_logs_from_dir(&dir);
std::fs::set_permissions(&routine_dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(!routine_dir.join("manual.log").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn migrate_trigger_logs_public_wrapper_runs() {
with_override_home(|_home| {
migrate_trigger_logs();
});
}