use super::{EMAIL_REGEX, Emails, extract_all_regex, extract_regex, validate::validate_url};
use crate::Info;
use url::Url;

#[test]
fn extract_regex_returns_first_match() {
    let found = extract_regex(
        "email first@example.com and second@example.com",
        EMAIL_REGEX,
    )
    .unwrap();

    assert_eq!(found, "first@example.com");
}

#[test]
fn extract_all_regex_errors_when_no_match() {
    assert!(extract_all_regex("no email here", EMAIL_REGEX).is_err());
}

#[test]
fn emails_post_process_sorts_and_deduplicates() {
    let mut emails = Emails {
        emails: vec!["z@example.com".into()],
    };

    emails
        .post_process("Contact a@example.com and z@example.com")
        .unwrap();

    assert_eq!(emails.emails, ["a@example.com", "z@example.com"]);
}

#[test]
fn validate_url_accepts_only_http_urls_with_hosts() {
    assert!(validate_url(&Url::parse("https://example.com").unwrap()).is_ok());
    assert!(validate_url(&Url::parse("ftp://example.com").unwrap()).is_err());
    assert!(validate_url(&Url::parse("mailto:test@example.com").unwrap()).is_err());
}
