feat(readarr): fetch Readarr book editions and expose them on the CLI

This commit is contained in:
Dark-Alex-17 committed 2026-08-27 14:58:47 -06:00
1 parent 817ebeaf90
commit 4c6a9ece4e
7 files changed
+330 -4

No files matched your search

+16
View File
@@ -28,6 +28,15 @@ pub enum ReadarrListCommand {
}, },
#[command(about = "List all authors in your Readarr library")] #[command(about = "List all authors in your Readarr library")]
Authors, Authors,
#[command(about = "List all editions for the book with the given ID")]
BookEditions {
#[arg(
long,
help = "The Readarr ID of the book whose editions you want to list",
required = true
)]
book_id: i64,
},
#[command(about = "List all books for the author with the given ID")] #[command(about = "List all books for the author with the given ID")]
Books { Books {
#[arg( #[arg(
@@ -106,6 +115,13 @@ impl<'a, 'b> CliCommandHandler<'a, 'b, ReadarrListCommand> for ReadarrListComman
.await?; .await?;
serde_json::to_string_pretty(&resp)? serde_json::to_string_pretty(&resp)?
} }
ReadarrListCommand::BookEditions { book_id } => {
let resp = self
.network
.handle_network_event(ReadarrEvent::GetBookEditions(book_id).into())
.await?;
serde_json::to_string_pretty(&resp)?
}
ReadarrListCommand::Books { author_id } => { ReadarrListCommand::Books { author_id } => {
let resp = self let resp = self
.network .network
@@ -96,6 +96,40 @@ mod tests {
); );
} }
#[test]
fn test_list_book_editions_requires_book_id() {
let result =
Cli::command().try_get_matches_from(["managarr", "readarr", "list", "book-editions"]);
assert_err!(&result);
assert_eq!(
result.unwrap_err().kind(),
ErrorKind::MissingRequiredArgument
);
}
#[test]
fn test_list_book_editions_success() {
let expected_args = ReadarrListCommand::BookEditions { book_id: 1 };
let result = Cli::try_parse_from([
"managarr",
"readarr",
"list",
"book-editions",
"--book-id",
"1",
]);
assert_ok!(&result);
let Some(Command::Readarr(ReadarrCommand::List(book_editions_command))) =
result.unwrap().command
else {
panic!("Unexpected command type");
};
assert_eq!(book_editions_command, expected_args);
}
#[test] #[test]
fn test_list_books_success() { fn test_list_books_success() {
let expected_args = ReadarrListCommand::Books { author_id: 1 }; let expected_args = ReadarrListCommand::Books { author_id: 1 };
@@ -221,6 +255,32 @@ mod tests {
assert_ok!(&result); assert_ok!(&result);
} }
#[tokio::test]
async fn test_handle_list_book_editions_command() {
let expected_book_id = 1;
let mut mock_network = MockNetworkTrait::new();
mock_network
.expect_handle_network_event()
.with(eq::<NetworkEvent>(
ReadarrEvent::GetBookEditions(expected_book_id).into(),
))
.times(1)
.returning(|_| {
Ok(Serdeable::Readarr(ReadarrSerdeable::Value(
json!({"testResponse": "response"}),
)))
});
let app_arc = Arc::new(Mutex::new(App::test_default()));
let list_book_editions_command = ReadarrListCommand::BookEditions { book_id: 1 };
let result =
ReadarrListCommandHandler::with(&app_arc, list_book_editions_command, &mut mock_network)
.handle()
.await;
assert_ok!(&result);
}
#[tokio::test] #[tokio::test]
async fn test_handle_list_disk_space_command() { async fn test_handle_list_disk_space_command() {
let mut mock_network = MockNetworkTrait::new(); let mut mock_network = MockNetworkTrait::new();
@@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use log::info; use log::info;
use crate::models::readarr_models::Book; use crate::models::readarr_models::{Book, Edition};
use crate::network::readarr_network::ReadarrEvent; use crate::network::readarr_network::ReadarrEvent;
use crate::network::{Network, RequestMethod}; use crate::network::{Network, RequestMethod};
@@ -32,6 +32,37 @@ impl Network<'_, '_> {
.await .await
} }
pub(in crate::network::readarr_network) async fn get_book_editions(
&mut self,
book_id: i64,
) -> Result<Vec<Edition>> {
info!("Fetching editions for Readarr book with ID: {book_id}");
let event = ReadarrEvent::GetBookEditions(book_id);
let request_props = self
.request_props_from(
event,
RequestMethod::Get,
None::<()>,
None,
Some(format!("bookId={book_id}")),
)
.await;
self
.handle_request::<(), Vec<Edition>>(request_props, |mut editions_vec, mut app| {
editions_vec.sort_by_key(|edition| edition.id);
let book_details_modal = app
.data
.readarr_data
.book_details_modal
.get_or_insert_default();
book_details_modal.editions.set_items(editions_vec);
})
.await
}
pub(in crate::network::readarr_network) async fn get_books( pub(in crate::network::readarr_network) async fn get_books(
&mut self, &mut self,
author_id: i64, author_id: i64,
@@ -1,10 +1,11 @@
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::models::readarr_models::{Book, ReadarrSerdeable}; use crate::models::readarr_models::{Book, Edition, ReadarrSerdeable};
use crate::models::servarr_data::readarr::modals::BookDetailsModal;
use crate::network::network_tests::test_utils::{MockServarrApi, test_network}; use crate::network::network_tests::test_utils::{MockServarrApi, test_network};
use crate::network::readarr_network::ReadarrEvent; use crate::network::readarr_network::ReadarrEvent;
use crate::network::readarr_network::readarr_network_test_utils::test_utils::{ use crate::network::readarr_network::readarr_network_test_utils::test_utils::{
BOOK_JSON, stale_book, BOOK_JSON, EDITION_JSON, stale_book, stale_edition,
}; };
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serde_json::{Value, json}; use serde_json::{Value, json};
@@ -59,6 +60,187 @@ mod tests {
assert_err!(result); assert_err!(result);
} }
#[tokio::test]
async fn test_handle_get_book_editions_event() {
let editions_json = json!([
{
"id": 3,
"bookId": 1,
"foreignEditionId": "test-foreign-edition-id-3",
"monitored": false,
"isEbook": true,
"title": "Third Edition",
"language": "eng",
"overview": "the third edition",
"format": "Kindle Edition",
"publisher": "Third Publisher",
"pageCount": 320,
"releaseDate": "2023-03-01T00:00:00Z",
"isbn13": "9780000000003",
"asin": "B000000003",
"ratings": { "votes": 15, "value": 8.4, "popularity": 1.2 }
},
{
"id": 1,
"bookId": 1,
"foreignEditionId": "test-foreign-edition-id-1",
"monitored": true,
"isEbook": false,
"title": "First Edition",
"language": "eng",
"overview": "the first edition",
"format": "Hardcover",
"publisher": "First Publisher",
"pageCount": 662,
"releaseDate": "2023-01-01T00:00:00Z",
"isbn13": "9780000000001",
"asin": "B000000001",
"ratings": { "votes": 15, "value": 8.4, "popularity": 1.2 }
},
{
"id": 2,
"bookId": 1,
"foreignEditionId": "test-foreign-edition-id-2",
"monitored": true,
"isEbook": false,
"title": "Second Edition",
"language": "eng",
"overview": "the second edition",
"format": "Paperback",
"publisher": "Second Publisher",
"pageCount": 128,
"releaseDate": "2023-02-01T00:00:00Z",
"isbn13": "9780000000002",
"asin": "B000000002",
"ratings": { "votes": 15, "value": 8.4, "popularity": 1.2 }
}
]);
let response: Vec<Edition> = serde_json::from_value(editions_json.clone()).unwrap();
let mut sorted_editions = response.clone();
sorted_editions.sort_by_key(|edition| edition.id);
let (mock, app, _server) = MockServarrApi::get()
.returns(editions_json)
.query("bookId=1")
.build_for(ReadarrEvent::GetBookEditions(1))
.await;
{
let mut app = app.lock().await;
app.server_tabs.set_index(3);
app.data.readarr_data.book_details_modal = Some(BookDetailsModal::default());
}
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::GetBookEditions(1))
.await;
mock.assert_async().await;
let ReadarrSerdeable::Editions(editions) = result.unwrap() else {
panic!("Expected Editions")
};
assert_eq!(editions, response);
assert_eq!(
app
.lock()
.await
.data
.readarr_data
.book_details_modal
.as_ref()
.unwrap()
.editions
.items,
sorted_editions
);
}
#[tokio::test]
async fn test_handle_get_book_editions_event_empty_book_details_modal() {
let expected_editions: Vec<Edition> = vec![serde_json::from_str(EDITION_JSON).unwrap()];
let (mock, app, _server) = MockServarrApi::get()
.returns(json!(
[serde_json::from_str::<Value>(EDITION_JSON).unwrap()]
))
.query("bookId=1")
.build_for(ReadarrEvent::GetBookEditions(1))
.await;
app.lock().await.server_tabs.set_index(3);
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::GetBookEditions(1))
.await;
mock.assert_async().await;
let ReadarrSerdeable::Editions(editions) = result.unwrap() else {
panic!("Expected Editions")
};
assert_eq!(editions, expected_editions);
let app = app.lock().await;
assert_some!(&app.data.readarr_data.book_details_modal);
assert_eq!(
app
.data
.readarr_data
.book_details_modal
.as_ref()
.unwrap()
.editions
.items,
expected_editions
);
}
#[tokio::test]
async fn test_handle_get_book_editions_event_failure() {
let stale_editions = vec![stale_edition()];
let (mock, app, _server) = MockServarrApi::get()
.returns(json!(
[serde_json::from_str::<Value>(EDITION_JSON).unwrap()]
))
.status(500)
.query("bookId=1")
.build_for(ReadarrEvent::GetBookEditions(1))
.await;
{
let mut app = app.lock().await;
app.server_tabs.set_index(3);
let mut book_details_modal = BookDetailsModal::default();
book_details_modal
.editions
.set_items(stale_editions.clone());
app.data.readarr_data.book_details_modal = Some(book_details_modal);
}
let mut network = test_network(&app);
let result = network
.handle_readarr_event(ReadarrEvent::GetBookEditions(1))
.await;
mock.assert_async().await;
assert_err!(result);
assert_eq!(
app
.lock()
.await
.data
.readarr_data
.book_details_modal
.as_ref()
.unwrap()
.editions
.items,
stale_editions
);
}
#[tokio::test] #[tokio::test]
async fn test_handle_get_books_event() { async fn test_handle_get_books_event() {
let books_json = json!([ let books_json = json!([
+6
View File
@@ -34,6 +34,7 @@ pub enum ReadarrEvent {
GetAuthorDetails(i64), GetAuthorDetails(i64),
GetAuthorHistory(i64), GetAuthorHistory(i64),
GetBookDetails(i64), GetBookDetails(i64),
GetBookEditions(i64),
GetBooks(i64), GetBooks(i64),
GetDiskSpace, GetDiskSpace,
GetHostConfig, GetHostConfig,
@@ -75,6 +76,7 @@ impl NetworkResource for ReadarrEvent {
| ReadarrEvent::UpdateAndScanAuthor(_) => "/command", | ReadarrEvent::UpdateAndScanAuthor(_) => "/command",
ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host", ReadarrEvent::GetHostConfig | ReadarrEvent::GetSecurityConfig => "/config/host",
ReadarrEvent::GetDiskSpace => "/diskspace", ReadarrEvent::GetDiskSpace => "/diskspace",
ReadarrEvent::GetBookEditions(_) => "/edition",
ReadarrEvent::HealthCheck => "/health", ReadarrEvent::HealthCheck => "/health",
ReadarrEvent::GetAuthorHistory(_) => "/history/author", ReadarrEvent::GetAuthorHistory(_) => "/history/author",
ReadarrEvent::GetLogs(_) => "/log", ReadarrEvent::GetLogs(_) => "/log",
@@ -137,6 +139,10 @@ impl Network<'_, '_> {
.get_book_details(book_id) .get_book_details(book_id)
.await .await
.map(ReadarrSerdeable::from), .map(ReadarrSerdeable::from),
ReadarrEvent::GetBookEditions(book_id) => self
.get_book_editions(book_id)
.await
.map(ReadarrSerdeable::from),
ReadarrEvent::GetBooks(author_id) => { ReadarrEvent::GetBooks(author_id) => {
self.get_books(author_id).await.map(ReadarrSerdeable::from) self.get_books(author_id).await.map(ReadarrSerdeable::from)
} }
@@ -2,7 +2,7 @@
#[allow(dead_code)] #[allow(dead_code)]
pub mod test_utils { pub mod test_utils {
use crate::models::readarr_models::{ use crate::models::readarr_models::{
AddAuthorBody, AddAuthorOptions, AddAuthorSearchResult, Author, AuthorStatus, Book, AddAuthorBody, AddAuthorOptions, AddAuthorSearchResult, Author, AuthorStatus, Book, Edition,
MonitorType, NewItemMonitorType, ReadarrHistoryData, ReadarrHistoryEventType, MonitorType, NewItemMonitorType, ReadarrHistoryData, ReadarrHistoryEventType,
ReadarrHistoryItem, ReadarrHistoryItem,
}; };
@@ -67,6 +67,24 @@ pub mod test_utils {
"grabbed": false "grabbed": false
}"#; }"#;
pub const EDITION_JSON: &str = r#"{
"id": 1,
"bookId": 1,
"foreignEditionId": "test-foreign-edition-id",
"monitored": true,
"isEbook": true,
"title": "Test Edition",
"language": "eng",
"overview": "some interesting description of the edition",
"format": "Paperback",
"publisher": "Test Publisher",
"pageCount": 662,
"releaseDate": "2023-01-01T00:00:00Z",
"isbn13": "9780000000001",
"asin": "B000000001",
"ratings": { "votes": 15, "value": 8.4, "popularity": 1.2 }
}"#;
pub fn stale_author() -> Author { pub fn stale_author() -> Author {
Author { Author {
id: 99, id: 99,
@@ -96,6 +114,18 @@ pub mod test_utils {
} }
} }
pub fn stale_edition() -> Edition {
Edition {
id: 99,
book_id: 99,
foreign_edition_id: "foreign-edition-99".to_owned(),
monitored: true,
is_ebook: true,
title: "Stale Edition".to_owned(),
..Edition::default()
}
}
pub fn stale_add_author_search_result() -> AddAuthorSearchResult { pub fn stale_add_author_search_result() -> AddAuthorSearchResult {
AddAuthorSearchResult { AddAuthorSearchResult {
foreign_author_id: "foreign-author-99".to_owned(), foreign_author_id: "foreign-author-99".to_owned(),
@@ -32,6 +32,7 @@ mod tests {
#[case(ReadarrEvent::GetHostConfig, "/config/host")] #[case(ReadarrEvent::GetHostConfig, "/config/host")]
#[case(ReadarrEvent::GetSecurityConfig, "/config/host")] #[case(ReadarrEvent::GetSecurityConfig, "/config/host")]
#[case(ReadarrEvent::GetDiskSpace, "/diskspace")] #[case(ReadarrEvent::GetDiskSpace, "/diskspace")]
#[case(ReadarrEvent::GetBookEditions(1), "/edition")]
#[case(ReadarrEvent::HealthCheck, "/health")] #[case(ReadarrEvent::HealthCheck, "/health")]
#[case(ReadarrEvent::GetAuthorHistory(1), "/history/author")] #[case(ReadarrEvent::GetAuthorHistory(1), "/history/author")]
#[case(ReadarrEvent::GetLogs(500), "/log")] #[case(ReadarrEvent::GetLogs(500), "/log")]