Cotton code
179 lines · 6.4 KB · RustRaw
//! The Messages reader's decisions, one row at a time and without a database.use super::*;#[test]fn it_reads_a_nanosecond_stamp_as_the_instant_it_names() {    // 2026-09-08T10:00:00Z is 810_554_400 seconds after Apple's epoch.    let seconds: i64 = 810_554_400;    assert_eq!(        apple_date_to_iso(Some(seconds * 1_000_000_000)).as_deref(),        Some("2026-09-08T10:00:00.000Z")    );}#[test]fn it_reads_an_older_files_seconds_by_their_magnitude_alone() {    let seconds: i64 = 810_554_400;    assert_eq!(        apple_date_to_iso(Some(seconds)).as_deref(),        Some("2026-09-08T10:00:00.000Z")    );}#[test]fn a_zero_date_means_the_row_never_got_one_and_is_not_the_year_2001() {    assert_eq!(apple_date_to_iso(Some(0)), None);    assert_eq!(apple_date_to_iso(Some(-1)), None);    assert_eq!(apple_date_to_iso(None), None);}#[test]fn an_absurd_stamp_is_arithmetic_that_holds_rather_than_a_panic() {    // The TypeScript answered `null` only when `new Date` gave an Invalid Date,    // which is ±8.64e15 ms away — further than any value this column can hold.    // So the largest stamp there is has a real instant, and the checked    // arithmetic here exists so a corrupt row can never be a crash mid-import.    assert!(apple_date_to_iso(Some(i64::MAX)).is_some());}#[test]fn four_channels_exist_and_the_carriers_label_is_not_one_of_them() {    assert_eq!(        channel_for_service(Some("iMessage")),        Some(MessageChannel::Imessage)    );    for label in ["SMS", "MMS", "RCS", " sms "] {        assert_eq!(channel_for_service(Some(label)), Some(MessageChannel::Sms));    }    assert_eq!(channel_for_service(Some("Carrier-Branded-Thing")), None);    assert_eq!(channel_for_service(None), None);}#[test]fn a_bare_national_number_is_promoted_to_the_stated_country_never_a_guessed_one() {    let handle = read_handle(Some("9876543210"), DEFAULT_COUNTRY_CODE).expect("a handle");    assert_eq!(handle.kind, HandleKind::Phone);    assert_eq!(handle.value, "919876543210");    assert_eq!(handle.raw, "9876543210");}#[test]fn an_apple_id_is_an_email_identity_and_is_lower_cased() {    let handle =        read_handle(Some("  Someone@Example.COM "), DEFAULT_COUNTRY_CODE).expect("a handle");    assert_eq!(handle.kind, HandleKind::Email);    assert_eq!(handle.value, "someone@example.com");    assert_eq!(handle.raw, "Someone@Example.COM");}#[test]fn a_sender_id_that_is_a_word_stays_a_label_and_never_becomes_a_number() {    // Most handles in a real file are these: bank alerts, OTP senders,    // deliveries. Forcing one into a phone files a bank under a number that is    // not a number.    let handle = read_handle(Some("AX-BANKOTP"), DEFAULT_COUNTRY_CODE).expect("a handle");    assert_eq!(handle.kind, HandleKind::Label);    assert_eq!(handle.value, "AX-BANKOTP");}#[test]fn something_with_an_at_sign_that_is_not_an_address_is_a_label_not_an_email() {    for raw in ["@", "a@", "@b", "a@b@c", "two words@example.com"] {        assert_eq!(            read_handle(Some(raw), DEFAULT_COUNTRY_CODE)                .expect("a handle")                .kind,            HandleKind::Label,            "{raw}"        );    }}#[test]fn an_empty_handle_is_no_handle_at_all() {    assert_eq!(read_handle(Some("   "), DEFAULT_COUNTRY_CODE), None);    assert_eq!(read_handle(None, DEFAULT_COUNTRY_CODE), None);}#[test]fn a_style_this_reader_does_not_know_is_no_thread_rather_than_a_guessed_dm() {    assert_eq!(thread_kind_for_style(Some(45)), Some(ThreadShape::Dm));    assert_eq!(thread_kind_for_style(Some(43)), Some(ThreadShape::Group));    assert_eq!(thread_kind_for_style(Some(0)), None);    assert_eq!(thread_kind_for_style(None), None);}#[test]fn it_names_the_six_tapbacks_apple_ships_and_takes_the_rest_from_the_row() {    for (kind, emoji) in [        (2000, "❤️"),        (2001, "👍"),        (2002, "👎"),        (2003, "😂"),        (2004, "‼️"),        (2005, "❓"),    ] {        let tapback = read_tapback(Some(kind), Some("guid-1"), None).expect("a tapback");        assert_eq!(tapback.emoji, emoji);        assert!(!tapback.removal);        assert_eq!(tapback.target_guid, "guid-1");    }    let custom = read_tapback(Some(2006), Some("guid-1"), Some(" 🦕 ")).expect("a tapback");    assert_eq!(custom.emoji, "🦕");}#[test]fn a_3000_series_row_is_the_same_reaction_being_taken_back() {    let tapback = read_tapback(Some(3001), Some("guid-1"), None).expect("a tapback");    assert_eq!(tapback.emoji, "👍");    assert!(tapback.removal);}#[test]fn a_custom_tapback_with_an_empty_emoji_column_has_told_us_nothing_to_store() {    assert_eq!(read_tapback(Some(2006), Some("guid-1"), Some("  ")), None);    assert_eq!(read_tapback(Some(2007), Some("guid-1"), None), None);}#[test]fn a_row_that_is_not_a_tapback_at_all_answers_none() {    assert_eq!(read_tapback(Some(0), Some("guid-1"), None), None);    assert_eq!(read_tapback(None, Some("guid-1"), None), None);    // The gap between the two series is not a tapback either.    assert_eq!(read_tapback(Some(2500), Some("guid-1"), None), None);    assert_eq!(read_tapback(Some(4000), Some("guid-1"), None), None);    // And one with nothing to point at cannot be applied.    assert_eq!(read_tapback(Some(2001), None, None), None);}#[test]fn a_reaction_belongs_to_the_message_whichever_part_of_it_was_tapped() {    assert_eq!(        target_guid_of(Some("p:0/GUID-1")).as_deref(),        Some("GUID-1")    );    assert_eq!(target_guid_of(Some("bp:GUID-1")).as_deref(), Some("GUID-1"));    assert_eq!(target_guid_of(Some("GUID-1")).as_deref(), Some("GUID-1"));    assert_eq!(target_guid_of(Some("  ")), None);    assert_eq!(target_guid_of(None), None);    assert_eq!(target_guid_of(Some("p:0/")), None);}#[test]fn an_unnamed_group_is_titled_by_who_is_in_it_which_is_what_messages_shows() {    let members = vec!["Ada".to_string(), "Grace".to_string()];    assert_eq!(group_title(Some("  Weekend  "), &members), "Weekend");    assert_eq!(group_title(Some("   "), &members), "Ada, Grace");    assert_eq!(group_title(None, &members), "Ada, Grace");    assert_eq!(group_title(None, &[]), "Group");}#[test]fn only_item_type_zero_is_a_message_and_the_rest_are_events_the_chat_recorded() {    assert!(is_message_row(Some(0)));    assert!(is_message_row(None));    for item_type in [1, 2, 3, 4, 5, 6] {        assert!(!is_message_row(Some(item_type)), "{item_type}");    }}