Cotton code
690 lines · 20.2 KB · RustRaw
//! Vectors for the three fingerprints, taken from the TypeScript's own suites//! (`messageFingerprint.test.ts`, `emailFingerprint.test.ts`,//! `statementFingerprint.test.ts`) and extended with cases computed by running//! the TypeScript.//!//! These are the highest-stakes vectors in the crate. A fingerprint that//! differs in one character makes every re-import mint the owner's whole//! message history a second time, and the v5 migration stop matching a fresh//! import — the txt2 algorithm is FROZEN (pinned 2026-08-25). If one of these//! fails, fix the regression, never the literal.use super::email::*;use super::message::*;use super::statement::*;fn fingerprint(sent_at: &str, body: &str, occurrence: u64) -> String {    content_fingerprint(&ContentFingerprintInput {        sent_at,        body,        occurrence,    })    .expect("a parseable instant")}fn email_fingerprint(sent_at: &str, subject: Option<&str>, body: &str, occurrence: u64) -> String {    email_content_fingerprint(&EmailContentFingerprintInput {        sent_at,        subject,        body,        occurrence,    })    .expect("a parseable instant")}fn statement(    account_key: &str,    posted_on: &str,    amount_minor: i64,    direction: Direction,    text: &str,    occurrence: u64,) -> String {    statement_line_fingerprint(&StatementLineFingerprintInput {        account_key,        posted_on,        amount_minor,        direction,        text,        occurrence,    })    .expect("a well-shaped line")}#[test]fn the_txt2_algorithm_is_frozen() {    // Pinned 2026-08-25 in the TypeScript, re-pinned here.    assert_eq!(        fingerprint(            "2025-07-26T04:42:45.000Z",            "Hey, are we still on for tomorrow?",            0        ),        "txt2:a8b76dfd3b44f33020ae85c5862bd139"    );}#[test]fn message_fingerprints_match_the_typescript() {    assert_eq!(        fingerprint(            "2025-07-26T04:42:45.000Z",            "Hey, are we still on for tomorrow?",            0        ),        "txt2:a8b76dfd3b44f33020ae85c5862bd139"    );    assert_eq!(        fingerprint("2025-07-26T04:42:45.000Z", "ok", 0),        "txt2:f178b349b21ccd96b2d8ca1953eb619a"    );    assert_eq!(        fingerprint("2025-07-26T04:42:12.000Z", "  ok ", 0),        "txt2:f178b349b21ccd96b2d8ca1953eb619a"    );    assert_eq!(        fingerprint("2025-07-26T04:42:45.000Z", "ok", 1),        "txt2:715b108d5c155eef93e897822bb49e65"    );    assert_eq!(        fingerprint("2026-01-01T00:00:00.000Z", "", 0),        "txt2:76fdf9f98e7d3ca9bf6bf9ece847e9c4"    );    assert_eq!(        fingerprint("2026-01-01T00:00:00.000Z", "quote \" and \\ backslash", 0),        "txt2:6450437c870227e46750d4d6821a75dd"    );    assert_eq!(        fingerprint("2026-01-01T00:00:00.000Z", "emoji \u{1f9f5} tail", 0),        "txt2:340b88f0ac247383335a3826a30839a9"    );    assert_eq!(        fingerprint(            "2026-01-01T00:00:00.000Z",            "\u{65e5}\u{672c}\u{8a9e}\u{306e}\u{30c6}\u{30ad}\u{30b9}\u{30c8}",            3        ),        "txt2:4e1d09dc7f88fe051f61d8a9c6f42f55"    );}#[test]fn minute_bucket_matches_the_typescript() {    assert_eq!(        minute_bucket("2025-07-26T04:42:45.000Z").expect("parses"),        "2025-07-26T04:42"    );    assert_eq!(        minute_bucket("2025-07-26T04:42:12.000Z").expect("parses"),        "2025-07-26T04:42"    );    assert_eq!(        minute_bucket("2025-07-26T04:42:45.123+05:30").expect("parses"),        "2025-07-25T23:12"    );    assert_eq!(        minute_bucket("2026-08-29T10:15:30.000Z").expect("parses"),        "2026-08-29T10:15"    );    assert_eq!(        minute_bucket("2026-08-29T10:15:59.000Z").expect("parses"),        "2026-08-29T10:15"    );    assert_eq!(        minute_bucket("2026-08-29T10:16:00.000Z").expect("parses"),        "2026-08-29T10:16"    );    assert_eq!(        minute_bucket("1970-01-01T00:00:00.000Z").expect("parses"),        "1970-01-01T00:00"    );    assert_eq!(        minute_bucket("2026-02-29T12:00:00.000Z").expect("parses"),        "2026-03-01T12:00"    );    assert_eq!(        minute_bucket("2024-02-29T12:00:00.000Z").expect("parses"),        "2024-02-29T12:00"    );    assert_eq!(        minute_bucket("2026-12-31T23:59:59.999Z").expect("parses"),        "2026-12-31T23:59"    );    assert_eq!(        minute_bucket("2026-01-01T00:00:00.000-05:00").expect("parses"),        "2026-01-01T05:00"    );    // An unparseable instant refuses, naming itself.    let error = minute_bucket("nope").expect_err("refuses");    assert!(        error.to_string().contains("not a parseable instant"),        "got {error}"    );}#[test]fn normalize_body_matches_the_typescript() {    assert_eq!(normalize_body_for_id("Hello\n  world "), "Hello world");    assert_eq!(normalize_body_for_id("  ok "), "ok");    assert_eq!(normalize_body_for_id("ok"), "ok");    assert_eq!(normalize_body_for_id("a\tb\r\nc"), "a b c");    assert_eq!(        normalize_body_for_id("\u{a0}nbsp\u{a0}here\u{a0}"),        "nbsp here"    );    assert_eq!(normalize_body_for_id("\u{feff}bom\u{feff}"), "bom");    assert_eq!(        normalize_body_for_id("emoji \u{1f9f5} and \u{65e5}\u{672c}\u{8a9e}"),        "emoji \u{1f9f5} and \u{65e5}\u{672c}\u{8a9e}"    );    assert_eq!(normalize_body_for_id("   "), "");    assert_eq!(normalize_body_for_id(""), "");    assert_eq!(        normalize_body_for_id("one\u{2028}two\u{2029}three"),        "one two three"    );    assert_eq!(normalize_body_for_id("trail\u{3000}"), "trail");}#[test]fn the_occurrence_counter_walks_the_same_sequence_every_run() {    let run = || {        let mut counter = ContentOccurrenceCounter::new();        let walk: Vec<u64> = [            ("2026-07-01T12:02:09.000Z", "ok"),            ("2026-07-01T12:02:09.000Z", "ok"),            ("2026-07-01T12:02:59.000Z", "ok"),            ("2026-07-01T12:02:09.000Z", "ok!"),            ("2026-07-01T12:03:00.000Z", "ok"),        ]        .into_iter()        .map(|(sent_at, body)| counter.next(sent_at, body).expect("parses"))        .collect();        walk    };    assert_eq!(run(), vec![0, 1, 2, 0, 0]);    assert_eq!(run(), run());}#[test]fn a_message_fingerprint_carries_no_identity() {    // Two different senders, same content, same minute — the SAME fingerprint.    // There is no sender parameter to pass, which is the whole point.    let a = fingerprint("2025-07-26T04:42:45.000Z", "ok", 0);    let b = fingerprint("2025-07-26T04:42:12.000Z", "  ok ", 0);    assert_eq!(a, b);    assert!(a.starts_with(CONTENT_EXTERNAL_ID_PREFIX));    assert_eq!(a.len(), CONTENT_EXTERNAL_ID_PREFIX.len() + 32);}#[test]fn message_id_external_ids_match_the_typescript() {    assert_eq!(        message_id_external_id(Some("CAF+abc123@mail.gmail.com")),        Some("mid:fa494051d5f05e045bcd7d87dc9fcdbc".to_string())    );    assert_eq!(        message_id_external_id(Some("<CAF+abc123@mail.gmail.com>")),        Some("mid:fa494051d5f05e045bcd7d87dc9fcdbc".to_string())    );    assert_eq!(        message_id_external_id(Some("  <caf+ABC123@Mail.Gmail.com>  ")),        Some("mid:fa494051d5f05e045bcd7d87dc9fcdbc".to_string())    );    assert_eq!(        message_id_external_id(Some("a@x.com")),        Some("mid:8d69993e730219070a760f0c9f62503f".to_string())    );    assert_eq!(        message_id_external_id(Some("b@x.com")),        Some("mid:bad4a07eea202d4f12b785505b8bdc09".to_string())    );    assert_eq!(message_id_external_id(Some("<>")), None);    assert_eq!(message_id_external_id(Some("   ")), None);    assert_eq!(        message_id_external_id(Some("<<double>>")),        Some("mid:21929361169bc2f0e1755a1df228082c".to_string())    );    assert_eq!(message_id_external_id(None), None);}#[test]fn normalize_message_id_matches_the_typescript() {    assert_eq!(normalize_message_id("<A@B.c>"), "a@b.c");    assert_eq!(normalize_message_id("  <A@B.c>  "), "a@b.c");    assert_eq!(normalize_message_id("<<A@B.c>>"), "<a@b.c>");    assert_eq!(normalize_message_id("A@B.c"), "a@b.c");}#[test]fn strip_reply_prefixes_matches_the_typescript() {    assert_eq!(strip_reply_prefixes("Re: Re: FWD: The plan"), "The plan");    assert_eq!(strip_reply_prefixes("Fw: Re [2]: budget"), "budget");    assert_eq!(        strip_reply_prefixes("  Regarding the plan "),        "Regarding the plan"    );    assert_eq!(strip_reply_prefixes("Re: The PLAN"), "The PLAN");    assert_eq!(strip_reply_prefixes("RE:RE:RE: nested"), "nested");    assert_eq!(strip_reply_prefixes("Fwd:  spaced   out "), "spaced out");    assert_eq!(strip_reply_prefixes("re [12] : bracket"), "bracket");    assert_eq!(        strip_reply_prefixes("Reply: not a prefix"),        "Reply: not a prefix"    );    assert_eq!(strip_reply_prefixes(""), "");    assert_eq!(strip_reply_prefixes("Re:"), "");}#[test]fn normalize_subject_matches_the_typescript() {    assert_eq!(normalize_subject_for_id(Some("Re: The PLAN")), "the plan");    assert_eq!(        normalize_subject_for_id(Some("FWD: Re: quarterly   invoice")),        "quarterly invoice"    );    assert_eq!(        normalize_subject_for_id(Some("Re: Quarterly invoice")),        "quarterly invoice"    );    assert_eq!(        normalize_subject_for_id(Some("Reply: not a prefix")),        "reply: not a prefix"    );    assert_eq!(normalize_subject_for_id(None), "");}#[test]fn email_fingerprints_match_the_typescript() {    assert_eq!(        email_fingerprint(            "2026-08-29T10:15:30.000Z",            Some("Re: Quarterly invoice"),            "Please find the invoice attached.\nThanks!",            0        ),        "eml2:bf74808ed252aa983689bbf38b8797aa"    );    assert_eq!(        email_fingerprint(            "2026-08-29T10:15:59.000Z",            Some("Re: Quarterly invoice"),            "Please find the invoice attached.\nThanks!",            0        ),        "eml2:bf74808ed252aa983689bbf38b8797aa"    );    assert_eq!(        email_fingerprint(            "2026-08-29T10:16:00.000Z",            Some("Re: Quarterly invoice"),            "Please find the invoice attached.\nThanks!",            0        ),        "eml2:13f7ff3333a7388fa8521d94fd987e9b"    );    assert_eq!(        email_fingerprint(            "2026-08-29T10:15:30.000Z",            Some("FWD: Re: quarterly   invoice"),            "Please find the invoice attached. Thanks!",            0        ),        "eml2:bf74808ed252aa983689bbf38b8797aa"    );    assert_eq!(        email_fingerprint(            "2026-08-29T10:15:30.000Z",            Some("Re: Quarterly invoice"),            "Different words.",            0        ),        "eml2:3744c4793eaa7a1e57e404060011a5a8"    );    assert_eq!(        email_fingerprint(            "2026-08-29T10:15:30.000Z",            Some("Re: Quarterly invoice"),            "Please find the invoice attached.\nThanks!",            1        ),        "eml2:4cda7e72ebb54d70969a764d7ee14b92"    );    assert_eq!(        email_fingerprint("2026-08-29T10:15:30.000Z", None, "no subject at all", 0),        "eml2:48e35a041fffa05fca10ce37c1649617"    );}#[test]fn an_email_fingerprint_carries_no_identity_either() {    let base = email_fingerprint(        "2026-08-29T10:15:30.000Z",        Some("Re: Quarterly invoice"),        "Please find the invoice attached.\nThanks!",        0,    );    assert!(base.starts_with(EMAIL_CONTENT_EXTERNAL_ID_PREFIX));    assert_eq!(base.len(), EMAIL_CONTENT_EXTERNAL_ID_PREFIX.len() + 32);    // The minute bucket, the reply chain and the whitespace all fold away.    assert_eq!(        base,        email_fingerprint(            "2026-08-29T10:15:59.000Z",            Some("FWD: Re: quarterly   invoice"),            "Please find the invoice attached. Thanks!",            0        )    );}#[test]fn the_email_counter_counts_identical_triples_in_walk_order() {    let mut counter = EmailOccurrenceCounter::new();    assert_eq!(        counter            .next("2026-08-29T10:15:00Z", Some("Hi"), "Same")            .expect("parses"),        0    );    assert_eq!(        counter            .next("2026-08-29T10:15:00Z", Some("Hi"), "Same")            .expect("parses"),        1    );    assert_eq!(        counter            .next("2026-08-29T10:15:00Z", Some("Hi"), "Other")            .expect("parses"),        0    );}#[test]fn normalise_line_text_matches_the_typescript() {    assert_eq!(        normalise_line_text("  UPI/DR/408   CORNER\tSTORE \n"),        "upi/dr/408 corner store"    );    assert_eq!(        normalise_line_text("NEFT-CR-XYZ0001234"),        "neft-cr-xyz0001234"    );    assert_eq!(        normalise_line_text("  UPI/DR/408123456789/CORNER  STORE "),        "upi/dr/408123456789/corner store"    );    assert_eq!(        normalise_line_text("UPI/DR/408123456789/CORNER STORE"),        "upi/dr/408123456789/corner store"    );    assert_eq!(        normalise_line_text("MiXeD CaSe \u{a0} NBSP"),        "mixed case nbsp"    );}#[test]fn statement_account_keys_match_the_typescript() {    assert_eq!(        statement_line_account_key(Some("Example Bank"), Some("4821"), "stmt-1"),        "example bank|4821"    );    assert_eq!(        statement_line_account_key(Some("Example Bank"), Some("XX4821"), "stmt-1"),        "example bank|4821"    );    assert_eq!(        statement_line_account_key(Some("Example Bank"), Some("**** 4821"), "stmt-1"),        "example bank|4821"    );    assert_eq!(        statement_line_account_key(Some("EXAMPLE BANK "), Some(" 4821"), "stmt-1"),        "example bank|4821"    );    assert_eq!(        statement_line_account_key(None, Some("4821"), "stmt-1"),        "stmt-1"    );    assert_eq!(        statement_line_account_key(Some("Example Bank"), None, "stmt-1"),        "stmt-1"    );    assert_eq!(statement_line_account_key(None, None, "stmt-1"), "stmt-1");    assert_eq!(        statement_line_account_key(Some("Example Bank"), Some("****"), "stmt-1"),        "stmt-1"    );    assert_eq!(        statement_line_account_key(Some("  "), Some("4821"), "stmt-1"),        "stmt-1"    );}#[test]fn statement_fingerprints_match_the_typescript() {    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0        ),        "stl:f5d6619e19d1b365731f20f01967e1c9"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "  UPI/DR/408123456789/CORNER  STORE ",            0        ),        "stl:f5d6619e19d1b365731f20f01967e1c9"    );    assert_eq!(        statement(            "other bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0        ),        "stl:7b0175b5ea8e25086685a03b0c5e6cff"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-03",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0        ),        "stl:51979cc9e7a79c4ac0ab63525d29190f"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120001,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0        ),        "stl:f45b976391f42dd6bc3af17963136d9c"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::In,            "UPI/DR/408123456789/CORNER STORE",            0        ),        "stl:47c0d09f5e2416870c0f25f8fac74fc5"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/OTHER STORE",            0        ),        "stl:98e8016b5aef2818e9fdd42e96a48fcd"    );    assert_eq!(        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            1        ),        "stl:facd0cfc7787c186a505734f22de8470"    );    assert_eq!(        statement("a|b", "2026-03-02", 120000, Direction::Out, "c", 0),        "stl:16d29d5a5809324f042fe88ade2111ad"    );    assert_eq!(        statement("a", "2026-03-02", 120000, Direction::Out, "b|c", 0),        "stl:1e940c452714f43758574e45592cd565"    );    assert_eq!(        statement("stmt-1", "2026-03-02", 1, Direction::In, "", 0),        "stl:93c8568315fb8fc43c169f0da45d5ff7"    );}#[test]fn a_statement_fingerprint_separates_every_part_that_means_something() {    let variants = [        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0,        ),        statement(            "other bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0,        ),        statement(            "example bank|4821",            "2026-03-03",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0,        ),        statement(            "example bank|4821",            "2026-03-02",            120001,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            0,        ),        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::In,            "UPI/DR/408123456789/CORNER STORE",            0,        ),        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/OTHER STORE",            0,        ),        statement(            "example bank|4821",            "2026-03-02",            120000,            Direction::Out,            "UPI/DR/408123456789/CORNER STORE",            1,        ),    ];    let unique: std::collections::BTreeSet<&String> = variants.iter().collect();    assert_eq!(unique.len(), 7);}#[test]fn a_statement_fingerprint_cannot_be_forged_by_a_text_spelling_another_line() {    // JSON, not a delimiter join: no value can smuggle the separator.    assert_ne!(        statement("a|b", "2026-03-02", 120000, Direction::Out, "c", 0),        statement("a", "2026-03-02", 120000, Direction::Out, "b|c", 0)    );}#[test]fn a_statement_line_refuses_an_amount_that_is_not_whole_positive_paise() {    for amount in [0, -1] {        assert!(            statement_line_fingerprint(&StatementLineFingerprintInput {                account_key: "example bank|4821",                posted_on: "2026-03-02",                amount_minor: amount,                direction: Direction::Out,                text: "x",                occurrence: 0,            })            .is_err(),            "amount {amount} should refuse"        );    }}#[test]fn a_statement_line_refuses_a_posted_date_that_is_not_an_iso_day() {    let error = statement_line_fingerprint(&StatementLineFingerprintInput {        account_key: "example bank|4821",        posted_on: "02/03/2026",        amount_minor: 1,        direction: Direction::Out,        text: "x",        occurrence: 0,    })    .expect_err("refuses");    assert!(error.to_string().contains("ISO date"), "got {error}");}#[test]fn the_statement_counter_keys_on_the_same_material_the_hash_does() {    let line = |text: &'static str| StatementLineFingerprintInput {        account_key: "example bank|4821",        posted_on: "2026-03-02",        amount_minor: 120000,        direction: Direction::Out,        text,        occurrence: 0,    };    let mut one = StatementLineOccurrenceCounter::new();    let mut two = StatementLineOccurrenceCounter::new();    one.next(&line("UPI/DR/408123456789/CORNER STORE"));    // A re-read that rewraps the text lands on the SAME counter slot.    assert_eq!(two.next(&line("  UPI/DR/408123456789/CORNER  STORE ")), 0);    assert_eq!(one.next(&line("  UPI/DR/408123456789/CORNER  STORE ")), 1);}