| 1 | //! `/rc` account-owned web remote control. |
| 2 | |
| 3 | use crate::commands::traits::{CommandInfo, RegisterCommand}; |
| 4 | use crate::localization::MessageId; |
| 5 | use crate::remote_control::RemoteControlAction; |
| 6 | use crate::tui::app::{App, AppAction}; |
| 7 | |
| 8 | use super::CommandResult; |
| 9 | |
| 10 | pub(in crate::commands) const COMMAND_INFO: CommandInfo = CommandInfo { |
| 11 | name: "rc", |
| 12 | aliases: &["remote-control"], |
| 13 | usage: "/rc [status|stop]", |
| 14 | description_id: MessageId::CmdRemoteControlDescription, |
| 15 | }; |
| 16 | |
| 17 | pub(in crate::commands) struct RemoteControlCmd; |
| 18 | |
| 19 | impl RegisterCommand for RemoteControlCmd { |
| 20 | fn info() -> &'static CommandInfo { |
| 21 | &COMMAND_INFO |
| 22 | } |
| 23 | |
| 24 | fn execute(app: &mut App, arg: Option<&str>) -> CommandResult { |
| 25 | match arg.map(str::trim).filter(|value| !value.is_empty()) { |
| 26 | None | Some("start") => { |
| 27 | if app.is_loading { |
| 28 | return CommandResult::error( |
| 29 | "Finish or interrupt the current turn before handing this session to the web.", |
| 30 | ); |
| 31 | } |
| 32 | CommandResult::with_message_and_action( |
| 33 | "Starting account-owned web remote control…", |
| 34 | AppAction::RemoteControl(RemoteControlAction::Start), |
| 35 | ) |
| 36 | } |
| 37 | Some("status") => CommandResult::message(app.remote_control.status_line()), |
| 38 | Some("stop") => CommandResult::with_message_and_action( |
| 39 | "Stopping web remote control…", |
| 40 | AppAction::RemoteControl(RemoteControlAction::Stop), |
| 41 | ), |
| 42 | Some(_) => CommandResult::error("Usage: /rc [status|stop]"), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[cfg(test)] |
| 48 | mod tests { |
| 49 | use super::*; |
| 50 | use crate::tui::app::TuiOptions; |
| 51 | use std::path::PathBuf; |
| 52 | |
| 53 | #[test] |
| 54 | fn start_is_blocked_during_an_active_turn() { |
| 55 | let options = TuiOptions { |
| 56 | ..crate::test_support::test_tui_options(PathBuf::from(".")) |
| 57 | }; |
| 58 | let mut app = crate::test_support::test_app_with_options(options); |
| 59 | app.is_loading = true; |
| 60 | let result = RemoteControlCmd::execute(&mut app, None); |
| 61 | assert!(result.is_error); |
| 62 | assert!(result.action.is_none()); |
| 63 | } |
| 64 | } |
| 65 |