From b24e3bf9db706244a0af6870816ea37f088fc5c1 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Thu, 21 Nov 2024 16:46:00 -0700 Subject: [PATCH] feat(cli): Support for deleting a root folder from Sonarr --- src/cli/sonarr/delete_command_handler.rs | 12 ++++ .../sonarr/delete_command_handler_tests.rs | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/cli/sonarr/delete_command_handler.rs b/src/cli/sonarr/delete_command_handler.rs index 7d958b3..0e1cd55 100644 --- a/src/cli/sonarr/delete_command_handler.rs +++ b/src/cli/sonarr/delete_command_handler.rs @@ -37,6 +37,11 @@ pub enum SonarrDeleteCommand { #[arg(long, help = "The ID of the indexer to delete", required = true)] indexer_id: i64, }, + #[command(about = "Delete the root folder with the given ID")] + RootFolder { + #[arg(long, help = "The ID of the root folder to delete", required = true)] + root_folder_id: i64, + }, } impl From for Command { @@ -87,6 +92,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrDeleteCommand> for SonarrDeleteComm .await?; serde_json::to_string_pretty(&resp)? } + SonarrDeleteCommand::RootFolder { root_folder_id } => { + let resp = self + .network + .handle_network_event((SonarrEvent::DeleteRootFolder(Some(root_folder_id))).into()) + .await?; + serde_json::to_string_pretty(&resp)? + } }; Ok(resp) diff --git a/src/cli/sonarr/delete_command_handler_tests.rs b/src/cli/sonarr/delete_command_handler_tests.rs index 378d2aa..9953225 100644 --- a/src/cli/sonarr/delete_command_handler_tests.rs +++ b/src/cli/sonarr/delete_command_handler_tests.rs @@ -123,6 +123,39 @@ mod tests { assert_eq!(delete_command, expected_args); } } + + #[test] + fn test_delete_root_folder_requires_arguments() { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "delete", "root-folder"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[test] + fn test_delete_root_folder_success() { + let expected_args = SonarrDeleteCommand::RootFolder { root_folder_id: 1 }; + + let result = Cli::try_parse_from([ + "managarr", + "sonarr", + "delete", + "root-folder", + "--root-folder-id", + "1", + ]); + + assert!(result.is_ok()); + + if let Some(Command::Sonarr(SonarrCommand::Delete(delete_command))) = result.unwrap().command + { + assert_eq!(delete_command, expected_args); + } + } } mod handler { @@ -224,5 +257,31 @@ mod tests { assert!(result.is_ok()); } + + #[tokio::test] + async fn test_handle_delete_root_folder_command() { + let expected_root_folder_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::DeleteRootFolder(Some(expected_root_folder_id)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let delete_root_folder_command = SonarrDeleteCommand::RootFolder { root_folder_id: 1 }; + + let result = + SonarrDeleteCommandHandler::with(&app_arc, delete_root_folder_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } } }