Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(goals): stop a whole-account spend counting the same money twice (#3219)

buzzromain3e504dc9792b
diff --git a/app/models/goal.rb b/app/models/goal.rb
index 5ea22052c9..fc2904ce12 100644
--- a/app/models/goal.rb
+++ b/app/models/goal.rb
@@ -547,8 +547,6 @@ class Goal < ApplicationRecord
link.lock!
stamp_consumption!(transaction) if transaction
- # A whole-account link reserves no fixed slice, so there is nothing to
- # shrink — it already takes only what the account has left.
if link.allocated_amount.present?
# Refused rather than clamped. Clamping released only what the link
# held while `consumed_amount` took the full figure, so the two sides
@@ -559,6 +557,32 @@ class Goal < ApplicationRecord
end
link.update!(allocated_amount: link.allocated_amount.to_d - amount)
+ else
+ # A whole-account link has no slice to shrink, so `consumed_amount`
+ # would be added to a backing that has not moved: the goal reads 6,000
+ # of 5,000 until the real transaction lands and the balance catches up.
+ # A fixed earmark never has that window, because shrinking it caps the
+ # backing straight away.
+ #
+ # Spending settles what the link claims. It stops taking "whatever is
+ # there" and takes what the goal still needs after this spend.
+ #
+ # Deliberately NOT "what it backs, minus the amount". That depends on
+ # whether the money has already left the account, and the app cannot
+ # know: a spend recorded before the sync lands needs the subtraction,
+ # the same spend recorded after it has already had one, and taking it
+ # twice reports 4,000 of a 5,000 goal that is whole. Capping at what
+ # is left to reach is the same answer in both orders, with nothing to
+ # infer.
+ backed = backing_within([ link.account_id ]).to_d
+
+ # Same refusal a fixed earmark gives, for the same reason: the link
+ # cannot have supplied money it never backed. `still_needed` cannot go
+ # negative — the target check above has already refused that.
+ raise ConsumptionRefused.new(:exceeds_earmark) if amount > backed
+
+ still_needed = target_amount.to_d - consumed_amount.to_d - amount
+ link.update!(allocated_amount: [ backed, still_needed ].min)
end
update!(consumed_amount: consumed_amount.to_d + amount)
diff --git a/test/models/goal_consumption_test.rb b/test/models/goal_consumption_test.rb
index 517e619e93..701bc0b405 100644
--- a/test/models/goal_consumption_test.rb
+++ b/test/models/goal_consumption_test.rb
@@ -45,8 +45,15 @@ class GoalConsumptionTest < ActiveSupport::TestCase
assert_equal 3_750, Goal.find(sibling.id).current_balance.to_d
end
- # A whole-account link reserves no fixed slice, so there is nothing to shrink.
- test "a whole-account link records the consumption and keeps its shape" do
+ # A whole-account link has no slice to shrink, so the consumption used to be
+ # recorded against a backing that had not moved — the goal reading 6,000 of
+ # 5,000 until the real transaction landed and the balance caught up. A fixed
+ # earmark never has that window, because shrinking it caps the backing at
+ # once.
+ #
+ # Spending settles what the link claims: it stops taking "whatever is there"
+ # and takes what is left after the spend.
+ test "spending from a whole-account link settles what it claims" do
account = fresh_account(5_000)
goal = @family.goals.create!(name: "Whole", target_amount: 5_000, currency: "USD") do |g|
g.goal_accounts.build(account: account)
@@ -55,7 +62,80 @@ class GoalConsumptionTest < ActiveSupport::TestCase
goal.consume!(1_000)
assert_equal 1_000, goal.consumed_amount
- assert_nil goal.goal_accounts.first.reload.allocated_amount
+ assert_equal 4_000, goal.goal_accounts.first.reload.allocated_amount.to_d,
+ "the link still claimed the whole account after part of it was spent"
+ end
+
+ # The figure that gave this away: held plus spent overshooting the target
+ # while the account had not moved yet.
+ test "a whole-account goal does not count the same money twice" do
+ account = fresh_account(5_000)
+ goal = @family.goals.create!(name: "Whole", target_amount: 5_000, currency: "USD") do |g|
+ g.goal_accounts.build(account: account)
+ end
+
+ goal.consume!(1_000)
+
+ assert_equal 5_000, goal.current_balance.to_d + goal.consumed_amount.to_d
+ end
+
+ # The order the two events arrive in is not fixed. A spend recorded before
+ # the sync lands needs the account's fall accounted for; the same spend
+ # recorded after it has already had it, and subtracting twice reported
+ # 4,000 of a 5,000 goal that was whole. Capping at what is left to reach is
+ # the same answer either way.
+ test "it is right when the balance falls before the spend is recorded" do
+ account = fresh_account(4_000)
+ goal = @family.goals.create!(name: "Late", target_amount: 5_000, currency: "USD") do |g|
+ g.goal_accounts.build(account: account)
+ end
+
+ goal.consume!(1_000)
+
+ assert_equal 5_000, goal.current_balance.to_d + goal.consumed_amount.to_d
+ end
+
+ # A goal holding more than it needs keeps counting only toward its target
+ # once it has spent something — the cap is what makes the two orders agree,
+ # and it applies the same way here.
+ test "a goal holding more than its target settles at its target" do
+ account = fresh_account(7_000)
+ goal = @family.goals.create!(name: "Over", target_amount: 5_000, currency: "USD") do |g|
+ g.goal_accounts.build(account: account)
+ end
+
+ goal.consume!(1_000)
+
+ assert_equal 5_000, goal.current_balance.to_d + goal.consumed_amount.to_d
+ end
+
+ # And it stays right once the spend actually lands, which is the state a
+ # fixed earmark has always handled and this one did not.
+ test "it is still right once the balance catches up" do
+ account = fresh_account(5_000)
+ goal = @family.goals.create!(name: "Whole", target_amount: 5_000, currency: "USD") do |g|
+ g.goal_accounts.build(account: account)
+ end
+ goal.consume!(1_000)
+
+ account.update!(balance: 4_000)
+ settled = Goal.find(goal.id)
+
+ assert_equal 5_000, settled.current_balance.to_d + settled.consumed_amount.to_d
+ end
+
+ # Nothing to take it from: the refusal is the same one a fixed earmark gives
+ # when the amount is larger than the slice.
+ test "spending more than the goal has left to reach is refused" do
+ account = fresh_account(1_000)
+ goal = @family.goals.create!(name: "Whole", target_amount: 5_000, currency: "USD") do |g|
+ g.goal_accounts.build(account: account)
+ end
+
+ error = assert_raises(Goal::ConsumptionRefused) { goal.consume!(2_000) }
+
+ assert_equal :exceeds_earmark, error.reason
+ assert_equal 0, goal.reload.consumed_amount
end
test "with several linked accounts the caller has to say which one" do