Cotton code
161 lines · 7.1 KB · RustRaw
//! `packages/core/src/engine/money/dates.ts` ships NO `__tests__` file — it is//! the one module of the five with none. So every assertion below was obtained//! by RUNNING the TypeScript (`cd packages/core && npx tsx …`) rather than//! ported from a test or reasoned out, and the coverage is deliberately wider//! than a ported file's would be: leap days both ways, every month end, both//! year boundaries, the century rules, and the two shapes that make//! `Date.parse` refuse.use super::*;// ── month_index ─────────────────────────────────────────────────────────────#[test]fn reads_the_absolute_month_index_of_an_iso_date() {    assert_eq!(month_index("2026-01-01"), Ok(24_312));    assert_eq!(month_index("2026-12-31"), Ok(24_323));    assert_eq!(month_index("1970-01-01"), Ok(23_640));    assert_eq!(month_index("0001-01-01"), Ok(12));    assert_eq!(month_index("2026-02-29"), Ok(24_313));}/// The regex checks the SHAPE and nothing else, so a month of 13 reads as one/// past December rather than refusing. Every caller that needs a real date/// validates with `period::assert_iso_date` instead — the split the TypeScript/// makes on purpose and the reason `coverage.ts` was changed to use the/// stricter one.#[test]fn does_not_check_that_the_month_or_day_is_real() {    assert_eq!(month_index("2026-13-01"), Ok(24_324));    assert_eq!(month_index("2026-00-15"), Ok(24_311));}#[test]fn refuses_anything_that_is_not_the_iso_shape() {    for bad in [        "2026-1-01",        "2026-01-1",        "26-01-01",        "",        "2026-01-01T00:00:00Z",    ] {        let error = month_index(bad).unwrap_err();        assert_eq!(error.kind(), "TypeError");        assert_eq!(            error.to_string(),            format!("expected an ISO date (YYYY-MM-DD), got \"{bad}\"")        );    }}// ── month_label ─────────────────────────────────────────────────────────────#[test]fn labels_a_month_index_back_as_yyyy_mm() {    assert_eq!(month_label(24_312), "2026-01");    assert_eq!(month_label(24_313), "2026-02");    assert_eq!(month_label(24_000), "2000-01");    assert_eq!(month_label(0), "0000-01");    assert_eq!(month_label(11), "0000-12");    assert_eq!(month_label(12), "0001-01");    assert_eq!(month_label(1), "0000-02");    assert_eq!(month_label(23), "0001-12");    assert_eq!(month_label(119_999), "9999-12");}/// Below zero the label goes strange, and the port keeps the strangeness rather/// than tidying it: the year is `Math.floor`ed and the month comes off/// JavaScript's TRUNCATED `%`, so index −1 is year −1 month 0 and `padStart`/// pads AROUND the minus sign. Nothing in the engine reaches a negative index;/// pinning it is what stops one implementation from being quietly "fixed".#[test]fn keeps_javascripts_own_answer_below_zero() {    assert_eq!(month_label(-1), "00-1-00");    assert_eq!(month_label(-12), "00-1-01");    assert_eq!(month_label(-13), "00-2-00");}// ── add_days ────────────────────────────────────────────────────────────────#[test]fn adds_and_subtracts_plain_days() {    assert_eq!(add_days("2026-03-14", 1).unwrap(), "2026-03-15");    assert_eq!(add_days("2026-03-14", -1).unwrap(), "2026-03-13");    assert_eq!(add_days("2026-03-14", 0).unwrap(), "2026-03-14");    assert_eq!(add_days("2026-03-14", 100_000).unwrap(), "2299-12-28");    assert_eq!(add_days("2026-03-14", -100_000).unwrap(), "1752-05-29");}#[test]fn walks_a_leap_day_from_both_sides() {    assert_eq!(add_days("2028-02-28", 1).unwrap(), "2028-02-29");    assert_eq!(add_days("2028-02-29", 1).unwrap(), "2028-03-01");    assert_eq!(add_days("2028-03-01", -1).unwrap(), "2028-02-29");    assert_eq!(add_days("2027-02-28", 1).unwrap(), "2027-03-01");    assert_eq!(add_days("2026-03-01", -1).unwrap(), "2026-02-28");    // The century rule, which a naive "%4" would get wrong in one direction.    assert_eq!(add_days("1900-03-01", -1).unwrap(), "1900-02-28");    assert_eq!(add_days("2000-03-01", -1).unwrap(), "2000-02-29");}#[test]fn walks_every_shape_of_month_end() {    assert_eq!(add_days("2026-01-31", 1).unwrap(), "2026-02-01");    assert_eq!(add_days("2026-02-01", -1).unwrap(), "2026-01-31");    assert_eq!(add_days("2026-04-30", 1).unwrap(), "2026-05-01");    assert_eq!(add_days("2026-05-01", -1).unwrap(), "2026-04-30");    assert_eq!(add_days("2026-08-31", 1).unwrap(), "2026-09-01");    assert_eq!(add_days("2026-11-30", 1).unwrap(), "2026-12-01");}#[test]fn crosses_a_year_boundary_in_both_directions() {    assert_eq!(add_days("2026-12-31", 1).unwrap(), "2027-01-01");    assert_eq!(add_days("2027-01-01", -1).unwrap(), "2026-12-31");    assert_eq!(add_days("2026-01-01", -1).unwrap(), "2025-12-31");    assert_eq!(add_days("0001-01-01", -1).unwrap(), "0000-12-31");    assert_eq!(add_days("0000-01-01", 1).unwrap(), "0000-01-02");    // A year is 365 days, and 366 across a leap February.    assert_eq!(add_days("2026-06-15", 365).unwrap(), "2027-06-15");    assert_eq!(add_days("2027-06-15", -365).unwrap(), "2026-06-15");    assert_eq!(add_days("2026-06-15", 366).unwrap(), "2027-06-16");}/// The ECMAScript Date Time String grammar admits any day 01–31 and then rolls/// it over; only a day or month OUTSIDE that range is refused. So a February/// 30th is a real answer here and a February 32nd is not — which is precisely/// why `coverage.ts` validates through `period::assert_iso_date` first.#[test]fn rolls_an_over_long_day_over_and_refuses_an_out_of_range_one() {    assert_eq!(add_days("2026-02-30", 1).unwrap(), "2026-03-03");    assert_eq!(add_days("2026-04-31", 1).unwrap(), "2026-05-02");    assert_eq!(add_days("2026-06-31", 0).unwrap(), "2026-07-01");    for bad in ["2026-13-01", "2026-00-10", "2026-01-32", "2026-01-00"] {        let error = add_days(bad, 1).unwrap_err();        assert_eq!(error.kind(), "RangeError");        assert_eq!(error.to_string(), format!("unparseable ISO date \"{bad}\""));    }}/// The shape check runs BEFORE the parse, so a malformed date fails as a/// `TypeError` here exactly as it does in `month_index`.#[test]fn refuses_a_malformed_date_before_it_parses_anything() {    let error = add_days("2026-3-01", 1).unwrap_err();    assert_eq!(error.kind(), "TypeError");    assert_eq!(        error.to_string(),        "expected an ISO date (YYYY-MM-DD), got \"2026-3-01\""    );}/// An answer outside the years 0000–9999 is no ISO date, so it is refused.#[test]fn refuses_an_answer_outside_four_digit_years() {    for (from, days) in [("9999-12-31", 1), ("2026-03-14", -100_000_000), ("2026-03-14", 100_000_000), ("0000-01-01", -1)] {        let error = add_days(from, days).unwrap_err();        assert_eq!(error.kind(), "RangeError");        assert_eq!(error.to_string(), "Invalid time value");    }}