from datetime import date from common_cents.screens.reports import ( _date_range, _month_options, _months_in_range, ) def test_months_in_range_same_month(): assert _months_in_range("2026-05-01", "2026-05-31") == 1 def test_months_in_range_multi_month(): assert _months_in_range("2026-01-01", "2026-12-31") == 12 def test_months_in_range_year_crossover(): assert _months_in_range("2025-11-01", "2026-02-15") == 4 def test_date_range_this_month(): import calendar start, end, label = _date_range("this-month") today = date.today() last_day = calendar.monthrange(today.year, today.month)[1] assert start == today.replace(day=1).isoformat() assert end == today.replace(day=last_day).isoformat() assert "This Month" in label def test_date_range_last_month(): start, end, _label = _date_range("last-month") today = date.today() end_d = date.fromisoformat(end) # last day of previous month assert end_d < today.replace(day=1) assert date.fromisoformat(start).day == 1 assert end_d.month == date.fromisoformat(start).month def test_date_range_ytd(): start, end, _label = _date_range("ytd") today = date.today() assert start == date(today.year, 1, 1).isoformat() assert end == today.isoformat() def test_date_range_last_12_months(): """Trailing 12 months = current month + 11 prior, so spans 12 distinct months.""" start, end, _label = _date_range("last-12") assert _months_in_range(start, end) == 12 def test_date_range_custom_month_bounds(): """Custom range expands YYYY-MM to first/last day of the chosen months.""" start, end, label = _date_range("custom", "2026-03", "2026-05") assert start == "2026-03-01" assert end == "2026-05-31" assert "Mar 2026" in label and "May 2026" in label def test_date_range_custom_single_month(): start, end, label = _date_range("custom", "2026-02", "2026-02") assert start == "2026-02-01" assert end == "2026-02-28" assert label == "Custom (Feb 2026)" def test_date_range_custom_swaps_when_inverted(): start, end, _label = _date_range("custom", "2026-12", "2026-01") assert start == "2026-01-01" assert end == "2026-12-31" def test_date_range_custom_falls_back_to_current_month_when_blank(): start, end, _label = _date_range("custom", "", "") today = date.today() assert start.startswith(today.strftime("%Y-%m")) assert start.endswith("-01") assert end.startswith(today.strftime("%Y-%m")) def test_month_options_descending_and_inclusive(): opts = _month_options(date(2026, 3, 1), date(2026, 5, 15)) # latest first, three months, label = abbr month + year, value = YYYY-MM assert opts == [ ("May 2026", "2026-05"), ("Apr 2026", "2026-04"), ("Mar 2026", "2026-03"), ] def test_month_options_year_crossover(): opts = _month_options(date(2025, 11, 1), date(2026, 1, 31)) assert [v for _, v in opts] == ["2026-01", "2025-12", "2025-11"]