From 1a65a7f3e7061bb0c4b294b73a8d5dcd6c19ae82 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Thu, 21 Nov 2024 14:49:36 -0700 Subject: [PATCH] feat(cli): Support for deleting a download 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 2bf8991..1de70e2 100644 --- a/src/cli/sonarr/delete_command_handler.rs +++ b/src/cli/sonarr/delete_command_handler.rs @@ -27,6 +27,11 @@ pub enum SonarrDeleteCommand { )] blocklist_item_id: i64, }, + #[command(about = "Delete the specified download")] + Download { + #[arg(long, help = "The ID of the download to delete", required = true)] + download_id: i64, + }, } impl From for Command { @@ -63,6 +68,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrDeleteCommand> for SonarrDeleteComm .await?; serde_json::to_string_pretty(&resp)? } + SonarrDeleteCommand::Download { download_id } => { + let resp = self + .network + .handle_network_event((SonarrEvent::DeleteDownload(Some(download_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 546f433..688f9c0 100644 --- a/src/cli/sonarr/delete_command_handler_tests.rs +++ b/src/cli/sonarr/delete_command_handler_tests.rs @@ -58,6 +58,39 @@ mod tests { assert_eq!(delete_command, expected_args); } } + + #[test] + fn test_delete_download_requires_arguments() { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "delete", "download"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[test] + fn test_delete_download_success() { + let expected_args = SonarrDeleteCommand::Download { download_id: 1 }; + + let result = Cli::try_parse_from([ + "managarr", + "sonarr", + "delete", + "download", + "--download-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 { @@ -107,5 +140,31 @@ mod tests { assert!(result.is_ok()); } + + #[tokio::test] + async fn test_handle_delete_download_command() { + let expected_download_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::DeleteDownload(Some(expected_download_id)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let delete_download_command = SonarrDeleteCommand::Download { download_id: 1 }; + + let result = + SonarrDeleteCommandHandler::with(&app_arc, delete_download_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } } }