//! `packages/core/__tests__/messages/mentions.test.ts`, case for case, plus the //! Node vectors for the UTF-16 indices this module hands a renderer. use super::*; fn candidate(phone: &str, name: &str) -> MentionCandidate { MentionCandidate { phone: phone.to_string(), name: name.to_string(), } } fn meera() -> MentionCandidate { candidate("919999000011", "Meera") } fn meera_iyer() -> MentionCandidate { candidate("919999000022", "Meera Iyer") } fn ravi() -> MentionCandidate { candidate("919999000033", "Ravi") } fn all() -> Vec { vec![meera(), meera_iyer(), ravi()] } // ── findMentionRanges ──────────────────────────────────────────────────────── #[test] fn matches_the_longest_name_first_so_a_full_name_is_not_read_as_a_first_name() { assert_eq!( find_mention_ranges("hi @Meera Iyer", &all()), vec![MentionRange { start: 3, end: 14, phone: "919999000022".to_string(), name: "Meera Iyer".to_string(), }] ); } #[test] fn requires_the_at_to_start_a_word() { assert_eq!( find_mention_ranges("mail ravi@Meera.com now", &all()), vec![] ); assert_eq!(find_mention_ranges("@Meera", &all()).len(), 1); assert_eq!(find_mention_ranges("hi @Meera", &all()).len(), 1); } #[test] fn requires_a_clean_right_boundary() { assert_eq!(find_mention_ranges("@Meerakumari said so", &all()), vec![]); assert_eq!(find_mention_ranges("@Meera, hello", &all()).len(), 1); assert_eq!(find_mention_ranges("@Meera's idea", &all()).len(), 1); } #[test] fn never_overlaps_two_ranges_and_returns_them_in_reading_order() { let ranges = find_mention_ranges("@Ravi and @Meera Iyer talked", &all()); let names: Vec<&str> = ranges.iter().map(|range| range.name.as_str()).collect(); assert_eq!(names, ["Ravi", "Meera Iyer"]); } // ── extractMentions ────────────────────────────────────────────────────────── #[test] fn gives_phones_de_duped_in_first_seen_order() { assert_eq!( extract_mentions("@Ravi @Meera @Ravi again", &all()), ["919999000033", "919999000011"] ); } #[test] fn is_empty_for_a_body_with_no_mentions_or_no_candidates() { assert!(extract_mentions("no names here", &all()).is_empty()); assert!(extract_mentions("@Meera", &[]).is_empty()); assert!(extract_mentions("", &all()).is_empty()); } #[test] fn ignores_a_candidate_with_a_blank_name_rather_than_matching_a_bare_at() { assert!(extract_mentions("@ hello", &[candidate("919999000044", "")]).is_empty()); } // ── getMentionQuery ────────────────────────────────────────────────────────── #[test] fn finds_the_query_between_the_nearest_word_starting_at_and_the_cursor() { assert_eq!( get_mention_query("hi @Mee", 7), Some(MentionQuery { query: "Mee".to_string(), start: 3 }) ); assert_eq!( get_mention_query("@", 1), Some(MentionQuery { query: String::new(), start: 0 }) ); } #[test] fn closes_once_the_query_holds_whitespace_or_the_at_is_mid_word() { assert_eq!(get_mention_query("hi @Mee ra", 10), None); assert_eq!(get_mention_query("a@Mee", 5), None); assert_eq!(get_mention_query("nothing", 7), None); assert_eq!(get_mention_query("hi @Mee", 0), None); } // ── insertMention ──────────────────────────────────────────────────────────── #[test] fn replaces_the_query_with_at_name_and_a_trailing_space_and_moves_the_cursor_after_it() { assert_eq!( insert_mention("hi @Mee", 3, 7, "Meera"), MentionInsertion { text: "hi @Meera ".to_string(), cursor: 10 } ); assert_eq!( insert_mention("hi @Mee there", 3, 7, "Meera"), MentionInsertion { text: "hi @Meera there".to_string(), cursor: 10 } ); } // ── segmentMentions ────────────────────────────────────────────────────────── #[test] fn splits_a_body_into_plain_and_mention_runs() { assert_eq!( segment_mentions("hi @Meera Iyer, done", &all()), vec![ MessageSegment::text("hi "), MessageSegment::mention("@Meera Iyer", "919999000022"), MessageSegment::text(", done"), ] ); } #[test] fn returns_one_text_run_when_nothing_matches_and_nothing_for_an_empty_body() { assert_eq!( segment_mentions("plain words", &all()), vec![MessageSegment::text("plain words")] ); assert_eq!(segment_mentions("", &all()), vec![]); assert_eq!( segment_mentions("hi", &[]), vec![MessageSegment::text("hi")] ); } // ── mentionsPhone ──────────────────────────────────────────────────────────── #[test] fn answers_the_one_question_that_overrides_mute() { let one = vec!["919999000011".to_string()]; assert!(mentions_phone(&one, Some("919999000011"))); assert!(!mentions_phone(&one, Some("919999000022"))); assert!(!mentions_phone(&[], Some("919999000011"))); assert!(!mentions_phone(&one, None)); } // ── Node vectors ───────────────────────────────────────────────────────────── /// Printed by running the TypeScript (`vec_mentions.ts`), pasted verbatim. /// /// These are the cases the whole UTF-16 argument exists for. An emoji before a /// mention moves its start by TWO, not by one, because `indexOf` counts code /// units — a renderer slicing the same string with a `chars()` index would /// highlight the wrong run, and a composer would put the cursor in the wrong /// place. #[test] fn node_vectors_for_utf16_indices() { let ranges = find_mention_ranges("\u{1f600} @Meera done", &all()); assert_eq!( ranges, vec![MentionRange { start: 3, end: 9, phone: "919999000011".to_string(), name: "Meera".to_string(), }] ); assert_eq!( get_mention_query("\u{1f600} @Mee", 7), Some(MentionQuery { query: "Mee".to_string(), start: 3 }) ); assert_eq!( insert_mention("\u{1f600} @Mee", 3, 7, "Meera"), MentionInsertion { text: "\u{1f600} @Meera ".to_string(), cursor: 10 } ); assert_eq!( segment_mentions("\u{1f600} @Meera done", &all()), vec![ MessageSegment::text("\u{1f600} "), MessageSegment::mention("@Meera", "919999000011"), MessageSegment::text(" done"), ] ); } /// A name whose right boundary is a non-ASCII letter, and one that ends against /// an emoji. `\p{L}` is the letter CATEGORY on both sides — Devanagari is a /// letter and closes the boundary, an emoji is not and opens it. #[test] fn node_vectors_for_the_word_boundary() { let people = vec![meera()]; // Followed by a Devanagari letter: still one word, so not a mention. assert_eq!(find_mention_ranges("@Meera\u{92e}", &people), vec![]); // Followed by an emoji: a clean boundary. assert_eq!(find_mention_ranges("@Meera\u{1f600}", &people).len(), 1); // A digit closes it (`\p{N}`), an em dash does not. assert_eq!(find_mention_ranges("@Meera2", &people), vec![]); assert_eq!(find_mention_ranges("@Meera\u{2014}now", &people).len(), 1); // A non-breaking space before the `@` is whitespace to ECMAScript, so the // mention still opens a word — and so is a BOM, which Rust's own // `char::is_whitespace` says is NOT whitespace. A NEL is the other way round: // Rust calls it whitespace and ECMAScript does not, so the `@` after one is // mid-word and this is NOT a mention. Both directions are Node's answer. assert_eq!(find_mention_ranges("hi\u{a0}@Meera", &people).len(), 1); assert_eq!(find_mention_ranges("hi\u{feff}@Meera", &people).len(), 1); assert_eq!(find_mention_ranges("hi\u{85}@Meera", &people).len(), 0); assert_eq!(find_mention_ranges("hi\t@Meera", &people).len(), 1); } /// A name that appears twice, and one candidate whose name contains another's — /// the de-dupe and the non-overlap rule, over indices Node printed. #[test] fn node_vectors_for_overlap_and_order() { let ranges = find_mention_ranges("@Meera Iyer and @Meera", &all()); let pairs: Vec<(usize, usize, &str)> = ranges .iter() .map(|range| (range.start, range.end, range.name.as_str())) .collect(); assert_eq!(pairs, [(0, 11, "Meera Iyer"), (16, 22, "Meera")]); assert_eq!( extract_mentions("@Meera Iyer and @Meera", &all()), ["919999000022", "919999000011"] ); }