feat(cli): Support for refreshing a specific series in Sonarr

This commit is contained in:
2024-11-22 19:13:57 -07:00
parent c3577a0724
commit eb06787bb2
3 changed files with 100 additions and 27 deletions
+16
View File
@@ -19,6 +19,15 @@ mod refresh_command_handler_tests;
pub enum SonarrRefreshCommand {
#[command(about = "Refresh all series data for all series in your Sonarr library")]
AllSeries,
#[command(about = "Refresh series data and scan disk for the series with the given ID")]
Series {
#[arg(
long,
help = "The ID of the series to refresh information on and to scan the disk for",
required = true
)]
series_id: i64,
},
}
impl From<SonarrRefreshCommand> for Command {
@@ -57,6 +66,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, SonarrRefreshCommand>
.await?;
serde_json::to_string_pretty(&resp)?
}
SonarrRefreshCommand::Series { series_id } => {
let resp = self
.network
.handle_network_event(SonarrEvent::UpdateAndScanSeries(Some(series_id)).into())
.await?;
serde_json::to_string_pretty(&resp)?
}
};
Ok(result)
@@ -20,7 +20,7 @@ mod tests {
mod cli {
use super::*;
use clap::Parser;
use clap::{error::ErrorKind, Parser};
use pretty_assertions::assert_eq;
use rstest::rstest;
@@ -31,6 +31,38 @@ mod tests {
assert!(result.is_ok());
}
#[test]
fn test_refresh_series_requires_series_id() {
let result = Cli::command().try_get_matches_from(["managarr", "sonarr", "refresh", "series"]);
assert!(result.is_err());
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_refresh_series_success() {
let expected_args = SonarrRefreshCommand::Series { series_id: 1 };
let result = Cli::try_parse_from([
"managarr",
"sonarr",
"refresh",
"series",
"--series-id",
"1",
]);
assert!(result.is_ok());
if let Some(Command::Sonarr(SonarrCommand::Refresh(refresh_command))) =
result.unwrap().command
{
assert_eq!(refresh_command, expected_args);
}
}
}
mod handler {
@@ -41,6 +73,7 @@ mod tests {
use serde_json::json;
use tokio::sync::Mutex;
use crate::{app::App, cli::sonarr::refresh_command_handler::SonarrRefreshCommandHandler};
use crate::{
cli::{sonarr::refresh_command_handler::SonarrRefreshCommand, CliCommandHandler},
network::sonarr_network::SonarrEvent,
@@ -57,8 +90,6 @@ mod tests {
#[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()
@@ -77,5 +108,31 @@ mod tests {
assert!(result.is_ok());
}
#[tokio::test]
async fn test_handle_refresh_series_command() {
let expected_series_id = 1;
let mut mock_network = MockNetworkTrait::new();
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
SonarrEvent::UpdateAndScanSeries(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 refresh_series_command = SonarrRefreshCommand::Series { series_id: 1 };
let result =
SonarrRefreshCommandHandler::with(&app_arc, refresh_series_command, &mut mock_network)
.handle()
.await;
assert!(result.is_ok());
}
}
}
+24 -24
View File
@@ -312,7 +312,7 @@ mod tests {
sonarr::{
add_command_handler::SonarrAddCommand, delete_command_handler::SonarrDeleteCommand,
get_command_handler::SonarrGetCommand, list_command_handler::SonarrListCommand,
SonarrCliHandler, SonarrCommand,
refresh_command_handler::SonarrRefreshCommand, SonarrCliHandler, SonarrCommand,
},
CliCommandHandler,
},
@@ -544,31 +544,31 @@ 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::<NetworkEvent>(
// 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 });
#[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::<NetworkEvent>(
SonarrEvent::UpdateAndScanSeries(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 refresh_series_command =
SonarrCommand::Refresh(SonarrRefreshCommand::Series { series_id: 1 });
// let result = SonarrCliHandler::with(&app_arc, refresh_movie_command, &mut mock_network)
// .handle()
// .await;
let result = SonarrCliHandler::with(&app_arc, refresh_series_command, &mut mock_network)
.handle()
.await;
// assert!(result.is_ok());
// }
assert!(result.is_ok());
}
#[tokio::test]
async fn test_start_task_command() {