From 5872a6ba723e3cf0cd669eb54df66e33006c7c60 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Wed, 20 Nov 2024 14:13:20 -0700 Subject: [PATCH] feat(cli): Support for fetching all Sonarr history events --- src/cli/sonarr/list_command_handler.rs | 8 +++ src/cli/sonarr/list_command_handler_tests.rs | 51 +++++++++++++++++++- src/network/sonarr_network.rs | 10 ++-- 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/src/cli/sonarr/list_command_handler.rs b/src/cli/sonarr/list_command_handler.rs index a03c993..b4a265e 100644 --- a/src/cli/sonarr/list_command_handler.rs +++ b/src/cli/sonarr/list_command_handler.rs @@ -32,6 +32,11 @@ pub enum SonarrListCommand { )] series_id: i64, }, + #[command(about = "Fetch all Sonarr history events")] + History { + #[arg(long, help = "How many history events to fetch", default_value_t = 500)] + events: u64, + }, #[command(about = "List all Sonarr indexers")] Indexers, #[command(about = "Fetch Sonarr logs")] @@ -88,6 +93,9 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrListCommand> for SonarrListCommandH SonarrListCommand::Episodes { series_id } => { execute_network_event!(self, SonarrEvent::GetEpisodes(Some(series_id))); } + SonarrListCommand::History { events: items } => { + execute_network_event!(self, SonarrEvent::GetHistory(Some(items))); + } SonarrListCommand::Indexers => { execute_network_event!(self, SonarrEvent::GetIndexers); } diff --git a/src/cli/sonarr/list_command_handler_tests.rs b/src/cli/sonarr/list_command_handler_tests.rs index c5f48b8..cf7253d 100644 --- a/src/cli/sonarr/list_command_handler_tests.rs +++ b/src/cli/sonarr/list_command_handler_tests.rs @@ -50,6 +50,27 @@ mod tests { ); } + #[test] + fn test_list_history_events_flag_requires_arguments() { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "list", "history", "--events"]); + + assert!(result.is_err()); + assert_eq!(result.unwrap_err().kind(), ErrorKind::InvalidValue); + } + + #[test] + fn test_list_history_default_values() { + let expected_args = SonarrListCommand::History { events: 500 }; + let result = Cli::try_parse_from(["managarr", "sonarr", "list", "history"]); + + assert!(result.is_ok()); + + if let Some(Command::Sonarr(SonarrCommand::List(history_command))) = result.unwrap().command { + assert_eq!(history_command, expected_args); + } + } + #[test] fn test_list_logs_events_flag_requires_arguments() { let result = @@ -69,8 +90,8 @@ mod tests { assert!(result.is_ok()); - if let Some(Command::Sonarr(SonarrCommand::List(refresh_command))) = result.unwrap().command { - assert_eq!(refresh_command, expected_args); + if let Some(Command::Sonarr(SonarrCommand::List(logs_command))) = result.unwrap().command { + assert_eq!(logs_command, expected_args); } } @@ -165,6 +186,32 @@ mod tests { assert!(result.is_ok()); } + #[tokio::test] + async fn test_handle_list_history_command() { + let expected_events = 1000; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::GetHistory(Some(expected_events)).into(), + )) + .times(1) + .returning(|_| { + Ok(Serdeable::Sonarr(SonarrSerdeable::Value( + json!({"testResponse": "response"}), + ))) + }); + let app_arc = Arc::new(Mutex::new(App::default())); + let list_history_command = SonarrListCommand::History { events: 1000 }; + + let result = + SonarrListCommandHandler::with(&app_arc, list_history_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } + #[tokio::test] async fn test_handle_list_logs_command() { let expected_events = 1000; diff --git a/src/network/sonarr_network.rs b/src/network/sonarr_network.rs index b81e851..e7cd2e2 100644 --- a/src/network/sonarr_network.rs +++ b/src/network/sonarr_network.rs @@ -108,8 +108,8 @@ impl<'a, 'b> Network<'a, 'b> { .get_episode_details(episode_id) .await .map(SonarrSerdeable::from), - SonarrEvent::GetHistory(items) => self - .get_sonarr_history(items) + SonarrEvent::GetHistory(events) => self + .get_sonarr_history(events) .await .map(SonarrSerdeable::from), SonarrEvent::GetHostConfig => self @@ -467,13 +467,13 @@ impl<'a, 'b> Network<'a, 'b> { .await } - async fn get_sonarr_history(&mut self, items: Option) -> Result { + async fn get_sonarr_history(&mut self, events: Option) -> Result { info!("Fetching all Sonarr history events"); - let event = SonarrEvent::GetHistory(items); + let event = SonarrEvent::GetHistory(events); let params = format!( "pageSize={}&sortDirection=descending&sortKey=time", - items.unwrap_or(500) + events.unwrap_or(500) ); let request_props = self .request_props_from(event, RequestMethod::Get, None::<()>, None, Some(params))