feat(cli): Sonarr CLI support for fetching all episodes for a given series

This commit is contained in:
2024-11-15 15:14:34 -07:00
parent 6dffc90e92
commit 1fe95d057b
4 changed files with 68 additions and 9 deletions
+2 -2
View File
@@ -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);
}
}
+12
View File
@@ -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,
@@ -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::<NetworkEvent>(
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;
+3 -7
View File
@@ -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();