//! The reader, over a SYNTHETIC library written into a temp directory.//!//! Nothing here reads a Photos library, and nothing here ever will: every uuid//! is the fixture's own and every picture is a handful of invented bytes.//!//! What these tests are about is the READ — presence, paging, the cursor, and//! what travels with a page. What a page writes is the object's//! (`apps/business/src/import/photos.ts`).usesuper::*;usecrate::ingest::sources::photos::fixture::{SyntheticAsset, SyntheticFace, SyntheticLibrary, SyntheticPerson,};usecrate::ingest::{IngestWork, SourceState};usecrate::test_support::temp_layout;/// A source over a library written beside this test's own data directory.fn source_over(library: std::path::PathBuf) -> (tempfile::TempDir, PhotosSource) {let (dir, layout) = temp_layout();let home = dir.path().join("home");let source = PhotosSource::new(layout, &home).at_path(library); (dir, source)}/// Every batch a read emitted, and the outcome.fn read_all(source: &PhotosSource, cursor: Option<&str>) -> (Vec<IngestBatch>, ReadOutcome) {letmut batches: Vec<IngestBatch> = Vec::new();let outcome = source .read(cursor, &mut |batch| { batches.push(batch);Ok(()) }) .expect("the read finishes"); (batches, outcome)}fn pages_of(batches: &[IngestBatch]) -> Vec<&PhotoWork> { batches .iter() .map(|batch| match &batch.work {IngestWork::Photos(work) => work.as_ref(), other => panic!("the Photos source emitted {other:?}"), }) .collect()}// -------------------------------------------------------------------- probing#[test]fn a_mac_with_no_photos_library_is_not_found_and_never_an_error() {let scratch = tempfile::tempdir().expect("a scratch directory");let (_dir, source) = source_over(scratch.path().join("nowhere.photoslibrary"));let status = source.probe(); assert_eq!(status.state, SourceState::NotFound); assert!(status.detail.is_some(), "the panel needs a sentence"); assert_eq!(status.total, None);}#[test]fn a_library_probes_ready_with_its_count_and_leaves_no_copy_behind() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = (1..=3).map(SyntheticAsset::photo).collect();let path = library.write();let (_dir, source) = source_over(path);let status = source.probe(); assert_eq!(status.state, SourceState::Ready); assert_eq!(status.total, Some(3));// A whole Photos database per probe would fill the disk in a week. assert!( !source.snapshot_dir().exists(),"the copy is discarded when the probe ends" );}#[test]fn a_schema_band_this_reader_has_not_been_measured_on_is_an_error_not_a_guess() {// Guessing across a band is how one person's faces end up on another// person's pictures. The reader refuses and says so.let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.model_version = 99_999; library.assets = vec![SyntheticAsset::photo(1)];let path = library.write();let (_dir, source) = source_over(path);let status = source.probe(); assert_eq!(status.state, SourceState::Error); assert!(status.detail.expect("a reason").contains("99999")); assert!(!source.snapshot_dir().exists());}// --------------------------------------------------------------------- paging#[test]fn reads_every_asset_in_key_order_and_answers_the_last_key_as_the_cursor() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = (1..=5).map(SyntheticAsset::photo).collect();let path = library.write();let (_dir, source) = source_over(path.clone());let (batches, outcome) = read_all(&source, None);let pages = pages_of(&batches); assert_eq!(pages.len(), 1); assert_eq!(pages[0].assets.len(), 5);let keys: Vec<i64> = pages[0].assets.iter().map(|asset| asset.row.pk).collect(); assert_eq!(keys, vec![1, 2, 3, 4, 5]); assert_eq!(outcome.cursor, "5"); assert_eq!(outcome.notes.get("read:photos"), Some(&5));// The LIVE library, not the copy: the copy holds the databases and none of// the pictures. assert_eq!(pages[0].library_path, path); assert_eq!(batches[0].cursor.as_deref(), Some("5")); assert!( !source.snapshot_dir().exists(),"the copy goes with the pass" );}#[test]fn pages_a_library_larger_than_one_batch() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = (1..=ASSETS_PER_BATCH + 3) .map(SyntheticAsset::photo) .collect();let path = library.write();let (_dir, source) = source_over(path);let (batches, outcome) = read_all(&source, None);let pages = pages_of(&batches); assert_eq!(pages.len(), 2); assert_eq!(pages[0].assets.len(), ASSETS_PER_BATCH as usize); assert_eq!(pages[1].assets.len(), 3);// The cursor rides each batch, so a run killed between the two continues// rather than copying and hashing the first hundred again. assert_eq!( batches[0].cursor.as_deref(),Some(ASSETS_PER_BATCH.to_string().as_str()) ); assert_eq!(outcome.cursor, (ASSETS_PER_BATCH + 3).to_string());}#[test]fn a_cursor_reads_only_what_is_past_it_and_an_exhausted_one_emits_nothing() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = (1..=4).map(SyntheticAsset::photo).collect();let path = library.write();let (_dir, source) = source_over(path);let (batches, outcome) = read_all(&source, Some("2"));let pages = pages_of(&batches); assert_eq!( pages[0].assets.iter().map(|a| a.row.pk).collect::<Vec<_>>(), vec![3, 4] ); assert_eq!(outcome.cursor, "4");let (batches, outcome) = read_all(&source, Some("4")); assert!(batches.is_empty(), "nothing past the end");// And the cursor does not go backwards on an empty pass. assert_eq!(outcome.cursor, "4");}#[test]fn a_cursor_that_will_not_parse_is_no_cursor_at_all() {// Re-reading is free — identity is the asset's uuid — and guessing a// position is not.let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = (1..=2).map(SyntheticAsset::photo).collect();let path = library.write();let (_dir, source) = source_over(path);for bad in ["", " ", "not a number", "-4"] {let (batches, _) = read_all(&source, Some(bad));let pages = pages_of(&batches); assert_eq!( pages[0].assets.len(),2,"cursor {bad:?} read from the start" ); }}// ---------------------------------------------------------------------- faces#[test]fn a_faces_cluster_travels_with_the_page_with_apples_merge_chain_already_followed() {// Photos merges two clusters by pointing one at the other rather than by// rewriting the faces, so the face still names the cluster it was found in.// Following the chain in the reader is what stops one person becoming two.let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = vec![SyntheticAsset::photo(1)]; library.persons = vec![SyntheticPerson { pk: 10, uuid: "cluster-old".to_string(), full_name: None, display_name: None, merge_target_pk: Some(11), contact_identifier: None, },SyntheticPerson { pk: 11, uuid: "cluster-kept".to_string(), full_name: Some("Nila Rao".to_string()), display_name: None, merge_target_pk: None, contact_identifier: Some("11111111-2222-4333-8444-555555555555".to_string()), }, ]; library.faces = vec![SyntheticFace { person_pk: Some(10), ..SyntheticFace::at("face-1", 1) }];let path = library.write();let (_dir, source) = source_over(path);let (batches, _) = read_all(&source, None);let pages = pages_of(&batches); assert_eq!(pages[0].assets[0].faces.len(), 1);// ONE cluster, and it is the one the merge kept. assert_eq!(pages[0].clusters.len(), 1);let cluster = pages[0] .clusters .get("cluster-kept") .expect("the kept cluster"); assert_eq!(cluster.person.full_name.as_deref(), Some("Nila Rao")); assert_eq!( cluster.contact_identifier.as_deref(),Some("11111111-2222-4333-8444-555555555555") );// …and the face still names the cluster it was found in, which is what the// applier resolves through the map. assert_eq!( pages[0].assets[0].faces[0].person_uuid.as_deref(),Some("cluster-old") );}#[test]fn a_page_with_no_faces_asks_the_people_table_nothing() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = vec![SyntheticAsset::photo(1)]; library.persons = vec![SyntheticPerson { pk: 10, uuid: "cluster-a".to_string(), full_name: Some("Nila Rao".to_string()), display_name: None, merge_target_pk: None, contact_identifier: None, }];let path = library.write();let (_dir, source) = source_over(path);let (batches, _) = read_all(&source, None);let pages = pages_of(&batches); assert!( pages[0].clusters.is_empty(),"a page nobody is in carries nobody" );}#[test]fn a_face_naming_a_cluster_the_people_table_does_not_carry_is_counted_never_invented() {let scratch = tempfile::tempdir().expect("a scratch directory");letmut library = SyntheticLibrary::at(scratch.path().join("lib")); library.assets = vec![SyntheticAsset::photo(1)]; library.faces = vec![SyntheticFace { person_pk: Some(404), ..SyntheticFace::at("face-1", 1) }];let path = library.write();let (_dir, source) = source_over(path);let (batches, outcome) = read_all(&source, None);let pages = pages_of(&batches); assert!(pages[0].clusters.is_empty()); assert_eq!(outcome.notes.get("read:photosClusterMissing"), Some(&1));// The face still travels: it lands pointing at no one, which is true. assert_eq!(pages[0].assets[0].faces.len(), 1);}// ------------------------------------------------------------------- triggers#[test]fn wakes_on_the_librarys_own_database_directory() {let scratch = tempfile::tempdir().expect("a scratch directory");let path = scratch.path().join("lib.photoslibrary");let (_dir, source) = source_over(path.clone());let triggers = source.triggers(); assert_eq!(triggers.watch, vec![path.join("database")]);// A floor under the watch, because macOS drops one on its own. assert!(triggers.every_ms > 0);}#[test]fn is_the_fourth_source_and_keys_its_cursor_under_its_own_name() {let scratch = tempfile::tempdir().expect("a scratch directory");let (_dir, source) = source_over(scratch.path().join("lib")); assert_eq!(source.id(), SourceId::Photos); assert_eq!(SourceId::Photos.as_str(), "photos"); assert_eq!(SourceId::parse("photos"), Some(SourceId::Photos)); assert_eq!(SourceId::Photos.cursor_key("mac:finch"), "ingest:mac:finch:photos:cursor"); assert!(SourceId::ALL.contains(&SourceId::Photos));}/// Writes an invented Photos library under `$WALK_HOME/Pictures`, for a Mac app flavour walked by hand with/// `CottonHome` pointing there. Six photos whose originals are a few invented bytes.#[test]#[ignore = "writes a walk home: WALK_HOME=<dir> cargo test -p cotton-client --lib write_walk_photos_library -- --ignored"]fn write_walk_photos_library() {let home = std::path::PathBuf::from(std::env::var("WALK_HOME").expect("WALK_HOME"));let dir = home.join("Pictures").join("Photos Library.photoslibrary");let _ = std::fs::remove_dir_all(&dir);letmut library = SyntheticLibrary::at(&dir); library.assets = (1..=6) .map(|pk| SyntheticAsset { bytes: Some(format!("invented photo {pk}").into_bytes()), ..SyntheticAsset::photo(pk) }) .collect();let path = library.write(); println!("wrote 6 invented photos to {}", path.display());}