Files
sure/test/models/savings_contribution_test.rb
Guillem Arias 77660d2ee4 feat(savings): add savings goals
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).
2026-05-11 11:20:37 +02:00

47 lines
1.6 KiB
Ruby

require "test_helper"
class SavingsContributionTest < ActiveSupport::TestCase
setup do
@goal = savings_goals(:vacation_italy)
@depository = accounts(:depository)
end
test "valid fixture contribution saves" do
assert savings_contributions(:vacation_italy_initial).valid?
end
test "amount must be positive" do
c = @goal.savings_contributions.new(account: @depository, amount: 0, currency: "USD", source: "manual", contributed_at: Date.current)
assert_not c.valid?
end
test "source must be manual or initial" do
c = @goal.savings_contributions.new(account: @depository, amount: 10, currency: "USD", source: "auto", contributed_at: Date.current)
assert_not c.valid?
end
test "currency syncs from goal when blank" do
c = @goal.savings_contributions.new(account: @depository, amount: 10, source: "manual", contributed_at: Date.current)
c.valid?
assert_equal @goal.currency, c.currency
end
test "account must be linked to goal" do
other_depository = Account.create!(
family: @goal.family,
accountable: Depository.new,
name: "Unlinked Depository",
currency: "USD",
balance: 100
)
c = @goal.savings_contributions.new(account: other_depository, amount: 10, currency: "USD", source: "manual", contributed_at: Date.current)
assert_not c.valid?
assert_includes c.errors[:account], "Account must be one of the goal's linked accounts."
end
test "manual? and initial? predicates" do
assert savings_contributions(:vacation_italy_initial).initial?
assert savings_contributions(:vacation_italy_manual).manual?
end
end