Cotton code
293 lines · 9.9 KB · RustRaw
//! `packages/core/__tests__/messages/links.test.ts`, case for case, plus the Node//! vectors for the trim, the UTF-16 indices and the WHATWG hostname.use super::*;use crate::messages::mentions::MentionCandidate;fn link(url: &str, start: usize, end: usize) -> LinkMatch {    LinkMatch {        url: url.to_string(),        start,        end,    }}// ── trimTrailingPunctuation ──────────────────────────────────────────────────#[test]fn drops_sentence_punctuation_that_is_not_part_of_the_url() {    assert_eq!(        trim_trailing_punctuation("https://a.com/b."),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b,"),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b!?"),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b\u{2026}"),        "https://a.com/b"    );}#[test]fn keeps_a_closing_bracket_the_url_opened_itself() {    assert_eq!(        trim_trailing_punctuation("https://en.wikipedia.org/wiki/Rust_(language)"),        "https://en.wikipedia.org/wiki/Rust_(language)"    );}#[test]fn drops_a_closing_bracket_the_sentence_opened() {    assert_eq!(        trim_trailing_punctuation("https://a.com/b)"),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b]"),        "https://a.com/b"    );}#[test]fn unstacks_punctuation_in_a_loop() {    assert_eq!(        trim_trailing_punctuation("https://a.com/)."),        "https://a.com/"    );}// ── findLinks ────────────────────────────────────────────────────────────────#[test]fn finds_explicit_http_s_urls_in_order_cleaned() {    assert_eq!(        find_links("see https://a.com/x. and http://b.dev now"),        vec![link("https://a.com/x", 4, 19), link("http://b.dev", 25, 37),]    );}#[test]fn leaves_a_bare_domain_alone_ordinary_prose_is_not_a_link() {    assert_eq!(find_links("visit cottondb.com today"), vec![]);    assert_eq!(find_links("mail me at a@b.com"), vec![]);}#[test]fn refuses_a_scheme_with_no_host() {    assert_eq!(find_links("https:// nothing"), vec![]);    assert_eq!(find_links("https:///x"), vec![]);}#[test]fn is_empty_for_an_empty_body() {    assert_eq!(find_links(""), vec![]);}/// The TypeScript's `/g` regex keeps a `lastIndex` between calls and has to be/// reset by hand; Rust's carries no cursor. The behaviour is what callers rely/// on, so it is asserted either way.#[test]fn does_not_carry_state_between_calls() {    let text = "https://a.com";    assert_eq!(find_links(text).len(), 1);    assert_eq!(find_links(text).len(), 1);}// ── extractFirstUrl ──────────────────────────────────────────────────────────#[test]fn is_what_the_composer_fetches_a_preview_for() {    assert_eq!(        extract_first_url("look https://a.com/x, then https://b.com").as_deref(),        Some("https://a.com/x")    );    assert_eq!(extract_first_url("no links"), None);}// ── segmentLinks and segmentMessageBody ──────────────────────────────────────#[test]fn splits_text_runs_on_links_and_leaves_mention_runs_whole() {    assert_eq!(        segment_links(&[            MessageSegment::text("see https://a.com now"),            MessageSegment::mention("@Meera", "919999000011"),        ]),        vec![            MessageSegment::text("see "),            MessageSegment::link("https://a.com", "https://a.com"),            MessageSegment::text(" now"),            MessageSegment::mention("@Meera", "919999000011"),        ]    );}#[test]fn runs_mentions_first_then_links_over_one_body() {    let candidates = vec![MentionCandidate {        phone: "919999000011".to_string(),        name: "Meera".to_string(),    }];    assert_eq!(        segment_message_body("@Meera see https://a.com", &candidates),        vec![            MessageSegment::mention("@Meera", "919999000011"),            MessageSegment::text(" see "),            MessageSegment::link("https://a.com", "https://a.com"),        ]    );}// ── displayDomain ────────────────────────────────────────────────────────────#[test]fn is_the_hostname_without_www_and_the_raw_string_when_it_will_not_parse() {    assert_eq!(        display_domain("https://www.cottondb.com/a/b"),        "cottondb.com"    );    assert_eq!(        display_domain("https://news.ycombinator.com"),        "news.ycombinator.com"    );    assert_eq!(display_domain("not a url"), "not a url");}// ── extractMessageUrls ───────────────────────────────────────────────────────#[test]fn gives_every_distinct_url_in_first_seen_order() {    assert_eq!(        extract_message_urls("a https://a.com/x. b https://b.dev c https://a.com/x"),        ["https://a.com/x", "https://b.dev"]    );    assert!(extract_message_urls("nothing here").is_empty());}// ── Node vectors ─────────────────────────────────────────────────────────────/// Printed by running the TypeScript (`vec_links.ts`), pasted verbatim.////// The hostname is WHATWG's, which is why this port uses the `url` crate rather/// than splitting on `/`: the host is lower-cased, an internationalised one is/// PUNYCODED, an IPv6 literal keeps its brackets, credentials are dropped, and a/// URL with no host at all answers with the empty string rather than with the/// input. Every one of those is a line a preview card would otherwise print/// wrongly.#[test]fn node_vectors_for_display_domain() {    assert_eq!(display_domain("https://WWW.EXAMPLE.COM/x"), "example.com");    assert_eq!(        display_domain("https://m\u{fc}nchen.example/x"),        "xn--mnchen-3ya.example"    );    assert_eq!(display_domain("http://[::1]:8080/x"), "[::1]");    assert_eq!(display_domain("mailto:a@b.com"), "");    assert_eq!(        display_domain("https://user:pw@example.com/x"),        "example.com"    );    // A trailing dot is part of a host name (the DNS root), so it survives.    assert_eq!(display_domain("https://example.com."), "example.com.");    assert_eq!(display_domain(""), "");}/// The scheme match is ASCII-case-insensitive and NOTHING more. JavaScript's `i`/// without the `u` flag refuses to fold U+017F (ſ) onto `s`; Rust's `(?i)` folds/// it, which would make `httpſ://a.com` a link on one side only. `(?i-u:https?)`/// is what keeps the two answers the same.#[test]fn node_vectors_for_the_case_fold() {    assert_eq!(        find_links("HTTPS://A.COM/X"),        vec![link("HTTPS://A.COM/X", 0, 15)]    );    assert_eq!(find_links("http\u{17f}://a.com"), vec![]);}/// Indices are UTF-16 code units, and the `end` is measured on the CLEANED url —/// so trailing punctuation the trim removed is outside the range a renderer/// highlights, and an emoji before or inside a URL shifts every index by two.#[test]fn node_vectors_for_utf16_indices() {    assert_eq!(        find_links("\u{1f600} see https://a.com/x now"),        vec![link("https://a.com/x", 7, 22)]    );    assert_eq!(        find_links("https://a.com/\u{1f600}x"),        vec![link("https://a.com/\u{1f600}x", 0, 17)]    );    assert_eq!(        find_links("https://a.com/b\u{2026} and https://a.com/b\u{201d}"),        vec![            link("https://a.com/b", 0, 15),            link("https://a.com/b", 21, 36)        ]    );    // Two spellings of one URL de-dupe to one, because the trim ran first.    assert_eq!(        extract_message_urls("https://a.com/b\u{2026} and https://a.com/b\u{201d}"),        ["https://a.com/b"]    );    // The balanced-bracket pass keeps the wikipedia bracket and drops the    // sentence's, in one call.    assert_eq!(        find_links("read (https://en.wikipedia.org/wiki/Rust_(language))."),        vec![link("https://en.wikipedia.org/wiki/Rust_(language)", 6, 51)]    );    // A query and a fragment stay; the comma after them does not.    assert_eq!(        find_links("https://a.com/x?q=1&r=2#frag,"),        vec![link("https://a.com/x?q=1&r=2#frag", 0, 28)]    );    // A no-break space ENDS a URL, because ECMAScript's `\s` contains it.    assert_eq!(        find_links("https://a.com/b\u{a0}c"),        vec![link("https://a.com/b", 0, 15)]    );}/// The trim's own edge cases: brackets are counted across the WHOLE match, so a/// URL that opens as many as it closes keeps them all.#[test]fn node_vectors_for_the_trim() {    assert_eq!(        trim_trailing_punctuation("https://a.com/((b))"),        "https://a.com/((b))"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b))"),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/b'\u{2019}"),        "https://a.com/b"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/[x]"),        "https://a.com/[x]"    );    assert_eq!(        trim_trailing_punctuation("https://a.com/x]"),        "https://a.com/x"    );}#[test]fn node_vectors_for_segmenting_around_an_emoji() {    assert_eq!(        segment_links(&[MessageSegment::text("\u{1f600} https://a.com end")]),        vec![            MessageSegment::text("\u{1f600} "),            MessageSegment::link("https://a.com", "https://a.com"),            MessageSegment::text(" end"),        ]    );}