//! The journal date rules, with the zone as an argument rather than a process//! variable, and vector tables covering each zone in turn.usesuper::*;const VECTORS: &[(&str, i64, &str)] = &[ ("UTC", 1_783_188_000_000, "2026-07-04"), ("UTC", 1_783_207_800_000, "2026-07-04"), ("UTC", 1_767_225_600_000, "2026-01-01"), ("UTC", 1_798_761_599_000, "2026-12-31"), ("UTC", 1_774_747_800_000, "2026-03-29"), ("UTC", 1_792_891_800_000, "2026-10-25"), ("UTC", 928_584_000_000, "1999-06-05"), ("UTC", 0, "1970-01-01"), ("Asia/Kolkata", 1_783_188_000_000, "2026-07-04"), ("Asia/Kolkata", 1_783_207_800_000, "2026-07-05"), ("Asia/Kolkata", 1_767_225_600_000, "2026-01-01"), ("Asia/Kolkata", 1_798_761_599_000, "2027-01-01"), ("Asia/Kolkata", 1_774_747_800_000, "2026-03-29"), ("Asia/Kolkata", 1_792_891_800_000, "2026-10-25"), ("Asia/Kolkata", 928_584_000_000, "1999-06-05"), ("Asia/Kolkata", 0, "1970-01-01"), ("America/New_York", 1_783_188_000_000, "2026-07-04"), ("America/New_York", 1_783_207_800_000, "2026-07-04"), ("America/New_York", 1_767_225_600_000, "2025-12-31"), ("America/New_York", 1_798_761_599_000, "2026-12-31"), ("America/New_York", 1_774_747_800_000, "2026-03-28"), ("America/New_York", 1_792_891_800_000, "2026-10-24"), ("America/New_York", 928_584_000_000, "1999-06-05"),// The one reading that is not merely a different DAY: an instant of 0 is// still 1969 in New York. ("America/New_York", 0, "1969-12-31"), ("Europe/London", 1_783_188_000_000, "2026-07-04"), ("Europe/London", 1_783_207_800_000, "2026-07-05"), ("Europe/London", 1_767_225_600_000, "2026-01-01"), ("Europe/London", 1_798_761_599_000, "2026-12-31"), ("Europe/London", 1_774_747_800_000, "2026-03-29"), ("Europe/London", 1_792_891_800_000, "2026-10-25"), ("Europe/London", 928_584_000_000, "1999-06-05"), ("Europe/London", 0, "1970-01-01"), ("Australia/Sydney", 1_783_188_000_000, "2026-07-05"), ("Australia/Sydney", 1_783_207_800_000, "2026-07-05"), ("Australia/Sydney", 1_767_225_600_000, "2026-01-01"), ("Australia/Sydney", 1_798_761_599_000, "2027-01-01"), ("Australia/Sydney", 1_774_747_800_000, "2026-03-29"), ("Australia/Sydney", 1_792_891_800_000, "2026-10-25"), ("Australia/Sydney", 928_584_000_000, "1999-06-05"), ("Australia/Sydney", 0, "1970-01-01"),];const PARSE_VECTORS: &[(&str, &str, i64)] = &[ ("UTC", "2026-07-04", 1_783_123_200_000), ("UTC", "2026-01-01", 1_767_225_600_000), ("UTC", "2026-03-29", 1_774_742_400_000), ("UTC", "2026-10-25", 1_792_886_400_000), ("UTC", "2026-12-31", 1_798_675_200_000), ("Asia/Kolkata", "2026-07-04", 1_783_103_400_000), ("Asia/Kolkata", "2026-01-01", 1_767_205_800_000), ("Asia/Kolkata", "2026-03-29", 1_774_722_600_000), ("Asia/Kolkata", "2026-10-25", 1_792_866_600_000), ("Asia/Kolkata", "2026-12-31", 1_798_655_400_000), ("America/New_York", "2026-07-04", 1_783_137_600_000), ("America/New_York", "2026-01-01", 1_767_243_600_000), ("America/New_York", "2026-03-29", 1_774_756_800_000), ("America/New_York", "2026-10-25", 1_792_900_800_000), ("America/New_York", "2026-12-31", 1_798_693_200_000), ("Europe/London", "2026-07-04", 1_783_119_600_000), ("Europe/London", "2026-01-01", 1_767_225_600_000),// 2026-03-29 is Britain's spring forward; its local midnight still exists// (the clocks jump at 01:00), so this is an ordinary day. ("Europe/London", "2026-03-29", 1_774_742_400_000), ("Europe/London", "2026-10-25", 1_792_882_800_000), ("Europe/London", "2026-12-31", 1_798_675_200_000), ("Australia/Sydney", "2026-07-04", 1_783_087_200_000), ("Australia/Sydney", "2026-01-01", 1_767_186_000_000), ("Australia/Sydney", "2026-03-29", 1_774_702_800_000), ("Australia/Sydney", "2026-10-25", 1_792_846_800_000), ("Australia/Sydney", "2026-12-31", 1_798_635_600_000),];#[test]fn formats_an_instant_as_the_local_yyyy_mm_dd() {// `new Date(2026, 6, 4, 23, 30)` in the TypeScript, spelled as the instant// that is 23:30 on 4 July in Asia/Kolkata. assert_eq!( journal_date_key(1_783_186_800_000, "Asia/Kolkata").as_deref(),Ok("2026-07-04") );}#[test]fn journal_date_key_matches_the_typescript() {for (zone, instant, expected) in VECTORS { assert_eq!( journal_date_key(*instant, zone).as_deref(),Ok(*expected),"for {zone} at {instant}" ); }}#[test]fn refuses_a_zone_it_does_not_know_rather_than_guessing_utc() { assert!(journal_date_key(0, "Mars/Olympus").is_err());}#[test]fn compares_by_calendar_day() {// 01:00 and 23:00 on the same Kolkata day.let one_am = 1_783_105_200_000;let eleven_pm = 1_783_184_400_000; assert_eq!( is_same_journal_date(one_am, eleven_pm, "Asia/Kolkata"),Ok(true) ); assert_eq!( is_same_journal_date(one_am, eleven_pm + 7_200_000, "Asia/Kolkata"),Ok(false) );}#[test]fn round_trips_with_journal_date_key() {for (zone, key, _) in PARSE_VECTORS {let instant = parse_journal_date(key, zone).expect("a well-shaped key"); assert_eq!(journal_date_key(instant, zone).as_deref(), Ok(*key)); }}#[test]fn parse_journal_date_matches_the_typescript() {for (zone, key, expected) in PARSE_VECTORS { assert_eq!( parse_journal_date(key, zone),Ok(*expected),"for {zone} {key}" ); }}#[test]fn refuses_a_malformed_key() {for key in ["2026/07/04","not-a-date","","2026-7-4","20260704","2026-07-04T00:00:00Z", ] { assert!( matches!( parse_journal_date(key, "UTC"),Err(JournalDateError::Key(_)) ),"for {key:?}" ); }}/// The error NAMES the key it could not read, the way the TypeScript's thrown/// message does — a caller reporting "invalid date" with no value is a bug/// report nobody can act on.#[test]fn the_refusal_names_the_key() {letErr(JournalDateError::Key(err)) = parse_journal_date("2026/07/04", "UTC") else { panic!("a malformed key is refused"); }; assert_eq!(err.to_string(), "Invalid journal date key: \"2026/07/04\"");}// ── The streak ────────────────────────────────────────────────────────────//// IST is the zone where a UTC day boundary silently moves a journal page to// the wrong date. The zone is an argument here, so these cases run without a// process variable — and the two IST-evening cases, added after a shipped// defect, are the ones that would go red if this crate ever fell back to UTC.//// `dayKey` has no port: `journal_date_key` above IS it, and the vectors at the// top of this file already hold the local-versus-UTC rule its three cases// asserted./// Saturday 8 August 2026, 10:00 IST — the TypeScript's frozen `NOW`.const NOW_IST: i64 = 1_786_163_400_000;const IST: &str = "Asia/Kolkata";fn days(keys: &[&str]) -> Vec<String> { keys.iter().map(|key| (*key).to_string()).collect()}fn streak(keys: &[&str], now_ms: i64) -> usize { journal_streak(&days(keys), now_ms, IST).expect("the zone is readable")}fn page(kind: &str, body: Option<&str>, day: Option<&str>) -> JournalPage {JournalPage { kind: kind.to_string(), body: body.map(str::to_string), journal_date: day.map(str::to_string), }}fn written(day: &str) -> JournalPage { page("journal", Some("Something happened."), Some(day))}#[test]fn a_streak_is_zero_when_nothing_has_been_written() { assert_eq!(streak(&[], NOW_IST), 0);}#[test]fn a_streak_counts_a_run_ending_today() { assert_eq!( streak(&["2026-08-06", "2026-08-07", "2026-08-08"], NOW_IST),3 );}/// Yesterday still counts: today is not over, and a streak that "breaks" at/// midnight would be a lie told at 00:01.#[test]fn a_streak_counts_a_run_ending_yesterday() { assert_eq!(streak(&["2026-08-06", "2026-08-07"], NOW_IST), 2);}#[test]fn a_run_that_ended_before_yesterday_is_over() { assert_eq!(streak(&["2026-08-05", "2026-08-06"], NOW_IST), 0);}#[test]fn a_streak_stops_at_the_first_gap_rather_than_counting_every_written_day() {// The old run of two is over; only the 7th and the 8th are current. assert_eq!( streak( &["2026-08-01", "2026-08-02", "2026-08-07", "2026-08-08"], NOW_IST ),2 );}#[test]fn one_written_day_is_a_streak_of_one() { assert_eq!(streak(&["2026-08-08"], NOW_IST), 1);}#[test]fn a_duplicate_date_counts_once() { assert_eq!( streak(&["2026-08-08", "2026-08-08", "2026-08-07"], NOW_IST),2 );}/// An unparseable key is IGNORED rather than breaking the run — a bad row in/// the middle of a good week is a data defect, not a lost streak.#[test]fn an_unparseable_date_is_ignored_rather_than_breaking_the_run() { assert_eq!( streak(&["2026-08-08", "not-a-date", "", "2026-08-07"], NOW_IST),2 );}#[test]fn a_future_date_does_not_start_a_streak() { assert_eq!(streak(&["2026-08-09", "2026-08-10"], NOW_IST), 0);}#[test]fn a_streak_counts_across_a_month_boundary() {// 1 September 2026, 10:00 IST. assert_eq!( streak( &["2026-08-30", "2026-08-31", "2026-09-01"],1_788_237_000_000 ),3 );}#[test]fn a_streak_counts_across_a_year_boundary() {// 1 January 2027, 10:00 IST. assert_eq!(streak(&["2026-12-31", "2027-01-01"], 1_798_777_800_000), 2);}#[test]fn written_today_asks_the_same_set_differently() {let today = |keys: &[&str]| written_today(&days(keys), NOW_IST, IST).expect("readable zone"); assert!(today(&["2026-08-08"])); assert!(!today(&["2026-08-07"])); assert!(!today(&[]));}#[test]fn a_written_day_is_a_journal_page_with_a_day_and_a_body() { assert_eq!( written_journal_days(&[written("2026-08-08")]), vec!["2026-08-08".to_string()] );}/// A plain note is not a page of the journal.#[test]fn a_plain_note_is_not_a_written_day() { assert!(written_journal_days(&[page("note", Some("x"), Some("2026-08-08"))]).is_empty());}/// Opening a page is not writing a day.#[test]fn an_empty_page_is_not_a_written_day() {for body in [Some(""), Some(" \n "), None] { assert!( written_journal_days(&[page("journal", body, Some("2026-08-08"))]).is_empty(),"{body:?} counted as a written day" ); }}#[test]fn a_page_with_no_day_has_no_identity_to_count() { assert!(written_journal_days(&[page("journal", Some("x"), None)]).is_empty());}#[test]fn every_written_day_is_kept_in_order_duplicates_and_all() { assert_eq!( written_journal_days(&[ written("2026-08-08"), page("note", Some("x"), Some("2026-08-07")), written("2026-08-07"), written("2026-08-07"), ]), days(&["2026-08-08", "2026-08-07", "2026-08-07"]) );}fn state(pages: &[JournalPage], now_ms: i64) -> JournalDayState { journal_day_state(pages, now_ms, IST).expect("the zone is readable")}#[test]fn the_day_state_reports_today_written_and_the_run_behind_it() { assert_eq!( state( &[ written("2026-08-08"), written("2026-08-07"), written("2026-08-06") ], NOW_IST ),JournalDayState { written_today: true, streak: 3 } );}#[test]fn the_day_state_reports_today_unwritten_while_a_run_still_stands() { assert_eq!( state(&[written("2026-08-07"), written("2026-08-06")], NOW_IST),JournalDayState { written_today: false, streak: 2 } );}#[test]fn the_day_state_is_the_empty_state_on_day_zero() { assert_eq!( state(&[], NOW_IST),JournalDayState { written_today: false, streak: 0 } );}#[test]fn an_empty_page_is_ignored_for_both_answers_at_once() { assert_eq!( state(&[page("journal", Some(" "), Some("2026-08-08"))], NOW_IST),JournalDayState { written_today: false, streak: 0 } );}/// **The defect this function exists for.** 9 August 19:00 IST is 13:30Z the/// same day, but an implementation that takes the local START of day and/// formats it as UTC lands on the 8th — which is how one surface came to say/// "Write today's page" one swipe from a Home row reading "Today's page/// written".#[test]fn an_ist_evening_reads_the_local_day_and_not_the_utc_one() {let ist_evening = 1_786_282_200_000; assert_eq!( state(&[written("2026-08-09")], ist_evening),JournalDayState { written_today: true, streak: 1 } ); assert_eq!( state(&[written("2026-08-08")], ist_evening),JournalDayState { written_today: false, streak: 1 } );}/// 00:21 IST on the 9th — the instant that surface got wrong. In UTC it/// is still the 8th, and yesterday's page must NOT read as today's.#[test]fn just_after_midnight_ist_the_new_day_has_no_page_yet() {let just_after_midnight = 1_786_215_060_000; assert_eq!( state(&[written("2026-08-08")], just_after_midnight),JournalDayState { written_today: false, streak: 1 } ); assert_eq!( state( &[written("2026-08-09"), written("2026-08-08")], just_after_midnight ),JournalDayState { written_today: true, streak: 2 } );}/// One list, one clock, one answer: two readers of it can no longer differ, because there is only one function to differ about.#[test]fn two_callers_reading_one_list_get_one_answer() {let notes = [written("2026-08-08"), page("note", Some("x"), None)];let home = state(¬es, NOW_IST);let journal = state(¬es, NOW_IST); assert_eq!(home, journal); assert!(home.written_today);}#[test]fn empty_day_is_not_a_written_journal_page() {let page = page("journal", Some("Synthetic journal"), Some("")); assert!(!is_written_journal_page(&page)); assert!(written_journal_days(&[page]).is_empty());}/// **The case the Swift copy left behind.**////// `CottonUI/Format/JournalStreak.swift` computed this streak a second time/// until 4i's Swift half deleted it, and this was the one case its own suite/// had that the cases above do not: every streak case here is in IST, which/// never shifts. So the Swift file was emptied without losing anything — the/// case was already standing HERE, beside the one implementation, which is/// where a case belongs when the function it guarded is gone.////// New York springs forward on 8 March 2026. An implementation that walks back/// a day by subtracting 86,400,000 ms from a local midnight lands an hour short/// across the transition and reads the same date twice, ending the run a day/// early.#[test]fn streak_survives_a_daylight_saving_transition() {let now = cotton_store::protocol::js::parse_date_ms("2026-03-09T13:00:00.000Z").expect("instant"); assert_eq!( journal_streak( &["2026-03-07".into(),"2026-03-08".into(),"2026-03-09".into() ], now,"America/New_York" ) .expect("streak"),3 );}