mirror of
https://github.com/we-promise/sure.git
synced 2026-05-30 15:59:02 +00:00
Reshape the goals feature to live on top of linked-account balances. A goal's balance is now the live balance of every depository account linked to it — no parallel ledger, no "log a contribution" step. The "Add contribution" affordance is replaced by a 7-day GoalPledge (kind: transfer | manual_save). GoalPledge::Reconciler matches incoming Transactions (via Account::ProviderImportAdapter) and Valuations (via Account::ReconciliationManager) against open pledges within ±5 days, ±$0.50, or ±1% — single hook covers every provider (Plaid, SimpleFIN, Lunchflow, Enable Banking, Brex, IBKR, Kraken, SnapTrade) plus manual balance edits. A 15-minute Sidekiq cron sweeps expired pledges. Goal model: balance derived from linked_accounts.sum(&:balance), new pace (90-day net non-transfer inflow), months_of_runway, last_matched_pledge_*, pledge_action_label_key (the "I just transferred…" vs "I just saved…" verb switch). UI: - Index gets a 3-card KPI strip (Contributed last 30d / Needs this month / On track) plus a pending-pledges callout. - Show page swaps the "Add contribution" CTA for the pledge modal, replaces the contribution list with a pending-pledge banner, and rebuilds the funding widget into per-account rows with a 12-bucket weekly sparkline and last-30 inflow. - Projection chart adds a required-line (dashed light from today → target) and a translucent pending-pledge bump at today's X. Schema (3 migrations): 1. goal_pledges table with PG enums (goal_pledge_kind, goal_pledge_status), open-by-expiry index, and unique-when-not-null matched_transaction_id. 2. Drop goal_contributions. 3. Partial unique index on transactions ((extra -> 'goal' ->> 'pledge_id')) built CONCURRENTLY so it doesn't block prod. After pulling: run bin/rails db:migrate, then commit the schema.rb sync separately (or let CI regenerate). Deferred to v1.1: allocation columns, contention/archived banners, "why is this behind?" diagnostic, reallocate flow, refresh-sync + Plaid throttle, unallocated-cash chip, joint-account approval, goal_activities log, polymorphic matched_entry_id/type for manual pledge audit.
89 lines
3.4 KiB
Ruby
89 lines
3.4 KiB
Ruby
require "test_helper"
|
|
|
|
class Assistant::Function::CreateGoalTest < ActiveSupport::TestCase
|
|
setup do
|
|
@user = users(:family_admin)
|
|
@family = @user.family
|
|
@depository = accounts(:depository)
|
|
@fn = Assistant::Function::CreateGoal.new(@user)
|
|
end
|
|
|
|
test "to_definition returns valid JSON shape" do
|
|
definition = @fn.to_definition
|
|
assert_equal "create_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 -> { Goal.count } => 1,
|
|
-> { GoalAccount.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 "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
|