From 539ad75fe6126db9d538494a91513a6d32c97e71 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 22 Nov 2024 16:21:43 -0700 Subject: [PATCH] feat(cli): Support for marking a Sonarr history item as 'failed' --- src/cli/sonarr/mod.rs | 16 ++++++++ src/cli/sonarr/sonarr_command_tests.rs | 55 ++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/cli/sonarr/mod.rs b/src/cli/sonarr/mod.rs index 73fd53d..658c56e 100644 --- a/src/cli/sonarr/mod.rs +++ b/src/cli/sonarr/mod.rs @@ -48,6 +48,15 @@ pub enum SonarrCommand { List(SonarrListCommand), #[command(about = "Clear the blocklist")] ClearBlocklist, + #[command(about = "Mark the Sonarr history item with the given ID as 'failed'")] + MarkHistoryItemAsFailed { + #[arg( + long, + help = "The Sonarr ID of the history item you wish to mark as 'failed'", + required = true + )] + history_item_id: i64, + }, #[command(about = "Trigger a manual search of releases for the episode with the given ID")] ManualEpisodeSearch { #[arg( @@ -130,6 +139,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrCommand> for SonarrCliHandler<'a, ' .await?; serde_json::to_string_pretty(&resp)? } + SonarrCommand::MarkHistoryItemAsFailed { history_item_id } => { + let _ = self + .network + .handle_network_event(SonarrEvent::MarkHistoryItemAsFailed(history_item_id).into()) + .await?; + "Sonarr history item marked as 'failed'".to_owned() + } SonarrCommand::ManualEpisodeSearch { episode_id } => { println!("Searching for episode releases. This may take a minute..."); let resp = self diff --git a/src/cli/sonarr/sonarr_command_tests.rs b/src/cli/sonarr/sonarr_command_tests.rs index 0d72ec9..be2f4b5 100644 --- a/src/cli/sonarr/sonarr_command_tests.rs +++ b/src/cli/sonarr/sonarr_command_tests.rs @@ -28,6 +28,31 @@ mod tests { assert!(result.is_ok()); } + #[rstest] + fn test_mark_history_item_as_failed_requires_history_item_id() { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "mark-history-item-as-failed"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[rstest] + fn test_mark_history_item_as_failed_requirements_satisfied() { + let result = Cli::command().try_get_matches_from([ + "managarr", + "sonarr", + "mark-history-item-as-failed", + "--history-item-id", + "1", + ]); + + assert!(result.is_ok()); + } + #[rstest] fn test_manual_season_search_requires_series_id() { let result = Cli::command().try_get_matches_from([ @@ -160,6 +185,36 @@ mod tests { assert!(result.is_ok()); } + #[tokio::test] + async fn test_mark_history_item_as_failed_command() { + let expected_history_item_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::MarkHistoryItemAsFailed(expected_history_item_id).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let mark_history_item_as_failed_command = + SonarrCommand::MarkHistoryItemAsFailed { history_item_id: 1 }; + + let result = SonarrCliHandler::with( + &app_arc, + mark_history_item_as_failed_command, + &mut mock_network, + ) + .handle() + .await; + + assert!(result.is_ok()); + } + #[tokio::test] async fn test_manual_episode_search_command() { let expected_episode_id = 1;