Files
sure/test/models/assistant/function/create_savings_goal_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

104 lines
3.9 KiB
Ruby

require "test_helper"
class Assistant::Function::CreateSavingsGoalTest < ActiveSupport::TestCase
setup do
@user = users(:family_admin)
@family = @user.family
@depository = accounts(:depository)
@fn = Assistant::Function::CreateSavingsGoal.new(@user)
end
test "to_definition returns valid JSON shape" do
definition = @fn.to_definition
assert_equal "create_savings_goal", definition[:name]
assert_kind_of String, definition[:description]
assert_equal "object", definition[:params_schema][:type]
assert_includes definition[:params_schema][:required], "name"
assert_includes definition[:params_schema][:required], "target_amount"
assert_includes definition[:params_schema][:required], "linked_account_names"
end
test "creates a goal with linked accounts" do
assert_difference -> { SavingsGoal.count } => 1,
-> { SavingsGoalAccount.count } => 1 do
result = @fn.call(
"name" => "Vacation",
"target_amount" => 1500,
"target_date" => 3.months.from_now.to_date.iso8601,
"linked_account_names" => [ @depository.name ]
)
assert result[:success]
assert_match(/Vacation/, result[:message])
assert result[:url].present?
assert_equal "USD", result[:currency]
end
end
test "creates a goal with initial contribution" do
assert_difference -> { SavingsContribution.count } => 1 do
@fn.call(
"name" => "Laptop fund",
"target_amount" => 2000,
"linked_account_names" => [ @depository.name ],
"initial_contribution" => { "amount" => 200, "source_account_name" => @depository.name }
)
end
contribution = SavingsContribution.order(created_at: :desc).first
assert_equal "initial", contribution.source
assert_equal 200, contribution.amount.to_i
end
test "soft error when name is missing" do
result = @fn.call("target_amount" => 100, "linked_account_names" => [ @depository.name ])
assert_equal false, result[:success]
assert_equal "name_required", result[:error]
end
test "soft error when target_amount is zero" do
result = @fn.call("name" => "X", "target_amount" => 0, "linked_account_names" => [ @depository.name ])
assert_equal false, result[:success]
assert_equal "target_amount_invalid", result[:error]
end
test "soft error when no linked accounts" do
result = @fn.call("name" => "X", "target_amount" => 100, "linked_account_names" => [])
assert_equal false, result[:success]
assert_equal "no_linked_accounts", result[:error]
assert_kind_of Array, result[:available_accounts]
assert(result[:available_accounts].all? { |a| a.is_a?(Hash) && a.key?(:name) })
end
test "soft error when account name doesn't match" do
result = @fn.call("name" => "X", "target_amount" => 100, "linked_account_names" => [ "Nonexistent Account" ])
assert_equal false, result[:success]
assert_equal "unknown_accounts", result[:error]
assert_includes result[:unknown_names], "Nonexistent Account"
end
test "soft error when currencies differ across linked accounts" do
eur = Account.create!(family: @family, accountable: Depository.new, name: "EUR Account", currency: "EUR", balance: 100)
result = @fn.call(
"name" => "Mixed",
"target_amount" => 100,
"linked_account_names" => [ @depository.name, eur.name ]
)
assert_equal false, result[:success]
assert_equal "currency_mismatch", result[:error]
end
test "scopes to the user's family" do
other_family = Family.create!(name: "Other", currency: "USD", locale: "en", country: "US", timezone: "UTC")
Account.create!(family: other_family, accountable: Depository.new, name: "Foreign Checking", currency: "USD", balance: 100)
result = @fn.call(
"name" => "X",
"target_amount" => 100,
"linked_account_names" => [ "Foreign Checking" ]
)
assert_equal false, result[:success]
assert_equal "unknown_accounts", result[:error]
end
end