Cotton code
223 lines · 7.4 KB · RustRaw
//! `packages/core/__tests__/messages/editWindow.test.ts`, case for case, plus//! the Node vectors for the countdown.use super::*;const SENT: &str = "2026-08-26T10:00:00.000Z";fn mine<'a>() -> WindowSubject<'a> {    WindowSubject {        direction: MessageDirection::Out,        sent_at: SENT,        deleted_at: None,        origin: Some(MessageOrigin::Live),    }}// ── the windows ──────────────────────────────────────────────────────────────#[test]fn is_15_minutes_to_edit_and_48_hours_to_unsend() {    assert_eq!(EDIT_WINDOW_MS, 15 * 60 * 1000);    assert_eq!(DELETE_FOR_EVERYONE_WINDOW_MS, 48 * 60 * 60 * 1000);}// ── canEdit ──────────────────────────────────────────────────────────────────#[test]fn allows_my_own_live_message_inside_the_window_and_at_the_boundary() {    assert!(can_edit(&mine(), "2026-08-26T10:00:00.000Z"));    assert!(can_edit(&mine(), "2026-08-26T10:14:59.999Z"));    assert!(can_edit(&mine(), "2026-08-26T10:15:00.000Z"));}#[test]fn refuses_once_the_window_has_passed() {    assert!(!can_edit(&mine(), "2026-08-26T10:15:00.001Z"));}#[test]fn refuses_somebody_elses_message_a_deleted_one_and_an_imported_one() {    assert!(!can_edit(        &WindowSubject {            direction: MessageDirection::In,            ..mine()        },        SENT    ));    assert!(!can_edit(        &WindowSubject {            deleted_at: Some(SENT),            ..mine()        },        SENT    ));    assert!(!can_edit(        &WindowSubject {            origin: Some(MessageOrigin::Imported),            ..mine()        },        SENT    ));}#[test]fn refuses_a_message_whose_clock_is_ahead_of_ours_rather_than_opening_forever() {    assert!(!can_edit(        &WindowSubject {            sent_at: "2026-08-26T11:00:00.000Z",            ..mine()        },        SENT    ));}#[test]fn refuses_an_unparseable_timestamp_instead_of_throwing_at_a_bubble() {    assert!(!can_edit(        &WindowSubject {            sent_at: "whenever",            ..mine()        },        SENT    ));    assert!(!can_edit(&mine(), "whenever"));}// ── canDeleteForEveryone ─────────────────────────────────────────────────────#[test]fn allows_my_own_live_message_for_48_hours() {    assert!(can_delete_for_everyone(&mine(), "2026-08-28T10:00:00.000Z"));    assert!(!can_delete_for_everyone(        &mine(),        "2026-08-28T10:00:00.001Z"    ));}#[test]fn refuses_somebody_elses_message_and_one_already_deleted() {    assert!(!can_delete_for_everyone(        &WindowSubject {            direction: MessageDirection::In,            ..mine()        },        SENT    ));    assert!(!can_delete_for_everyone(        &WindowSubject {            deleted_at: Some(SENT),            ..mine()        },        SENT    ));}// ── editWindowRemainingMs ────────────────────────────────────────────────────#[test]fn counts_down_and_floors_at_zero() {    assert_eq!(edit_window_remaining_ms(SENT, SENT), EDIT_WINDOW_MS);    assert_eq!(        edit_window_remaining_ms(SENT, "2026-08-26T10:05:00.000Z"),        10 * 60 * 1000    );    assert_eq!(        edit_window_remaining_ms(SENT, "2026-08-26T11:00:00.000Z"),        0    );    assert_eq!(edit_window_remaining_ms("whenever", SENT), 0);}// ── Node vectors ─────────────────────────────────────────────────────────────/// Printed by running the TypeScript (`vec_editwindow.ts`), pasted verbatim.////// `Date.parse` is the part a Rust port can most easily get subtly wrong, and/// each of these names a spelling the file actually holds: an offset instant (a/// device that wrote its own zone), a second-precision stamp (what the v9/// migrator writes straight through SQL), a date-only stamp, and a fractional/// second longer than the grammar allows — every engine truncates the extra/// digits rather than rounding them, and 899 999 rather than 900 000 is what/// says so.#[test]fn node_vectors_for_the_countdown() {    // 2026-08-26T15:30:00+05:30 is 10:00:00Z — the same instant as SENT, so the    // whole window is still open. So is 04:30-05:30, from the other side.    assert_eq!(        edit_window_remaining_ms("2026-08-26T15:30:00+05:30", SENT),        900_000    );    assert_eq!(        edit_window_remaining_ms("2026-08-26T04:30:00-05:30", SENT),        900_000    );    // Seconds precision, five minutes in.    assert_eq!(        edit_window_remaining_ms("2026-08-26T09:55:00Z", SENT),        600_000    );    // `.9994` truncates to `.999`, so one millisecond of the window is gone —    // not zero, which a rounding parser would have produced.    assert_eq!(        edit_window_remaining_ms("2026-08-26T09:59:59.9994Z", SENT),        899_999    );    // A date-only stamp is midnight UTC — ten hours ago, so the window is shut.    assert_eq!(edit_window_remaining_ms("2026-08-26", SENT), 0);    assert_eq!(edit_window_remaining_ms("2026-08-25T10:00:00Z", SENT), 0);    // A clock far ahead of ours: the subtraction is not clamped, only the floor    // is, so the countdown is enormous — and `can_edit` refuses it anyway.    assert_eq!(        edit_window_remaining_ms("9999-12-31T00:00:00Z", SENT),        251_614_476_900_000    );}/// **The one place this port cannot match Node, said out loud.**////// A date-TIME with no zone designator (`2026-08-26T09:58:00.000`) is LOCAL time/// per the ECMAScript Date Time String Format, so `Date.parse` gives a different/// instant on every machine — on the Mac this was measured on (Asia/Calcutta) it/// is 04:28Z, and the edit window reads as long shut. A pure library has no local/// zone to read, so [`cotton_store::protocol::js::parse_date_ms`] takes the form/// as UTC and this port inherits that.////// It is not reachable from a file: every instant a Cotton row holds is written/// through the store's timestamp codec, which normalises to `…Z`. The vector/// above therefore uses only zone-explicit and date-only spellings, and this test/// pins the Rust reading so the divergence is a documented decision rather than a/// surprise the day something writes a naive stamp.#[test]fn a_zoneless_date_time_is_read_as_utc_here_and_as_local_time_in_node() {    assert_eq!(        edit_window_remaining_ms("2026-08-26T09:58:00.000", SENT),        780_000    );}/// The same parses, seen through the predicate a menu item is drawn from.#[test]fn node_vectors_for_the_windows() {    assert!(can_edit(        &WindowSubject {            sent_at: "2026-08-26T15:30:00+05:30",            ..mine()        },        SENT    ));    assert!(!can_edit(        &WindowSubject {            sent_at: "2026-08-26",            ..mine()        },        SENT    ));    assert!(can_delete_for_everyone(        &WindowSubject {            sent_at: "2026-08-26",            ..mine()        },        SENT    ));}