Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
85.4 KB
Raw Blame
require "test_helper"

class GoalTest < ActiveSupport::TestCase
  include EntriesTestHelper

  setup do
    @family = families(:dylan_family)
    @depository = accounts(:depository)
    @connected = accounts(:connected)
    @goal = goals(:vacation_italy)
  end

  test "valid fixture goal saves" do
    assert @goal.valid?
  end

  # Two whole-account links on ONE account each claim the entire balance, so
  # the account is counted twice. The pro-rata haircut in backing_share_for
  # only scales FIXED earmarks: `others_fixed` sums allocated_amount, and an
  # unallocated link contributes nil.to_d == 0, so the two links never see
  # each other. GoalAccount now refuses to create this state (see
  # GoalAccountTest), but rows that predate the guard still read this way —
  # this test pins the behaviour those rows get, and is the reason the guard
  # exists.
  test "two grandfathered whole-account links each claim the full balance" do
    livret = Account.create!(
      family: @family, accountable: Depository.new,
      name: "Livret A", currency: "USD", balance: 6_000
    )
    precaution = @family.goals.create!(
      name: "Precaution", target_amount: 5_000, currency: "USD",
      goal_accounts: [ GoalAccount.new(account: livret) ]
    )
    # Built with a fixed earmark so the new guard lets it through, then
    # forced to NULL behind the validation's back — exactly the shape of a
    # row written before the guard shipped.
    vacances = @family.goals.create!(
      name: "Vacances", target_amount: 5_000, currency: "USD",
      goal_accounts: [ GoalAccount.new(account: livret, allocated_amount: 1) ]
    )
    vacances.goal_accounts.first.update_column(:allocated_amount, nil)

    assert_equal 6_000, precaution.reload.current_balance.to_i
    assert_equal 6_000, vacances.reload.current_balance.to_i
    assert_equal 12_000, precaution.current_balance.to_i + vacances.current_balance.to_i,
                 "6 000 of balance backing 12 000 of goals — the double count B7 guards against"
    assert_equal 100, precaution.progress_percent.to_i
    assert_equal 100, vacances.progress_percent.to_i
  end

  # The confirm dialog renders `body` as text, so the goal name goes in as it
  # is; escaping it would show "&amp;" in the dialog.
  test "deletion_confirm names the goal as it is written" do
    @goal.name = "Rock & Roll <3"

    assert_includes @goal.deletion_confirm.to_data_attribute[:body], "Rock & Roll <3"
  end

  # Derived, not a hardcoded list: a locale added later ships a goals YAML
  # without these keys otherwise. `fallback: false` carries as much weight —
  # the backend has I18n::Backend::Fallbacks mixed in, so a missing German key
  # resolves to the English string and a plain `.present?` assertion passes
  # while a German family reads English in the dialog.
  test "confirm_delete copy resolves in every locale that ships goal translations" do
    locales = Rails.root.glob("config/locales/views/goals/*.yml").map { |f| f.basename(".yml").to_s.to_sym }
    assert_operator locales.size, :>=, 7, "goal locale files disappeared — check the glob"

    locales.each do |locale|
      body = I18n.t("goals.show.confirm_delete_body", name: "Wedding", locale: locale, fallback: false, default: nil)
      assert body, "#{locale} is missing goals.show.confirm_delete_body"
      assert_includes body, "Wedding", "#{locale} dropped the %{name} interpolation"

      %w[confirm_delete_title confirm_delete_cta].each do |key|
        assert I18n.t("goals.show.#{key}", locale: locale, fallback: false, default: nil).present?,
               "#{locale} is missing goals.show.#{key}"
      end
    end
  end

  # The days-left segment is returned separately so the view can keep it
  # unbroken; joined into one string it wrapped after "days" on a phone.
  test "header_summary_parts keeps the days-left phrase in its own segment" do
    # Pinned: the count is `target_date - Date.current`, so a suite that crossed
    # midnight between setting the date and reading it would report 183.
    travel_to Date.new(2026, 6, 1) do
      # Target well above the linked account's balance, or the goal reads as
      # :reached and drops the days-left segment on purpose.
      @goal.target_amount = 1_000_000
      @goal.target_date = Date.current + 184

      parts = @goal.header_summary_parts

      assert_equal 2, parts.size
      assert_match(/184 days left/, parts.last)
      assert_no_match(/days left/, parts.first)
    end
  end

  test "header_summary_parts omits days left once the target is reached" do
    @goal.target_date = 184.days.from_now.to_date
    @goal.complete!

    assert_equal 1, @goal.header_summary_parts.size
  end

  test "header_summary_parts is a single segment without a target date" do
    @goal.target_date = nil

    assert_equal 1, @goal.header_summary_parts.size
  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 "color must match hex format" do
    @goal.color = "red; cursor: pointer"
    assert_not @goal.valid?
    assert_includes @goal.errors[:color], "is invalid"
  end

  test "color accepts standard 6-digit hex" do
    @goal.color = "#abcdef"
    assert @goal.valid?, @goal.errors.full_messages.to_sentence
  end

  test "display_status follows AASM state after pause! on the same instance" do
    @goal.update!(color: "#4da568") if @goal.color.blank?
    initial = @goal.display_status
    @goal.pause!
    assert_equal :paused, @goal.display_status, "stale memo would have returned #{initial.inspect}"
  end

  test "must have at least one linked account on create" do
    new_goal = @family.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

  # A new goal counts what its accounts are worth today unless the user
  # chose otherwise, whatever kind of account backs it.
  test "investment accounts are fundable and a new goal defaults to the market value basis" do
    investment = accounts(:investment)
    new_goal = @family.goals.new(name: "Inv", target_amount: 100, currency: "USD")
    new_goal.goal_accounts.build(account: investment)
    assert new_goal.valid?, new_goal.errors.full_messages.to_sentence
    new_goal.save!
    assert_equal "balance", new_goal.reload.progress_basis
  end

  test "a new investment-backed goal keeps the contributions basis it was given" do
    new_goal = @family.goals.create!(name: "Inv", target_amount: 100, currency: "USD", progress_basis: "contributions") do |g|
      g.goal_accounts.build(account: accounts(:investment))
    end
    assert_equal "contributions", new_goal.reload.progress_basis
  end

  test "an unknown progress basis is rejected" do
    goal = goals(:emergency_fund)
    goal.progress_basis = "speculative"
    assert_not goal.valid?
    assert goal.errors[:progress_basis].any?
  end

  test "crypto accounts are fundable and stay on the basis the goal was given" do
    new_goal = @family.goals.new(name: "BTC", target_amount: 100, currency: "USD")
    new_goal.goal_accounts.build(account: accounts(:crypto))
    assert new_goal.valid?, new_goal.errors.full_messages.to_sentence
    new_goal.save!
    assert_equal "balance", new_goal.reload.progress_basis
  end

  test "non-fundable account types are rejected" do
    credit = accounts(:credit_card)
    new_goal = @family.goals.new(name: "Test", target_amount: 100, currency: "USD")
    new_goal.goal_accounts.build(account: credit)
    assert_not new_goal.valid?
    assert_includes new_goal.errors[:linked_accounts], "All linked accounts must be cash, investment or crypto accounts."
  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.goals.new(name: "T", target_amount: 100, currency: "USD")
    new_goal.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 may use a different currency from the goal" do
    eur_account = Account.create!(
      family: @family,
      accountable: Depository.new,
      name: "Euro Cash",
      currency: "EUR",
      balance: 100
    )
    new_goal = @family.goals.new(name: "T", target_amount: 100, currency: "USD")
    new_goal.goal_accounts.build(account: eur_account)
    assert new_goal.valid?
  end

  test "currency can't change once linked accounts exist" do
    assert @goal.linked_accounts.exists?
    @goal.currency = "EUR"
    assert_not @goal.valid?
    assert_includes @goal.errors[:currency], "Can't change the currency after the goal is linked to accounts."
  end

  # A whole-account link takes what is LEFT of the balance once other goals'
  # fixed earmarks are set aside — not the gross balance. This used to assert
  # the gross figure, which only held because the fixtures had three goals
  # claiming `depository` in full at once: the very state the exclusivity rule
  # forbids, and one where the same money was counted three times.
  test "current_balance sums linked account balances net of other goals' earmarks" do
    account_ids = @goal.linked_accounts.map(&:id)
    others_fixed = GoalAccount.where(account_id: account_ids)
                              .where.not(goal_id: @goal.id)
                              .sum(:allocated_amount)
    expected = @goal.linked_accounts.sum(&:balance).to_d - others_fixed

    assert_operator others_fixed, :>, 0, "fixtures should exercise the netting"
    assert_equal expected, @goal.current_balance.to_d
  end

  test "progress_percent caps at 100" do
    @goal.target_amount = 1
    assert_equal 100, @goal.progress_percent
  end

  test "progress_percent stays below 100 while remaining amount is positive" do
    account = Account.create!(
      family: @family,
      accountable: Depository.new,
      name: "Almost There Savings",
      currency: "USD",
      balance: BigDecimal("999.50")
    )

    goal = @family.goals.create!(
      name: "Almost There",
      target_amount: BigDecimal("1000"),
      currency: "USD"
    ) { |new_goal| new_goal.goal_accounts.build(account: account) }

    assert_equal BigDecimal("0.5"), goal.remaining_amount
    assert_equal 99, goal.progress_percent
    assert_equal :no_target_date, goal.status
  end

  test "status stays reached for a goal completed while underfunded" do
    account = Account.create!(
      family: @family,
      accountable: Depository.new,
      name: "Completed Underfunded Savings",
      currency: "USD",
      balance: BigDecimal("999.50")
    )

    goal = @family.goals.create!(
      name: "Completed Underfunded",
      target_amount: BigDecimal("1000"),
      currency: "USD"
    ) { |new_goal| new_goal.goal_accounts.build(account: account) }

    goal.complete!

    assert_equal BigDecimal("0.5"), goal.remaining_amount
    assert_equal 100, goal.progress_percent
    assert_equal :reached, Goal.find(goal.id).status
  end

  test "progress_percent is 0 for empty active goal" do
    fresh = goals(:car_paydown)
    fresh.update!(target_amount: 10_000)
    fresh.linked_accounts.update_all(balance: 0)
    # Refetch instead of poking @current_balance directly so the test
    # exercises the real memo lifecycle (a request reads progress_percent
    # on a freshly-loaded record after the underlying balances changed).
    reloaded = Goal.find(fresh.id)
    assert_equal 0, reloaded.progress_percent
  end

  test "remaining_amount is non-negative" do
    @goal.target_amount = 1
    assert_equal 0, @goal.remaining_amount
  end

  test "pace is zero on a goal whose linked accounts have no transactions" do
    fresh_account = Account.create!(
      family: @family,
      accountable: Depository.new,
      name: "Empty Savings",
      currency: "USD",
      balance: 0
    )
    fresh = @family.goals.create!(
      name: "Fresh goal",
      target_amount: 100,
      currency: "USD"
    ) { |g| g.goal_accounts.build(account: fresh_account) }

    assert_equal 0, fresh.pace.to_d
  end

  test "pace averages 90-day net inflow, excluding pending and excluded entries" do
    account = Account.create!(
      family: @family,
      accountable: Depository.new,
      name: "Pace Savings",
      currency: "USD",
      balance: 0
    )
    goal = @family.goals.create!(
      name: "Pace goal",
      target_amount: 10_000,
      currency: "USD"
    ) { |g| g.goal_accounts.build(account: account) }

    # Three inflows over the 90-day window. Sure convention: inflows are
    # negative. Net = -900 → pace = 900 / 3 = 300.
    create_transaction(account: account, amount: -300, date: 80.days.ago.to_date)
    create_transaction(account: account, amount: -300, date: 40.days.ago.to_date)
    create_transaction(account: account, amount: -300, date: 5.days.ago.to_date)

    # Pending inflow that must be excluded by `Transaction.excluding_pending`.
    pending_entry = create_transaction(account: account, amount: -1_000, date: 10.days.ago.to_date)
    pending_entry.transaction.update!(extra: { "plaid" => { "pending" => true } })

    # User-excluded outflow that must be excluded by `entries.excluded = false`.
    excluded_entry = create_transaction(account: account, amount: 500, date: 20.days.ago.to_date)
    excluded_entry.update!(excluded: true)

    # Entry outside the 90-day window — must be ignored.
    create_transaction(account: account, amount: -10_000, date: 200.days.ago.to_date)

    assert_equal 300, goal.pace.to_d
  end

  test "months_of_runway is nil when goal has a target date" do
    assert_not_nil @goal.target_date
    assert_nil @goal.months_of_runway
  end

  test "months_of_runway is nil when pace is zero" do
    fresh = goals(:emergency_fund)
    assert_nil fresh.months_of_runway
  end

  test "AASM transitions" do
    fresh = 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
    @goal.linked_accounts.update_all(balance: 100)
    assert_equal :no_target_date, @goal.status
  end

  test "display_status returns :archived for archived goal regardless of progress" do
    @goal.save!
    @goal.archive!
    assert_equal :archived, @goal.display_status
  end

  test "display_status returns :paused for paused goal regardless of progress" do
    @goal.save!
    @goal.pause!
    assert_equal :paused, @goal.display_status
  end

  test "display_status falls through to status for active goals" do
    @goal.target_amount = 1
    assert_equal :reached, @goal.display_status
  end

  test "any_connected_account? reflects plaid_account presence" do
    assert @goal.any_connected_account?
    only_manual = goals(:emergency_fund)
    only_manual.goal_accounts.where(account_id: @connected.id).destroy_all
    assert_not only_manual.reload.any_connected_account?
  end

  test "pledge_action_label_key flips on manual-only goals" do
    assert_equal "goals.show.pledge_just_transferred", @goal.pledge_action_label_key
    @goal.goal_accounts.where(account_id: @connected.id).destroy_all
    # After removing the only connected account, the goal is manual-only;
    # the copy must flip to "pledge_just_saved" so users aren't told to
    # wait for a sync that won't run. Refetch to exercise the real
    # request lifecycle rather than poking a memo on the same instance.
    reloaded = Goal.find(@goal.id)
    assert_equal "goals.show.pledge_just_saved", reloaded.pledge_action_label_key
  end

  test "explicit allocation backs only the earmarked slice" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Split Savings", currency: "USD", balance: 5_000)
    goal = @family.goals.create!(name: "Earmarked", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1_000)
    end
    assert_equal BigDecimal("1000"), goal.current_balance.to_d
  end

  test "explicit allocation is capped at the account balance via the haircut" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Over Earmark", currency: "USD", balance: 800)
    goal = @family.goals.create!(name: "Over", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 5_000)
    end
    assert_equal BigDecimal("800"), goal.current_balance.to_d
  end

  test "unallocated link claims the balance left after another goal's earmark" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Shared Savings", currency: "USD", balance: 5_000)
    earmarked = @family.goals.create!(name: "Earmarked", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 2_000)
    end
    whole = @family.goals.create!(name: "Whole", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account) # NULL = whole-balance remainder
    end
    assert_equal BigDecimal("2000"), earmarked.current_balance.to_d
    assert_equal BigDecimal("3000"), whole.current_balance.to_d
    # The two goals' shares of the shared account never exceed its balance.
    assert_equal account.balance.to_d, earmarked.current_balance.to_d + whole.current_balance.to_d
  end

  test "over-earmarked account scales fixed slices pro-rata" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Contested Savings", currency: "USD", balance: 5_000)
    a = @family.goals.create!(name: "Goal A", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 4_000)
    end
    b = @family.goals.create!(name: "Goal B", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 4_000)
    end
    # sum_fixed 8000 > balance 5000 -> each scaled by 5000/8000 -> 2500.
    assert_equal BigDecimal("2500"), a.current_balance.to_d
    assert_equal BigDecimal("2500"), b.current_balance.to_d
    assert_equal account.balance.to_d, a.current_balance.to_d + b.current_balance.to_d
  end

  test "archived goals release their earmark from the shared pool" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Release Savings", currency: "USD", balance: 5_000)
    whole = @family.goals.create!(name: "Whole", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    earmarked = @family.goals.create!(name: "Earmarked", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 2_000)
    end
    assert_equal BigDecimal("3000"), Goal.find(whole.id).current_balance.to_d
    earmarked.archive!
    # Archived goal no longer reserves its slice -> whole reclaims it.
    assert_equal BigDecimal("5000"), Goal.find(whole.id).current_balance.to_d
  end

  # --- Lot B1: a reached goal lets go of its money ---

  # The whole scenario from the plan, as one regression test. Before B1 the
  # last line read 2500 / 50%: the completed goal kept reserving, the
  # untouched precaution goal was cut in half by the pro-rata haircut, and
  # archiving froze the wrong figure into the history.
  test "completing a goal frees its siblings and keeps its own history straight" do
    livret = Account.create!(family: @family, accountable: Depository.new, name: "Livret B1", currency: "USD", balance: 10_000)
    precaution = @family.goals.create!(name: "Precaution B1", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: livret, allocated_amount: 5_000)
    end
    vacances = @family.goals.create!(name: "Vacances B1", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: livret, allocated_amount: 5_000)
    end

    assert_equal BigDecimal("5000"), Goal.find(precaution.id).current_balance.to_d
    assert_equal BigDecimal("5000"), Goal.find(vacances.id).current_balance.to_d

    vacances.complete!
    livret.update!(balance: 5_000)

    assert_equal BigDecimal("5000"), Goal.find(precaution.id).current_balance.to_d,
                 "the untouched goal must keep its full earmark once its sibling is done"
    assert_equal 100, Goal.find(precaution.id).progress_percent.to_i
    assert_equal BigDecimal("5000"), Goal.find(vacances.id).current_balance.to_d,
                 "a completed goal reports what it reached, not what is left"

    vacances.archive!
    assert_equal BigDecimal("5000"), Goal.find(vacances.id).current_balance.to_d,
                 "archiving after completion must not rewrite the frozen figure"
  end

  test "a completed goal releases its earmark from the shared pool" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Pool Savings", currency: "USD", balance: 5_000)
    whole = @family.goals.create!(name: "Whole C", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    earmarked = @family.goals.create!(name: "Earmarked C", target_amount: 2_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 2_000)
    end

    assert_equal BigDecimal("3000"), Goal.find(whole.id).current_balance.to_d
    earmarked.complete!
    assert_equal BigDecimal("5000"), Goal.find(whole.id).current_balance.to_d
  end

  test "a completed goal keeps its amount when the balance later drops" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Spent Savings", currency: "USD", balance: 4_000)
    goal = @family.goals.create!(name: "Frozen", target_amount: 4_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end

    goal.complete!
    assert_equal BigDecimal("4000"), goal.completed_amount.to_d
    assert goal.completed_at.present?

    account.update!(balance: 100)
    assert_equal BigDecimal("4000"), Goal.find(goal.id).current_balance.to_d
  end

  # `archive` accepts a goal straight from active or paused, so an archived
  # goal that was never completed has no frozen amount — the read guard is
  # `completed_amount.present?`, not `completed?`.
  test "a goal archived without being completed keeps the live calculation" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Direct Archive", currency: "USD", balance: 3_000)
    goal = @family.goals.create!(name: "Straight to archive", target_amount: 3_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end

    goal.archive!
    assert_nil goal.completed_amount

    account.update!(balance: 250)
    assert_equal BigDecimal("250"), Goal.find(goal.id).current_balance.to_d
  end

  test "reopen and unarchive hand the goal back to the live calculation" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Thaw Savings", currency: "USD", balance: 2_000)

    [ :reopen, :unarchive ].each_with_index do |event, i|
      goal = @family.goals.create!(name: "Thaw #{i}", target_amount: 2_000, currency: "USD") do |g|
        g.goal_accounts.build(account: account, allocated_amount: 1_000)
      end
      goal.complete!
      assert_equal BigDecimal("1000"), goal.completed_amount.to_d

      goal.archive! if event == :unarchive
      goal.public_send("#{event}!")

      assert_nil goal.reload.completed_amount, "#{event} must clear the frozen amount"
      assert_nil goal.completed_at
      account.update!(balance: 500)
      assert_equal BigDecimal("500"), Goal.find(goal.id).current_balance.to_d
      account.update!(balance: 2_000)
      goal.destroy!
    end
  end

  # Pause means "I have stopped feeding this", not "I have released it".
  test "a paused goal keeps reserving its earmark" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Paused Savings", currency: "USD", balance: 5_000)
    whole = @family.goals.create!(name: "Whole P", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    earmarked = @family.goals.create!(name: "Earmarked P", target_amount: 2_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 2_000)
    end

    earmarked.pause!
    assert_equal BigDecimal("3000"), Goal.find(whole.id).current_balance.to_d,
                 "a paused goal must keep its slice out of the pool's reach"
  end

  # free_to_earmark and the pool read the same set of goals — Goal::RELEASED_STATES.
  # If they disagreed, the account would advertise headroom the goals deny.
  test "free_to_earmark and the shared pool agree after a completion" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Agreement Savings", currency: "USD", balance: 5_000)
    goal = @family.goals.create!(name: "Agreeable", target_amount: 1_500, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1_500)
    end

    assert_equal BigDecimal("1500"), account.goal_earmarked_total
    goal.complete!
    account.reload

    assert_equal BigDecimal("0"), account.goal_earmarked_total
    assert_equal BigDecimal("5000"), account.free_to_earmark
    assert_empty Goal.pooled_allocations_for(@family)[account.id].to_a
  end

  test "goals are one_off by default and kind is constrained" do
    assert @goal.one_off?
    assert_not @goal.maintained?

    @goal.kind = "nonsense"
    assert_not @goal.valid?
  end

  # --- Lot B3a: reserves to maintain ---

  test "a reserve is funded at its floor and depleted below it" do
    goal = reserve_goal(balance: 6_000, target: 6_000)
    assert_equal :funded, goal.status

    goal.linked_accounts.first.update!(balance: 4_500)
    assert_equal :depleted, Goal.find(goal.id).status
  end

  # The floor sits between the account's value (6,000) and what was put in
  # (4,000), so the basis alone decides whether the reserve is whole.
  test "a reserve on an investment account on the balance basis counts market gains" do
    goal = investment_reserve(progress_basis: "balance")

    assert_equal :funded, goal.status
    assert_equal BigDecimal("6000"), goal.current_balance.to_d
  end

  test "a reserve on an investment account on the contributions basis leaves market gains out" do
    goal = investment_reserve(progress_basis: "contributions")

    assert_equal :depleted, goal.status
    assert_equal BigDecimal("4000"), goal.current_balance.to_d
  end

  # `complete` releases the earmark. For a reserve that is the opposite of the
  # point, so the transition is refused outright rather than hidden in the UI.
  test "a reserve cannot be completed" do
    goal = reserve_goal(balance: 6_000, target: 6_000)

    assert_not goal.may_complete?
    assert_raises(AASM::InvalidTransition) { goal.complete! }
    assert_equal "active", goal.reload.state
    assert_nil goal.completed_amount
  end

  test "a one_off goal at its target can still be completed" do
    goal = reserve_goal(balance: 6_000, target: 6_000)
    goal.update!(kind: "one_off")

    assert goal.may_complete?
  end

  # monthly_target_amount and pace both derive from target_date, which a
  # reserve does not have — "save X/month to catch up" would be advice about
  # a deadline that does not exist.
  test "a depleted reserve is never behind pace" do
    goal = reserve_goal(balance: 1_000, target: 6_000)

    assert_equal :depleted, goal.status
    assert_not goal.behind_pace?
  end

  # ACTIVE_DISPLAY_STATUS_RANK falls back to 4 for anything unranked, so a
  # missing :depleted entry would sort a drained reserve dead last.
  # Named so the alphabetical tie-break works AGAINST the reserve: only the
  # rank can put it first. Unranked, :depleted would fall back to 4 and land
  # behind the open-ended goal's 2.
  test "a depleted reserve sorts ahead of a goal that needs nothing" do
    drained = reserve_goal(balance: 100, target: 6_000, name: "Zzz drained")
    open_ended = @family.goals.create!(
      name: "Aaa open ended", target_amount: 1_000, currency: "USD"
    ) do |g|
      g.goal_accounts.build(account: Account.create!(
        family: @family, accountable: Depository.new,
        name: "Open Ended Pot", currency: "USD", balance: 900
      ))
    end

    sorted = Goal.active_display_sort([ open_ended, drained ])

    assert_equal :depleted, drained.status
    assert_equal :no_target_date, open_ended.status
    assert_equal drained.id, sorted.first.id,
                 "a reserve below its floor must not sort below a goal that needs nothing"
  end

  # The reserve keeps reserving: a withdrawal creates a shortfall, it does not
  # release the earmark the way completing a one-off does.
  test "a withdrawal leaves a reserve's earmark in place" do
    goal = reserve_goal(balance: 6_000, target: 6_000, allocated: 6_000)
    account = goal.linked_accounts.first

    assert_equal BigDecimal("6000"), account.goal_earmarked_total

    account.update!(balance: 4_000)
    account.reload

    assert_equal BigDecimal("6000"), account.goal_earmarked_total,
                 "a reserve holds its earmark whatever the balance does"
    assert_equal :depleted, Goal.find(goal.id).status
    assert_equal BigDecimal("2000"), Goal.find(goal.id).remaining_amount.to_d
  end

  # --- Lot B3b: a floor expressed in months of expenses ---

  test "months mode is only for a reserve, and needs a number of months" do
    goal = reserve_goal(balance: 6_000, target: 6_000)

    goal.target_mode = "months_of_expenses"
    assert_not goal.valid?, "months mode without target_months must be refused"

    goal.target_months = 6
    assert goal.valid?, goal.errors.full_messages.to_sentence

    goal.kind = "one_off"
    assert_not goal.valid?, "a one-off goal has no months-of-expenses floor"
  end

  test "target_months without months mode is refused" do
    goal = reserve_goal(balance: 6_000, target: 6_000)
    goal.target_months = 6

    assert_not goal.valid?
  end

  # target_amount stays the source of truth — the aggregates that read it must
  # keep working with no knowledge of the mode.
  test "refreshing the floor moves every figure that reads target_amount" do
    goal = reserve_goal(balance: 3_000, target: 1_000)
    goal.update!(target_mode: "months_of_expenses", target_months: 6)
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))

    assert_equal BigDecimal("3000"), goal.refresh_target_from_expenses!

    refreshed = Goal.find(goal.id)
    assert_equal BigDecimal("3000"), refreshed.target_amount.to_d
    assert_equal :funded, refreshed.status
    assert_equal 100, refreshed.progress_percent
  end

  # A family with no spending history computes a floor of zero, which would
  # both break the target_amount > 0 constraint and read as "your reserve is
  # complete". The figure the user has been saving against stands.
  test "a zero median leaves the floor untouched" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    goal = reserve_goal(balance: 3_000, target: 1_000)
    goal.update!(target_mode: "months_of_expenses", target_months: 6)
    assert_equal BigDecimal("3000"), goal.reload.target_amount.to_d

    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("0"))

    assert_nil goal.refresh_target_from_expenses!
    assert_equal BigDecimal("3000"), goal.reload.target_amount.to_d
  end

  # A reserve created in months mode must be right immediately: waiting for
  # the 1st would make the feature's first impression its least convincing.
  test "creating a months-mode reserve computes its floor straight away" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    account = Account.create!(
      family: @family, accountable: Depository.new,
      name: "Fresh Reserve Pot", currency: "USD", balance: 100
    )

    goal = @family.goals.create!(
      name: "Fresh reserve", target_amount: 1, currency: "USD",
      kind: "maintained", target_mode: "months_of_expenses", target_months: 6
    ) { |g| g.goal_accounts.build(account: account) }

    assert_equal BigDecimal("3000"), goal.reload.target_amount.to_d
  end

  test "changing the number of months recomputes the floor at once" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    goal = reserve_goal(balance: 100, target: 1)
    goal.update!(target_mode: "months_of_expenses", target_months: 6)
    assert_equal BigDecimal("3000"), goal.reload.target_amount.to_d

    goal.update!(target_months: 3)
    assert_equal BigDecimal("1500"), goal.reload.target_amount.to_d
  end

  # The monthly job owns the cadence: an unrelated edit must not quietly move
  # a financial figure the user has been saving against.
  test "renaming a months-mode reserve leaves its floor alone" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    goal = reserve_goal(balance: 100, target: 1)
    goal.update!(target_mode: "months_of_expenses", target_months: 6)

    IncomeStatement.any_instance.unstub(:median_expense)
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("900"))
    goal.update!(name: "Renamed reserve")

    assert_equal BigDecimal("3000"), goal.reload.target_amount.to_d,
                 "only the job, or a change of months, may move the floor"
  end

  test "a fixed reserve ignores the refresh entirely" do
    goal = reserve_goal(balance: 3_000, target: 1_000)
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))

    assert_nil goal.refresh_target_from_expenses!
    assert_equal BigDecimal("1000"), goal.reload.target_amount.to_d
  end

  # The form asks this BEFORE the user has picked a kind or a number of months,
  # to decide whether to keep offering the amount field. So it must answer for
  # the family alone, not for what this goal happens to be right now.
  test "derivability is answered for the family, whatever the goal is set to" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    goal = reserve_goal(balance: 3_000, target: 1_000)

    assert goal.months_target_derivable?,
           "a one-off with no months set still has spending to derive from"
  end

  # With nothing to derive from, the typed amount is the only way to give the
  # reserve a target. The form has to be told, or it hides the only field that
  # would let the user do it.
  test "a family with no spending history can derive nothing" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("0"))
    goal = reserve_goal(balance: 3_000, target: 1_000)

    assert_not goal.months_target_derivable?
  end

  test "derivability follows the goal's currency, not the family's" do
    IncomeStatement.any_instance.stubs(:median_expense).returns(BigDecimal("500"))
    goal = reserve_goal(balance: 3_000, target: 1_000)
    goal.stubs(:currency).returns("EUR")
    Money.any_instance.stubs(:exchange_to).raises(
      Money::ConversionError.new(from_currency: "USD", to_currency: "EUR", date: Date.current)
    )

    assert_not goal.months_target_derivable?,
               "no rate for the day means no figure to show, so the field must stay"
  end

  test "account free_to_earmark subtracts non-archived fixed earmarks" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Headroom Savings", currency: "USD", balance: 5_000)
    @family.goals.create!(name: "Earmarker", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1_500)
    end
    assert_equal BigDecimal("1500"), account.goal_earmarked_total
    assert_equal BigDecimal("3500"), account.free_to_earmark
  end

  test "an overdrawn account backs nothing for fixed or whole-balance links" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Overdrawn", currency: "USD", balance: BigDecimal("-100"))
    fixed = @family.goals.create!(name: "Fixed OD", target_amount: 1_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 50)
    end
    whole = @family.goals.create!(name: "Whole OD", target_amount: 1_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    assert_equal 0.to_d, fixed.current_balance.to_d
    assert_equal 0.to_d, whole.current_balance.to_d
  end

  test "an archived goal still shows its own earmark, not the whole balance" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Archived Earmark", currency: "USD", balance: 5_000)
    earmarked = @family.goals.create!(name: "Archived Fixed", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 2_000)
    end
    earmarked.archive!
    # Excluded from the shared pool, but its own earmark is read from its own
    # goal_accounts — so it still reports 2,000, not the whole 5,000.
    assert_equal BigDecimal("2000"), Goal.find(earmarked.id).current_balance.to_d
  end

  test "earmark edits to an existing linked account persist via goal.save!" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Autosave Savings", currency: "USD", balance: 5_000)
    goal = @family.goals.create!(name: "Autosave goal", target_amount: 10_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account) # NULL = whole balance
    end
    ga = goal.goal_accounts.first
    assert_nil ga.allocated_amount
    ga.allocated_amount = 1_500
    goal.save! # autosave: true must persist the dirty existing child
    assert_equal BigDecimal("1500"), goal.goal_accounts.first.reload.allocated_amount
  end

  test "progress_percent memo resets after complete! on the same instance" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Memo Savings", currency: "USD", balance: 100)
    goal = @family.goals.create!(name: "Memo goal", target_amount: 1_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    assert_operator goal.progress_percent, :<, 100 # memoize the underfunded value
    goal.complete!
    assert_equal 100, goal.progress_percent, "stale memo would still report the pre-complete percent"
  end

  test "contributions basis excludes market gains" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage", currency: "USD", balance: 10_000)
    account.balances.create!(date: 10.days.ago.to_date, balance: 10_000, currency: "USD", net_market_flows: 3_000)
    goal = @family.goals.create!(name: "Invest goal", target_amount: 20_000, currency: "USD", progress_basis: "contributions") do |g|
      g.goal_accounts.build(account: account)
    end
    assert_equal "contributions", goal.progress_basis
    # 10,000 value − 3,000 market gain = 7,000 contributed.
    assert_equal BigDecimal("7000"), goal.current_balance.to_d
    assert_equal BigDecimal("10000"), goal.market_value_money.amount
  end

  test "reopen transitions a completed goal back to active" do
    fresh = goals(:emergency_fund)
    fresh.complete!
    assert fresh.completed?
    fresh.reopen!
    assert fresh.active?
  end

  test "investment accounts default to transfer pledge kind, never manual_save" do
    assert_equal "transfer", accounts(:investment).default_pledge_kind
  end

  test "manual crypto accounts default to transfer pledge kind, never manual_save" do
    crypto = accounts(:crypto)
    assert crypto.manual?
    assert_equal "transfer", crypto.default_pledge_kind
  end

  test "a valuation delta on a crypto account never matches a pledge" do
    crypto = accounts(:crypto)
    goal = goals(:vacation_italy)
    goal.goal_accounts.create!(account: crypto)
    pledge = goal.goal_pledges.create!(account: crypto, amount: 100, kind: "manual_save")
    entry = crypto.entries.create!(name: "Reconciliation", date: Date.current, amount: 1100, currency: crypto.currency,
                                   entryable: Valuation.new(kind: "reconciliation"))
    assert_not pledge.matches?(entry, valuation_delta: 100)
  end

  # Linking an account never re-bases a goal. Before, this flipped a
  # balance goal to contributions, so an explicit choice of market value could
  # not survive the next save.
  test "adding an investment account via update keeps a balance goal on balance" do
    goal = goals(:emergency_fund)
    assert_equal "balance", goal.progress_basis
    goal.goal_accounts.build(account: accounts(:investment))
    goal.save!
    assert_equal "balance", goal.reload.progress_basis
  end

  test "an explicit market value choice survives saving and linking another account" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage M", currency: "USD", balance: 10_000)
    goal = @family.goals.create!(name: "Deposit", target_amount: 20_000, currency: "USD", progress_basis: "balance") do |g|
      g.goal_accounts.build(account: account)
    end
    goal.update!(name: "House deposit")
    assert_equal "balance", goal.reload.progress_basis

    goal.goal_accounts.build(account: accounts(:investment))
    goal.save!
    assert_equal "balance", goal.reload.progress_basis
  end

  test "a save does not re-base an existing contributions goal" do
    goal = goals(:emergency_fund)
    goal.update_columns(progress_basis: "contributions")
    goal.update!(name: "Renamed fund")
    assert_equal "contributions", goal.reload.progress_basis
  end

  # Delta, not presence: the same goal read on each basis differs by exactly the
  # account's recorded market gain, so a basis that silently stopped mattering
  # (or a reader that ignored it) would show no difference.
  test "switching basis moves the goal by exactly the market gain" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage S", currency: "USD", balance: 10_000)
    account.balances.create!(date: 10.days.ago.to_date, balance: 10_000, currency: "USD", net_market_flows: 3_000)
    goal = @family.goals.create!(name: "Switch goal", target_amount: 20_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    # Each figure is read off a fresh record, so this measures the basis on its
    # own. Reading through the instance that was saved is the next test's job.
    on_market_value = Goal.find(goal.id).current_balance.to_d

    goal.update!(progress_basis: "contributions")
    on_contributions = Goal.find(goal.id).current_balance.to_d

    goal.update!(progress_basis: "balance")
    back_on_market_value = Goal.find(goal.id).current_balance.to_d

    assert_equal BigDecimal("10000"), on_market_value
    assert_equal BigDecimal("3000"), on_market_value - on_contributions
    assert_equal on_market_value, back_on_market_value
  end

  # The instance that took the save is the one a controller renders, so its
  # memos must follow the new basis. Read before and after on the SAME object:
  # a memo left standing would return the old figure and show no difference.
  test "changing basis refreshes the figures on the saved instance" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage R", currency: "USD", balance: 10_000)
    account.balances.create!(date: 10.days.ago.to_date, balance: 10_000, currency: "USD", net_market_flows: 3_000)
    goal = @family.goals.create!(name: "Refresh goal", target_amount: 20_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account)
    end
    before_balance = goal.current_balance.to_d
    before_money = goal.current_balance_money
    before_percent = goal.progress_percent
    before_remaining = goal.remaining_amount.to_d

    goal.update!(progress_basis: "contributions")

    assert_equal BigDecimal("3000"), before_balance - goal.current_balance.to_d
    assert_equal Money.new(3_000, "USD"), before_money - goal.current_balance_money
    assert_equal BigDecimal("3000"), goal.remaining_amount.to_d - before_remaining
    assert_equal 50, before_percent
    assert_equal 35, goal.progress_percent
  end

  # A completed goal reports the amount frozen when it closed, measured on the
  # basis it had then. Switching basis afterwards would put the new label on
  # the old figure, so the switch is refused until the goal is reopened.
  test "a completed goal refuses a basis change and keeps its snapshot" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage C", currency: "USD", balance: 10_000)
    account.balances.create!(date: 10.days.ago.to_date, balance: 10_000, currency: "USD", net_market_flows: 3_000)
    goal = @family.goals.create!(name: "Closed invest", target_amount: 20_000, currency: "USD", progress_basis: "contributions") do |g|
      g.goal_accounts.build(account: account)
    end
    goal.complete!
    snapshot = goal.reload.completed_amount
    assert_equal BigDecimal("7000"), snapshot.to_d

    assert_not goal.update(progress_basis: "balance")
    assert goal.errors.of_kind?(:progress_basis, :locked_once_completed)
    assert_equal "contributions", goal.reload.progress_basis
    assert_equal snapshot, goal.completed_amount
  end

  # Reopening in the same write must not slip the change through: the persisted
  # snapshot is what the lock reads, as kind_locked_while_released does.
  test "a completed goal refuses a basis change written alongside state active" do
    goal = goals(:emergency_fund)
    goal.complete!

    assert_not goal.update(state: "active", progress_basis: "contributions")
    assert goal.errors.of_kind?(:progress_basis, :locked_once_completed)
    assert_equal "balance", goal.reload.progress_basis
  end

  # The edit form resubmits the basis the goal already has, so an ordinary edit
  # of a completed goal must not trip the lock.
  test "a completed goal still saves an edit that resubmits its own basis" do
    goal = goals(:emergency_fund)
    goal.complete!

    assert goal.update(name: "Fund, done", progress_basis: "balance")
    assert_equal "Fund, done", goal.reload.name
  end

  test "an archived goal that never completed can still change basis" do
    goal = goals(:emergency_fund)
    goal.archive!
    assert_nil goal.reload.completed_amount

    assert goal.update(progress_basis: "contributions")
    assert_equal "contributions", goal.reload.progress_basis
  end

  test "reopening a completed goal frees its basis again" do
    goal = goals(:emergency_fund)
    goal.complete!
    goal.reopen!

    assert goal.update(progress_basis: "contributions")
    assert_equal "contributions", goal.reload.progress_basis
  end

  test "earmark is respected on a contributions-basis goal" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Brokerage2", currency: "USD", balance: 10_000)
    account.balances.create!(date: 5.days.ago.to_date, balance: 10_000, currency: "USD", net_market_flows: 2_000)
    goal = @family.goals.create!(name: "Earmarked invest", target_amount: 20_000, currency: "USD", progress_basis: "contributions") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1_000)
    end
    assert_equal "contributions", goal.progress_basis
    # net contributed = 10,000 − 2,000 = 8,000; earmark 1,000 ≤ 8,000 → 1,000.
    assert_equal BigDecimal("1000"), goal.current_balance.to_d
  end

  test "behind_pace? excludes paused goals even when their raw status is behind" do
    goal = goals(:vacation_italy)
    goal.stubs(:status).returns(:behind)

    assert goal.behind_pace?

    goal.update!(state: "paused")

    assert goal.paused?
    assert_not goal.behind_pace?
  end

  test "summary_for counts behind goals via behind_pace? and sums one currency" do
    behind = goals(:vacation_italy)
    behind.stubs(:status).returns(:behind)
    paused_behind = goals(:emergency_fund)
    paused_behind.update!(state: "paused")
    paused_behind.stubs(:status).returns(:behind)

    summary = Goal.summary_for([ behind, paused_behind ], currency: "USD")

    assert_equal 1, summary[:behind_count]
    assert_kind_of Money, summary[:saved_money]
  end

  # --- Restoring a goal must not recreate a whole-account overlap ---
  #
  # The door-side tests — writing a link onto a contested account — live in
  # goal_account_test.rb. These cover the other way in: a state change, which
  # writes no link at all and so slips past that validation entirely.

  test "restoring an archived goal is refused when its account was claimed meanwhile" do
    account = standoff_account
    away = whole_account_goal("Away", account)
    away.archive!

    # Legitimate while `away` holds nothing: an archived goal releases its
    # accounts, so this claim is exactly what the pool expects.
    whole_account_goal("Claimer", account)

    away.reload
    assert_not away.unarchive!, "expected the restore to be refused"
    assert_equal "archived", away.reload.state
    assert_match "Claimer", away.errors.full_messages.to_sentence
  end

  test "restoring is allowed once the other goal earmarks a fixed slice instead" do
    account = standoff_account
    away = whole_account_goal("Away", account)
    away.archive!
    claimer = whole_account_goal("Claimer", account)

    claimer.goal_accounts.first.update!(allocated_amount: 2_000)

    away.reload
    assert away.unarchive!, away.errors.full_messages.to_sentence
    assert_equal "active", away.reload.state
  end

  test "restoring an archived goal whose account is still free is untouched" do
    account = standoff_account
    away = whole_account_goal("Away", account)
    away.archive!

    away.reload
    assert away.unarchive!, away.errors.full_messages.to_sentence
    assert_equal "active", away.reload.state
  end

  # `paused` is not a released state: a paused goal never let go of its
  # accounts, so nothing can legitimately have claimed one meanwhile. The guard
  # that restricts this check to restores from a released state is what keeps a
  # user holding a LEGACY overlap — data the rule predates — from being
  # stranded on a goal they merely shelved. Built through update_column, since
  # the overlap is exactly what the door now refuses to write.
  test "resuming a paused goal is never blocked, even on a legacy overlap" do
    account = standoff_account
    goal = whole_account_goal("Shelved", account)
    squatter = @family.goals.create!(name: "Squatter", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1)
    end
    squatter.goal_accounts.first.update_column(:allocated_amount, nil)

    goal.pause!

    assert goal.reload.resume!, goal.errors.full_messages.to_sentence
    assert_equal "active", goal.reload.state
  end

  # Once a goal is closed, `current_balance` returns the frozen amount and stops
  # tracking its accounts. Spending them afterwards used to send the projection
  # ratio past 1 — a frozen amount over a live 100 scaled every historical point
  # by the difference, drawing a chart that never happened.
  #
  # The series is stubbed at its collaborator rather than built from Balance
  # rows: ChartSeriesBuilder returns zeros for a fixture account here, and a
  # series of zeros multiplies to zero whatever the ratio, so the assertion
  # would pass without proving anything.
  test "the projection never scales the saved series past the accounts it came from" do
    goal = @family.goals.create!(name: "Closed trip", target_amount: 4_000, currency: "USD") do |g|
      g.goal_accounts.build(account: @depository, allocated_amount: 4_000)
    end
    goal.complete!
    @depository.update!(balance: 100)

    historical = OpenStruct.new(date: Date.current, value: Money.new(5_000, "USD"))
    Balance::ChartSeriesBuilder.any_instance
                               .stubs(:balance_series)
                               .returns(OpenStruct.new(values: [ historical ]))

    payload = Goal.find(goal.id).projection_payload

    # The series is the WHOLE linked-account history, scaled to this goal's
    # share of it. A share cannot exceed the whole, so no point may come out
    # above the historical figure it was scaled from — whatever the frozen
    # amount says. Unclamped this rendered 5,000 x 33.
    assert_not_empty payload[:saved_series], "empty series would make this assertion vacuous"
    assert_operator payload[:saved_series].first[:value].to_d, :<=, 5_000,
                    "a saved point outran the history it was scaled from"
  end


  # AASM runs an event's `after` hook on the non-bang form too, which does not
  # save. The completion snapshot used to be written there, so `complete` left
  # the row `active` in the database carrying a frozen amount and a completion
  # date — a goal still being funded that everything keying off
  # `completed_amount.present?` read as closed.
  test "complete without the bang stamps nothing" do
    goal = completable_goal

    goal.complete

    row = Goal.where(id: goal.id).pick(:state, :completed_amount, :completed_at)
    assert_equal "active", row[0]
    assert_nil row[1]
    assert_nil row[2]
  end

  test "complete! freezes the amount alongside the state it belongs to" do
    goal = completable_goal

    goal.complete!

    row = Goal.where(id: goal.id).pick(:state, :completed_amount, :completed_at)
    assert_equal "completed", row[0]
    assert_equal 4_000, row[1].to_d
    assert_not_nil row[2]
  end

  test "reopening without the bang thaws nothing" do
    goal = completable_goal
    goal.complete!

    goal.reopen

    assert_equal 4_000, Goal.where(id: goal.id).pick(:completed_amount).to_d
  end

  test "reopen! hands the goal back to the live calculation" do
    goal = completable_goal
    goal.complete!

    goal.reopen!

    assert_nil Goal.where(id: goal.id).pick(:completed_amount)
  end

  # --- Review follow-ups on the reserves lot ---

  test "a released goal cannot be turned into a reserve without reopening" do
    goal = @family.goals.create!(name: "Trip", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: standoff_account)
    end
    goal.complete!

    goal.reload.kind = "maintained"

    assert_not goal.valid?
    assert_includes goal.errors[:kind], "Reopen this goal before turning it into a reserve."
  end

  test "reopening first lets the conversion through" do
    goal = @family.goals.create!(name: "Trip", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: standoff_account)
    end
    goal.complete!
    goal.reload.reopen!

    assert goal.reload.update(kind: "maintained"), goal.errors.full_messages.to_sentence
  end

  # The form hides the date for a reserve, so one can only arrive through a
  # conversion or a crafted request. Either way it must not survive: a stored
  # deadline would drive a pace the reserve does not have.
  test "a reserve drops any target date it is given" do
    goal = reserve_goal(balance: 3_000, target: 6_000)

    assert goal.update(target_date: 6.months.from_now.to_date), goal.errors.full_messages.to_sentence
    assert_nil goal.reload.target_date
  end

  test "converting a dated goal into a reserve clears its deadline" do
    goal = @family.goals.create!(
      name: "Trip", target_amount: 5_000, currency: "USD", target_date: 6.months.from_now.to_date
    ) { |g| g.goal_accounts.build(account: standoff_account) }

    goal.update!(kind: "maintained")

    assert_nil goal.reload.target_date
  end

  test "a drained reserve gets a status callout telling it what is missing" do
    goal = reserve_goal(balance: 4_000, target: 6_000)

    assert_equal :depleted, goal.status
    assert_includes goal.status_callout_context.to_s, "2,000"
  end

  test "a reserve at its level has no callout to make" do
    goal = reserve_goal(balance: 6_000, target: 6_000)

    assert_equal :funded, goal.status
    assert_nil goal.status_callout_context
  end

  # Setting state and kind in one write skips the `reopen` transition, so
  # `completed_amount` never thaws. Reading the in-memory state let that
  # through: the goal looked already-reopened while its frozen snapshot
  # survived, and an active reserve reported it forever.
  test "reopening and converting in a single write is refused" do
    goal = @family.goals.create!(name: "Trip", target_amount: 5_000, currency: "USD") do |g|
      g.goal_accounts.build(account: standoff_account)
    end
    goal.complete!
    frozen = goal.reload.completed_amount

    assert_not goal.update(state: "active", kind: "maintained")
    assert_includes goal.errors[:kind], "Reopen this goal before turning it into a reserve."
    assert_equal "completed", goal.reload.state
    assert_equal frozen, goal.reload.completed_amount
  end


  # A reserve holds a level: there is no finish line to project toward and no
  # target to have "hit". It does not reach the projection panel today, but
  # this method reads as the single source of truth for that subtitle.
  test "a reserve is never told it has hit a target" do
    reserve = @family.goals.create!(
      name: "Precaution", target_amount: 1_000, currency: @family.currency, kind: "maintained"
    ) { |g| g.goal_accounts.build(account: attention_pot(1_000), allocated_amount: 1_000) }

    assert_equal :funded, reserve.status
    assert_equal I18n.t("goals.show.projection.reserve"), reserve.projection_summary
  end

  # `needs_attention?` names the pair once. Three places were spelling it out
  # and the Plan card had already fallen behind, leaving a depleted reserve
  # with an amber pill beside a neutral progress bar.
  test "a depleted reserve wants attention just as a goal off its pace does" do
    reserve = @family.goals.create!(
      name: "Precaution", target_amount: 6_000, currency: @family.currency, kind: "maintained"
    ) { |g| g.goal_accounts.build(account: attention_pot(1_000), allocated_amount: 1_000) }

    assert_equal :depleted, reserve.status
    assert reserve.needs_attention?
  end

  test "a funded reserve wants nothing" do
    reserve = @family.goals.create!(
      name: "Precaution", target_amount: 1_000, currency: @family.currency, kind: "maintained"
    ) { |g| g.goal_accounts.build(account: attention_pot(1_000), allocated_amount: 1_000) }

    assert_not reserve.needs_attention?
  end

  # --- months-of-expenses review follow-ups ---

  # The median comes back in FAMILY currency; target_amount is stored in the
  # GOAL's. A EUR reserve in a USD family would otherwise read a 3,000 dollar
  # floor as 3,000 euros, and rewrite it that way every month.
  test "a reserve in another currency gets its floor converted" do
    account = Account.create!(family: @family, accountable: Depository.new,
                              name: "EUR pot", currency: "EUR", balance: 1_000)
    goal = @family.goals.create!(
      name: "Precaution", target_amount: 1_000, currency: "EUR",
      kind: "maintained", target_mode: "months_of_expenses", target_months: 6
    ) { |g| g.goal_accounts.build(account: account, allocated_amount: 1_000) }

    # 500/month family currency x 6 months = 3,000, at a rate of 0.9 = 2,700.
    IncomeStatement.any_instance.stubs(:median_expense).returns(500)
    Money.any_instance.stubs(:exchange_to).returns(Money.new(2_700, "EUR"))

    goal.update!(target_amount: 1)

    assert_equal 2_700, goal.reload.target_amount.to_d
  end

  # The floor is derived in this mode. Without this the edit form could
  # persist an arbitrary figure under a "six months of expenses" label until
  # the next monthly refresh.
  test "a typed amount cannot stand in for a derived floor" do
    goal = months_based_reserve
    IncomeStatement.any_instance.stubs(:median_expense).returns(500)

    goal.update!(target_amount: 99)

    assert_equal 3_000, goal.reload.target_amount.to_d
  end

  # Nothing to derive from and a typed figure on its way in: a stale floor
  # beats a wrong one wearing a computed label.
  test "with no spending history a typed amount does not replace the floor" do
    goal = months_based_reserve
    IncomeStatement.any_instance.stubs(:median_expense).returns(0)

    goal.update!(target_amount: 99)

    assert_equal 3_000, goal.reload.target_amount.to_d
  end


  # The ring is drawn from `progress_percent`, which counts money held plus
  # money already spent on the goal. The headline figure showed only the first
  # half, so a goal that had spent part of its savings sat at a 100% ring
  # beside "3,000 of 5,000" — the two disagreeing on the same card, with
  # nothing to say which one to believe.
  test "the headline figure agrees with the ring after a spend" do
    goal = spent_goal

    assert_equal 100, goal.progress_percent
    assert_equal 5_000, goal.progress_amount_money.amount.to_d,
      "the figure beside the ring still reported the account balance"
  end

  # The amount is part of the total above it, never a second figure beside it:
  # a reader should have nothing to add up.
  test "what was used is reported as part of the total, and only when there is some" do
    goal = spent_goal

    assert goal.any_consumption?
    assert_equal 2_000, goal.consumed_amount_money.amount.to_d
    assert_equal goal.progress_amount_money.amount.to_d,
                 goal.current_balance.to_d + goal.consumed_amount_money.amount.to_d
  end

  test "a goal that has spent nothing says nothing about it" do
    account = Account.create!(family: @family, accountable: Depository.new,
                              name: "Quiet pot", currency: @family.currency, balance: 1_000)
    goal = @family.goals.create!(name: "Quiet", target_amount: 1_000, currency: @family.currency) do |g|
      g.goal_accounts.build(account: account, allocated_amount: 1_000)
    end

    assert_not goal.any_consumption?
  end

  # The ring's saved segment goes through d3.color, which cannot parse a CSS
  # variable; a goal with no colour must still hand it a real colour (#4030).
  test "a goal without a colour gives its ring a real colour" do
    goal = ring_goal(color: "#6471eb")
    goal.update_columns(color: nil)

    saved = Goal.find(goal.id).to_donut_segments_json.find { |segment| segment[:id] == "saved" }

    assert_equal Goal::COLORS.first, saved[:color]
    assert_match(/\A#\h{6}\z/, saved[:color])
  end

  test "a goal with a colour keeps it in its ring, and the remaining track is unchanged" do
    goal = ring_goal(color: "#6471eb")

    segments = Goal.find(goal.id).to_donut_segments_json.index_by { |segment| segment[:id] }

    assert_equal "#6471eb", segments["saved"][:color]
    assert_equal "var(--budget-unused-fill)", segments["unused"][:color]
  end

  # One fallback for both: a colourless goal's ring and avatar show the same
  # colour, so changing either fallback alone fails here.
  test "a colourless goal's ring and avatar fall back to the same colour" do
    goal = ring_goal(color: "#6471eb")
    goal.update_columns(color: nil)
    goal = Goal.find(goal.id)

    saved = goal.to_donut_segments_json.find { |segment| segment[:id] == "saved" }

    assert_equal Goals::AvatarComponent.new(goal: goal).color, saved[:color]
  end

  # --- pace on accounts whose value moves through valuations ---
  #
  # Pace used to read only transaction entries, so an account maintained by
  # balance updates (a manually tracked superannuation fund) always reported 0
  # and every dated goal on it read "behind". Balance rows are created by hand
  # with known dates and values rather than by running a sync, so each
  # precondition is set rather than assumed.

  test "pace on a valuation-tracked account is its balance change over the last year" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000)
    goal = goal_on(account, target_amount: 200_000)

    assert goal.pace_measurable?
    assert_equal 1000, goal.pace.to_d

    # The delta: without a recent balance update there is nothing to measure.
    account.entries.where(entryable_type: "Valuation").destroy_all
    assert_not Goal.find(goal.id).pace_measurable?
  end

  # A goal can now link an account in another currency. The balance path
  # measures in the account's currency, so its pace is converted to the goal's
  # at today's rate, as the transaction path is.
  test "a valuation-tracked pace in another currency is converted to the goal's" do
    ExchangeRate.stubs(:provider).returns(nil)
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000, currency: "EUR")
    goal = goal_on(account, target_amount: 300_000)

    # No rate: the 1,000 EUR/mo is left out and reported, never read as dollars.
    assert goal.pace_measurable?
    assert_equal 0, goal.pace.to_d
    assert goal.currency_conversion_incomplete?

    ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", rate: 1.5, date: Date.current)
    goal = Goal.find(goal.id)
    assert_equal 1500, goal.pace.to_d
    assert_not goal.currency_conversion_incomplete?
  end

  test "a valuation-tracked goal whose required pace is below its balance pace is on track" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000)
    # 9,600 to find over 12 months: 800/mo required against a 1,000/mo pace.
    goal = goal_on(account, target_amount: 121_600, target_date: 360.days.from_now.to_date)

    assert_equal 800, goal.monthly_target_amount.to_d
    assert_equal :on_track, goal.status
  end

  test "on the contributions basis a valuation-tracked pace leaves out market movement" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000)
    account.balances.create!(date: 100.days.ago.to_date, balance: 105_000, currency: "USD", net_market_flows: 2_400)

    # Fixed slices, so the two goals can share the account. Pace is whole-account
    # by design, so the slice does not change it.
    balance_goal = goal_on(account, target_amount: 200_000, basis: "balance", name: "Balance basis", allocated: 1_000)
    contributions_goal = goal_on(account, target_amount: 200_000, basis: "contributions",
                                          name: "Contributions basis", allocated: 1_000)

    assert_equal 1000, balance_goal.pace.to_d
    # (12,000 change − 2,400 market gain) / 12.
    assert_equal 800, contributions_goal.pace.to_d
  end

  test "a shrinking valuation-tracked account is measured and behind" do
    account = valuation_tracked_account(balance: 88_000, year_ago: 100_000)
    goal = goal_on(account, target_amount: 120_000, target_date: 360.days.from_now.to_date)

    assert goal.pace_measurable?
    assert_equal(-1000, goal.pace.to_d)
    assert_equal :behind, goal.status
  end

  test "an account with only an opening anchor has no measurable pace" do
    account = Account.create!(family: @family, accountable: Investment.new, name: "Second super",
                              currency: "USD", balance: 94_000)
    create_valuation(account: account, amount: 94_000, date: 30.days.ago.to_date, kind: "opening_anchor")
    # A year of balance history, so only the anchor's kind decides: an opening
    # balance is where tracking started, not a recorded update.
    account.balances.create!(date: 400.days.ago.to_date, balance: 80_000, start_cash_balance: 80_000, currency: "USD")
    goal = goal_on(account, target_amount: 200_000, target_date: 2.years.from_now.to_date)

    assert_not goal.pace_measurable?
    assert_equal :pace_unknown, goal.status
    assert_not goal.behind_pace?
    assert_equal I18n.t("goals.show.projection.pace_unknown"), goal.projection_summary
    assert_equal I18n.t("goals.show.status_callout.pace_unknown"), goal.status_callout_context

    payload = goal.projection_payload
    assert_nil payload[:avg_monthly]
    assert_nil payload[:projection_shortfall_label]
    assert_equal goal.current_balance.to_d, goal.projection_end_amount
  end

  test "under a year of balance history is not read as growth from zero" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000, history_from: 200.days.ago.to_date)
    goal = goal_on(account, target_amount: 121_600, target_date: 360.days.from_now.to_date)

    assert_equal :pace_unknown, goal.status
    assert_equal 0, goal.pace.to_d
  end

  # A recent valuation shows the balance moves outside transactions, so an old
  # transaction must not make the account a measured zero (dormant).
  test "a recent valuation on under a year of history stays unmeasured despite an older transaction" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000, history_from: 200.days.ago.to_date)
    create_transaction(account: account, amount: -1_000, date: 150.days.ago.to_date)
    goal = goal_on(account, target_amount: 121_600, target_date: 360.days.from_now.to_date)

    assert_not goal.pace_measurable?
    assert_equal :pace_unknown, goal.status
  end

  test "a balance update exactly a year old still counts, one a day older does not" do
    on_edge = valuation_tracked_account(balance: 112_000, year_ago: 100_000, valuation_on: Date.current - 365)
    past_edge = valuation_tracked_account(balance: 112_000, year_ago: 100_000, valuation_on: Date.current - 366)

    assert goal_on(on_edge, target_amount: 200_000, name: "Edge").pace_measurable?
    assert_not goal_on(past_edge, target_amount: 200_000, name: "Past edge").pace_measurable?
  end

  test "a counted transaction in the window keeps the account on transaction pace" do
    account = valuation_tracked_account(balance: 112_000, year_ago: 100_000)
    create_transaction(account: account, amount: -300, date: 10.days.ago.to_date)
    goal = goal_on(account, target_amount: 200_000)

    # 300 inflow / 3, the 12,000 balance change ignored.
    assert_equal 100, goal.pace.to_d

    account.entries.where(entryable_type: "Transaction").destroy_all
    assert_equal 1000, Goal.find(goal.id).pace.to_d
  end

  # The transaction path leaves excluded entries out of pace; the balance path
  # reads stored balances, which include them. Without taking them back out, an
  # excluded deposit that pushed the account off the transaction path would be
  # counted after all, through the balance change.
  test "an excluded deposit does not count towards a valuation-tracked pace" do
    account = valuation_tracked_account(balance: 124_000, year_ago: 100_000)
    goal = goal_on(account, target_amount: 200_000)
    # 24,000 change / 12 before anything is excluded.
    assert_equal 2000, goal.pace.to_d

    create_transaction(account: account, amount: -12_000, date: 10.days.ago.to_date, excluded: true)

    # Still on the balance path (an excluded entry is no counted transaction),
    # and the excluded 12,000 leaves (24,000 − 12,000) / 12.
    assert_equal 1000, Goal.find(goal.id).pace.to_d
  end

  # The stored balance converts each entry to the account's currency, so the
  # excluded sum taken back out of it has to be in that currency too.
  test "an excluded deposit in another currency comes out at its converted amount" do
    account = valuation_tracked_account(balance: 124_000, year_ago: 100_000)
    ExchangeRate.create!(date: 10.days.ago.to_date, from_currency: "EUR", to_currency: "USD", rate: 2)
    create_transaction(account: account, amount: -6_000, currency: "EUR", date: 10.days.ago.to_date, excluded: true)

    # (24,000 − 12,000) / 12; read raw, the 6,000 would leave 1,500.
    assert_equal 1000, goal_on(account, target_amount: 200_000).pace.to_d
  end

  # The balance leaves out an entry it cannot convert, so there is nothing of
  # it in the change to take back out.
  test "an excluded entry with no exchange rate is left alone, as the balance leaves it" do
    account = valuation_tracked_account(balance: 124_000, year_ago: 100_000)
    create_transaction(account: account, amount: -6_000, currency: "EUR", date: 10.days.ago.to_date, excluded: true)

    assert_equal 2000, goal_on(account, target_amount: 200_000).pace.to_d
  end

  # A split parent is always excluded, but the balance counts its children
  # instead of it. Taking the parent out as well would remove the money twice.
  test "a split parent is not taken out of a valuation-tracked pace on top of its children" do
    account = valuation_tracked_account(balance: 124_000, year_ago: 100_000)
    parent = create_transaction(account: account, amount: -12_000, date: 10.days.ago.to_date)
    parent.split!([
      { name: "Gift", amount: -8_000, excluded: "1" },
      { name: "Refund", amount: -4_000, excluded: "1" }
    ])
    assert parent.reload.excluded?

    # The children's 12,000 out once: (24,000 − 12,000) / 12.
    assert_equal 1000, goal_on(account, target_amount: 200_000).pace.to_d
  end

  test "an excluded entry older than the pace year does not move the balance pace" do
    account = valuation_tracked_account(balance: 124_000, year_ago: 100_000)
    create_transaction(account: account, amount: -12_000, date: 380.days.ago.to_date, excluded: true)

    # Already inside the year-old balance it is measured from.
    assert_equal 2000, goal_on(account, target_amount: 200_000).pace.to_d
  end

  # An account whose only transactions are excluded has no counted history, so
  # it is not a measured zero.
  test "an account whose only transactions are excluded is not dormant" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Excluded only",
                              currency: "USD", balance: 1_000)
    create_transaction(account: account, amount: -1_000, date: 200.days.ago.to_date, excluded: true)
    goal = goal_on(account, target_amount: 5_000, target_date: 360.days.from_now.to_date)

    assert_not goal.pace_measurable?
    assert_equal :pace_unknown, goal.status

    account.entries.update_all(excluded: false)
    assert Goal.find(goal.id).pace_measurable?
  end

  # A pending transaction can still be reversed, so it is no evidence that the
  # account has a history of being paid into. pace_for leaves it out too.
  test "an account whose only transaction is pending is not dormant" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Pending only",
                              currency: "USD", balance: 1_000)
    entry = create_transaction(account: account, amount: -1_000, date: 200.days.ago.to_date)
    entry.transaction.update!(extra: { "plaid" => { "pending" => true } })
    goal = goal_on(account, target_amount: 5_000, target_date: 360.days.from_now.to_date)

    assert_not goal.pace_measurable?
    assert_equal :pace_unknown, goal.status

    entry.transaction.update!(extra: {})
    assert Goal.find(goal.id).pace_measurable?
  end

  test "a cash account nobody has paid into lately is still measured and behind" do
    account = Account.create!(family: @family, accountable: Depository.new, name: "Dormant savings",
                              currency: "USD", balance: 1_000)
    create_transaction(account: account, amount: -1_000, date: 200.days.ago.to_date)
    goal = goal_on(account, target_amount: 5_000, target_date: 360.days.from_now.to_date)

    assert goal.pace_measurable?
    assert_equal 0, goal.pace.to_d
    assert_equal :behind, goal.status
  end

  test "one unmeasurable account makes the goal unmeasurable, two measured accounts sum" do
    cash = Account.create!(family: @family, accountable: Depository.new, name: "Paid savings",
                           currency: "USD", balance: 900)
    create_transaction(account: cash, amount: -900, date: 10.days.ago.to_date)
    anchored = Account.create!(family: @family, accountable: Depository.new, name: "Anchored savings",
                               currency: "USD", balance: 500)
    create_valuation(account: anchored, amount: 500, date: 30.days.ago.to_date, kind: "opening_anchor")
    valued = valuation_tracked_account(balance: 112_000, year_ago: 100_000, accountable: Depository.new)

    mixed = goal_on([ cash, anchored ], target_amount: 50_000, target_date: 360.days.from_now.to_date, name: "Mixed")
    assert_equal :pace_unknown, mixed.status
    # The measured half has a pace of its own (300/mo); the projection still
    # claims nothing beyond today's balance.
    assert_equal 300, mixed.pace.to_d
    assert_equal mixed.current_balance.to_d, mixed.projection_end_amount

    # Fixed slices: `cash` is already claimed in full by the goal above.
    measured = goal_on([ cash, valued ], target_amount: 500_000, name: "Measured", allocated: 100)
    assert measured.pace_measurable?
    assert_equal 1300, measured.pace.to_d
  end

  test "an open-ended goal with an unmeasurable account claims no runway" do
    cash = Account.create!(family: @family, accountable: Depository.new, name: "Paid savings",
                           currency: "USD", balance: 900)
    create_transaction(account: cash, amount: -900, date: 10.days.ago.to_date)
    anchored = Account.create!(family: @family, accountable: Depository.new, name: "Anchored savings",
                               currency: "USD", balance: 500)
    create_valuation(account: anchored, amount: 500, date: 30.days.ago.to_date, kind: "opening_anchor")

    measured = goal_on(cash, target_amount: 50_000, name: "Measured", allocated: 450)
    assert_equal BigDecimal("1.5"), measured.months_of_runway

    # The measured half still has a pace of 300/mo of its own.
    mixed = goal_on([ cash, anchored ], target_amount: 50_000, name: "Mixed", allocated: 450)
    assert_equal 300, mixed.pace.to_d
    assert_nil mixed.months_of_runway
  end

  test "a goal whose pace cannot be measured sorts beside open-ended goals" do
    on_track = goal_on(valuation_tracked_account(balance: 112_000, year_ago: 100_000),
                       target_amount: 121_600, target_date: 360.days.from_now.to_date, name: "Zeta on track")
    anchored = Account.create!(family: @family, accountable: Depository.new, name: "Anchored pot",
                               currency: "USD", balance: 500)
    create_valuation(account: anchored, amount: 500, date: 30.days.ago.to_date, kind: "opening_anchor")
    unknown = goal_on(anchored, target_amount: 5_000, target_date: 360.days.from_now.to_date, name: "Middle unknown")
    reserve = reserve_goal(balance: 2_000, target: 1_000, allocated: 1_000, name: "Alpha reserve")

    assert_equal :on_track, on_track.status
    assert_equal :pace_unknown, unknown.status
    assert_equal :funded, reserve.status
    assert_equal [ on_track.id, unknown.id, reserve.id ], Goal.active_display_sort([ reserve, unknown, on_track ]).map(&:id)
  end

  test "an open-ended valuation-tracked goal gets a runway from its balance pace" do
    goal = goal_on(valuation_tracked_account(balance: 112_000, year_ago: 100_000), target_amount: 500_000)

    assert_equal :no_target_date, goal.status
    assert_equal BigDecimal("112.0"), goal.months_of_runway
  end

  test "the evidence behind pace is read once for the family, not once per goal" do
    goal_on(valuation_tracked_account(balance: 112_000, year_ago: 100_000), target_amount: 500_000, name: "Super A")
    goal_on(valuation_tracked_account(balance: 50_000, year_ago: 40_000), target_amount: 500_000, name: "Super B")

    goals = Goal.prepared_for(@family)
    assert_operator goals.size, :>=, 5

    # Uncached: the query cache would otherwise answer a per-goal fallback
    # read, and assert_no_queries does not count cached queries.
    ActiveRecord::Base.uncached do
      assert_no_queries do
        goals.each do |goal|
          goal.pace
          goal.pace_measurable?
        end
      end
    end
  end

  # pace_for, balance_pace_for and market_flows_for all start from the same
  # family-wide list of linked accounts.
  test "preparing the goals reads the family's linked accounts once" do
    goal_on(valuation_tracked_account(balance: 112_000, year_ago: 100_000), target_amount: 500_000)

    plucks = 0
    sub = ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
      plucks += 1 if payload[:sql].to_s.include?('SELECT DISTINCT "goal_accounts"."account_id"')
    end
    ActiveRecord::Base.uncached { Goal.prepared_for(@family) }

    assert_equal 1, plucks
  ensure
    ActiveSupport::Notifications.unsubscribe(sub) if sub
  end

  private
    # A manual account whose value moves only through balance updates: a
    # balance row at `history_from`, a reconciliation valuation on
    # `valuation_on`, today's value on the account itself, no transactions.
    def valuation_tracked_account(balance:, year_ago:, history_from: 400.days.ago.to_date,
                                  valuation_on: 30.days.ago.to_date, accountable: Investment.new, currency: "USD")
      account = Account.create!(family: @family, accountable: accountable,
                                name: "Super #{SecureRandom.hex(3)}", currency: currency, balance: balance)
      account.balances.create!(date: history_from, balance: year_ago, start_cash_balance: year_ago, currency: currency)
      create_valuation(account: account, amount: balance, date: valuation_on, currency: currency)
      account
    end

    # Reloaded, so every figure is read fresh rather than from a memo.
    def goal_on(accounts, target_amount:, target_date: nil, basis: "balance", name: "Pace goal #{SecureRandom.hex(3)}", allocated: nil)
      goal = @family.goals.create!(name: name, target_amount: target_amount, target_date: target_date, currency: "USD") do |g|
        Array(accounts).each { |account| g.goal_accounts.build(account: account, allocated_amount: allocated) }
      end
      goal.update_column(:progress_basis, basis)
      Goal.find(goal.id)
    end

    # A goal with something saved and something left, so both segments exist.
    def ring_goal(color:)
      account = Account.create!(family: @family, accountable: Depository.new, name: "Ring savings", currency: "USD", balance: 2_000)
      @family.goals.create!(name: "Ring goal", target_amount: 5_000, currency: "USD", color: color) do |g|
        g.goal_accounts.build(account: account)
      end
    end


    # 5,000 saved, 2,000 of it since spent on the thing itself.
    def spent_goal
      account = Account.create!(family: @family, accountable: Depository.new,
                                name: "Spent pot #{SecureRandom.hex(3)}",
                                currency: @family.currency, balance: 5_000)
      goal = @family.goals.create!(name: "Trip", target_amount: 5_000, currency: @family.currency) do |g|
        g.goal_accounts.build(account: account, allocated_amount: 5_000)
      end
      goal.consume!(2_000)
      goal
    end

    # Its own account, so the shared-pool haircut does not make the frozen
    # figure depend on what the fixtures happen to claim.
    def completable_goal
      account = Account.create!(family: @family, accountable: Depository.new,
                                name: "Close pot #{SecureRandom.hex(3)}",
                                currency: @family.currency, balance: 4_000)
      @family.goals.create!(name: "Trip", target_amount: 4_000, currency: @family.currency) do |g|
        g.goal_accounts.build(account: account, allocated_amount: 4_000)
      end
    end

    def attention_pot(balance)
      Account.create!(family: @family, accountable: Depository.new,
                      name: "Pot #{SecureRandom.hex(3)}",
                      currency: @family.currency, balance: balance)
  end

    def months_based_reserve
      account = Account.create!(family: @family, accountable: Depository.new,
                                name: "Reserve pot #{SecureRandom.hex(3)}",
                                currency: @family.currency, balance: 3_000)
      IncomeStatement.any_instance.stubs(:median_expense).returns(500)
      @family.goals.create!(
        name: "Precaution", target_amount: 3_000, currency: @family.currency,
        kind: "maintained", target_mode: "months_of_expenses", target_months: 6
      ) { |g| g.goal_accounts.build(account: account, allocated_amount: 3_000) }
    end
    # A fresh account: the fixtures deliberately carry three goals holding
    # whole-account links on `depository`, a legacy overlap the exclusivity
    # rule tolerates but which would muddy every assertion here.
    def standoff_account
      Account.create!(
        family: @family, accountable: Depository.new,
        name: "Standoff Savings #{SecureRandom.hex(4)}", currency: "USD", balance: 6_000
      )
    end

    def whole_account_goal(name, account)
      @family.goals.create!(name: name, target_amount: 5_000, currency: "USD") do |goal|
        goal.goal_accounts.build(account: account)
      end
    end

    def investment_reserve(progress_basis:)
      account = Account.create!(family: @family, accountable: Investment.new, name: "Reserve brokerage", currency: "USD", balance: 6_000)
      account.balances.create!(date: 10.days.ago.to_date, balance: 6_000, currency: "USD", net_market_flows: 2_000)
      @family.goals.create!(name: "Invested reserve", target_amount: 5_000, currency: "USD", kind: "maintained", progress_basis: progress_basis) do |g|
        g.goal_accounts.build(account: account)
      end
    end

    def reserve_goal(balance:, target:, allocated: nil, name: "Emergency reserve")
      account = Account.create!(
        family: @family, accountable: Depository.new,
        name: "#{name} Pot", currency: "USD", balance: balance
      )
      @family.goals.create!(name: name, target_amount: target, currency: "USD", kind: "maintained") do |g|
        g.goal_accounts.build(account: account, allocated_amount: allocated)
      end
    end

    # :funded and paused both used to rank 3, so a paused goal whose name sorted
    # first jumped ahead of a reserve that was whole.
    test "a paused goal sorts behind a funded reserve whatever its name" do
      pot = ->(name) {
        Account.create!(family: @family, accountable: Depository.new, name: name,
                        currency: @family.currency, balance: 2_000)
      }
      reserve = @family.goals.create!(
        name: "Zeta reserve", target_amount: 1_000, currency: @family.currency, kind: "maintained"
      ) { |g| g.goal_accounts.build(account: pot.call("Zeta pot"), allocated_amount: 1_000) }
      paused = @family.goals.create!(
        name: "Alpha goal", target_amount: 1_000, currency: @family.currency
      ) { |g| g.goal_accounts.build(account: pot.call("Alpha pot"), allocated_amount: 1_000) }
      paused.pause!

      sorted = Goal.active_display_sort([ paused, reserve ])

      assert_equal [ reserve.id, paused.id ], sorted.map(&:id)
    end
end