//! `people.fillCities` on a real replica: the people are created and edited //! through `write`, exactly as the app does, and the fill runs over the file //! those writes left. Every person here is invented. use serde_json::{json, Map, Value}; use crate::shell::client::phone_tests::attached; use crate::shell::client::ClientController; fn args(value: Value) -> Map { value.as_object().cloned().expect("an object") } /// A person, created the way the app creates one; answers their id. fn person(client: &ClientController, fields: Value) -> String { let answer = client .write("create_person", &args(fields)) .expect("create_person"); let id = answer .pointer("/answer/id") .and_then(Value::as_str) .unwrap_or_else(|| panic!("an id in {answer}")); id.to_string() } fn edit(client: &ClientController, id: &str, fields: Value) { let mut patch = args(fields); patch.insert("id".to_string(), json!(id)); client .write("update_person", &patch) .expect("update_person"); } /// `(city, timezone)` as the row holds them now. fn stored(client: &ClientController, id: &str) -> (Option, Option) { client .with_writer(|replica| { let row = replica .driver .get( "SELECT city, timezone FROM cotton_people WHERE id = ?", &[rusqlite::types::Value::Text(id.to_string())], ) .map_err(|error| error.to_string())? .ok_or_else(|| "no such person".to_string())?; Ok(( row.text("city").map(str::to_string), row.text("timezone").map(str::to_string), )) }) .expect("the row") } fn queued_people_ops(client: &ClientController) -> i64 { client .with_writer(|replica| { replica .driver .get( "SELECT COUNT(*) AS n FROM _outbox WHERE tbl = 'cotton_people'", &[], ) .map_err(|error| error.to_string())? .and_then(|row| row.int("n")) .ok_or_else(|| "no count".to_string()) }) .expect("the outbox") } fn filled_ids(answer: &Value) -> Vec { answer["filled"] .as_array() .expect("a list") .iter() .map(|row| row["id"].as_str().expect("an id").to_string()) .collect() } #[test] fn a_person_with_a_zone_and_no_city_gets_the_zone_s_main_city_and_keeps_the_zone() { let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Ines Example", "timezone": "Europe/London" }), ); let answer = super::fill(&client).expect("a pass"); assert_eq!( answer["filled"], json!([{ "id": id, "city": "London", "timezone": "Europe/London" }]) ); assert_eq!( stored(&client, &id), ( Some("London".to_string()), Some("Europe/London".to_string()) ) ); } #[test] fn the_fill_is_queued_to_sync_like_any_other_edit() { let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Tomas Example", "timezone": "Asia/Tokyo" }), ); let before = queued_people_ops(&client); super::fill(&client).expect("a pass"); assert!( queued_people_ops(&client) > before, "the fill for {id} left nothing in the outbox to push" ); } #[test] fn a_city_that_is_already_set_is_never_overwritten() { let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Wren Example", "city": "Somewhere small", "timezone": "Europe/London" }), ); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); assert_eq!(stored(&client, &id).0.as_deref(), Some("Somewhere small")); } #[test] fn a_person_with_a_city_as_text_and_no_zone_is_left_exactly_as_they_are() { // No bulk geocoding: the text stays and no zone is invented for it. let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Oda Example", "city": "The old mill" }), ); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); assert_eq!( stored(&client, &id), (Some("The old mill".to_string()), None) ); } #[test] fn a_second_pass_finds_nobody() { let (_dir, client) = attached(); person( &client, json!({ "fullName": "Ada Example", "timezone": "America/Argentina/Buenos_Aires" }), ); let first = super::fill(&client).expect("a pass"); assert_eq!(first["filled"][0]["city"], json!("Buenos Aires")); let before = queued_people_ops(&client); let second = super::fill(&client).expect("a pass"); assert_eq!(second, json!({ "filled": [] })); assert_eq!( queued_people_ops(&client), before, "an idle pass wrote something" ); } #[test] fn a_city_the_owner_took_away_is_not_filled_again() { // The owner cleared a city — Cotton's or his own. He has answered; filling // it again would be arguing with him. A person's zone is the city's place // companion (rise 138), so clearing the city by words clears its zone too. let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Pia Example", "timezone": "Europe/Berlin" }), ); super::fill(&client).expect("a pass"); edit(&client, &id, json!({ "city": null })); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); assert_eq!( stored(&client, &id), (None, None) ); } #[test] fn a_zone_that_names_no_place_or_is_not_a_zone_fills_nothing() { let (_dir, client) = attached(); let utc = person( &client, json!({ "fullName": "Kai Example", "timezone": "Etc/UTC" }), ); let nowhere = person( &client, json!({ "fullName": "Lior Example", "timezone": "Middle/Earth" }), ); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); assert_eq!(stored(&client, &utc).0, None); assert_eq!(stored(&client, &nowhere).0, None); } #[test] fn a_renamed_zone_fills_the_city_by_its_current_name_and_leaves_the_zone_as_it_was() { let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Ravi Example", "timezone": "Asia/Calcutta" }), ); super::fill(&client).expect("a pass"); assert_eq!( stored(&client, &id), ( Some("Kolkata".to_string()), Some("Asia/Calcutta".to_string()) ) ); } #[test] fn an_archived_person_is_not_touched() { let (_dir, client) = attached(); let id = person( &client, json!({ "fullName": "Noor Example", "timezone": "Africa/Nairobi" }), ); client .write("archive_person", &args(json!({ "id": id }))) .expect("archive_person"); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); } /// `city_filled_at` as the row holds it now. fn stamp(client: &ClientController, id: &str) -> Option { client .with_writer(|replica| { Ok(replica .driver .get( "SELECT city_filled_at FROM cotton_people WHERE id = ?", &[rusqlite::types::Value::Text(id.to_string())], ) .map_err(|error| error.to_string())? .and_then(|row| row.text("city_filled_at").map(str::to_string))) }) .expect("the row") } /// RULE: one pass stamps every person it fills with ONE instant — the pass is what Home lists and what the undo names. #[test] fn every_person_of_one_pass_carries_the_pass_s_instant() { let (_dir, client) = attached(); let oslo = person(&client, json!({ "fullName": "Maren Example", "timezone": "Europe/Oslo" })); let tokyo = person(&client, json!({ "fullName": "Tomas Example", "timezone": "Asia/Tokyo" })); let unfilled = person(&client, json!({ "fullName": "Kai Example", "timezone": "Etc/UTC" })); super::fill(&client).expect("a pass"); let at = stamp(&client, &oslo).expect("the fill is stamped"); assert_eq!(stamp(&client, &tokyo).as_deref(), Some(at.as_str())); assert_eq!(stamp(&client, &unfilled), None, "a person the pass did not fill is not stamped"); } /// RULE: a person the fill stamped is never filled again, even on a device that never saw the clear in its history — the stamp syncs, the history does not. #[test] fn a_stamped_person_is_never_filled_again_even_with_no_history_on_this_device() { let (_dir, client) = attached(); let id = person(&client, json!({ "fullName": "Pia Example", "timezone": "Europe/Berlin" })); super::fill(&client).expect("a pass"); edit(&client, &id, json!({ "city": null })); // A device that joined after the clear holds no version row of it. client .with_writer(|replica| { replica .driver .run("DELETE FROM cotton_changes", &[]) .map_err(|error| error.to_string()) }) .expect("history forgotten"); let answer = super::fill(&client).expect("a pass"); assert!(filled_ids(&answer).is_empty(), "{answer}"); assert_eq!(stored(&client, &id).0, None); } /// RULE: undoing a pass clears every city of it that still stands, leaves a city the owner changed since, keeps the stamp so the fill never returns, and syncs like any other edit. #[test] fn undoing_a_pass_clears_its_standing_cities_and_nothing_the_owner_changed() { let (_dir, client) = attached(); let oslo = person(&client, json!({ "fullName": "Maren Example", "timezone": "Europe/Oslo" })); let bath = person(&client, json!({ "fullName": "Wren Example", "timezone": "Europe/London" })); super::fill(&client).expect("a pass"); let at = stamp(&client, &oslo).expect("stamped"); edit(&client, &bath, json!({ "city": "Bath" })); let before = queued_people_ops(&client); let answer = super::unfill(&client, &at).expect("the undo"); assert_eq!(answer, json!({ "cleared": [oslo.clone()] })); assert!(queued_people_ops(&client) > before, "the undo left nothing to push"); assert_eq!(stored(&client, &oslo), (None, Some("Europe/Oslo".to_string()))); assert_eq!(stored(&client, &bath).0.as_deref(), Some("Bath")); assert_eq!(stamp(&client, &oslo).as_deref(), Some(at.as_str()), "the stamp stays"); assert!(filled_ids(&super::fill(&client).expect("a pass")).is_empty()); assert_eq!(super::unfill(&client, &at).expect("again"), json!({ "cleared": [] })); }