use super::*; use crate::media::book_file::read_epub; use crate::media::book_file::tests::{zip, CONTAINER}; use std::collections::HashMap; /// A folder in memory: each file's path and bytes. struct Folder(HashMap>); impl Folder { fn files(&self) -> Vec { self.0.keys().cloned().collect() } fn walk(&self, held: &[HeldBook]) -> FolderWalk { walk_folder(&self.files(), |path| self.0.get(path).cloned(), |path| read_epub(&zip_bytes(&self.0[path])), held) } } /// An archive's bytes, read back through the test reader. fn zip_bytes(bytes: &[u8]) -> crate::media::book_file::tests::Bytes { crate::media::book_file::tests::Bytes(bytes.to_vec()) } /// An invented EPUB whose package says `metadata`. fn epub(metadata: &str) -> Vec { let opf = format!(r#"{metadata}"#); zip(&[("META-INF/container.xml", CONTAINER), ("OEBPS/content.opf", opf.as_bytes()), ("OEBPS/inside.jpg", b"\xff\xd8 the cover inside")]).0 } /// An invented Calibre `metadata.opf`, as Calibre writes one (EPUB 2 shape). fn metadata_opf(title: &str, author: &str, isbn: &str, series: &str, index: &str) -> Vec { format!( r#" 7 0e7a-invented {title} {author} 0101-01-01T00:00:00+00:00 {isbn} Invented Calibre's Press "# ) .into_bytes() } fn calibre_library() -> Folder { let mut files = HashMap::new(); // A Calibre book: its metadata.opf says more than its EPUB, and other than it. files.insert("Hollis Brand/A Lamp for Winter (7)/A Lamp for Winter - Hollis Brand.epub".into(), epub("lamp_winter_finalenFrom the file.")); files.insert("Hollis Brand/A Lamp for Winter (7)/A Lamp for Winter - Hollis Brand.pdf".into(), b"%PDF-1.4 the same book".to_vec()); files.insert("Hollis Brand/A Lamp for Winter (7)/metadata.opf".into(), metadata_opf("A Lamp for Winter", "Hollis Brand", "9780000000019", "Winter Lamps", "2.0")); files.insert("Hollis Brand/A Lamp for Winter (7)/cover.jpg".into(), b"\xff\xd8 calibre's cover".to_vec()); // A Calibre book held as a PDF alone. files.insert("Mirel Okafor/Salt Roads (8)/Salt Roads - Mirel Okafor.pdf".into(), b"%PDF-1.4 invented".to_vec()); files.insert("Mirel Okafor/Salt Roads (8)/metadata.opf".into(), metadata_opf("Salt Roads", "Mirel Okafor", "9780000000026", "", "1.0")); // A Calibre book already on the shelf, by its ISBN. files.insert("Irena Valcourt/The Glass Orchard (9)/The Glass Orchard.epub".into(), epub("The Glass Orchard")); files.insert("Irena Valcourt/The Glass Orchard (9)/metadata.opf".into(), metadata_opf("The Glass Orchard", "Irena Valcourt", "9780000000033", "", "1.0")); // A loose EPUB, and a second copy of it under another name (the same Open Library edition). files.insert("loose/orchard-notes.epub".into(), epub("Orchard NotesOL5678M")); files.insert("loose/orchard-notes copy.epub".into(), epub("Orchard Notesopenlibrary:OL5678M")); // A loose file that is no EPUB, and files that are no books. files.insert("loose/broken.epub".into(), b"not a zip".to_vec()); files.insert("Calibre Library/metadata.db".into(), b"sqlite".to_vec()); files.insert("loose/readme.txt".into(), b"words".to_vec()); Folder(files) } /// RULE: a Calibre library walks in from its metadata.opf and cover first and /// its file second: one book per Calibre folder (its EPUB before its PDF), /// Calibre's title, author, ISBN, series, subjects, publisher and rating, /// the EPUB filling only what metadata.opf leaves unsaid; a book already /// held by its ISBN, or met earlier in the walk by its Open Library id, is /// skipped and counted; a loose EPUB that will not read is said by name. #[test] fn a_calibre_library_walks_in_from_its_metadata_and_cover() { let held = [HeldBook { isbn: Some("9780000000033".into()), ..HeldBook::default() }]; let walk = calibre_library().walk(&held); let files: Vec<&str> = walk.books.iter().map(|book| book.file.as_str()).collect(); assert_eq!( files, vec!["Hollis Brand/A Lamp for Winter (7)/A Lamp for Winter - Hollis Brand.epub", "Mirel Okafor/Salt Roads (8)/Salt Roads - Mirel Okafor.pdf", "loose/orchard-notes copy.epub"], "{walk:#?}" ); assert_eq!(walk.duplicates, 2, "the held Glass Orchard and the second Orchard Notes"); assert_eq!(walk.unreadable.len(), 1); assert_eq!(walk.unreadable[0].0, "loose/broken.epub"); let lamp = &walk.books[0]; assert!(lamp.calibre); assert_eq!(lamp.format, "epub"); assert_eq!(lamp.cover, Some(WalkedCover::Beside("Hollis Brand/A Lamp for Winter (7)/cover.jpg".into())), "Calibre's cover, not the one inside"); let columns = lamp.columns(); assert_eq!(columns["title"], "A Lamp for Winter", "metadata.opf's title, not the file's"); assert_eq!(columns["author"], "Hollis Brand"); assert_eq!(columns["isbn"], "9780000000019"); assert_eq!(columns["series"], "Winter Lamps"); assert_eq!(columns["series_number"], 2.0); assert_eq!(columns["publisher"], "Calibre's Press"); assert_eq!(columns["subjects"], "Invented"); assert_eq!(columns["rating"], 3.0, "Calibre's six of ten"); assert_eq!(columns["language"], "en", "the file fills what metadata.opf leaves unsaid"); assert_eq!(columns["description"], "From the file."); assert!(!columns.contains_key("year"), "Calibre's unknown date is no year"); let salt = &walk.books[1]; assert!(salt.calibre); assert_eq!((salt.format, salt.cover.clone()), ("pdf", None)); assert_eq!(salt.facts.title.as_deref(), Some("Salt Roads")); assert!(!salt.columns().contains_key("series"), "an empty calibre:series is no series"); let loose = &walk.books[2]; assert!(!loose.calibre); assert!(matches!(loose.cover, Some(WalkedCover::Inside(_))), "a loose EPUB's own cover"); assert_eq!(loose.facts.ol_edition.as_deref(), Some("OL5678M")); assert!(!loose.columns().contains_key("rating")); assert_eq!(walk.answer(), "Added 3 books, 2 from a Calibre library. 2 books were here already (the same ISBN or Open Library id). 1 file could not be read."); } /// RULE: the walk's answer says what came in in words: none, all from /// Calibre, one here already. #[test] fn a_walks_answer_in_words() { assert_eq!(FolderWalk::default().answer(), "Added no books."); let mut lone = Folder(HashMap::new()); lone.0.insert("A/B (1)/b.epub".into(), epub("B")); lone.0.insert("A/B (1)/metadata.opf".into(), metadata_opf("B", "A", "9780000000040", "", "1")); lone.0.insert("c.pdf".into(), b"%PDF".to_vec()); let walk = lone.walk(&[HeldBook { isbn: None, ol_work: None, ol_edition: Some("OL1M".into()) }]); assert_eq!(walk.answer(), "Added 2 books, 1 from a Calibre library."); assert_eq!(walk.books.iter().find(|book| book.file == "c.pdf").and_then(|book| book.facts.title.clone()).as_deref(), Some("c"), "a PDF's title is its name"); let again = lone.walk(&[HeldBook { isbn: Some("9780000000040".into()), ..HeldBook::default() }]); assert_eq!(again.answer(), "Added 1 book. 1 book was here already (the same ISBN or Open Library id)."); }