159 lines · 7.9 KB · RustRaw
use super::*;fn counts(pairs: &[(&str, u64)]) -> BTreeMap<String, u64> { pairs.iter().map(|(k, v)| (k.to_string(), *v)).collect()}fn on() -> State { State { on: true, ..State::default() }}/// Every line a phone is handed must exist: an empty one would be drawn blank.fn whole(value: &Value) { match value { Value::String(text) => assert!(!text.is_empty() && !text.contains('{'), "unfilled line: {text:?}"), Value::Object(map) => map.values().for_each(whole), Value::Array(items) => items.iter().for_each(whole), _ => {} }}#[test]fn only_apps_with_their_own_database_back_up_and_only_signed_out() { for kit in ["notes", "days", "story", "keys", "sites"] { assert!(row(kit, false, Platform::Ios, &State::default(), None).is_some(), "{kit}"); assert!(row(kit, true, Platform::Ios, &State::default(), None).is_none(), "{kit} signed in syncs instead"); } for kit in ["messages", "data", "mail"] { assert!(row(kit, false, Platform::Android, &State::default(), None).is_none(), "{kit} keeps no backup"); }}#[test]fn the_row_says_how_backup_stands() { let line = |state: &State, when| row("story", false, Platform::Ios, state, when).unwrap()["line"].as_str().unwrap().to_string(); assert_eq!(line(&State::default(), None), "Off"); assert_eq!(line(&on(), None), "On · not backed up yet"); assert_eq!(line(&on(), Some("9:53")), "On · backed up 9:53"); let full = State { trouble: Some(Trouble::Full), ..on() }; assert_eq!(line(&full, Some("9:53")), "iCloud is full"); let full = State { trouble: Some(Trouble::Full), ..on() }; assert_eq!(row("story", false, Platform::Android, &full, None).unwrap()["line"], "Google is full"); let off_with_trouble = State { trouble: Some(Trouble::Full), ..State::default() }; assert_eq!(line(&off_with_trouble, None), "Off", "trouble is said only while on");}#[test]fn the_key_line_says_honestly_which_key_the_person_has() { let ios = |held, saved| key_line(Platform::Ios, Store { held, cloud: None, old: false }, saved); assert_eq!(ios(true, false), "key.ios_store"); assert_eq!(ios(true, true), "key.ios_both"); assert_eq!(ios(false, true), "key.code_only"); assert_eq!(ios(false, false), "key.neither"); let android = |held, cloud, old, saved| key_line(Platform::Android, Store { held, cloud: Some(cloud), old }, saved); assert_eq!(android(true, true, false, false), "key.android_store"); assert_eq!(android(true, true, false, true), "key.android_both"); assert_eq!(android(true, false, false, false), "key.android_no_lock", "no screen lock: Google keeps no cloud copy"); assert_eq!(android(true, true, true, false), "key.android_old", "too old for Google's cloud restore"); assert_eq!(android(true, false, false, true), "key.code_only"); // iPhone never claims iCloud Keychain is on: it cannot know. assert!(resolve(SET, "key.ios_store", &[], &[]).unwrap().contains("If it's off"));}#[test]fn the_page_is_whole_on_both_phones_in_every_state() { let troubles = [None, Some(Trouble::IcloudOff), Some(Trouble::Full), Some(Trouble::TooBig), Some(Trouble::FolderGone), Some(Trouble::Failed { why: "the disk is full".into() })]; for platform in [Platform::Ios, Platform::Android] { for trouble in &troubles { for last in [None, Some(Last { at: "2026-11-25T09:53:00Z".into(), bytes: 2_500_000, place: "icloud".into() })] { let state = State { trouble: trouble.clone(), last, folder: Some("Google Drive › Cotton".into()), ..on() }; let answer = page("Story", platform, &state, Store { held: true, cloud: Some(true), old: false }, Some("9:53")); whole(&answer); } } } let answer = page("Story", Platform::Android, &on(), Store::default(), None); assert_eq!(answer["last"], "Not backed up yet"); assert!(answer["files_left_out"].as_str().unwrap().contains("25 MB"), "Android says what Google's cap leaves out");}#[test]fn a_trouble_carries_its_act() { let state = State { trouble: Some(Trouble::TooBig), ..on() }; let answer = page("Story", Platform::Android, &state, Store::default(), Some("3 Nov")); assert_eq!(answer["trouble"]["act"]["id"], "choose_folder"); assert!(answer["trouble"]["line"].as_str().unwrap().contains("3 Nov")); let state = State { trouble: Some(Trouble::IcloudOff), ..on() }; assert_eq!(page("Words", Platform::Ios, &state, Store::default(), None)["trouble"]["act"]["id"], "save_to_files");}#[test]fn a_backup_is_due_after_changes_and_never_twice_in_a_quarter_hour() { let now = 1_000_000; assert!(!due(&State::default(), false, Some(now), None, now, true), "off: never, even by hand"); assert!(!due(&on(), true, Some(now), None, now, true), "signed in: sync instead"); assert!(due(&on(), false, None, None, now, false), "never backed up: due"); assert!(due(&on(), false, Some(now - 10), Some(now - AFTER_CHANGES - 20), now, false)); assert!(!due(&on(), false, Some(now - 10), Some(now - 60), now, false), "too soon after the last"); assert!(!due(&on(), false, Some(now - 5000), Some(now - 4000), now, false), "nothing changed since"); assert!(due(&on(), false, Some(now - 5000), Some(now - 4000), now, true), "by hand: always");}#[test]fn a_restore_counts_first_and_never_replaces_without_asking() { let there = counts(&[("scripts", 3), ("scenes", 41)]); let fresh = restore_ask("Story", "story", &counts(&[("scripts", 0)]), &there, "3 Nov", "ios"); assert_eq!(fresh["fresh"], true); assert_eq!(fresh["line"], "Backup from 3 Nov on an iPhone: 3 scripts, 41 scenes."); let replace = restore_ask("Story", "story", &counts(&[("scripts", 2), ("scenes", 9)]), &there, "3 Nov", "android"); assert_eq!(replace["fresh"], false); let line = replace["line"].as_str().unwrap(); assert!(line.contains("This phone has 2 scripts, 9 scenes."), "{line}"); assert!(line.contains("3 scripts, 41 scenes"), "{line}"); assert!(line.contains("30 days"), "{line}"); whole(&fresh); whole(&replace);}#[test]fn counts_read_as_a_person_says_them() { assert_eq!(counts_said("days", &counts(&[("events", 1), ("todos", 1204)])), "1 event, 1,204 to-dos"); assert_eq!(counts_said("notes", &counts(&[("notes", 0), ("files", 1)])), "1 photo or file"); assert_eq!(counts_said("notes", &counts(&[])), "nothing yet");}#[test]fn every_refusal_has_its_words_and_the_key_ones_ask_for_the_code() { let names = |kit: &str| if kit == "notes" { "Words".to_string() } else { kit.to_string() }; for refusal in [ Refusal::NotABackup, Refusal::Newer, Refusal::OtherApp("notes".into()), Refusal::OtherKey, Refusal::CodeTyped(crate::vault::recovery_code::Problem::Check), Refusal::Damaged, Refusal::Cut, Refusal::Io("no space".into()), ] { let said = refusal_said(&refusal, "Story", "Story.cottonbackup", &names); whole(&said); assert_eq!(said["ask_code"].is_object(), matches!(refusal, Refusal::OtherKey | Refusal::CodeTyped(_)), "{refusal:?}"); } let other = refusal_said(&Refusal::OtherApp("notes".into()), "Story", "Story.cottonbackup", &names); assert_eq!(other["said"], "This is a backup of Words, not Story. Open it in Words."); for gentle in [Refusal::Damaged, Refusal::Cut] { assert!(refusal_said(&gentle, "Story", "f", &names)["said"].as_str().unwrap().contains("Nothing on this phone was changed")); }}#[test]fn the_code_page_and_names_are_plain() { let page = code_page("Story", "7K3Q-9XWD-0000-1111-2222-3333"); assert_eq!(page["groups"].as_array().unwrap().len(), 6); whole(&page); assert_eq!(file_name("Books."), "Books.cottonbackup"); assert_eq!(file_name("Story"), "Story.cottonbackup"); assert!(fits_google(GOOGLE_CAP) && !fits_google(GOOGLE_CAP + 1)); assert_eq!(size_said(2_500_000), "2.4 MB"); assert_eq!(size_said(10), "1 KB");}