Cotton code
139 lines · 5.9 KB · RustRaw
//! `packages/core/__tests__/messages/unread.test.ts`, case for case.use super::*;fn seqs(values: &[i64]) -> Vec<Option<i64>> {    values.iter().map(|value| Some(*value)).collect()}// ── unreadCount ──────────────────────────────────────────────────────────────#[test]fn counts_inbound_live_rows_past_the_cursor() {    assert_eq!(unread_count(2, &seqs(&[1, 2, 3, 4])), 2);    assert_eq!(unread_count(0, &seqs(&[1, 2, 3])), 3);    assert_eq!(unread_count(9, &seqs(&[1, 2, 3])), 0);    assert_eq!(unread_count(0, &[]), 0);}/// RULE: A message the server has not numbered yet is never counted as unread./// seq is NULL until the server folds the row. A NULL is not "seq 0" and it is/// not ahead of any cursor — it is a row nobody else has seen yet.#[test]fn never_counts_a_row_the_server_has_not_numbered() {    assert_eq!(unread_count(2, &[Some(1), Some(2), None, None, Some(3)]), 1);    assert_eq!(unread_count(0, &[None, None]), 0);}// ── firstUnreadSeq ───────────────────────────────────────────────────────────#[test]fn is_where_the_divider_goes_and_none_when_there_is_nothing_new() {    assert_eq!(first_unread_seq(2, &seqs(&[1, 2, 3, 4])), Some(3));    assert_eq!(first_unread_seq(2, &seqs(&[4, 1, 3, 2])), Some(3));    assert_eq!(first_unread_seq(9, &seqs(&[1, 2, 3])), None);    assert_eq!(first_unread_seq(0, &[None]), None);}// ── nextReadSeq ──────────────────────────────────────────────────────────────#[test]fn only_ever_moves_forward() {    assert_eq!(next_read_seq(2, 5), 5);    assert_eq!(next_read_seq(5, 2), 5);    assert_eq!(next_read_seq(0, 0), 0);    assert_eq!(next_read_seq(5, -1), 5);}// ── isThreadUnread ───────────────────────────────────────────────────────────#[test]fn is_true_when_rows_are_unread_or_when_the_person_said_so() {    assert!(is_thread_unread(&ThreadUnread {        read_seq: 2,        unread_marked_at: None,        unread_count: 3,    }));    assert!(!is_thread_unread(&ThreadUnread {        read_seq: 9,        unread_marked_at: None,        unread_count: 0,    }));    assert!(is_thread_unread(&ThreadUnread {        read_seq: 9,        unread_marked_at: Some("2026-08-26T10:00:00.000Z"),        unread_count: 0,    }));}// ── isMuted ──────────────────────────────────────────────────────────────────const NOW: &str = "2026-08-26T10:00:00.000Z";#[test]fn is_false_when_nothing_is_set() {    assert!(!is_muted(None, NOW));}#[test]fn is_true_until_the_instant_passes() {    assert!(is_muted(Some("2026-08-26T18:00:00.000Z"), NOW));    assert!(!is_muted(Some("2026-08-26T10:00:00.000Z"), NOW));    // A mute that expired a millisecond ago is over.    assert!(!is_muted(Some("2026-08-26T09:59:59.999Z"), NOW));}#[test]fn is_true_forever_for_the_frozen_far_date() {    assert!(is_muted(Some(MUTE_ALWAYS_UNTIL), NOW));    assert!(is_muted(        Some(MUTE_ALWAYS_UNTIL),        "2099-01-01T00:00:00.000Z"    ));    assert!(is_muted_forever(Some(MUTE_ALWAYS_UNTIL)));    // The SAME instant, spelled the way the store spells it. The file's timestamp    // codec normalises every value to `.000Z`, so a mute written through the store    // comes back in this form while a file the v9 migrator touched holds the    // constant's own spelling — one predicate has to answer yes to both, or    // "Muted" reads as "Muted until 9999" on half the rows.    assert!(is_muted_forever(Some("9999-12-31T00:00:00.000Z")));    assert!(!is_muted_forever(Some("2026-08-26T18:00:00.000Z")));    assert!(!is_muted_forever(Some("not a date")));    assert!(!is_muted_forever(None));}#[test]fn is_false_rather_than_throwing_on_a_timestamp_that_will_not_parse() {    assert!(!is_muted(Some("forever"), NOW));}// ── Node vectors ─────────────────────────────────────────────────────────────/// Printed by running the TypeScript (`vec_unread.ts`), pasted verbatim.////// The sentinel is the point: it reaches a file in several spellings and the/// predicate is an INSTANT comparison, so every spelling of midnight on/// 9999-12-31 UTC must answer yes and every neighbouring instant must answer no./// The offset spelling is the one a hand-written string comparison gets wrong.#[test]fn node_vectors_for_the_forever_sentinel() {    assert!(is_muted_forever(Some("9999-12-31T00:00:00Z")));    assert!(is_muted_forever(Some("9999-12-31T00:00:00.000Z")));    assert!(is_muted_forever(Some("9999-12-31T05:30:00+05:30")));    // A DATE-ONLY stamp is midnight UTC, which IS the sentinel instant — so the    // bare date answers yes as well. Node says so; reasoning about the constant's    // spelling rather than its instant would have said no.    assert!(is_muted_forever(Some("9999-12-31")));    assert!(!is_muted_forever(Some("9999-12-31T00:00:00.001Z")));    assert!(!is_muted_forever(Some("")));}/// And the same instants through `isMuted`, which must not fall back to the/// ordinary `until > now` comparison for a sentinel it recognised.#[test]fn node_vectors_for_is_muted() {    assert!(is_muted(Some("9999-12-31T05:30:00+05:30"), NOW));    assert!(is_muted(Some("9999-12-31"), NOW));    assert!(!is_muted(Some("2026-08-26T10:00:00.001+05:30"), NOW));    assert!(is_muted(Some("2026-08-26T16:00:00.000+05:30"), NOW));}