diff --git a/app/helpers/insights_helper.rb b/app/helpers/insights_helper.rb index 6ee5ca339..349d660f1 100644 --- a/app/helpers/insights_helper.rb +++ b/app/helpers/insights_helper.rb @@ -141,13 +141,15 @@ module InsightsHelper return facts["account"] || facts["name"] end - if start_date == start_date.beginning_of_month && end_date == start_date.end_of_month - format = start_date.year == Date.current.year ? "%B" : "%B %Y" - return I18n.l(start_date, format: format) - end - days = (end_date - start_date).to_i - if start_date >= Date.current - 1 + if rolling_period_insight?(insight) && start_date >= Date.current - 1 + t("insights.meta.next_n_days", count: days) + elsif rolling_period_insight?(insight) && end_date >= Date.current - 1 + t("insights.meta.last_n_days", count: days) + elsif start_date == start_date.beginning_of_month && end_date == start_date.end_of_month + format = start_date.year == Date.current.year ? "%B" : "%B %Y" + I18n.l(start_date, format: format) + elsif start_date >= Date.current - 1 t("insights.meta.next_n_days", count: days) elsif end_date >= Date.current - 1 t("insights.meta.last_n_days", count: days) @@ -155,4 +157,8 @@ module InsightsHelper t("insights.meta.date_range", from: I18n.l(start_date, format: :short), to: I18n.l(end_date, format: :short)) end end + + def rolling_period_insight?(insight) + insight.insight_type.in?(%w[cash_flow_warning net_worth_milestone]) + end end diff --git a/test/helpers/insights_helper_test.rb b/test/helpers/insights_helper_test.rb index 84af79661..038fcbcad 100644 --- a/test/helpers/insights_helper_test.rb +++ b/test/helpers/insights_helper_test.rb @@ -70,13 +70,39 @@ class InsightsHelperTest < ActionView::TestCase end test "meta line labels a forward-looking window as next N days" do - insight = build_insight( - "cash_flow_warning", - period_start: Date.current, - period_end: Date.current + 30 - ) + travel_to Date.new(2026, 8, 1) do + insight = build_insight( + "cash_flow_warning", + period_start: Date.current, + period_end: Date.current + 30 + ) - assert_equal "Cash flow · Next 30 days", insight_meta_line(insight) + assert_equal "Cash flow · Next 30 days", insight_meta_line(insight) + end + end + + test "meta line labels a backward-looking rolling window as last N days" do + travel_to Date.new(2026, 8, 31) do + insight = build_insight( + "net_worth_milestone", + period_start: Date.current - 30, + period_end: Date.current + ) + + assert_equal "Net worth · Last 30 days", insight_meta_line(insight) + end + end + + test "meta line keeps monthly insight periods labeled as the month on boundaries" do + travel_to Date.new(2026, 8, 1) do + insight = build_insight( + "budget_at_risk", + period_start: Date.current.beginning_of_month, + period_end: Date.current.end_of_month + ) + + assert_equal "Budget · August", insight_meta_line(insight) + end end test "meta line falls back to the subject when there is no period" do