mirror of
https://github.com/we-promise/sure.git
synced 2026-05-31 08:19:03 +00:00
Adds a standalone Savings goals feature: a piggy-bank style tracker that lets a family set a target, link one or more Depository accounts as funding sources, and log manual contributions over time. Supersedes #1569 (closed) — same intent, redesigned per reviewer + Discord feedback. What this adds: - New `/savings_goals` sidebar entry (piggy-bank icon) with index, show, state-filtered tabs (all/active/paused/completed/archived), and a 2-step modal stepper for creation (Identity → Review). - Multi-account funding via a `SavingsGoalAccount` join: a goal requires ≥1 linked Depository account (checking/savings/HSA/CD/money-market), and all linked accounts must share the goal's currency. - Tracker balance model: goal balance = SUM(contributions.amount). No auto-flow from account balances. Contributions are pure logical records and don't move money between accounts. - Manual contributions modal scoped to the goal's linked accounts. Initial contributions seeded at creation can't be deleted; manual ones can. - AASM lifecycle: active / paused / completed / archived. Hard-delete only after archive. - Status pills (On track / Behind / Reached / No date) derived from pace vs target_date. - AI Assistant tool `create_savings_goal` lets the sidebar chat create a goal end-to-end from a natural-language prompt; soft errors carry the available-accounts list back to the LLM (mirrors the existing `import_bank_statement` pattern). - Family-scoped throughout (`Current.family`-only access, account family-scoping enforced both in controllers and the AI tool). - Demo data seed wires up 4 sample goals across the Depository accounts. Intentionally out of scope (separate PRs / v1.1): - Auto-fund from budget surplus + Sidekiq cron + budget-show card. - Dashboard "Savings goals" widget. - "Behind pace" projection chart on the detail page. - `evaluate_savings_goal_feasibility` LLM tool (level-setting before create_savings_goal). - Spend-less goals inside Budgets. - Family-member-private goals (deferred investigation).
136 lines
4.3 KiB
Ruby
136 lines
4.3 KiB
Ruby
require "test_helper"
|
|
|
|
class SavingsGoalTest < ActiveSupport::TestCase
|
|
setup do
|
|
@family = families(:dylan_family)
|
|
@depository = accounts(:depository)
|
|
@connected = accounts(:connected)
|
|
@goal = savings_goals(:vacation_italy)
|
|
end
|
|
|
|
test "valid fixture goal saves" do
|
|
assert @goal.valid?
|
|
end
|
|
|
|
test "name is required" do
|
|
@goal.name = ""
|
|
assert_not @goal.valid?
|
|
assert_includes @goal.errors[:name], "can't be blank"
|
|
end
|
|
|
|
test "target_amount must be positive" do
|
|
@goal.target_amount = 0
|
|
assert_not @goal.valid?
|
|
end
|
|
|
|
test "must have at least one linked account on create" do
|
|
new_goal = @family.savings_goals.new(name: "Test", target_amount: 100, currency: "USD")
|
|
assert_not new_goal.valid?
|
|
assert_match(/at least one/i, new_goal.errors[:base].join)
|
|
end
|
|
|
|
test "linked accounts must be depository" do
|
|
investment = accounts(:investment)
|
|
new_goal = @family.savings_goals.new(name: "Test", target_amount: 100, currency: "USD")
|
|
new_goal.savings_goal_accounts.build(account: investment)
|
|
assert_not new_goal.valid?
|
|
assert_includes new_goal.errors[:linked_accounts], "All linked accounts must be Depository (checking, savings, HSA, CD, money-market)."
|
|
end
|
|
|
|
test "linked accounts must belong to family" do
|
|
other_family = Family.create!(name: "Other", currency: "USD", locale: "en", country: "US", timezone: "UTC")
|
|
foreign_account = Account.create!(
|
|
family: other_family,
|
|
accountable: Depository.new,
|
|
name: "Foreign",
|
|
currency: "USD",
|
|
balance: 100
|
|
)
|
|
new_goal = @family.savings_goals.new(name: "T", target_amount: 100, currency: "USD")
|
|
new_goal.savings_goal_accounts.build(account: foreign_account)
|
|
assert_not new_goal.valid?
|
|
assert_includes new_goal.errors[:linked_accounts], "Linked accounts must belong to the same family as the goal."
|
|
end
|
|
|
|
test "linked accounts must share currency with goal" do
|
|
eur_account = Account.create!(
|
|
family: @family,
|
|
accountable: Depository.new,
|
|
name: "Euro Cash",
|
|
currency: "EUR",
|
|
balance: 100
|
|
)
|
|
new_goal = @family.savings_goals.new(name: "T", target_amount: 100, currency: "USD")
|
|
new_goal.savings_goal_accounts.build(account: eur_account)
|
|
assert_not new_goal.valid?
|
|
assert_includes new_goal.errors[:linked_accounts], "All linked accounts must share the same currency."
|
|
end
|
|
|
|
test "currency can't change after contributions exist" do
|
|
assert @goal.savings_contributions.exists?
|
|
@goal.currency = "EUR"
|
|
assert_not @goal.valid?
|
|
assert_includes @goal.errors[:currency], "Can't change the currency after a goal has contributions."
|
|
end
|
|
|
|
test "current_balance sums contributions" do
|
|
expected = @goal.savings_contributions.sum(:amount)
|
|
assert_equal expected, @goal.current_balance
|
|
end
|
|
|
|
test "with_current_balance scope precomputes balance" do
|
|
loaded = @family.savings_goals.with_current_balance.find(@goal.id)
|
|
expected = @goal.savings_contributions.sum(:amount)
|
|
assert_equal expected.to_f, loaded.current_balance.to_f
|
|
end
|
|
|
|
test "progress_percent caps at 100" do
|
|
@goal.target_amount = 1
|
|
assert_equal 100, @goal.progress_percent
|
|
end
|
|
|
|
test "progress_percent is 0 for empty active goal" do
|
|
fresh = savings_goals(:car_paydown)
|
|
fresh.target_amount = 10000
|
|
assert_equal 0, fresh.progress_percent
|
|
end
|
|
|
|
test "remaining_amount is non-negative" do
|
|
@goal.target_amount = 1
|
|
assert_equal 0, @goal.remaining_amount
|
|
end
|
|
|
|
test "AASM transitions" do
|
|
fresh = savings_goals(:emergency_fund)
|
|
assert fresh.active?
|
|
fresh.pause!
|
|
assert fresh.paused?
|
|
fresh.resume!
|
|
assert fresh.active?
|
|
fresh.complete!
|
|
assert fresh.completed?
|
|
fresh.archive!
|
|
assert fresh.archived?
|
|
fresh.unarchive!
|
|
assert fresh.active?
|
|
end
|
|
|
|
test "status: reached when balance >= target" do
|
|
@goal.target_amount = 1
|
|
assert_equal :reached, @goal.status
|
|
end
|
|
|
|
test "status: no_target_date when target_date is nil" do
|
|
@goal.target_date = nil
|
|
@goal.target_amount = 10_000
|
|
assert_equal :no_target_date, @goal.status
|
|
end
|
|
|
|
test "advisory_lock_key_for is stable per family" do
|
|
k1 = SavingsGoal.advisory_lock_key_for(@family.id)
|
|
k2 = SavingsGoal.advisory_lock_key_for(@family.id)
|
|
assert_equal k1, k2
|
|
assert_kind_of Integer, k1
|
|
end
|
|
end
|