feat(readarr): search for new Readarr authors and expose the command on the CLI
This commit is contained in:
7 files changed
+244
-4
No files matched your search
@@ -2,10 +2,12 @@ use anyhow::Result;
|
||||
use log::info;
|
||||
|
||||
use crate::models::Route;
|
||||
use crate::models::readarr_models::Author;
|
||||
use crate::models::readarr_models::{AddAuthorSearchResult, Author};
|
||||
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
|
||||
use crate::models::stateful_table::StatefulTable;
|
||||
use crate::network::readarr_network::ReadarrEvent;
|
||||
use crate::network::{Network, RequestMethod};
|
||||
use urlencoding::encode;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "readarr_authors_network_tests.rs"]
|
||||
@@ -55,4 +57,44 @@ impl Network<'_, '_> {
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub(in crate::network::readarr_network) async fn search_author(
|
||||
&mut self,
|
||||
query: String,
|
||||
) -> Result<Vec<AddAuthorSearchResult>> {
|
||||
info!("Searching for author: {query}");
|
||||
let event = ReadarrEvent::SearchNewAuthor(String::new());
|
||||
|
||||
let request_props = self
|
||||
.request_props_from(
|
||||
event,
|
||||
RequestMethod::Get,
|
||||
None::<()>,
|
||||
None,
|
||||
Some(format!("term={}", encode(&query))),
|
||||
)
|
||||
.await;
|
||||
|
||||
let result = self
|
||||
.handle_request::<(), Vec<AddAuthorSearchResult>>(request_props, |author_vec, mut app| {
|
||||
if author_vec.is_empty() {
|
||||
app.pop_and_push_navigation_stack(ActiveReadarrBlock::AddAuthorEmptySearchResults.into());
|
||||
} else if let Some(add_searched_authors) =
|
||||
app.data.readarr_data.add_searched_authors.as_mut()
|
||||
{
|
||||
add_searched_authors.set_items(author_vec);
|
||||
} else {
|
||||
let mut add_searched_authors = StatefulTable::default();
|
||||
add_searched_authors.set_items(author_vec);
|
||||
app.data.readarr_data.add_searched_authors = Some(add_searched_authors);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
if result.is_err() {
|
||||
self.app.lock().await.data.readarr_data.add_searched_authors = Some(StatefulTable::default());
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::models::readarr_models::{Author, ReadarrSerdeable};
|
||||
use crate::models::readarr_models::{AddAuthorSearchResult, Author, ReadarrSerdeable};
|
||||
use crate::models::servarr_data::readarr::readarr_data::ActiveReadarrBlock;
|
||||
use crate::models::stateful_table::StatefulTable;
|
||||
use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
|
||||
use crate::network::readarr_network::ReadarrEvent;
|
||||
use crate::network::readarr_network::readarr_network_test_utils::test_utils::{
|
||||
AUTHOR_JSON, stale_author,
|
||||
ADD_AUTHOR_SEARCH_RESULT_JSON, AUTHOR_JSON, stale_add_author_search_result, stale_author,
|
||||
};
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::{Value, json};
|
||||
@@ -187,4 +188,104 @@ mod tests {
|
||||
stale_authors
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_search_new_author_event() {
|
||||
let search_results_json =
|
||||
json!([serde_json::from_str::<Value>(ADD_AUTHOR_SEARCH_RESULT_JSON).unwrap()]);
|
||||
let expected_results: Vec<AddAuthorSearchResult> =
|
||||
serde_json::from_value(search_results_json.clone()).unwrap();
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(search_results_json)
|
||||
.query("term=test%20%26%20author")
|
||||
.build_for(ReadarrEvent::SearchNewAuthor("test & author".to_owned()))
|
||||
.await;
|
||||
app.lock().await.server_tabs.set_index(3);
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::SearchNewAuthor("test & author".to_owned()))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
let ReadarrSerdeable::AddAuthorSearchResults(search_results) = result.unwrap() else {
|
||||
panic!("Expected AddAuthorSearchResults")
|
||||
};
|
||||
|
||||
assert_eq!(search_results, expected_results);
|
||||
assert_eq!(
|
||||
app
|
||||
.lock()
|
||||
.await
|
||||
.data
|
||||
.readarr_data
|
||||
.add_searched_authors
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.items,
|
||||
expected_results
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_search_new_author_event_navigates_to_empty_results_when_empty() {
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(json!([]))
|
||||
.query("term=nonexistent")
|
||||
.build_for(ReadarrEvent::SearchNewAuthor("nonexistent".to_owned()))
|
||||
.await;
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
app.push_navigation_stack(ActiveReadarrBlock::AddAuthorSearchResults.into());
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::SearchNewAuthor("nonexistent".to_owned()))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
assert_ok!(result);
|
||||
let app = app.lock().await;
|
||||
assert_none!(&app.data.readarr_data.add_searched_authors);
|
||||
assert_eq!(
|
||||
app.get_current_route(),
|
||||
ActiveReadarrBlock::AddAuthorEmptySearchResults.into()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_handle_search_new_author_event_sets_empty_table_on_api_error() {
|
||||
let (mock, app, _server) = MockServarrApi::get()
|
||||
.returns(json!([serde_json::from_str::<Value>(
|
||||
ADD_AUTHOR_SEARCH_RESULT_JSON
|
||||
)
|
||||
.unwrap()]))
|
||||
.status(500)
|
||||
.query("term=nonexistent")
|
||||
.build_for(ReadarrEvent::SearchNewAuthor("nonexistent".to_owned()))
|
||||
.await;
|
||||
{
|
||||
let mut app = app.lock().await;
|
||||
app.server_tabs.set_index(3);
|
||||
let mut stale_table = StatefulTable::default();
|
||||
stale_table.set_items(vec![stale_add_author_search_result()]);
|
||||
app.data.readarr_data.add_searched_authors = Some(stale_table);
|
||||
}
|
||||
let mut network = test_network(&app);
|
||||
|
||||
let result = network
|
||||
.handle_readarr_event(ReadarrEvent::SearchNewAuthor("nonexistent".to_owned()))
|
||||
.await;
|
||||
|
||||
mock.assert_async().await;
|
||||
|
||||
assert_err!(result);
|
||||
let app = app.lock().await;
|
||||
assert_some!(&app.data.readarr_data.add_searched_authors);
|
||||
assert_is_empty!(app.data.readarr_data.add_searched_authors.as_ref().unwrap());
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ pub enum ReadarrEvent {
|
||||
GetUpdates,
|
||||
HealthCheck,
|
||||
ListAuthors,
|
||||
SearchNewAuthor(String),
|
||||
StartTask(ReadarrTaskName),
|
||||
}
|
||||
|
||||
@@ -47,6 +48,7 @@ impl NetworkResource for ReadarrEvent {
|
||||
fn resource(&self) -> &'static str {
|
||||
match &self {
|
||||
ReadarrEvent::GetAuthorDetails(_) | ReadarrEvent::ListAuthors => "/author",
|
||||
ReadarrEvent::SearchNewAuthor(_) => "/author/lookup",
|
||||
ReadarrEvent::GetQueuedEvents | ReadarrEvent::StartTask(_) => "/command",
|
||||
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
|
||||
ReadarrEvent::GetDiskSpace => "/diskspace",
|
||||
@@ -135,6 +137,9 @@ impl Network<'_, '_> {
|
||||
.await
|
||||
.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::ListAuthors => self.list_authors().await.map(ReadarrSerdeable::from),
|
||||
ReadarrEvent::SearchNewAuthor(query) => {
|
||||
self.search_author(query).await.map(ReadarrSerdeable::from)
|
||||
}
|
||||
ReadarrEvent::StartTask(task_name) => self
|
||||
.start_readarr_task(task_name)
|
||||
.await
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#[cfg(test)]
|
||||
#[allow(dead_code)]
|
||||
pub mod test_utils {
|
||||
use crate::models::readarr_models::{Author, AuthorStatus, NewItemMonitorType};
|
||||
use crate::models::readarr_models::{
|
||||
AddAuthorSearchResult, Author, AuthorStatus, NewItemMonitorType,
|
||||
};
|
||||
use crate::models::servarr_models::RootFolder;
|
||||
|
||||
pub const AUTHOR_JSON: &str = r#"{
|
||||
@@ -31,6 +33,18 @@ pub mod test_utils {
|
||||
}
|
||||
}"#;
|
||||
|
||||
pub const ADD_AUTHOR_SEARCH_RESULT_JSON: &str = r#"{
|
||||
"foreignAuthorId": "test-foreign-id",
|
||||
"authorName": "Test Author",
|
||||
"status": "continuing",
|
||||
"ended": false,
|
||||
"overview": "some interesting description of the author",
|
||||
"authorType": "Person",
|
||||
"disambiguation": "American novelist",
|
||||
"genres": ["science fiction"],
|
||||
"ratings": { "votes": 15, "value": 8.4, "popularity": 1.2 }
|
||||
}"#;
|
||||
|
||||
pub fn stale_author() -> Author {
|
||||
Author {
|
||||
id: 99,
|
||||
@@ -49,6 +63,15 @@ pub mod test_utils {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stale_add_author_search_result() -> AddAuthorSearchResult {
|
||||
AddAuthorSearchResult {
|
||||
foreign_author_id: "foreign-author-99".to_owned(),
|
||||
author_name: "Stale Author".into(),
|
||||
status: AuthorStatus::Continuing,
|
||||
..AddAuthorSearchResult::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stale_root_folder() -> RootFolder {
|
||||
RootFolder {
|
||||
id: 99,
|
||||
|
||||
@@ -15,6 +15,7 @@ mod tests {
|
||||
#[rstest]
|
||||
#[case(ReadarrEvent::GetAuthorDetails(1), "/author")]
|
||||
#[case(ReadarrEvent::ListAuthors, "/author")]
|
||||
#[case(ReadarrEvent::SearchNewAuthor(String::new()), "/author/lookup")]
|
||||
#[case(ReadarrEvent::GetQueuedEvents, "/command")]
|
||||
#[case(ReadarrEvent::StartTask(Default::default()), "/command")]
|
||||
#[case(ReadarrEvent::GetHostConfig, "/config/host")]
|
||||
|
||||
Reference in new issue
Block a user