#![allow(
clippy::missing_docs_in_private_items,
reason = "test helpers and fixtures do not need doc comments"
)]
use super::*;
use crate::routines::{new_store, slugify};
struct TempHome(std::path::PathBuf);
impl TempHome {
fn set() -> Self {
let dir = std::env::temp_dir().join(format!("moadim-svctest-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).expect("create temp home");
unsafe {
std::env::set_var("MOADIM_HOME_OVERRIDE", &dir);
}
Self(dir)
}
}
impl Drop for TempHome {
fn drop(&mut self) {
unsafe {
std::env::remove_var("MOADIM_HOME_OVERRIDE");
}
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn create_req_with_title(title: &str) -> CreateRoutineRequest {
CreateRoutineRequest {
disabled_reason: None,
model: None,
schedule: "@daily".into(),
schedules: vec![],
title: title.into(),
agent: "claude".into(),
prompt: "p".into(),
goal: None,
repositories: vec![],
machines: vec![crate::machine::current_machine()],
enabled: true,
ttl_secs: None,
max_runtime_secs: None,
power_saving_exempt: false,
tags: vec![],
env: std::collections::HashMap::new(),
failure_threshold: None,
notifications: Default::default(),
}
}
#[test]
fn svc_create_flag_not_found() {
let _home = TempHome::set();
let store = new_store();
let result = svc_create_flag(&store, "missing", "bug", "desc", "general");
assert!(matches!(result, Err(AppError::NotFound)));
}
#[test]
fn svc_create_flag_rejects_blank_type_and_description() {
let _home = TempHome::set();
let store = new_store();
let created = svc_create(&store, create_req_with_title("Svc Flag Blank ZZZ")).unwrap();
let id = created.routine.id;
assert!(matches!(
svc_create_flag(&store, &id, " ", "desc", "general"),
Err(AppError::BadRequest(_))
));
assert!(matches!(
svc_create_flag(&store, &id, "bug", " ", "general"),
Err(AppError::BadRequest(_))
));
}
#[test]
fn svc_create_flag_rejects_unknown_scope() {
let _home = TempHome::set();
let store = new_store();
let created = svc_create(&store, create_req_with_title("Svc Flag Scope ZZZ")).unwrap();
let id = created.routine.id;
assert!(matches!(
svc_create_flag(&store, &id, "bug", "desc", "nowhere"),
Err(AppError::BadRequest(_))
));
}
#[test]
fn svc_create_flag_persists_and_refreshes_prompt() {
let _home = TempHome::set();
let store = new_store();
let title = "Svc Flag Create ZZZ";
let created = svc_create(&store, create_req_with_title(title)).unwrap();
let id = created.routine.id;
let flag = svc_create_flag(&store, &id, "bug", "broken thing", "general").unwrap();
assert_eq!(flag.category, "bug");
assert_eq!(flag.description, "broken thing");
let slug = slugify(title);
let prompt =
std::fs::read_to_string(crate::paths::routine_compiled_prompt_path(&slug)).unwrap();
assert!(prompt.contains("Open flags"));
assert!(prompt.contains("broken thing"));
}
#[cfg(unix)]
#[test]
fn svc_create_flag_returns_internal_on_create_flag_failure() {
use std::os::unix::fs::PermissionsExt as _;
let _home = TempHome::set();
let title = "Svc Flag Create Mkdir Fail ZZZ";
let store = new_store();
let created = svc_create(&store, create_req_with_title(title)).unwrap();
let id = created.routine.id;
let dir = crate::paths::routine_dir(&slugify(title));
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap();
let result = svc_create_flag(&store, &id, "bug", "broken", "general");
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(matches!(result, Err(AppError::Internal)));
}
#[cfg(unix)]
#[test]
fn svc_create_flag_returns_internal_on_write_failure_after_flag_created() {
use std::os::unix::fs::PermissionsExt as _;
let _home = TempHome::set();
let title = "Svc Flag Create Write Fail ZZZ";
let store = new_store();
let created = svc_create(&store, create_req_with_title(title)).unwrap();
let id = created.routine.id;
let slug = slugify(title);
std::fs::create_dir_all(crate::paths::routine_flags_dir(&slug)).unwrap();
let dir = crate::paths::routine_dir(&slug);
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap();
let result = svc_create_flag(&store, &id, "bug", "broken", "general");
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(matches!(result, Err(AppError::Internal)));
}
include!("svc_list_flags_not_found_tests.rs");