| 1 | //! Operator controls for durable scheduled automations. |
| 2 | |
| 3 | use codewhale_command_contract::facets::CommandPresentationContext; |
| 4 | use codewhale_command_contract::handler::{CommandCapabilities, CommandContexts, CommandHandler}; |
| 5 | use codewhale_command_contract::metadata::{CommandInfo, RegisterCommand}; |
| 6 | |
| 7 | use crate::commands::CommandResult; |
| 8 | use crate::tui::app::{AppAction, AutomationAction}; |
| 9 | |
| 10 | pub(in crate::commands) const COMMAND_INFO: CommandInfo = CommandInfo { |
| 11 | name: "automation", |
| 12 | aliases: &["automations", "scheduled"], |
| 13 | usage: "/automation [list|show <id>|print <id>|pause <id>|resume <id>|adopt <id>|delete <id> [--confirm <token>]|run <id>]", |
| 14 | description_key: "cmd_automation_description", |
| 15 | }; |
| 16 | |
| 17 | pub(in crate::commands) struct AutomationCmd; |
| 18 | |
| 19 | impl RegisterCommand<CommandResult> for AutomationCmd { |
| 20 | fn info() -> &'static CommandInfo { |
| 21 | &COMMAND_INFO |
| 22 | } |
| 23 | |
| 24 | fn handler() -> CommandHandler<CommandResult> { |
| 25 | CommandHandler::Contextual { |
| 26 | capabilities: CommandCapabilities::PRESENTATION, |
| 27 | handler: automation_contextual, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn automation_contextual(contexts: CommandContexts<'_>, arg: Option<&str>) -> CommandResult { |
| 33 | let mut parts = contexts.into_parts(); |
| 34 | let Some(presentation) = parts.presentation.as_deref_mut() else { |
| 35 | return CommandResult::error("Command capability unavailable: presentation"); |
| 36 | }; |
| 37 | automation(presentation, arg) |
| 38 | } |
| 39 | |
| 40 | fn automation( |
| 41 | presentation: &mut dyn CommandPresentationContext, |
| 42 | args: Option<&str>, |
| 43 | ) -> CommandResult { |
| 44 | let raw = args.unwrap_or("").trim(); |
| 45 | // Bare `/automation` opens the room; `list` keeps the text receipt for |
| 46 | // scripts and transcripts. |
| 47 | if raw.is_empty() { |
| 48 | return action(AutomationAction::Open { focus: None }); |
| 49 | } |
| 50 | if raw.eq_ignore_ascii_case("list") { |
| 51 | return action(AutomationAction::List); |
| 52 | } |
| 53 | |
| 54 | let mut parts = raw.split_whitespace(); |
| 55 | let verb = parts.next().unwrap_or("").to_ascii_lowercase(); |
| 56 | |
| 57 | match verb.as_str() { |
| 58 | "show" | "status" => single_id(presentation, &mut parts, |id| AutomationAction::Open { |
| 59 | focus: Some(id), |
| 60 | }), |
| 61 | "print" => single_id(presentation, &mut parts, AutomationAction::Show), |
| 62 | "pause" => single_id(presentation, &mut parts, AutomationAction::Pause), |
| 63 | "resume" => single_id(presentation, &mut parts, AutomationAction::Resume), |
| 64 | "adopt" => single_id(presentation, &mut parts, AutomationAction::Adopt), |
| 65 | "delete" | "remove" | "rm" => delete(presentation, &mut parts), |
| 66 | "run" | "trigger" => single_id(presentation, &mut parts, AutomationAction::Run), |
| 67 | _ => usage_error(presentation), |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fn single_id<'a>( |
| 72 | presentation: &mut dyn CommandPresentationContext, |
| 73 | parts: &mut impl Iterator<Item = &'a str>, |
| 74 | make_action: fn(String) -> AutomationAction, |
| 75 | ) -> CommandResult { |
| 76 | let Some(id) = parts.next() else { |
| 77 | return usage_error(presentation); |
| 78 | }; |
| 79 | if parts.next().is_some() { |
| 80 | return usage_error(presentation); |
| 81 | } |
| 82 | action(make_action(id.to_string())) |
| 83 | } |
| 84 | |
| 85 | fn delete<'a>( |
| 86 | presentation: &mut dyn CommandPresentationContext, |
| 87 | parts: &mut impl Iterator<Item = &'a str>, |
| 88 | ) -> CommandResult { |
| 89 | let Some(id) = parts.next() else { |
| 90 | return usage_error(presentation); |
| 91 | }; |
| 92 | let confirmation = match (parts.next(), parts.next(), parts.next()) { |
| 93 | (None, None, None) => None, |
| 94 | (Some(flag), Some(token), None) if flag.eq_ignore_ascii_case("--confirm") => { |
| 95 | Some(token.to_string()) |
| 96 | } |
| 97 | _ => return usage_error(presentation), |
| 98 | }; |
| 99 | action(AutomationAction::Delete { |
| 100 | id: id.to_string(), |
| 101 | confirmation, |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | fn usage_error(presentation: &mut dyn CommandPresentationContext) -> CommandResult { |
| 106 | match presentation.translate("automation_usage", &[]) { |
| 107 | Ok(text) => CommandResult::error(text), |
| 108 | // The key is catalog-known; a translation failure must still fail |
| 109 | // safely without exposing a raw lookup key (D3). |
| 110 | Err(_) => CommandResult::error( |
| 111 | "Usage: /automation [list|show <id>|pause <id>|resume <id>|adopt <id>|delete <id> [--confirm <token>]|run <id>]", |
| 112 | ), |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn action(action: AutomationAction) -> CommandResult { |
| 117 | CommandResult::action(AppAction::Automation(action)) |
| 118 | } |
| 119 | |
| 120 | #[cfg(test)] |
| 121 | mod tests { |
| 122 | use super::*; |
| 123 | |
| 124 | struct FakePresentation; |
| 125 | impl CommandPresentationContext for FakePresentation { |
| 126 | fn translate(&self, key: &str, _r: &[(&str, &str)]) -> Result<String, String> { |
| 127 | if key == "automation_usage" { |
| 128 | Ok("Usage: /automation [list|show <id>|pause <id>|resume <id>|delete <id> [--confirm <token>]|run <id>]".to_string()) |
| 129 | } else { |
| 130 | Err("unknown translation key".to_string()) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fn parsed(args: Option<&str>) -> Option<AutomationAction> { |
| 136 | match automation(&mut FakePresentation, args).action { |
| 137 | Some(AppAction::Automation(action)) => Some(action), |
| 138 | _ => None, |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn parses_list_show_and_mutations() { |
| 144 | assert_eq!(parsed(None), Some(AutomationAction::Open { focus: None })); |
| 145 | assert_eq!(parsed(Some("list")), Some(AutomationAction::List)); |
| 146 | assert_eq!( |
| 147 | parsed(Some("show auto_1")), |
| 148 | Some(AutomationAction::Open { |
| 149 | focus: Some("auto_1".to_string()) |
| 150 | }) |
| 151 | ); |
| 152 | assert_eq!( |
| 153 | parsed(Some("print auto_1")), |
| 154 | Some(AutomationAction::Show("auto_1".to_string())) |
| 155 | ); |
| 156 | assert_eq!( |
| 157 | parsed(Some("pause auto_1")), |
| 158 | Some(AutomationAction::Pause("auto_1".to_string())) |
| 159 | ); |
| 160 | assert_eq!( |
| 161 | parsed(Some("resume auto_1")), |
| 162 | Some(AutomationAction::Resume("auto_1".to_string())) |
| 163 | ); |
| 164 | assert_eq!( |
| 165 | parsed(Some("delete auto_1")), |
| 166 | Some(AutomationAction::Delete { |
| 167 | id: "auto_1".to_string(), |
| 168 | confirmation: None, |
| 169 | }) |
| 170 | ); |
| 171 | assert_eq!( |
| 172 | parsed(Some("run auto_1")), |
| 173 | Some(AutomationAction::Run("auto_1".to_string())) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn accepts_operator_aliases() { |
| 179 | assert_eq!( |
| 180 | parsed(Some("status auto_1")), |
| 181 | Some(AutomationAction::Open { |
| 182 | focus: Some("auto_1".to_string()) |
| 183 | }) |
| 184 | ); |
| 185 | assert_eq!( |
| 186 | parsed(Some("rm auto_1")), |
| 187 | Some(AutomationAction::Delete { |
| 188 | id: "auto_1".to_string(), |
| 189 | confirmation: None, |
| 190 | }) |
| 191 | ); |
| 192 | assert_eq!( |
| 193 | parsed(Some("trigger auto_1")), |
| 194 | Some(AutomationAction::Run("auto_1".to_string())) |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn validates_missing_ids_and_unknown_actions() { |
| 200 | for verb in ["show", "pause", "resume", "delete", "run", "unknown"] { |
| 201 | let result = automation(&mut FakePresentation, Some(verb)); |
| 202 | assert!(result.message.is_some(), "{verb} should show usage"); |
| 203 | assert!(result.action.is_none()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn delete_confirmation_is_explicit_and_exact() { |
| 209 | assert_eq!( |
| 210 | parsed(Some("delete auto_1 --confirm receipt")), |
| 211 | Some(AutomationAction::Delete { |
| 212 | id: "auto_1".to_string(), |
| 213 | confirmation: Some("receipt".to_string()), |
| 214 | }) |
| 215 | ); |
| 216 | for invalid in [ |
| 217 | "delete auto_1 --confirm", |
| 218 | "delete auto_1 receipt", |
| 219 | "delete auto_1 --confirm receipt extra", |
| 220 | ] { |
| 221 | let result = automation(&mut FakePresentation, Some(invalid)); |
| 222 | assert!(result.is_error, "{invalid} should be rejected"); |
| 223 | assert!(result.action.is_none()); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn handler_is_contextual_and_requests_presentation_facet() { |
| 229 | let CommandHandler::Contextual { |
| 230 | capabilities, |
| 231 | handler, |
| 232 | } = AutomationCmd::handler() |
| 233 | else { |
| 234 | panic!("automation must be contextual"); |
| 235 | }; |
| 236 | assert_eq!(capabilities, CommandCapabilities::PRESENTATION); |
| 237 | let missing = handler(CommandContexts::empty(), Some("list")); |
| 238 | assert!(missing.is_error); |
| 239 | assert_eq!( |
| 240 | missing.message.as_deref(), |
| 241 | Some("Error: Command capability unavailable: presentation") |
| 242 | ); |
| 243 | assert_eq!( |
| 244 | AutomationCmd::info().description_key, |
| 245 | "cmd_automation_description" |
| 246 | ); |
| 247 | assert_eq!(AutomationCmd::info().aliases, &["automations", "scheduled"]); |
| 248 | } |
| 249 | } |
| 250 |