From 1fe95d057bcb80798204430f63fba49dd4a75abe Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Fri, 15 Nov 2024 15:14:34 -0700 Subject: [PATCH] feat(cli): Sonarr CLI support for fetching all episodes for a given series --- src/cli/radarr/list_command_handler_tests.rs | 4 +- src/cli/sonarr/list_command_handler.rs | 12 +++++ src/cli/sonarr/list_command_handler_tests.rs | 51 ++++++++++++++++++++ src/main.rs | 10 ++-- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/src/cli/radarr/list_command_handler_tests.rs b/src/cli/radarr/list_command_handler_tests.rs index 2252d03..f922de9 100644 --- a/src/cli/radarr/list_command_handler_tests.rs +++ b/src/cli/radarr/list_command_handler_tests.rs @@ -80,8 +80,8 @@ mod tests { assert!(result.is_ok()); - if let Some(Command::Radarr(RadarrCommand::List(refresh_command))) = result.unwrap().command { - assert_eq!(refresh_command, expected_args); + if let Some(Command::Radarr(RadarrCommand::List(credits_command))) = result.unwrap().command { + assert_eq!(credits_command, expected_args); } } diff --git a/src/cli/sonarr/list_command_handler.rs b/src/cli/sonarr/list_command_handler.rs index 00756e3..227ba9a 100644 --- a/src/cli/sonarr/list_command_handler.rs +++ b/src/cli/sonarr/list_command_handler.rs @@ -21,6 +21,15 @@ mod list_command_handler_tests; pub enum SonarrListCommand { #[command(about = "List all items in the Sonarr blocklist")] Blocklist, + #[command(about = "List the episodes for the series with the given ID")] + Episodes { + #[arg( + long, + help = "The Sonarr ID of the series whose episodes you wish to fetch", + required = true + )] + series_id: i64, + }, #[command(about = "Fetch Sonarr logs")] Logs { #[arg(long, help = "How many log events to fetch", default_value_t = 500)] @@ -65,6 +74,9 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrListCommand> for SonarrListCommandH SonarrListCommand::Blocklist => { execute_network_event!(self, SonarrEvent::GetBlocklist); } + SonarrListCommand::Episodes { series_id } => { + execute_network_event!(self, SonarrEvent::GetEpisodes(Some(series_id))); + } SonarrListCommand::Logs { events, output_in_log_format, diff --git a/src/cli/sonarr/list_command_handler_tests.rs b/src/cli/sonarr/list_command_handler_tests.rs index b7a5c9a..b004b0a 100644 --- a/src/cli/sonarr/list_command_handler_tests.rs +++ b/src/cli/sonarr/list_command_handler_tests.rs @@ -31,6 +31,17 @@ mod tests { assert!(result.is_ok()); } + #[test] + fn test_list_episodes_requires_series_id() { + let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "list", "episodes"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + #[test] fn test_list_logs_events_flag_requires_arguments() { let result = @@ -54,6 +65,20 @@ mod tests { assert_eq!(refresh_command, expected_args); } } + + #[test] + fn test_list_episodes_success() { + let expected_args = SonarrListCommand::Episodes { series_id: 1 }; + let result = + Cli::try_parse_from(["managarr", "sonarr", "list", "episodes", "--series-id", "1"]); + + assert!(result.is_ok()); + + if let Some(Command::Sonarr(SonarrCommand::List(episodes_command))) = result.unwrap().command + { + assert_eq!(episodes_command, expected_args); + } + } } mod handler { @@ -102,6 +127,32 @@ mod tests { assert!(result.is_ok()); } + #[tokio::test] + async fn test_handle_list_episodes_command() { + let expected_series_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::GetEpisodes(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 list_episodes_command = SonarrListCommand::Episodes { series_id: 1 }; + + let result = + SonarrListCommandHandler::with(&app_arc, list_episodes_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/main.rs b/src/main.rs index 4123f5f..5e201f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use crossterm::execute; use crossterm::terminal::{ disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, }; +use human_panic::metadata; use log::{error, warn}; use network::NetworkTrait; use ratatui::backend::CrosstermBackend; @@ -287,14 +288,9 @@ fn create_cert(cert_path: &String, servarr_name: &str) -> Certificate { #[cfg(not(debug_assertions))] fn panic_hook(info: &PanicHookInfo<'_>) { - use human_panic::{handle_dump, print_msg, Metadata}; + use human_panic::{handle_dump, print_msg}; - let meta = Metadata { - version: env!("CARGO_PKG_VERSION").into(), - name: env!("CARGO_PKG_NAME").into(), - authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(), - homepage: env!("CARGO_PKG_HOMEPAGE").into(), - }; + let meta = metadata!(); let file_path = handle_dump(&meta, info); disable_raw_mode().unwrap(); execute!(io::stdout(), LeaveAlternateScreen).unwrap();