use super::*; use serde_json::Value; use std::path::Path; fn vectors_dir() -> std::path::PathBuf { Path::new(&std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR")).join("vectors") } /// RULE: every host reads a package document the same way. Each vector in /// vectors/opf.json names an OPF in vectors/opf/ and what the rule answers /// for it (`book.opf`: every fact and the book columns they fill); the /// Worker's and the fetcher's tests read the same file. With /// COTTON_WRITE_OPF_VECTORS=1 the answers are written instead (review them). #[test] fn every_vector_reads_as_it_says() { let path = vectors_dir().join("opf.json"); let mut vectors: Vec = serde_json::from_str(&std::fs::read_to_string(&path).expect("vectors/opf.json")).expect("JSON"); let write = std::env::var("COTTON_WRITE_OPF_VECTORS").is_ok(); for vector in &mut vectors { let file = vector["file"].as_str().expect("file").to_string(); let opf = std::fs::read_to_string(vectors_dir().join("opf").join(&file)).expect("the OPF"); let answer = crate::boundary::call("book.opf", &serde_json::json!({ "opf": opf }).to_string()).expect("an answer"); if write { vector["answer"] = answer; } else { assert_eq!(answer, vector["answer"], "{file}"); } } if write { std::fs::write(&path, serde_json::to_string_pretty(&vectors).expect("JSON") + "\n").expect("written"); } } /// RULE: the container names the package; a container naming none is none. #[test] fn the_container_names_the_package() { let container = br#" "#; assert_eq!(container_rootfile(container).as_deref(), Some("OEBPS/content.opf")); assert_eq!(container_rootfile(b""), None); assert_eq!(crate::boundary::call("book.container", r#"{"xml":""}"#), Ok(serde_json::json!({ "opf_path": null }))); } /// RULE: text that is no package says nothing, and is never refused. #[test] fn what_is_no_package_says_nothing() { assert_eq!(read(b"not xml ").columns(), serde_json::Map::new()); assert!(crate::boundary::call("book.opf", r#"{"opf":7}"#).unwrap_err().starts_with("book.opf: the input does not read")); } /// RULE: identifiers are read by what they are: Calibre's unknown date is no /// year, a UUID is no DOI, no ISBN and no Open Library id, and the bare /// forms read. #[test] fn identifiers_are_read_by_what_they_are() { assert_eq!(doi_in("10.1000/182").as_deref(), Some("10.1000/182")); assert_eq!(doi_in("https://doi.org/10.1000/ABC.1").as_deref(), Some("10.1000/ABC.1")); assert_eq!(doi_in("urn:uuid:10.1000/x"), None); assert_eq!(doi_in("10.12/short"), None, "a registrant is four digits at least"); assert_eq!(open_library_in("OL7353617M"), Some(OpenLibrary::Edition("OL7353617M".into()))); assert_eq!(open_library_in("/works/OL45883W"), Some(OpenLibrary::Work("OL45883W".into()))); assert_eq!(open_library_in("OLM"), None); assert_eq!(open_library_in("urn:uuid:5f9c0a7e"), None); assert_eq!(isbn_in("urn:uuid:1234567890", false), None, "ten digits nothing calls an ISBN"); let calibre_unknown = read(br#"0101-01-01T00:00:00+00:00"#); assert_eq!(calibre_unknown.year, None); } /// RULE: a reference in a title reads as what it stands for (quick-xml hands /// it over as its own event), and one XML does not know stays as written. #[test] fn references_in_the_metadata_read_as_their_characters() { let facts = read(br#"Pride & Prejudice — Volé ISmith & Sons"#); assert_eq!(facts.title.as_deref(), Some("Pride & Prejudice \u{2014} Vol\u{e9} I")); assert_eq!(facts.publisher.as_deref(), Some("Smith & Sons")); }