From eb06787bb2222e56389a41c4979d87548a9cd1bb Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 22 Nov 2024 19:13:57 -0700 Subject: [PATCH] feat(cli): Support for refreshing a specific series in Sonarr --- src/cli/sonarr/refresh_command_handler.rs | 16 +++++ .../sonarr/refresh_command_handler_tests.rs | 63 ++++++++++++++++++- src/cli/sonarr/sonarr_command_tests.rs | 48 +++++++------- 3 files changed, 100 insertions(+), 27 deletions(-) diff --git a/src/cli/sonarr/refresh_command_handler.rs b/src/cli/sonarr/refresh_command_handler.rs index 2f50175..11077af 100644 --- a/src/cli/sonarr/refresh_command_handler.rs +++ b/src/cli/sonarr/refresh_command_handler.rs @@ -19,6 +19,15 @@ mod refresh_command_handler_tests; pub enum SonarrRefreshCommand { #[command(about = "Refresh all series data for all series in your Sonarr library")] AllSeries, + #[command(about = "Refresh series data and scan disk for the series with the given ID")] + Series { + #[arg( + long, + help = "The ID of the series to refresh information on and to scan the disk for", + required = true + )] + series_id: i64, + }, } impl From for Command { @@ -57,6 +66,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrRefreshCommand> .await?; serde_json::to_string_pretty(&resp)? } + SonarrRefreshCommand::Series { series_id } => { + let resp = self + .network + .handle_network_event(SonarrEvent::UpdateAndScanSeries(Some(series_id)).into()) + .await?; + serde_json::to_string_pretty(&resp)? + } }; Ok(result) diff --git a/src/cli/sonarr/refresh_command_handler_tests.rs b/src/cli/sonarr/refresh_command_handler_tests.rs index f200424..c1fd427 100644 --- a/src/cli/sonarr/refresh_command_handler_tests.rs +++ b/src/cli/sonarr/refresh_command_handler_tests.rs @@ -20,7 +20,7 @@ mod tests { mod cli { use super::*; - use clap::Parser; + use clap::{error::ErrorKind, Parser}; use pretty_assertions::assert_eq; use rstest::rstest; @@ -31,6 +31,38 @@ mod tests { assert!(result.is_ok()); } + + #[test] + fn test_refresh_series_requires_series_id() { + let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "refresh", "series"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[test] + fn test_refresh_series_success() { + let expected_args = SonarrRefreshCommand::Series { series_id: 1 }; + let result = Cli::try_parse_from([ + "managarr", + "sonarr", + "refresh", + "series", + "--series-id", + "1", + ]); + + assert!(result.is_ok()); + + if let Some(Command::Sonarr(SonarrCommand::Refresh(refresh_command))) = + result.unwrap().command + { + assert_eq!(refresh_command, expected_args); + } + } } mod handler { @@ -41,6 +73,7 @@ mod tests { use serde_json::json; use tokio::sync::Mutex; + use crate::{app::App, cli::sonarr::refresh_command_handler::SonarrRefreshCommandHandler}; use crate::{ cli::{sonarr::refresh_command_handler::SonarrRefreshCommand, CliCommandHandler}, network::sonarr_network::SonarrEvent, @@ -57,8 +90,6 @@ mod tests { #[case] refresh_command: SonarrRefreshCommand, #[case] expected_sonarr_event: SonarrEvent, ) { - use crate::{app::App, cli::sonarr::refresh_command_handler::SonarrRefreshCommandHandler}; - let mut mock_network = MockNetworkTrait::new(); mock_network .expect_handle_network_event() @@ -77,5 +108,31 @@ mod tests { assert!(result.is_ok()); } + + #[tokio::test] + async fn test_handle_refresh_series_command() { + let expected_series_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::UpdateAndScanSeries(Some(expected_series_id)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let refresh_series_command = SonarrRefreshCommand::Series { series_id: 1 }; + + let result = + SonarrRefreshCommandHandler::with(&app_arc, refresh_series_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } } } diff --git a/src/cli/sonarr/sonarr_command_tests.rs b/src/cli/sonarr/sonarr_command_tests.rs index 36d0870..a5b580e 100644 --- a/src/cli/sonarr/sonarr_command_tests.rs +++ b/src/cli/sonarr/sonarr_command_tests.rs @@ -312,7 +312,7 @@ mod tests { sonarr::{ add_command_handler::SonarrAddCommand, delete_command_handler::SonarrDeleteCommand, get_command_handler::SonarrGetCommand, list_command_handler::SonarrListCommand, - SonarrCliHandler, SonarrCommand, + refresh_command_handler::SonarrRefreshCommand, SonarrCliHandler, SonarrCommand, }, CliCommandHandler, }, @@ -544,31 +544,31 @@ mod tests { assert!(result.is_ok()); } - // #[tokio::test] - // async fn test_sonarr_cli_handler_delegates_refresh_commands_to_the_refresh_command_handler() { - // let expected_series_id = 1; - // let mut mock_network = MockNetworkTrait::new(); - // mock_network - // .expect_handle_network_event() - // .with(eq::( - // SonarrEvent::UpdateAndScan(Some(expected_movie_id)).into(), - // )) - // .times(1) - // .returning(|_| { - // Ok(Serdeable::Sonarr(SonarrSerdeable::Value( - // json!({"testResponse": "response"}), - // ))) - // }); - // let app_arc = Arc::new(Mutex::new(App::default())); - // let refresh_all_series_command = - // SonarrCommand::Refresh(SonarrRefreshCommand::Movie { movie_id: 1 }); + #[tokio::test] + async fn test_sonarr_cli_handler_delegates_refresh_commands_to_the_refresh_command_handler() { + let expected_series_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::UpdateAndScanSeries(Some(expected_series_id)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let refresh_series_command = + SonarrCommand::Refresh(SonarrRefreshCommand::Series { series_id: 1 }); - // let result = SonarrCliHandler::with(&app_arc, refresh_movie_command, &mut mock_network) - // .handle() - // .await; + let result = SonarrCliHandler::with(&app_arc, refresh_series_command, &mut mock_network) + .handle() + .await; - // assert!(result.is_ok()); - // } + assert!(result.is_ok()); + } #[tokio::test] async fn test_start_task_command() {