343 lines · 13.2 KB · RustRaw
//! The port of `apps/cotton/__tests__/photosLibrary.test.ts` — the library//! on disk: presence, the copy, the band, and the reader's queries, over a//! SYNTHETIC library with the real column names.//!//! Nothing here reads a real Photos library, and nothing here ever will.use super::*;use crate::ingest::sources::photos::fixture::{ plist_xml, uuid_of, PlistValue, SyntheticAsset, SyntheticFace, SyntheticIndexRow, SyntheticLibrary, SyntheticPerson,};fn scratch() -> tempfile::TempDir { tempfile::tempdir().expect("a scratch directory")}/// The copy directory a run would use — `<scratch>/copy`, standing in for the/// client's snapshot directory.fn copy_of(library: &std::path::Path, scratch: &std::path::Path) -> Result<LibraryCopy, String> { copy_library_database(library, &scratch.join("copy"))}// ------------------------------------------------------------------- presence#[test]fn finds_a_library_and_says_when_there_is_none() { let scratch = scratch(); let dir = SyntheticLibrary::at(scratch.path().join("lib")).write(); assert_eq!(library_presence(&dir), LibraryPresence::Found); assert_eq!( library_presence(&scratch.path().join("nowhere")), LibraryPresence::Missing );}#[test]fn a_folder_with_no_database_inside_it_is_missing_not_found() { // A `.photoslibrary` that is there and holds nothing this reader can use is // not a library — and saying "found" would send the run at a file that does // not exist. let scratch = scratch(); let empty = scratch.path().join("empty.photoslibrary"); std::fs::create_dir_all(&empty).expect("the folder"); assert_eq!(library_presence(&empty), LibraryPresence::Missing);}// --------------------------------------------------------------------- plists#[test]fn passes_an_xml_plist_through_without_plutil_bom_and_leading_whitespace_included() { let scratch = scratch(); let xml = plist_xml(&[("PLModelVersion", PlistValue::Integer(19_607))]); assert!(as_xml_plist(xml.as_bytes()).is_some()); assert_eq!( plist_to_xml(xml.as_bytes(), scratch.path()).expect("it reads"), xml ); let with_bom = format!("\u{feff}\n {xml}"); assert!(plist_to_xml(with_bom.as_bytes(), scratch.path()) .expect("it reads") .contains("<key>PLModelVersion</key>")); // A real library writes BINARY plists, and those take the `plutil` path. assert!(as_xml_plist(b"bplist00").is_none());}#[cfg(target_os = "macos")]#[test]fn converts_a_binary_plist_through_plutil_on_a_mac() { use std::io::Write; let scratch = scratch(); let xml = plist_xml(&[("PLModelVersion", PlistValue::Integer(19_607))]); let source = scratch.path().join("in.plist"); std::fs::write(&source, &xml).expect("the xml plist"); let child = std::process::Command::new("plutil") .args(["-convert", "binary1", "-o", "-"]) .arg(&source) .stdout(std::process::Stdio::piped()) .stdin(std::process::Stdio::null()) .spawn() .expect("plutil is on a Mac"); let out = child.wait_with_output().expect("plutil answers"); let _ = std::io::stdout().flush(); assert!(out.status.success()); assert!(as_xml_plist(&out.stdout).is_none(), "it really is binary"); assert!(plist_to_xml(&out.stdout, scratch.path()) .expect("plutil reads it") .contains("<integer>19607</integer>"));}// ----------------------------------------------------------------- the copy#[test]fn copies_the_database_out_reads_the_version_pair_and_removes_the_copy_on_discard() { let scratch = scratch(); let dir = SyntheticLibrary::at(scratch.path().join("lib")).write(); let copy = copy_of(&dir, scratch.path()).expect("a copy"); assert_eq!(copy.model_version, 19_607); assert_eq!(copy.library_schema_version, 5001); assert_eq!(copy.photos, "Photos 11.1"); assert!(copy.photos_db.exists()); assert!(copy.psi_db.as_ref().expect("an index").exists()); copy.discard(); assert!(!copy.dir.exists());}#[test]fn refuses_a_band_it_was_not_tested_on_and_leaves_no_copy_behind() { let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); library.library_schema_version = 5002; let dir = library.write(); let refusal = copy_of(&dir, scratch.path()).expect_err("a band nobody tested"); assert!(refusal.contains("library schema 5002")); assert!(refusal.contains("Nothing was imported")); assert!( !scratch.path().join("copy").exists(), "a refused library leaves no copy on the disk" );}#[test]fn has_no_index_handle_when_the_library_has_no_search_index() { // A Mac that has never run analysis. Every asset then imports with no place // and no labels at all, which is a state rather than a missing feature. let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); library.index = None; let dir = library.write(); let copy = copy_of(&dir, scratch.path()).expect("a copy"); assert_eq!(copy.psi_db, None); let reader = LibraryReader::open(©).expect("a reader"); assert_eq!( reader.place_for(&uuid_of(1)).expect("a place"), PlaceReading::default() ); copy.discard();}#[test]fn a_library_that_does_not_say_which_photos_wrote_it_is_refused() { let scratch = scratch(); let dir = SyntheticLibrary::at(scratch.path().join("lib")).write(); // The metadata row goes, which is what an unreadable or absent plist looks // like from here. let db = crate::test_support::open_fixture_db(&dir.join("database").join("Photos.sqlite")); db.execute("DELETE FROM Z_METADATA", []) .expect("the delete"); drop(db); let refusal = copy_of(&dir, scratch.path()).expect_err("no version"); assert!(refusal.contains("does not say which version of Photos wrote it")); assert!(refusal.contains("Nothing was imported"));}// ---------------------------------------------------------------- the reader#[test]fn walks_assets_in_key_order_skipping_the_bin_and_hidden_ones_with_attributes_joined() { let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); let mut third = SyntheticAsset::photo(3); third.camera_make = Some("Maker".to_string()); third.camera_model = Some("Model".to_string()); third.title = Some("Title".to_string()); third.long_description = Some("Words".to_string()); let mut binned = SyntheticAsset::photo(1); binned.trashed = 1; let mut hidden = SyntheticAsset::photo(2); hidden.hidden = 1; library.assets = vec![third, binned, hidden, SyntheticAsset::photo(4)]; let dir = library.write(); let copy = copy_of(&dir, scratch.path()).expect("a copy"); let reader = LibraryReader::open(©).expect("a reader"); // The bin and the hidden album are the person's own answer about what they // want kept, and it is followed. assert_eq!(reader.count_assets().expect("a count"), 2); let first = reader.assets_after(0, 1).expect("a page"); assert_eq!(first.len(), 1); assert_eq!(first[0].pk, 3); assert_eq!(first[0].uuid, uuid_of(3)); assert_eq!(first[0].camera_make.as_deref(), Some("Maker")); assert_eq!(first[0].camera_model.as_deref(), Some("Model")); assert_eq!(first[0].title.as_deref(), Some("Title")); assert_eq!(first[0].long_description.as_deref(), Some("Words")); assert_eq!(first[0].timezone_offset, Some(19_800)); let next: Vec<i64> = reader .assets_after(3, 10) .expect("a page") .iter() .map(|row| row.pk) .collect(); assert_eq!(next, vec![4]); assert!(reader.assets_after(4, 10).expect("a page").is_empty()); copy.discard();}#[test]fn reads_faces_per_asset_people_with_merges_followed_and_the_indexs_reading_of_an_asset() { let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = vec![SyntheticAsset::photo(1)]; library.persons = vec![ SyntheticPerson { pk: 10, uuid: "P-A".to_string(), full_name: Some("Person A".to_string()), contact_identifier: Some("ABCDEF12-3456-7890-ABCD-EF1234567890".to_string()), ..SyntheticPerson::default() }, SyntheticPerson { pk: 11, uuid: "P-B".to_string(), merge_target_pk: Some(10), ..SyntheticPerson::default() }, SyntheticPerson { pk: 12, uuid: "P-C".to_string(), merge_target_pk: Some(11), ..SyntheticPerson::default() }, ]; let mut face = SyntheticFace::at("F1", 1); face.center_y = 0.5; face.person_pk = Some(12); library.faces = vec![face]; library.index = Some(vec![ SyntheticIndexRow { uuid: uuid_of(1), category: 5, content: "Townsville".to_string(), }, SyntheticIndexRow { uuid: uuid_of(1), category: 1500, content: "Beach".to_string(), }, ]); let dir = library.write(); let copy = copy_of(&dir, scratch.path()).expect("a copy"); let reader = LibraryReader::open(©).expect("a reader"); let faces = reader.faces_for(1).expect("the faces"); assert_eq!(faces.len(), 1); assert_eq!(faces[0].uuid, "F1"); assert_eq!(faces[0].person_pk, Some(12)); assert_eq!(faces[0].person_uuid.as_deref(), Some("P-C")); assert!((faces[0].size - 0.1).abs() < 1e-10); // A merge chain followed all the way: Photos points one cluster at another // rather than rewriting the faces, so a face still names the cluster it was // found in — and following the chain is what stops one person becoming two. let people = reader.people().expect("the people"); assert_eq!(people.get(&12).map(|row| row.uuid.as_str()), Some("P-A")); assert_eq!(people.get(&11).map(|row| row.uuid.as_str()), Some("P-A")); assert_eq!( people.get(&10).and_then(|row| row.full_name.as_deref()), Some("Person A") ); let archive = reader .contact_archive(10) .expect("it reads") .expect("an archive"); assert!(String::from_utf8_lossy(&archive).contains("carddav-uuids")); assert_eq!(reader.contact_archive(11).expect("it reads"), None); let place = reader.place_for(&uuid_of(1)).expect("a place"); assert_eq!(place.city.as_deref(), Some("Townsville")); assert_eq!(place.labels, vec!["Beach"]); // An asset the index has not reached is not an error — it is most of a // library on a Mac that has only just started analysing. assert_eq!( reader.place_for(&uuid_of(2)).expect("a place"), PlaceReading::default() ); copy.discard();}#[test]fn a_merge_cycle_in_apples_own_table_ends_rather_than_looping_for_ever() { let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); library.persons = vec![ SyntheticPerson { pk: 20, uuid: "P-X".to_string(), merge_target_pk: Some(21), ..SyntheticPerson::default() }, SyntheticPerson { pk: 21, uuid: "P-Y".to_string(), merge_target_pk: Some(20), ..SyntheticPerson::default() }, ]; let dir = library.write(); let copy = copy_of(&dir, scratch.path()).expect("a copy"); let reader = LibraryReader::open(©).expect("a reader"); // What it lands on matters less than that it lands: a cycle inside an // import is a Mac that never finishes. let people = reader.people().expect("the people"); assert_eq!(people.len(), 2); copy.discard();}// -------------------------------------------------------------------- measure// RULE: a band is admitted from a measurement, never a blanket allow: the// measure reads the version pair of a library the band table refuses, says// whether each of the reader's statements prepares on it, and leaves no copy.#[test]fn measures_a_library_outside_every_band_without_admitting_it() { let scratch = scratch(); let mut library = SyntheticLibrary::at(scratch.path().join("lib")); library.model_version = 270_010_300; library.library_schema_version = 6000; let dir = library.write(); let measured = measure_library(&dir, &scratch.path().join("measure")).expect("it measures"); assert_eq!((measured.model_version, measured.library_schema_version), (270_010_300, 6000)); assert!(measured.admitted.is_err()); assert!(measured.statements.iter().all(|(_, prepared)| prepared.is_ok()), "{:?}", measured.statements); assert_eq!(measured.statements.len(), 6); assert_eq!(measured.assets, Some(0)); assert!(!scratch.path().join("measure").exists()); // A column the reader selects that the library lacks is named. let db = rusqlite::Connection::open(dir.join("database/Photos.sqlite")).unwrap(); db.execute_batch("ALTER TABLE ZPERSON DROP COLUMN ZFACECOUNT").unwrap(); drop(db); let measured = measure_library(&dir, &scratch.path().join("measure")).expect("it measures"); let people = measured.statements.iter().find(|(name, _)| *name == "people").unwrap(); assert!(people.1.as_ref().unwrap_err().contains("ZFACECOUNT"), "{:?}", people.1);}