//! `people.printsNeeded` and `capture.avatar {photoId}` on a real replica: //! people are made through `write` as the app makes them, photos and faces //! are put in as the Photos pass leaves them. Every person and photo here is //! invented. use serde_json::{json, Map, Value}; use crate::fixture::WORKSPACE; use crate::shell::client::phone_tests::attached; use crate::shell::client::ClientController; fn person(client: &ClientController, name: &str) -> String { let mut fields = Map::new(); fields.insert("fullName".to_string(), json!(name)); let answer = client.write("create_person", &fields).expect("create_person"); answer .pointer("/answer/id") .and_then(Value::as_str) .unwrap_or_else(|| panic!("an id in {answer}")) .to_string() } fn sql(client: &ClientController, statement: &str, params: &[&str]) { let params: Vec = params .iter() .map(|value| rusqlite::types::Value::Text(value.to_string())) .collect(); client .with_writer(|replica| { replica .driver .run(statement, ¶ms) .map(|_| ()) .map_err(|error| error.to_string()) }) .expect(statement); } /// A photo as the Photos pass leaves it; `n` makes the sha. Answers its id. fn photo(client: &ClientController, n: u8, taken_at: &str) -> String { let sha = format!("{:064x}", n); let id = format!("photo:{sha}"); sql( client, "INSERT INTO cotton_photos (id, workspace_id, key, sha256, content_type, size_bytes, \ taken_at, orientation, origin_handle, created_at, updated_at) \ VALUES (?, ?, ?, ?, 'image/jpeg', 10, ?, 6, ?, ?, ?)", &[ &id, WORKSPACE, &format!("f/photos/{sha}.jpg"), &sha, taken_at, &format!("PH-{n}/L0/001"), taken_at, taken_at, ], ); id } /// A face on a photo. `confirmed` puts the person in `person_id`; otherwise /// they are only the suggestion. fn face(client: &ClientController, id: &str, photo: &str, who: &str, confirmed: bool, quality: &str, w: &str) { let column = if confirmed { "person_id" } else { "suggested_person_id" }; sql( client, &format!( "INSERT INTO cotton_media_faces (id, workspace_id, record_kind, record_id, source, \ source_id, x, y, w, h, quality, {column}, created_at, updated_at) \ VALUES (?, ?, 'photo', ?, 'apple', ?, 0.1, 0.2, CAST(? AS REAL), CAST(? AS REAL), \ CAST(? AS REAL), ?, '2026-09-01T00:00:00.000Z', '2026-09-01T00:00:00.000Z')" ), &[id, WORKSPACE, photo, id, w, w, quality, who], ); } fn needed(client: &ClientController) -> Vec { let answer = client .read_rows("people.printsNeeded", &mut |ctx| { super::needed(ctx.db(), ctx.workspace_id) }) .expect("the read"); answer["prints"].as_array().cloned().expect("a prints list") } fn listed(client: &ClientController, who: &str) -> Option { needed(client).into_iter().find(|print| print["personId"] == who) } fn capture(client: &ClientController, dir: &std::path::Path, who: &str, photo_id: Option<&str>) -> Result { let path = dir.join(format!("{who}-print.jpg")); std::fs::write(&path, format!("a print of {who} from {photo_id:?}")).expect("a synthetic file"); let mut request = json!({ "personId": who, "path": path.to_string_lossy(), "mime": "image/jpeg" }); if let Some(photo_id) = photo_id { request["photoId"] = json!(photo_id); } let parsed = crate::phone::media::validate_avatar(&request).map_err(|error| error.message)?; let layout = client.layout().clone(); client.write_rows("capture.avatar", &mut |ctx| { crate::phone::media::write_avatar(ctx, &layout, &parsed) }) } fn origin_of(client: &ClientController, who: &str) -> Option { client .with_writer(|replica| { let row = replica .driver .get( "SELECT avatar_photo_id FROM cotton_people WHERE id = ?", &[rusqlite::types::Value::Text(who.to_string())], ) .map_err(|error| error.to_string())? .ok_or_else(|| "no such person".to_string())?; Ok(row.text("avatar_photo_id").map(str::to_string)) }) .expect("the person") } #[test] fn a_person_with_a_confirmed_face_and_no_print_is_listed_with_their_best_face() { let (_dir, client) = attached(); let asha = person(&client, "Asha Test"); let old = photo(&client, 1, "2026-01-01T00:00:00.000Z"); let sharp = photo(&client, 2, "2025-01-01T00:00:00.000Z"); face(&client, "f1", &old, &asha, true, "0.4", "0.5"); face(&client, "f2", &sharp, &asha, true, "0.9", "0.1"); let print = listed(&client, &asha).expect("Asha is owed a print"); // Quality before size and age: the sharp face wins. assert_eq!(print["photoId"], json!(sharp)); assert_eq!(print["originHandle"], "PH-2/L0/001"); assert_eq!(print["orientation"], 6); assert_eq!(print["box"], json!({ "x": 0.1, "y": 0.2, "w": 0.1, "h": 0.1 })); } #[test] fn only_a_live_person_without_a_print_and_a_confirmed_face_on_a_live_photo_is_owed_one() { let (dir, client) = attached(); let suggested = person(&client, "Only Suggested"); let archived = person(&client, "Archived Person"); let binned = person(&client, "Binned Photo"); let printed = person(&client, "Has A Print"); let live = photo(&client, 3, "2026-02-01T00:00:00.000Z"); let in_bin = photo(&client, 4, "2026-02-01T00:00:00.000Z"); sql(&client, "UPDATE cotton_photos SET trashed_at = '2026-03-01T00:00:00.000Z' WHERE id = ?", &[&in_bin]); face(&client, "s1", &live, &suggested, false, "0.9", "0.3"); face(&client, "a1", &live, &archived, true, "0.9", "0.3"); face(&client, "b1", &in_bin, &binned, true, "0.9", "0.3"); face(&client, "p1", &live, &printed, true, "0.9", "0.3"); sql(&client, "UPDATE cotton_people SET archived_at = '2026-03-01T00:00:00.000Z' WHERE id = ?", &[&archived]); capture(&client, dir.path(), &printed, Some(&live)).expect("the print"); let owed: Vec = needed(&client).into_iter().map(|print| print["personId"].clone()).collect(); for who in [&suggested, &archived, &binned, &printed] { assert!(!owed.contains(&json!(who)), "{who} should not be owed a print: {owed:?}"); } } #[test] fn a_print_cut_from_a_photo_names_it_and_a_hand_picked_one_clears_it() { let (dir, client) = attached(); let ravi = person(&client, "Ravi Test"); let from = photo(&client, 5, "2026-02-01T00:00:00.000Z"); face(&client, "r1", &from, &ravi, true, "0.8", "0.3"); assert!(listed(&client, &ravi).is_some()); capture(&client, dir.path(), &ravi, Some(&from)).expect("the print"); assert_eq!(origin_of(&client, &ravi).as_deref(), Some(from.as_str())); // Made once: the pass does not ask for it again. assert!(listed(&client, &ravi).is_none()); capture(&client, dir.path(), &ravi, None).expect("a picked picture"); assert_eq!(origin_of(&client, &ravi), None); } #[test] fn a_print_whose_photo_went_to_the_bin_is_cut_again_from_the_next_face() { let (dir, client) = attached(); let mira = person(&client, "Mira Test"); let first = photo(&client, 6, "2026-02-01T00:00:00.000Z"); let next = photo(&client, 7, "2026-01-01T00:00:00.000Z"); face(&client, "m1", &first, &mira, true, "0.9", "0.3"); face(&client, "m2", &next, &mira, true, "0.5", "0.3"); capture(&client, dir.path(), &mira, Some(&first)).expect("the print"); assert!(listed(&client, &mira).is_none()); sql(&client, "UPDATE cotton_photos SET trashed_at = '2026-03-01T00:00:00.000Z' WHERE id = ?", &[&first]); let again = listed(&client, &mira).expect("owed a new print"); assert_eq!(again["photoId"], json!(next)); } #[test] fn a_print_naming_a_photo_this_file_does_not_have_is_refused() { let (dir, client) = attached(); let lena = person(&client, "Lena Test"); let error = capture(&client, dir.path(), &lena, Some(&format!("photo:{:064x}", 99))).expect_err("refused"); assert!(error.contains("no such photo"), "{error}"); assert_eq!(origin_of(&client, &lena), None); }