//! Mandatory test 4: export, then import into an empty library, reproduces books, states, shelves, highlights and //! stats exactly. mod common; use common::{events, info}; use liber_library::export::{from_json, to_json, write_zip}; use liber_library::library::{Colour, Field, Mark}; use liber_library::stats::stats; use liber_library::{Clock, Event, Library, Op}; use proptest::prelude::*; use serde_json::json; use std::io::Read; fn assert_round_trip(lib: &Library) { let json = to_json(lib, "2026-11-17T10:00:00Z"); let back = from_json(&json).expect("import"); assert_eq!(&back, lib); for y in [None, Some(2024), Some(2025), Some(2026)] { assert_eq!(stats(&back, y), stats(lib, y), "stats {y:?}"); } // Exporting the imported library gives the same file. assert_eq!(to_json(&back, "2026-11-17T10:00:00Z"), json); } proptest! { #![proptest_config(ProptestConfig { cases: 150, ..ProptestConfig::default() })] #[test] fn random_libraries_round_trip(seed in any::(), n in 1usize..120) { let mut all = events(seed, "phone", n, 0); all.extend(events(seed ^ 0x5555, "desk", n / 2, 90_000)); let lib = Library::from_events(&all); assert_round_trip(&lib); } } #[test] fn a_library_with_conflicts_round_trips() { let mut c = Clock::default(); let mut d = Clock::default(); let mut e = |id: &str, dev: &str, t: u64, op: Op| { let hlc = if dev == "a" { c.tick(t) } else { d.tick(t) }; Event { id: id.into(), hlc, device: dev.into(), op } }; let evs = vec![ e("1", "a", 1, Op::BookAdd { book: "b".into(), info: info(0), restore: false }), e("2", "a", 2, Op::BookSet { book: "b".into(), field: Field::Review, value: json!("A"), base: None }), e( "3", "a", 3, Op::BookSet { book: "b".into(), field: Field::Review, value: json!("B"), base: Some("2".into()) }, ), e( "4", "b", 3, Op::BookSet { book: "b".into(), field: Field::Review, value: json!("C"), base: Some("2".into()) }, ), e( "5", "a", 4, Op::HighlightAdd { id: "h".into(), book: "b".into(), kind: Mark::Note, anchor: json!({"quote": {"exact": "slovo"}}), colour: Colour::Green, note: "x".into(), created: 4, }, ), e( "6", "a", 5, Op::HighlightSet { id: "h".into(), colour: None, note: Some("y".into()), base: Some("5".into()) }, ), e( "7", "b", 5, Op::HighlightSet { id: "h".into(), colour: None, note: Some("z".into()), base: Some("5".into()) }, ), e("8", "a", 6, Op::BookAdd { book: "same-isbn".into(), info: info(1), restore: false }), e( "9", "a", 7, Op::BookSet { book: "b".into(), field: Field::Isbn13, value: json!("9780441172719"), base: None }, ), ]; let lib = Library::from_events(&evs); assert!(lib.books["b"].review.conflict.is_some()); assert!(lib.highlights["h"].note.conflict.is_some()); assert_eq!(lib.books.len(), 2, "two books share an ISBN after an edit; the export keeps both"); assert_round_trip(&lib); } #[test] fn the_zip_holds_json_csv_markdown_and_files() { let mut all = events(42, "phone", 200, 0); all.push(Event { id: "file".into(), hlc: liber_library::Hlc { wall: u64::MAX / 2, logical: 0 }, device: "phone".into(), op: Op::BookFileAdd { book: "b-valka".into(), file: liber_library::library::BookFile { sha256: "ab".repeat(32), format: "epub".into(), size: 5, name: "v.epub".into(), }, }, }); let lib = Library::from_events(&all); let bytes = write_zip(&lib, Vec::new(), "2026-11-17T10:00:00Z", &mut |_, f| { (f.sha256 == "ab".repeat(32)).then(|| Box::new(&b"EPUB!"[..]) as Box) }) .unwrap(); let z = liber_format::zip::Zip::parse(&bytes, &liber_format::Limits::default()).unwrap(); let names: Vec<&str> = z.entries().iter().map(|e| e.name.as_str()).collect(); assert!(names.contains(&"library.json") && names.contains(&"books.csv")); if !lib.highlights.is_empty() { assert!(names.iter().any(|n| n.starts_with("highlights/") && n.ends_with(".md"))); } let file = names.iter().find(|n| n.starts_with("files/")).expect("the book file"); assert_eq!(z.read_name(file).unwrap().unwrap(), b"EPUB!"); let json = String::from_utf8(z.read_name("library.json").unwrap().unwrap()).unwrap(); assert_eq!(from_json(&json).unwrap(), lib); } #[test] fn a_foreign_or_newer_export_is_refused() { assert!(from_json("{}").is_err()); assert!(from_json(r#"{"format":"liber-export","version":2,"exported_at":"x","library":{"books":{},"highlights":{},"sessions":{},"goals":{}}}"#).is_err()); assert!(from_json(r#"{"format":"liber-export","version":1,"exported_at":"x","library":{"books":{},"highlights":{},"sessions":{},"goals":{}},"extra":1}"#).is_err()); }