From b101708409739c8f3e6167f3ccba08ddceba4970 Mon Sep 17 00:00:00 2001 From: joaocbatista <100242707+joaocbatista@users.noreply.github.com> Date: Sun, 31 May 2026 15:35:38 +0100 Subject: [PATCH] Fix Start month on Last 6 Months report off by 1 (#2070) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Bump version to next iteration after v0.7.1-rc.1.1 release * fix: correct last_6_months period to show exactly 6 months Default start date was snapping to beginning_of_month 6 months ago, producing a 7-month window (e.g. Nov 1 – May 31). Fix computes the start as (end_of_month + 1 day - 6 months).beginning_of_month so the default window is consistent with the navigation arrows and Today button. * improved test --------- Co-authored-by: github-actions[bot] --- app/controllers/reports_controller.rb | 2 +- test/controllers/reports_controller_test.rb | 36 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 9c5bd46a5..c31df1eb0 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -245,7 +245,7 @@ class ReportsController < ApplicationController when :ytd Date.current.beginning_of_year.to_date when :last_6_months - 6.months.ago.beginning_of_month.to_date + (Date.current.end_of_month + 1.day - 6.months).beginning_of_month.to_date when :custom 1.month.ago.to_date else diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rb index 298459d1f..87a6c2bca 100644 --- a/test/controllers/reports_controller_test.rb +++ b/test/controllers/reports_controller_test.rb @@ -41,6 +41,42 @@ class ReportsControllerTest < ActionDispatch::IntegrationTest test "index with last 6 months period" do get reports_path(period_type: :last_6_months) assert_response :ok + + expected_end = Date.current.end_of_month + expected_start = (expected_end + 1.day - 6.months).beginning_of_month + + # Should show exactly 6 months, not 7 + assert_equal 6, (expected_end.year * 12 + expected_end.month) - (expected_start.year * 12 + expected_start.month) + 1 + + # Page should include both boundary months + assert_includes @response.body, expected_start.strftime("%b %Y") + assert_includes @response.body, expected_end.strftime("%b %Y") + + # Page should NOT include the months immediately outside the window + prev_month = expected_start - 1.month + next_month = expected_end + 1.month + assert_not_includes @response.body, prev_month.strftime("%b %Y") + assert_not_includes @response.body, next_month.strftime("%b %Y") + end + + test "last 6 months default start date is consistent with navigation" do + # First load (no params) should produce the same start/end as the navigation arrows would + get reports_path(period_type: :last_6_months) + assert_response :ok + + expected_end = Date.current.end_of_month + expected_start = (expected_end + 1.day - 6.months).beginning_of_month + + # Navigate forward from one window back — should land on the same default window + prev_start = expected_start - 6.months + prev_end = prev_start + 6.months - 1.day + + get reports_path(period_type: :last_6_months, start_date: prev_start, end_date: prev_end) + assert_response :ok + + # The next-window link should point to the same dates as the default + assert_select "a[href=?]", + reports_path(period_type: :last_6_months, start_date: expected_start, end_date: expected_end) end test "index shows empty state when no transactions" do