Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(insights): project month-to-date spend over the whole month (#3424)

Nate Mendesced8d0ccb611
diff --git a/app/models/insight/generators/spending_anomaly_generator.rb b/app/models/insight/generators/spending_anomaly_generator.rb
index 4c739baade..df7e4c8b45 100644
--- a/app/models/insight/generators/spending_anomaly_generator.rb
+++ b/app/models/insight/generators/spending_anomaly_generator.rb
@@ -19,7 +19,12 @@ class Insight::Generators::SpendingAnomalyGenerator < Insight::Generator
return [] if current.empty?
baseline = baseline_spend(period)
- pace_factor = period.days.to_f / elapsed_days
+ # The "current month" period ends today rather than at month end, so its own
+ # #days is the elapsed count and dividing by it would make the projection a
+ # no-op. Take the month's true length from its start date, which is also
+ # correct for families on a custom month start.
+ month_days = ((period.start_date + 1.month) - period.start_date).to_i
+ pace_factor = month_days.to_f / elapsed_days
anomalies = current.filter_map do |category_id, data|
baseline_amount = baseline[category_id]
diff --git a/test/models/insight/generators/spending_anomaly_generator_test.rb b/test/models/insight/generators/spending_anomaly_generator_test.rb
new file mode 100644
index 0000000000..d0480867ee
--- /dev/null
+++ b/test/models/insight/generators/spending_anomaly_generator_test.rb
@@ -0,0 +1,58 @@
+require "test_helper"
+
+class Insight::Generators::SpendingAnomalyGeneratorTest < ActiveSupport::TestCase
+ include EntriesTestHelper
+
+ setup do
+ @family = families(:dylan_family)
+ @category = @family.categories.create!(name: "Pace Test Cat", color: "#101010", lucide_icon: "circle")
+ end
+
+ # Anchor two months back so the relative-date fixture entries (always in the
+ # real current month) can't leak into the baseline or current periods. Day 10
+ # gives 10 elapsed days, comfortably past MIN_ELAPSED_DAYS.
+ def anchor
+ @anchor ||= (Date.current - 2.months).change(day: 10)
+ end
+
+ def month_start(offset)
+ (anchor.beginning_of_month - offset.months).beginning_of_month
+ end
+
+ def spend(month, total)
+ create_transaction(category: @category, amount: total, date: month.change(day: 3), name: "test #{month}")
+ end
+
+ def generate
+ Insight::Generators::SpendingAnomalyGenerator.new(@family).generate
+ end
+
+ # $1,000 spent by day 10 against a $3,000 monthly baseline is exactly on pace:
+ # projected over a 28-31 day month it lands within 7% of the baseline, well
+ # inside DEVIATION_THRESHOLD_PCT. Without the pace factor the raw $1,000 reads
+ # as 67% below baseline and produces a high-priority false alarm.
+ test "does not flag a category that is on pace for its baseline" do
+ travel_to anchor do
+ 3.times { |i| spend(month_start(i + 1), 3_000) }
+ spend(anchor.beginning_of_month, 1_000)
+
+ assert_empty generate
+ end
+ end
+
+ # The same $1,000 by day 10, but against a $1,000 monthly baseline, projects to
+ # roughly triple the usual month and must be flagged. Without the pace factor
+ # the raw total matches the baseline exactly and nothing is reported.
+ test "flags a category running well ahead of its baseline" do
+ travel_to anchor do
+ 3.times { |i| spend(month_start(i + 1), 1_000) }
+ spend(anchor.beginning_of_month, 1_000)
+
+ insights = generate
+
+ assert_equal 1, insights.size
+ assert_equal "above", insights.first.metadata[:direction]
+ assert_equal "high", insights.first.priority
+ end
+ end
+end