//! The fixtures the personal object's import routes are tested against. //! //! The Rust structs are the single source of the shapes: these tests build one //! synthetic body per route, serialise it exactly as the Mac's importer sends //! it, and compare it with `fixtures/import/.json`. Run them with //! `COTTON_WRITE_FIXTURES=1` to rewrite the files after a shape changes. //! Nothing real goes in here: every name, number and id is made up. use std::collections::BTreeMap; use std::path::PathBuf; use cotton_core::import::envelope::{ Direction, EnvelopeMedia, EnvelopeMessage, EnvelopeReaction, EnvelopeThread, EnvelopeThreadParticipant, ImportBatch, }; use serde::Serialize; use serde_json::{json, Map, Value}; use super::sources::photos::apple::{AppleAssetRow, AppleFaceRow, ApplePersonRow, PlaceReading}; use super::sources::photos::{PhotoAsset, PhotoCluster, PhotoWork}; use super::DeviceCall; const DATABASE: &str = "00000000-0000-4000-8000-000000000001"; fn meta(value: Value) -> Map { match value { Value::Object(map) => map, _ => unreachable!("meta is an object"), } } pub(crate) fn messages_fixture() -> ImportBatch { let participant = |label: &str, phone: &str| EnvelopeThreadParticipant { label: label.to_string(), phone: Some(phone.to_string()), email: None, person_id: None, handle: None, }; ImportBatch { workspace_id: DATABASE.to_string(), channel: "whatsapp".to_string(), thread: Some(EnvelopeThread { kind: "group".to_string(), title: "Hill walk".to_string(), external_key: "wa:group:120363000000000001@g.us".to_string(), participants: vec![ participant("Asha Example", "919800000001"), participant("Ravi Example", "919800000002"), ], }), dm: None, messages: vec![ EnvelopeMessage { phone: None, email: None, sender_label: Some("Asha Example".to_string()), display_name: None, direction: Direction::In, body: "Leaving at six?".to_string(), media: None, sent_at: "2026-10-20T00:30:00Z".to_string(), external_id: "3EB0A1B2C3D4E5F60001".to_string(), reply_to_external_id: None, reactions: Some(vec![EnvelopeReaction { emoji: "👍".to_string(), actor: "919800000002".to_string(), at: Some("2026-10-20T00:31:00Z".to_string()), }]), meta: None, }, EnvelopeMessage { phone: None, email: None, sender_label: None, display_name: None, direction: Direction::Out, body: "Yes, the trailhead.".to_string(), media: Some(vec![EnvelopeMedia { key: format!( "{DATABASE}/messages/6f1c3c52-5b0e-5d47-9a43-2a1f0f6d2b11/\ 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.jpg" ), content_type: "image/jpeg".to_string(), name: Some("trailhead.jpg".to_string()), size_bytes: Some(48213.0), width: Some(1600.0), height: Some(1200.0), contact: None, duration_ms: None, playback_key: None, playback_content_type: None, file_key: None, kind: None, waveform: None, preview: None, }]), sent_at: "2026-10-20T00:32:00Z".to_string(), external_id: "3EB0A1B2C3D4E5F60002".to_string(), reply_to_external_id: Some("3EB0A1B2C3D4E5F60001".to_string()), reactions: None, meta: Some(meta(json!({ "attachment": true }))), }, ], new_thread_id: None, } } #[derive(Serialize)] struct CallsBody { calls: Vec, } pub(crate) fn calls_fixture() -> Vec { vec![ DeviceCall { source: "callHistory".to_string(), external_id: "5C1A0E2B-0000-4000-8000-00000000C001".to_string(), provider: "phone".to_string(), direction: "in".to_string(), outcome: "answered".to_string(), started_at: "2026-10-21T04:15:00Z".to_string(), duration_s: 312, handle: Some("+919800000001".to_string()), handle_kind: "phone".to_string(), name: Some("Asha Example".to_string()), device: Some("mini".to_string()), meta: Map::new(), }, DeviceCall { source: "callHistory".to_string(), external_id: "5C1A0E2B-0000-4000-8000-00000000C002".to_string(), provider: "facetime".to_string(), direction: "out".to_string(), outcome: "missed".to_string(), started_at: "2026-10-21T09:02:10Z".to_string(), duration_s: 0, handle: Some("ravi@example.com".to_string()), handle_kind: "email".to_string(), name: None, device: Some("mini".to_string()), meta: meta(json!({ "video": true })), }, ] } pub(crate) fn photos_fixture() -> PhotoWork { let person = ApplePersonRow { pk: 7, uuid: "A7C1D2E3-0000-4000-8000-0000000000A7".to_string(), full_name: Some("Asha Example".to_string()), display_name: Some("Asha".to_string()), face_count: Some(12), merge_target_pk: None, }; let asset = PhotoAsset { row: AppleAssetRow { pk: 101, uuid: "B0B0B0B0-0000-4000-8000-000000000101".to_string(), kind: 0, kind_subtype: Some(0), uti: Some("public.heic".to_string()), directory: Some("B".to_string()), filename: Some("B0B0B0B0-0000-4000-8000-000000000101.heic".to_string()), saved_asset_type: Some(6), date_created: Some(782_000_000.5), timezone_offset: Some(19_800), latitude: Some(30.4532), longitude: Some(78.0712), width: Some(4032), height: Some(3024), orientation: Some(1), favorite: Some(1), 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: Some("Apple".to_string()), camera_model: Some("iPhone 17 Pro".to_string()), original_filename: Some("IMG_0101.HEIC".to_string()), overall_aesthetic_score: Some(0.81), curation_score: Some(0.6), scene_analysis_version: Some(12), title: None, long_description: None, }, place: PlaceReading { name: Some("Trailhead".to_string()), city: Some("Mussoorie".to_string()), region: Some("Uttarakhand".to_string()), country: Some("India".to_string()), labels: vec!["Mountain".to_string(), "Sky".to_string()], }, faces: vec![AppleFaceRow { uuid: "F0F0F0F0-0000-4000-8000-000000000F01".to_string(), asset_pk: 101, center_x: 0.42, center_y: 0.61, size: 0.08, quality: Some(0.73), person_pk: Some(person.pk), person_uuid: Some(person.uuid.clone()), is_in_trash: Some(0), hidden: Some(0), }], sha256: Some("2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae".to_string()), }; let mut clusters = BTreeMap::new(); clusters.insert( person.uuid.clone(), PhotoCluster { person, contact_identifier: Some("C0C0C0C0-0000-4000-8000-0000000000C7:ABPerson".to_string()), }, ); PhotoWork { library_path: PathBuf::from("/Users/example/Pictures/Photos Library.photoslibrary"), assets: vec![asset], clusters, } } fn check(name: &str, body: &impl Serialize) { let path = cotton_store::manifest_dir() .join("fixtures/import") .join(format!("{name}.json")); let mut text = serde_json::to_string_pretty(body).expect("serialises"); text.push('\n'); if std::env::var_os("COTTON_WRITE_FIXTURES").is_some() { std::fs::create_dir_all(path.parent().expect("parent")).expect("fixture dir"); std::fs::write(&path, &text).expect("writes the fixture"); return; } let held = std::fs::read_to_string(&path) .unwrap_or_else(|e| panic!("{}: {e} — run with COTTON_WRITE_FIXTURES=1", path.display())); assert_eq!( held, text, "{name}.json no longer matches the Rust shape — run with COTTON_WRITE_FIXTURES=1" ); } #[test] fn messages_fixture_is_the_import_batch_shape() { check("messages", &messages_fixture()); } #[test] fn calls_fixture_is_the_device_call_shape() { check("calls", &CallsBody { calls: calls_fixture() }); } #[test] fn photos_fixture_is_the_photo_work_shape() { check("photos", &photos_fixture()); } #[test] fn the_fixtures_read_back_into_the_structs() { let batch: ImportBatch = serde_json::from_value(serde_json::to_value(messages_fixture()).unwrap()).unwrap(); assert_eq!(batch, messages_fixture()); let work: PhotoWork = serde_json::from_value(serde_json::to_value(photos_fixture()).unwrap()).unwrap(); assert_eq!(work, photos_fixture()); let calls: Vec = serde_json::from_value(serde_json::to_value(calls_fixture()).unwrap()).unwrap(); assert_eq!(calls, calls_fixture()); }