//! The port of `apps/cotton/__tests__/photosApple.test.ts` — the pure half
//! of the importer, asserted without a database.
//!
//! Every uuid, name and place here is invented. Nothing real ever goes in this
//! tree, and no test in it reads a Photos library.
use super::*;
fn asset() -> AppleAssetRow {
AppleAssetRow {
pk: 1,
uuid: "00000000-0000-4000-8000-000000000001".to_string(),
kind: 0,
kind_subtype: Some(0),
uti: Some("public.heic".to_string()),
directory: Some("A".to_string()),
filename: Some("00000000-0000-4000-8000-000000000001.heic".to_string()),
saved_asset_type: Some(6),
date_created: Some(0.0),
timezone_offset: Some(19_800),
latitude: Some(-180.0),
longitude: Some(-180.0),
width: Some(4032),
height: Some(3024),
orientation: Some(1),
favorite: Some(0),
hdr_type: Some(0),
depth_type: Some(0),
adjustments_state: Some(0),
avalanche_uuid: None,
is_detected_screenshot: Some(0),
camera_capture_device: Some(0),
camera_make: None,
camera_model: None,
original_filename: None,
overall_aesthetic_score: Some(0.5),
curation_score: Some(0.5),
scene_analysis_version: Some(0),
title: None,
long_description: None,
}
}
fn plist_xml(key: &str, value: i64) -> String {
format!("{key}{value}")
}
/// The shape `ZCONTACTMATCHINGDICTIONARY` has once it is XML: a keyed archive
/// whose `$objects` array holds the key names and then their values.
fn contact_archive_xml(identifier: &str) -> String {
format!(
"$objects\
$null\
first-nameSomeone\
carddav-uuids{identifier}:ABPerson\
"
)
}
// ------------------------------------------------------------------ the band
#[test]
fn accepts_the_tested_band_and_refuses_every_other_with_the_numbers_in_the_sentence() {
assert_eq!(check_band(19_607, 5001), Ok("Photos 11.1"));
let refused = check_band(20_104, 5002).expect_err("a band nobody tested");
assert!(refused.contains("20104"));
assert!(refused.contains("5002"));
assert!(refused.contains("Nothing was imported"));
// The schema has to match EXACTLY: a model version inside the range with
// the wrong schema is still a library this reader has not seen.
assert!(check_band(19_607, 5000).is_err());
}
#[test]
fn admits_the_measured_macos_27_pair_and_refuses_its_neighbours() {
assert_eq!(check_band(270_010_300, 5001), Ok("Photos on macOS 27.0"));
assert!(check_band(270_010_299, 5001).is_err());
assert!(check_band(270_010_301, 5001).is_err());
assert!(check_band(270_010_300, 5002).is_err());
assert!(check_band(270_010_300, 5000).is_err());
}
#[test]
fn reads_both_versions_out_of_plist_xml() {
assert_eq!(
model_version_from_plist_xml(&plist_xml("PLModelVersion", 19_607)),
Some(19_607)
);
assert_eq!(
library_schema_version_from_plist_xml(&plist_xml("LibrarySchemaVersion", 5001)),
Some(5001)
);
assert_eq!(model_version_from_plist_xml(""), None);
// Whitespace between the tags is how a real plist is written.
assert_eq!(
model_version_from_plist_xml("PLModelVersion\n\t19607"),
Some(19_607)
);
}
// ------------------------------------------------------------- time and place
#[test]
fn converts_core_data_seconds_from_the_2001_epoch() {
assert_eq!(
apple_date_to_iso(Some(0.0)).as_deref(),
Some("2001-01-01T00:00:00.000Z")
);
let august = chrono::DateTime::parse_from_rfc3339("2026-08-14T09:00:00Z")
.expect("a literal instant")
.timestamp_millis();
let seconds = (august - APPLE_EPOCH_MS) as f64 / 1000.0;
assert_eq!(
apple_date_to_iso(Some(seconds)).as_deref(),
Some("2026-08-14T09:00:00.000Z")
);
assert_eq!(apple_date_to_iso(None), None);
assert_eq!(apple_date_to_iso(Some(f64::NAN)), None);
}
#[test]
fn reads_minus_180_as_no_location_and_only_writes_a_pair() {
assert_eq!(coordinate_of(Some(-180.0)), None);
assert_eq!(coordinate_of(Some(12.97)), Some(12.97));
let mut half = asset();
half.latitude = Some(12.97);
half.longitude = Some(-180.0);
let read = enrichment_of(&half, &PlaceReading::default());
assert_eq!(
read.latitude, None,
"half a coordinate is a point in the sea"
);
assert_eq!(read.longitude, None);
let mut both = asset();
both.latitude = Some(12.97);
both.longitude = Some(77.59);
let read = enrichment_of(&both, &PlaceReading::default());
assert_eq!(read.latitude, Some(12.97));
assert_eq!(read.longitude, Some(77.59));
}
#[test]
fn folds_the_search_indexs_categories_into_a_place_first_value_wins() {
let place = place_from_index_rows(&[
(1500, "Beach".to_string()),
(5, "Townsville".to_string()),
(5, "Other".to_string()),
(12, "Country".to_string()),
(1500, "Sunset".to_string()),
(999, "ignored".to_string()),
(5, " ".to_string()),
]);
assert_eq!(place.city.as_deref(), Some("Townsville"));
assert_eq!(place.country.as_deref(), Some("Country"));
assert_eq!(place.labels, vec!["Beach", "Sunset"]);
assert_eq!(place.name, None);
assert_eq!(place.region, None);
}
#[test]
fn splits_a_uuid_into_the_indexs_two_signed_halves() {
let (first, second) = psi_uuid_halves("FFFFFFFF-FFFF-FFFF-8000-000000000001").expect("a uuid");
assert_eq!(first, -1);
assert_eq!(second, i64::MIN + 1);
assert_eq!(psi_uuid_halves("nope"), None);
}
// -------------------------------------------------------- formats and subtypes
#[test]
fn maps_the_measured_type_identifiers_and_refuses_the_rest() {
let heic = format_for(Some("public.heic")).expect("a format");
assert_eq!(heic.content_type, "image/heic");
assert_eq!(heic.ext, "heic");
assert_eq!(heic.kind, MediaKind::Photo);
let movie = format_for(Some("com.apple.quicktime-movie")).expect("a format");
assert_eq!(movie.content_type, "video/quicktime");
assert_eq!(movie.ext, "mov");
assert_eq!(movie.kind, MediaKind::Video);
// The two the measured library holds that the media limits do not admit —
// counted by type, never guessed at.
assert_eq!(format_for(Some("public.tiff")), None);
assert_eq!(format_for(Some("public.jpeg-2000")), None);
assert_eq!(format_for(None), None);
}
#[test]
fn reads_every_subtype_flag_and_counts_an_unknown_code_rather_than_guessing() {
let mut row = asset();
row.kind_subtype = Some(10);
assert_eq!(subtypes_of(&row).subtypes, vec!["screenshot"]);
let mut row = asset();
row.is_detected_screenshot = Some(1);
assert_eq!(subtypes_of(&row).subtypes, vec!["screenshot"]);
let mut row = asset();
row.kind_subtype = Some(2);
row.camera_capture_device = Some(1);
row.avalanche_uuid = Some("burst".to_string());
row.hdr_type = Some(3);
row.depth_type = Some(1);
row.adjustments_state = Some(1);
assert_eq!(
subtypes_of(&row).subtypes,
vec!["live", "selfie", "burst", "hdr", "portrait", "edited"]
);
let mut video = asset();
video.kind = 1;
video.kind_subtype = Some(100);
let read = subtypes_of(&video);
assert!(read.subtypes.is_empty());
assert_eq!(read.unknown_subtype, Some(100));
let mut row = asset();
row.kind_subtype = Some(103);
assert_eq!(subtypes_of(&row).subtypes, vec!["screen_recording"]);
}
#[test]
fn a_flag_named_twice_is_written_once() {
// `ZKINDSUBTYPE = 10` and `ZISDETECTEDSCREENSHOT = 1` are the same fact
// twice, and a column holding it twice would fail the members check.
let mut row = asset();
row.kind_subtype = Some(10);
row.is_detected_screenshot = Some(1);
assert_eq!(subtypes_of(&row).subtypes, vec!["screenshot"]);
}
#[test]
fn every_subtype_this_reader_writes_is_one_the_column_admits() {
let mut row = asset();
row.kind_subtype = Some(1);
row.is_detected_screenshot = Some(1);
row.camera_capture_device = Some(1);
row.avalanche_uuid = Some("burst".to_string());
row.hdr_type = Some(1);
row.depth_type = Some(1);
row.adjustments_state = Some(1);
for name in subtypes_of(&row).subtypes {
assert!(
PHOTO_SUBTYPES.contains(&name.as_str()),
"{name} is not a subtype the column admits"
);
}
for code in [1, 2, 10, 101, 102, 103] {
let named = named_subtype(code).expect("a named code");
assert!(PHOTO_SUBTYPES.contains(&named), "{named} is not admitted");
}
}
// ------------------------------------------------------------------ enrichment
#[test]
fn carries_offset_camera_orientation_favourite_and_the_filename() {
let mut row = asset();
row.camera_make = Some("Maker".to_string());
row.camera_model = Some("Model 1".to_string());
row.favorite = Some(1);
row.original_filename = Some("IMG_0001.HEIC".to_string());
row.orientation = Some(6);
let read = enrichment_of(&row, &PlaceReading::default());
assert_eq!(read.taken_offset, Some(19_800));
assert_eq!(read.camera_make.as_deref(), Some("Maker"));
assert_eq!(read.camera_model.as_deref(), Some("Model 1"));
assert!(read.favourite);
assert_eq!(read.original_filename.as_deref(), Some("IMG_0001.HEIC"));
assert_eq!(read.orientation, Some(6));
assert!(read.labels.is_empty());
// EXIF is 1…8, and a number outside it is not an orientation.
let mut row = asset();
row.orientation = Some(9);
assert_eq!(
enrichment_of(&row, &PlaceReading::default()).orientation,
None
);
}
#[test]
fn writes_apples_scores_only_when_analysis_ran_on_this_mac() {
// 0.5 is what an unanalysed row carries EVERYWHERE, and a placeholder is
// not a score.
let read = enrichment_of(&asset(), &PlaceReading::default());
assert_eq!(read.aesthetic_score, None);
assert_eq!(read.curation_score, None);
let mut row = asset();
row.overall_aesthetic_score = Some(0.71);
row.curation_score = Some(0.2);
row.scene_analysis_version = Some(3);
let read = enrichment_of(&row, &PlaceReading::default());
assert_eq!(read.aesthetic_score, Some(0.71));
assert_eq!(read.curation_score, Some(0.2));
}
#[test]
fn a_score_outside_the_columns_range_is_clamped_rather_than_refused() {
let mut row = asset();
row.overall_aesthetic_score = Some(1.4);
row.curation_score = Some(-3.0);
row.scene_analysis_version = Some(3);
let read = enrichment_of(&row, &PlaceReading::default());
assert_eq!(read.aesthetic_score, Some(1.0));
assert_eq!(read.curation_score, Some(-1.0));
}
#[test]
fn takes_the_place_and_labels_the_index_found() {
let place = PlaceReading {
name: Some("Somewhere".to_string()),
city: Some("Townsville".to_string()),
labels: vec!["beach".to_string()],
..PlaceReading::default()
};
let read = enrichment_of(&asset(), &place);
assert_eq!(read.place_name.as_deref(), Some("Somewhere"));
assert_eq!(read.place_city.as_deref(), Some("Townsville"));
assert_eq!(read.labels, vec!["beach"]);
}
#[test]
fn makes_a_caption_of_the_title_and_description_the_person_typed() {
assert_eq!(caption_of(&asset()), None);
let mut row = asset();
row.title = Some(" Trip ".to_string());
assert_eq!(caption_of(&row).as_deref(), Some("Trip"));
row.long_description = Some("Day one".to_string());
assert_eq!(caption_of(&row).as_deref(), Some("Trip — Day one"));
// Whitespace alone is not a caption.
let mut blank = asset();
blank.title = Some(" ".to_string());
assert_eq!(caption_of(&blank), None);
}
// ------------------------------------------------------- where the original is
#[test]
fn resolves_the_three_directory_shapes_and_names_an_unknown_one() {
assert_eq!(
original_path_of(Some("A"), Some("x.heic"), Some(6)),
OriginalPath::Library("originals/A/x.heic".to_string())
);
assert_eq!(
original_path_of(Some("scope-1/uuid-1"), Some("x.heic"), Some(4)),
OriginalPath::Shared("scopes/cloudsharing/data/scope-1/uuid-1/x.heic".to_string())
);
assert_eq!(
original_path_of(Some("/Volumes/Disk/Pictures"), Some("x.jpg"), Some(10)),
OriginalPath::Referenced("/Volumes/Disk/Pictures/x.jpg".to_string())
);
assert!(matches!(
original_path_of(Some("??"), Some("x.jpg"), Some(6)),
OriginalPath::Unknown(_)
));
assert!(matches!(
original_path_of(Some("A"), None, Some(6)),
OriginalPath::Unknown(_)
));
// A referenced asset whose directory is not absolute names nothing this
// Mac can open, and saying so beats building a path out of a fragment.
assert!(matches!(
original_path_of(Some("Pictures"), Some("x.jpg"), Some(10)),
OriginalPath::Unknown(_)
));
}
// ------------------------------------------------------------------ faces
#[test]
fn flips_apples_bottom_left_centre_and_doubles_the_half_side_into_a_top_left_box() {
// Centre (0.5, 0.6) from the bottom, half-side 0.1 → top-left (0.4, 0.3),
// 0.2 square.
let found = face_box_of(0.5, 0.6, 0.1).expect("a box");
assert!((found.x - 0.4).abs() < 1e-10);
assert!((found.y - 0.3).abs() < 1e-10);
assert!((found.w - 0.2).abs() < 1e-10);
assert!((found.h - 0.2).abs() < 1e-10);
}
#[test]
fn clamps_to_the_unit_square_and_refuses_a_box_with_no_area() {
let edge = face_box_of(0.05, 0.05, 0.1).expect("a box");
assert_eq!(edge.x, 0.0);
assert!((edge.y - 0.85).abs() < 1e-10);
assert!((edge.w - 0.15).abs() < 1e-10);
assert!((edge.h - 0.15).abs() < 1e-10);
assert_eq!(face_box_of(0.5, 0.5, 0.0), None);
assert_eq!(face_box_of(f64::NAN, 0.5, 0.1), None);
assert_eq!(face_box_of(0.5, f64::NAN, 0.1), None);
// Wholly outside: the clamp leaves nothing, and nothing is not a face.
assert_eq!(face_box_of(-5.0, 0.5, 0.1), None);
}
#[test]
fn reads_minus_one_as_an_unmeasured_quality() {
assert_eq!(face_quality_of(Some(-1.0)), None);
assert_eq!(face_quality_of(Some(0.51)), Some(0.51));
assert_eq!(face_quality_of(None), None);
// The column is 0…1, and a reading above it is clamped rather than refused.
assert_eq!(face_quality_of(Some(1.4)), Some(1.0));
}
// ------------------------------------------------------------------ people
#[test]
fn names_a_cluster_from_its_full_name_then_its_display_name_else_nothing() {
assert_eq!(
person_name_of(Some(" A Person "), None).as_deref(),
Some("A Person")
);
assert_eq!(
person_name_of(Some(""), Some("Nick")).as_deref(),
Some("Nick")
);
assert_eq!(person_name_of(None, Some(" ")), None);
assert_eq!(person_name_of(None, None), None);
}
#[test]
fn compares_names_without_case_or_spacing() {
assert_eq!(normalise_name(" A Person "), "a person");
assert_eq!(normalise_name("a person"), normalise_name("A Person"));
}
#[test]
fn reads_the_contacts_identifier_out_of_the_keyed_archive_or_nothing() {
assert_eq!(
contact_identifier_from_plist_xml(&contact_archive_xml(
"ABCDEF12-3456-7890-ABCD-EF1234567890"
))
.as_deref(),
Some("ABCDEF12-3456-7890-ABCD-EF1234567890")
);
// The empty dictionary — 44 of the 54 clusters in the measured library.
assert_eq!(
contact_identifier_from_plist_xml("$null"),
None
);
// The key is there and names no card.
assert_eq!(
contact_identifier_from_plist_xml(
"carddav-uuids$null"
),
None
);
}