Fix Start month on Last 6 Months report off by 1 (#2070)

* 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] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
joaocbatista
2026-05-31 15:35:38 +01:00
committed by GitHub
parent 61a235f765
commit b101708409
2 changed files with 37 additions and 1 deletions

View File

@@ -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

View File

@@ -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