diff --git a/src/cli/radarr/refresh_command_handler.rs b/src/cli/radarr/refresh_command_handler.rs index 3101a98..f329249 100644 --- a/src/cli/radarr/refresh_command_handler.rs +++ b/src/cli/radarr/refresh_command_handler.rs @@ -18,7 +18,7 @@ mod refresh_command_handler_tests; #[derive(Debug, Clone, PartialEq, Eq, Subcommand)] pub enum RadarrRefreshCommand { - #[command(about = "Refresh all movie data for all movies in your library")] + #[command(about = "Refresh all movie data for all movies in your Radarr library")] AllMovies, #[command(about = "Refresh movie data and scan disk for the movie with the given ID")] Movie { diff --git a/src/cli/radarr/refresh_command_handler_tests.rs b/src/cli/radarr/refresh_command_handler_tests.rs index 30be57b..3c43830 100644 --- a/src/cli/radarr/refresh_command_handler_tests.rs +++ b/src/cli/radarr/refresh_command_handler_tests.rs @@ -82,7 +82,7 @@ mod tests { #[case(RadarrRefreshCommand::Collections, RadarrEvent::UpdateCollections)] #[case(RadarrRefreshCommand::Downloads, RadarrEvent::UpdateDownloads)] #[tokio::test] - async fn test_handle_list_blocklist_command( + async fn test_handle_refresh_command( #[case] refresh_command: RadarrRefreshCommand, #[case] expected_radarr_event: RadarrEvent, ) { diff --git a/src/cli/sonarr/mod.rs b/src/cli/sonarr/mod.rs index 409ef00..68b2b23 100644 --- a/src/cli/sonarr/mod.rs +++ b/src/cli/sonarr/mod.rs @@ -6,6 +6,7 @@ use clap::Subcommand; use delete_command_handler::{SonarrDeleteCommand, SonarrDeleteCommandHandler}; use get_command_handler::{SonarrGetCommand, SonarrGetCommandHandler}; use list_command_handler::{SonarrListCommand, SonarrListCommandHandler}; +use refresh_command_handler::{SonarrRefreshCommand, SonarrRefreshCommandHandler}; use tokio::sync::Mutex; use crate::{ @@ -20,6 +21,7 @@ mod add_command_handler; mod delete_command_handler; mod get_command_handler; mod list_command_handler; +mod refresh_command_handler; #[cfg(test)] #[path = "sonarr_command_tests.rs"] @@ -47,6 +49,11 @@ pub enum SonarrCommand { about = "Commands to list attributes from your Sonarr instance" )] List(SonarrListCommand), + #[command( + subcommand, + about = "Commands to refresh the data in your Sonarr instance" + )] + Refresh(SonarrRefreshCommand), #[command(about = "Clear the blocklist")] ClearBlocklist, #[command(about = "Mark the Sonarr history item with the given ID as 'failed'")] @@ -179,6 +186,11 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrCommand> for SonarrCliHandler<'a, ' .handle() .await? } + SonarrCommand::Refresh(update_command) => { + SonarrRefreshCommandHandler::with(self.app, update_command, self.network) + .handle() + .await? + } SonarrCommand::ClearBlocklist => { self .network diff --git a/src/cli/sonarr/refresh_command_handler.rs b/src/cli/sonarr/refresh_command_handler.rs new file mode 100644 index 0000000..2f50175 --- /dev/null +++ b/src/cli/sonarr/refresh_command_handler.rs @@ -0,0 +1,64 @@ +use std::sync::Arc; + +use clap::Subcommand; +use tokio::sync::Mutex; + +use crate::{ + app::App, + cli::{CliCommandHandler, Command}, + network::{sonarr_network::SonarrEvent, NetworkTrait}, +}; + +use super::SonarrCommand; + +#[cfg(test)] +#[path = "refresh_command_handler_tests.rs"] +mod refresh_command_handler_tests; + +#[derive(Debug, Clone, PartialEq, Eq, Subcommand)] +pub enum SonarrRefreshCommand { + #[command(about = "Refresh all series data for all series in your Sonarr library")] + AllSeries, +} + +impl From for Command { + fn from(value: SonarrRefreshCommand) -> Self { + Command::Sonarr(SonarrCommand::Refresh(value)) + } +} + +pub(super) struct SonarrRefreshCommandHandler<'a, 'b> { + _app: &'a Arc>>, + command: SonarrRefreshCommand, + network: &'a mut dyn NetworkTrait, +} + +impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrRefreshCommand> + for SonarrRefreshCommandHandler<'a, 'b> +{ + fn with( + _app: &'a Arc>>, + command: SonarrRefreshCommand, + network: &'a mut dyn NetworkTrait, + ) -> Self { + SonarrRefreshCommandHandler { + _app, + command, + network, + } + } + + async fn handle(self) -> anyhow::Result { + let result = match self.command { + SonarrRefreshCommand::AllSeries => { + let resp = self + .network + .handle_network_event(SonarrEvent::UpdateAllSeries.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 new file mode 100644 index 0000000..f200424 --- /dev/null +++ b/src/cli/sonarr/refresh_command_handler_tests.rs @@ -0,0 +1,81 @@ +#[cfg(test)] +mod tests { + use pretty_assertions::assert_eq; + + use crate::cli::{ + sonarr::{refresh_command_handler::SonarrRefreshCommand, SonarrCommand}, + Command, + }; + use crate::Cli; + use clap::CommandFactory; + + #[test] + fn test_sonarr_refresh_command_from() { + let command = SonarrRefreshCommand::AllSeries; + + let result = Command::from(command.clone()); + + assert_eq!(result, Command::Sonarr(SonarrCommand::Refresh(command))); + } + + mod cli { + use super::*; + use clap::Parser; + use pretty_assertions::assert_eq; + use rstest::rstest; + + #[rstest] + fn test_refresh_commands_have_no_arg_requirements(#[values("all-series")] subcommand: &str) { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "refresh", subcommand]); + + assert!(result.is_ok()); + } + } + + mod handler { + use rstest::rstest; + use std::sync::Arc; + + use mockall::predicate::eq; + use serde_json::json; + use tokio::sync::Mutex; + + use crate::{ + cli::{sonarr::refresh_command_handler::SonarrRefreshCommand, CliCommandHandler}, + network::sonarr_network::SonarrEvent, + }; + use crate::{ + models::{sonarr_models::SonarrSerdeable, Serdeable}, + network::{MockNetworkTrait, NetworkEvent}, + }; + + #[rstest] + #[case(SonarrRefreshCommand::AllSeries, SonarrEvent::UpdateAllSeries)] + #[tokio::test] + async fn test_handle_refresh_command( + #[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() + .with(eq::(expected_sonarr_event.into())) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + + let result = SonarrRefreshCommandHandler::with(&app_arc, refresh_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 306525e..36d0870 100644 --- a/src/cli/sonarr/sonarr_command_tests.rs +++ b/src/cli/sonarr/sonarr_command_tests.rs @@ -544,6 +544,32 @@ 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 }); + + // let result = SonarrCliHandler::with(&app_arc, refresh_movie_command, &mut mock_network) + // .handle() + // .await; + + // assert!(result.is_ok()); + // } + #[tokio::test] async fn test_start_task_command() { let expected_task_name = SonarrTaskName::ApplicationUpdateCheck;