From 68830a8789b9df99e979545e6554dd4ae133bb8b Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 22 Nov 2024 17:22:41 -0700 Subject: [PATCH] feat(cli): Support for testing an individual Sonarr indexer --- src/cli/sonarr/mod.rs | 14 ++++++++ src/cli/sonarr/sonarr_command_tests.rs | 49 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/cli/sonarr/mod.rs b/src/cli/sonarr/mod.rs index 870223e..421d560 100644 --- a/src/cli/sonarr/mod.rs +++ b/src/cli/sonarr/mod.rs @@ -90,6 +90,13 @@ pub enum SonarrCommand { )] task_name: SonarrTaskName, }, + #[command( + about = "Test the indexer with the given ID. Note that a successful test returns an empty JSON body; i.e. '{}'" + )] + TestIndexer { + #[arg(long, help = "The ID of the indexer to test", required = true)] + indexer_id: i64, + }, } impl From for Command { @@ -185,6 +192,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrCommand> for SonarrCliHandler<'a, ' .await?; serde_json::to_string_pretty(&resp)? } + SonarrCommand::TestIndexer { indexer_id } => { + let resp = self + .network + .handle_network_event(SonarrEvent::TestIndexer(Some(indexer_id)).into()) + .await?; + serde_json::to_string_pretty(&resp)? + } }; Ok(result) diff --git a/src/cli/sonarr/sonarr_command_tests.rs b/src/cli/sonarr/sonarr_command_tests.rs index fc0988e..57f1422 100644 --- a/src/cli/sonarr/sonarr_command_tests.rs +++ b/src/cli/sonarr/sonarr_command_tests.rs @@ -164,6 +164,30 @@ mod tests { assert!(result.is_ok()); } + + #[rstest] + fn test_test_indexer_requires_indexer_id() { + let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "test-indexer"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[test] + fn test_test_indexer_requirements_satisfied() { + let result = Cli::command().try_get_matches_from([ + "managarr", + "sonarr", + "test-indexer", + "--indexer-id", + "1", + ]); + + assert!(result.is_ok()); + } } mod handler { @@ -437,5 +461,30 @@ mod tests { assert!(result.is_ok()); } + + #[tokio::test] + async fn test_test_indexer_command() { + let expected_indexer_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::TestIndexer(Some(expected_indexer_id)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let test_indexer_command = SonarrCommand::TestIndexer { indexer_id: 1 }; + + let result = SonarrCliHandler::with(&app_arc, test_indexer_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } } }