From df1eea22abbf3ba11adca38f69f7f6b7e0cb8739 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Wed, 20 Nov 2024 14:58:54 -0700 Subject: [PATCH] feat(cli): Support for fetching history for a given series ID --- src/cli/sonarr/list_command_handler.rs | 12 +++++ src/cli/sonarr/list_command_handler_tests.rs | 57 ++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/cli/sonarr/list_command_handler.rs b/src/cli/sonarr/list_command_handler.rs index b4a265e..b95ac8d 100644 --- a/src/cli/sonarr/list_command_handler.rs +++ b/src/cli/sonarr/list_command_handler.rs @@ -55,6 +55,15 @@ pub enum SonarrListCommand { QueuedEvents, #[command(about = "List all series in your Sonarr library")] Series, + #[command(about = "Fetch all history events for the series with the given ID")] + SeriesHistory { + #[arg( + long, + help = "The Sonarr ID of the series whose history you wish to fetch", + required = true + )] + series_id: i64, + }, } impl From for Command { @@ -127,6 +136,9 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrListCommand> for SonarrListCommandH SonarrListCommand::Series => { execute_network_event!(self, SonarrEvent::ListSeries); } + SonarrListCommand::SeriesHistory { series_id } => { + execute_network_event!(self, SonarrEvent::GetSeriesHistory(Some(series_id))); + } } Ok(()) diff --git a/src/cli/sonarr/list_command_handler_tests.rs b/src/cli/sonarr/list_command_handler_tests.rs index cf7253d..6d7d9c1 100644 --- a/src/cli/sonarr/list_command_handler_tests.rs +++ b/src/cli/sonarr/list_command_handler_tests.rs @@ -108,6 +108,37 @@ mod tests { assert_eq!(episodes_command, expected_args); } } + + #[test] + fn test_list_series_history_requires_series_id() { + let result = + Cli::command().try_get_matches_from(["managarr", "sonarr", "list", "series-history"]); + + assert!(result.is_err()); + assert_eq!( + result.unwrap_err().kind(), + ErrorKind::MissingRequiredArgument + ); + } + + #[test] + fn test_list_series_history_success() { + let expected_args = SonarrListCommand::SeriesHistory { series_id: 1 }; + let result = Cli::try_parse_from([ + "managarr", + "sonarr", + "list", + "series-history", + "--series-id", + "1", + ]); + + assert!(result.is_ok()); + + if let Some(Command::Sonarr(SonarrCommand::List(series_command))) = result.unwrap().command { + assert_eq!(series_command, expected_args); + } + } } mod handler { @@ -239,5 +270,31 @@ mod tests { assert!(result.is_ok()); } + + #[tokio::test] + async fn test_handle_list_series_history_command() { + let expected_series_id = 1; + let mut mock_network = MockNetworkTrait::new(); + mock_network + .expect_handle_network_event() + .with(eq::( + SonarrEvent::GetSeriesHistory(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_series_history_command = SonarrListCommand::SeriesHistory { series_id: 1 }; + + let result = + SonarrListCommandHandler::with(&app_arc, list_series_history_command, &mut mock_network) + .handle() + .await; + + assert!(result.is_ok()); + } } }