Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
37.5 KB
Raw Blame
require "test_helper"

class Loan::PayoffChartTest < ActiveSupport::TestCase
  setup do
    @family = families(:dylan_family)
    @today = Date.new(2027, 1, 15)
    # What the picker hands over for "All" on @today. The real all-time period
    # ends on Date.current, and these tests sit on a pinned date.
    @all_time = Period.new(key: "all_time", start_date: Date.new(2020, 1, 1), end_date: @today)
  end

  test "carries the recorded balance, the original schedule and the projection" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today).payload

    assert payload[:actual].length > 1
    assert payload[:scheduled].length > 1
    assert payload[:projected].length > 1
    assert_equal %w[actual scheduled projected], payload[:visible].map(&:to_s)
    assert_equal @today.iso8601, payload[:today]
    assert_equal "USD", payload[:currency]
    assert_not payload.key?(:accelerated), "the what-if left this tranche (#100 decision 10)"
  end

  # The amount borrowed is the first thing a payoff chart should show, and
  # every point after it is the schedule's own row for that date.
  test "the scheduled series is the schedule, opening at origination with the full principal" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today).payload
    rows = loan.amortization_schedule.payments.index_by { |row| row.date.iso8601 }

    assert_equal loan.origination_date.iso8601, payload[:scheduled].first[:date]
    assert_equal loan.amortization_schedule.principal.to_f, payload[:scheduled].first[:balance]
    payload[:scheduled].drop(1).each do |point|
      assert_equal rows.fetch(point[:date]).ending_balance.amount.to_f, point[:balance],
        "the scheduled point on #{point[:date]} must be the schedule's own row"
    end
    assert_equal loan.amortization_schedule.payoff_date.iso8601, payload[:scheduled].last[:date]
  end

  test "the projection opens at today's real balance" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today).payload

    assert_equal @today.iso8601, payload[:projected].first[:date]
    assert_equal loan.account.balance.to_f, payload[:projected].first[:balance]
  end

  # A period picked on a loan's chart means what it means on every other
  # chart: 90D is the last ninety days.
  # The recorded series runs from the window's start to today, and never before
  # origination. Queried past today it would carry today's balance forward as
  # a flat line.
  test "the actual series runs from the window's start to today, in the loan's currency" do
    loan = on_contract_loan

    whole_life = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload
    assert_equal @today.iso8601, whole_life[:actual].last[:date]
    assert_operator Date.iso8601(whole_life[:actual].first[:date]), :>=, loan.origination_date

    window_start = @today - 90
    ninety_days = Period.new(key: "last_90_days", start_date: window_start, end_date: @today)
    clipped = Loan::PayoffChart.new(loan, as_of: @today, period: ninety_days).payload
    assert_equal window_start.iso8601, clipped[:actual].first[:date]
    assert_equal @today.iso8601, clipped[:actual].last[:date]
    assert clipped[:actual].all? { |point| Date.iso8601(point[:date]).between?(window_start, @today) }

    assert_equal "USD", clipped[:currency]
    assert_equal loan.account.balance.to_f, whole_life[:actual].last[:balance],
      "the recorded balance is read in the loan's own currency, so no exchange rate applies"
  end

  # Under All the domain is the loan's whole life. Under a window it is the
  # period's own dates, opening no earlier than the loan: a 5Y window on a
  # one-year-old loan opens at origination.
  test "the domain is the whole life under All and the picked period otherwise, clamped at origination" do
    loan = on_contract_loan
    start = loan.origination_date
    whole_life_end = [ loan.amortization_schedule.payoff_date, loan.payoff_projection(as_of: @today).payoff_date ].max

    whole_life = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload
    assert_equal start.iso8601, whole_life[:domain_start]
    assert_equal whole_life_end.iso8601, whole_life[:domain_end]

    window_starts.each do |key, window_start|
      period = Period.new(key: key, start_date: window_start, end_date: @today)
      windowed = Loan::PayoffChart.new(loan, as_of: @today, period: period).payload
      expected_start = [ window_start, start ].max
      assert_equal expected_start.iso8601, windowed[:domain_start], "#{key} must open on #{expected_start}"
      assert_equal @today.iso8601, windowed[:domain_end], "#{key} must end today, where the period does"
    end
  end

  # A window ends today, so the projection has at most one point inside the
  # domain -- today -- and a point is not a line. The legend must not promise
  # one; All reaches past today and draws it.
  test "forward series are visible under All alone" do
    loan = on_contract_loan

    window_starts.each do |key, window_start|
      period = Period.new(key: key, start_date: window_start, end_date: @today)
      windowed = Loan::PayoffChart.new(loan, as_of: @today, period: period).payload
      assert_equal %w[actual scheduled], windowed[:visible].map(&:to_s), key
      assert windowed[:projected].length > 1, "the series is still in the payload; only its legend entry is withheld"
    end

    whole_life = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload
    assert_includes whole_life[:visible].map(&:to_s), "projected"
  end

  # A loan drawn down years before the app
  # tracked it -- a 2016 mortgage added in 2024 -- must draw its recorded
  # balance under every period it offers, not the contract alone. Windows that
  # counted forward from origination ended before the history began.
  test "an older loan whose history starts after origination draws recorded balances under every window" do
    loan = older_loan
    first_recorded = Date.new(2024, 1, 1)

    window_starts.each do |key, window_start|
      period = Period.new(key: key, start_date: window_start, end_date: @today)
      payload = Loan::PayoffChart.new(loan, as_of: @today, period: period).payload

      assert_equal [ window_start, loan.origination_date ].max.iso8601, payload[:domain_start], key
      assert_equal @today.iso8601, payload[:domain_end], key
      assert_equal [ window_start, first_recorded ].max.iso8601, payload[:actual].first[:date],
        "#{key}: the recorded series starts where the window or the history does, whichever is later"
      assert_equal @today.iso8601, payload[:actual].last[:date], key
      assert_equal %w[actual scheduled], payload[:visible].map(&:to_s), "#{key}: both the recorded balance and the contract draw a line"
    end

    whole_life = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload
    assert_equal loan.origination_date.iso8601, whole_life[:domain_start]
    assert_equal first_recorded.iso8601, whole_life[:actual].first[:date]
    assert_equal %w[actual scheduled projected], whole_life[:visible].map(&:to_s)
  end

  # The picker's choice is shared with every account, so a loan page can be
  # opened under a period its chart does not offer. That shows the whole life.
  test "a saved period the loan chart does not offer shows the loan's whole life" do
    loan = on_contract_loan
    whole_life = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload

    %w[last_7_days last_30_days current_week last_month].each do |key|
      period = Period.new(key: key, start_date: @today - 30, end_date: @today)
      payload = Loan::PayoffChart.new(loan, as_of: @today, period: period).payload
      assert_equal whole_life.values_at(:domain_start, :domain_end), payload.values_at(:domain_start, :domain_end),
        "#{key} is not a loan window, so the chart shows the whole life"
    end
  end

  # WINDOW_KEYS is matched against the saved Period key. A key that is not a
  # real Period key would never match, and that window would silently show the
  # whole life instead.
  test "every loan window is a shared period key" do
    assert_empty Loan::PayoffChart::WINDOW_KEYS - Period::PERIODS.keys,
      "loan chart windows must use Period::PERIODS keys"
  end

  # Overlapping the schedule IS the on-track picture; the projection is not
  # withheld for agreeing with the contract.
  test "the projection is drawn when it overlaps the schedule and withheld only when it cannot run" do
    on_track = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload
    assert on_track[:projected].length > 1
    assert_includes on_track[:visible].map(&:to_s), "projected"

    cleared = build_loan
    cleared.account.update!(balance: 0)
    paid_off = Loan::PayoffChart.new(cleared.reload, as_of: @today).payload
    assert_empty paid_off[:projected]
    assert_not_includes paid_off[:visible].map(&:to_s), "projected"
  end

  # A loan drawn down today has one recorded point at most and no history to
  # compare against; the page must still render.
  test "a loan originated today renders a valid payload with no exception" do
    # Created on the pinned day itself: an origination date may not lie in the
    # future, and @today is ahead of the calendar.
    account = travel_to(@today) do
      Account.create!(
        family: @family, name: "New Loan", balance: 500_000, currency: "USD",
        accountable: Loan.new(subtype: "mortgage", interest_rate: 6, term_months: 24,
                              rate_type: "fixed", start_date: @today)
      )
    end
    account.balances.create!(date: @today, balance: 500_000, currency: "USD",
                             start_cash_balance: 500_000, flows_factor: -1)

    payload = Loan::PayoffChart.new(account.loan, as_of: @today, period: @all_time).payload

    assert_equal @today.iso8601, payload[:scheduled].first[:date]
    assert_equal @today.iso8601, payload[:domain_start]
    assert_operator payload[:actual].length, :<=, 1
    assert_not_includes payload[:visible].map(&:to_s), "actual", "one point is not a line"
    assert_includes payload[:visible].map(&:to_s), "projected"
  end

  test "no payload at all for a loan with no schedule" do
    loan = build_loan(rate_type: "")

    assert_nil Loan::PayoffChart.new(loan, as_of: @today).payload
  end

  test "a loan with no recorded balances yet draws no actual series and does not raise" do
    loan = build_loan
    loan.account.balances.delete_all

    payload = Loan::PayoffChart.new(loan.reload, as_of: @today).payload

    assert_empty payload[:actual]
    assert payload[:scheduled].length > 1
  end

  test "the accessible description names the balance and each payoff date separately" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today).payload
    schedule = loan.amortization_schedule
    projection = loan.payoff_projection(as_of: @today)

    assert_includes payload[:aria_description], projection.current_balance.format
    assert_includes payload[:aria_description], I18n.l(schedule.payoff_date, format: :long)
    assert_includes payload[:aria_description], I18n.l(projection.payoff_date, format: :long)
    assert_not_includes payload[:aria_description], I18n.t(
      "UI.account.chart.loan.aria_actual_history_starts",
      date: I18n.l(loan.origination_date, format: :long)
    )
  end

  # The tooltip on each scheduled payment splits it into principal and interest
  # (#4006), so a screen reader is told the same detail exists.
  test "the accessible description says each scheduled payment's tooltip splits principal and interest" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload

    composition = composition_sentence
    assert_match(/Principal.*Interest/, composition)
    assert_includes payload[:aria_description], composition
  end

  # The tooltip stops at plotted dates with no split when no scheduled payment
  # falls in the window, so the description must not promise one there.
  test "the accessible description does not mention the split for a window past the last scheduled payment" do
    loan = build_loan
    as_of = loan.amortization_schedule.payoff_date + 45
    window = Period.new(key: "current_month", start_date: as_of.beginning_of_month, end_date: as_of)

    payload = Loan::PayoffChart.new(loan, as_of: as_of, period: window).payload

    assert_empty payload[:scheduled].select { |point|
      point.key?(:principal) && Date.iso8601(point[:date]).between?(Date.iso8601(payload[:domain_start]), Date.iso8601(payload[:domain_end]))
    }, "the window must hold no scheduled payment for this test to mean anything"
    assert_not_includes payload[:aria_description], composition_sentence

    whole_life = Loan::PayoffChart.new(loan, as_of: as_of).payload
    assert_includes whole_life[:aria_description], composition_sentence
  end

  # The tooltip stops at a payment on the window's start edge, as the chart
  # controller includes it, and shows its split, so the description says so.
  test "the accessible description mentions the split for a window starting on the final repayment" do
    loan = build_loan
    final = loan.amortization_schedule.payoff_date
    as_of = final + 5
    window = Period.new(key: "current_month", start_date: final, end_date: as_of)

    payload = Loan::PayoffChart.new(loan, as_of: as_of, period: window).payload

    assert_equal final.iso8601, payload[:scheduled].last[:date], "the final repayment must sit on the window's start"
    assert_includes payload[:aria_description], composition_sentence
  end

  # A window between two payments holds none, so the tooltip stops at the
  # plotted dates in it, and the scheduled row there shows the nearest
  # payment's split.
  test "the accessible description mentions the split for a window between two payments" do
    loan = build_loan
    dates = loan.amortization_schedule.payments.map(&:date)
    gap_start = dates[5] + 1
    window = Period.new(key: "current_month", start_date: gap_start, end_date: dates[6] - 1)

    payload = Loan::PayoffChart.new(loan, as_of: dates[6] - 1, period: window).payload

    assert_empty payload[:scheduled].select { |point| Date.iso8601(point[:date]).between?(gap_start, dates[6] - 1) },
      "the window must hold no scheduled point for this test to mean anything"
    assert_includes payload[:aria_description], composition_sentence
  end

  test "the accessible description dates recorded history when it starts after origination" do
    loan = on_contract_loan
    first_recorded_date = loan.origination_date >> 6
    loan.account.balances.where(date: ...first_recorded_date).delete_all

    payload = Loan::PayoffChart.new(loan.reload, as_of: @today).payload

    assert_equal first_recorded_date.iso8601, payload[:actual].first[:date]
    assert_includes payload[:aria_description], I18n.t(
      "UI.account.chart.loan.aria_actual_history_starts",
      date: I18n.l(first_recorded_date, format: :long)
    )
  end

  test "German loan chart translations cover the English chart keys" do
    english = I18n.t("UI.account.chart.loan", locale: :en)
    german = I18n.t("UI.account.chart.loan", locale: :de)

    assert_equal english.keys.sort, german.keys.sort
  end

  # The chart controller sets aria-roledescription from labels.interactive_chart
  # and falls back to English when the payload does not carry it.
  test "the payload carries a localized interactive chart role description" do
    english = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload
    assert_equal "interactive chart", english[:labels][:interactive_chart]

    german = I18n.with_locale(:de) { Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload }
    assert_equal "interaktives Diagramm", german[:labels][:interactive_chart]
  end

  # A borrower too far behind has no payoff date. The description must say so
  # rather than interpolating a bare nil into a sentence.
  test "the description says so when the contract no longer pays the loan off" do
    loan = build_loan
    loan.account.update!(balance: 400_000)

    payload = Loan::PayoffChart.new(loan, as_of: @today).payload

    assert_nil payload[:projected_payoff_date]
    assert_match I18n.t("UI.account.chart.loan.no_payoff"), payload[:aria_description]
    # The balloon travels with the payload for the notice; on a loan that does
    # pay off it is nil, so a card is never quoted a figure of zero.
    assert_operator payload[:balloon], :>, 0
    assert_nil Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload[:balloon]
  end

  # Without an extra the payload is exactly what it was before the extra
  # existed: no key, no visible entry, no label.
  test "without an extra projection the payload is unchanged" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today, period: @all_time).payload

    assert_equal %i[actual scheduled projected], Loan::PayoffChart::SERIES
    assert_equal %i[
      today locale currency currency_precision domain_start domain_end actual scheduled projected visible
      scheduled_payoff_date projected_payoff_date months_saved interest_saved balloon labels aria_description
    ], payload.keys
    assert_equal %i[actual scheduled projected], payload[:visible]
    assert_equal %i[actual scheduled projected principal interest interest_share principal_share today interactive_chart], payload[:labels].keys
  end

  test "an extra projection draws its own line, named in the legend and the description" do
    loan = on_contract_loan
    projection = loan.payoff_projection(as_of: @today)
    extra = loan.payoff_projection(as_of: @today, extra: 5_000)

    payload = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time,
                                    projection: projection, extra_projection: extra).payload

    assert_equal({ date: @today.iso8601, balance: projection.current_balance.amount.to_f }, payload[:extra].first,
      "the extra line opens at today's balance, as the projection does")
    assert_equal extra.payoff_date.iso8601, payload[:extra].last[:date]
    assert_equal extra.payoff_date.iso8601, payload[:extra_payoff_date]
    assert_includes payload[:visible], :extra
    assert_equal I18n.t("UI.account.chart.loan.extra"), payload[:labels][:extra]
    assert_includes payload[:aria_description], I18n.t(
      "UI.account.chart.loan.aria_extra_payoff", date: I18n.l(extra.payoff_date, format: :long)
    )
    # The other series do not move for it.
    plain = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time, projection: projection).payload
    assert_equal plain[:projected], payload[:projected]
    assert_equal plain[:scheduled], payload[:scheduled]
  end

  # An extra too small to clear a loan that is far behind still draws, and the
  # description says it does not pay the loan off rather than quoting nil.
  test "the description says so when even the extra does not pay the loan off" do
    loan = build_loan
    loan.account.update!(balance: 400_000)
    extra = loan.payoff_projection(as_of: @today, extra: 10)

    payload = Loan::PayoffChart.new(loan, as_of: @today, extra_projection: extra).payload

    assert_nil payload[:extra_payoff_date]
    assert_includes payload[:aria_description], I18n.t(
      "UI.account.chart.loan.aria_extra_payoff", date: I18n.t("UI.account.chart.loan.no_payoff")
    )
  end

  # Owner review of #3474: the chart has no data table any more; the Schedule
  # tab carries the figures, so the payload builds no rows for one.
  test "the payload carries no data table rows" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today, period: @all_time).payload

    assert_not payload.key?(:rows), "nothing renders table rows, so the payload must not build them"
  end

  # The recorded series never starts before the loan does. A balance row dated
  # before origination (an account opened, then a loan recorded against it
  # later) must not become a lead-in, whatever window is picked: a window that
  # opens before the loan is clamped at origination.
  test "the actual series never starts before origination, whatever the window" do
    loan = on_contract_loan
    loan.account.balances.create!(date: Date.new(2025, 12, 1), balance: 0, currency: "USD",
                                  start_cash_balance: 0, flows_factor: -1)
    five_years = Period.new(key: "last_5_years", start_date: @today - 5.years, end_date: @today)

    [ @all_time, five_years ].each do |period|
      payload = Loan::PayoffChart.new(loan.reload, as_of: @today, period: period).payload

      assert_equal loan.origination_date.iso8601, payload[:actual].first[:date],
        "the first recorded point is origination, not the pre-origination row (#{period.key})"
      assert payload[:actual].none? { |point| Date.iso8601(point[:date]) < loan.origination_date }
    end
  end

  # #100 acceptance criterion: the chart's endpoint and the card quote one date.
  test "the projected series ends on the projected payoff date" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload

    assert_equal payload[:projected_payoff_date], payload[:projected].last[:date]
    assert_equal payload[:scheduled_payoff_date], payload[:scheduled].last[:date]
  end

  # The layout hard-codes `lang="en"`, so the chart cannot learn the locale
  # from the document; the payload carries it for the tooltip's date and
  # money formatting (Codex on we-promise/sure#3474).
  test "the payload carries the request locale for the tooltip" do
    loan = on_contract_loan

    assert_equal "en", Loan::PayoffChart.new(loan, as_of: @today).payload[:locale]
    I18n.with_locale(:de) do
      assert_equal "de", Loan::PayoffChart.new(loan, as_of: @today).payload[:locale]
    end
  end


  # The domain was recomputed for every plotted
  # point and table row, and for a loan with no start date each computation
  # looked origination up again -- about 4,400 statements for one page under
  # All. The work a payload does must not grow with the length of the schedule.
  test "building the payload does not repeat its lookups per scheduled payment" do
    short = build_undated_loan(term_months: 24)
    long = build_undated_loan(term_months: 360)

    short_count = sql_statements { Loan::PayoffChart.new(short, as_of: @today, period: @all_time).payload }
    long_count = sql_statements { Loan::PayoffChart.new(long, as_of: @today, period: @all_time).payload }

    assert_equal short_count, long_count,
      "a 360-payment schedule ran #{long_count} statements where a 24-payment one ran #{short_count}"
  end

  # we-promise/sure#3958: the tooltip says what each scheduled payment is made
  # of. The figures are the schedule's own rows, so the chart and the Schedule
  # tab's table cannot disagree.
  test "each scheduled payment point carries the payment's principal and interest" do
    loan = on_contract_loan
    payload = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload
    rows = loan.amortization_schedule.payments.index_by { |row| row.date.iso8601 }

    payments = payload[:scheduled].drop(1)
    assert_equal rows.size, payments.size
    payments.each do |point|
      row = rows.fetch(point[:date])
      assert_equal row.principal.amount.to_f, point[:principal], "principal on #{point[:date]}"
      assert_equal row.interest.amount.to_f, point[:interest], "interest on #{point[:date]}"
      assert_in_delta row.payment.amount.to_f, point[:principal] + point[:interest], 0.005,
        "the split on #{point[:date]} must add up to the payment"
    end
  end

  # The point of showing the split: early payments are mostly interest, late
  # ones mostly principal. If this ever fails the tooltip is telling users
  # something comfortable and false.
  test "the split on a long loan is interest-heavy at the start and principal-heavy at the end" do
    payments = Loan::PayoffChart.new(older_loan, as_of: @today, period: @all_time).payload[:scheduled].drop(1)

    assert_operator payments.first[:interest], :>, payments.first[:principal]
    assert_operator payments.last[:principal], :>, payments.last[:interest]
    assert_operator payments.first[:interest], :>, payments.last[:interest]
  end

  # Only contracted payments are split. The opening point is the amount
  # borrowed, not a payment, and the projection is a forecast.
  test "the opening point, the projection and the recorded balance carry no split" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today, period: @all_time).payload

    assert_not payload[:scheduled].first.key?(:principal)
    assert_not payload[:scheduled].first.key?(:interest)
    [ :projected, :actual ].each do |key|
      assert payload[key].any?, "#{key} must have points for this to mean anything"
      assert payload[key].none? { |point| point.key?(:principal) || point.key?(:interest) },
        "the #{key} series must not carry a split"
    end
  end

  # CodeRabbit on #305: the split is shown at the currency's own precision, as
  # the Schedule tab shows it, so a small component never rounds to zero. The
  # payload carries that precision because Intl's default can differ from the
  # app's (BTC is 8 here, 2 to Intl).
  test "the payload carries the loan currency's precision for the split" do
    loan = on_contract_loan
    assert_equal 2, Loan::PayoffChart.new(loan, as_of: @today).payload[:currency_precision]

    loan.account.update_columns(currency: "JPY")
    assert_equal 0, Loan::PayoffChart.new(loan.reload, as_of: @today).payload[:currency_precision]
  end

  # The tooltip names the figures with the Schedule tab's own column labels.
  test "the payload labels the split with the schedule table's own words" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload

    assert_equal I18n.t("loans.tabs.schedule.principal"), payload[:labels][:principal]
    assert_equal I18n.t("loans.tabs.schedule.interest"), payload[:labels][:interest]
  end

  # The shading under the scheduled line splits it by each
  # repayment's interest and principal share. The description says the same in
  # words, from the schedule's own rows: interest over what was paid, across
  # the first twelve payments and the last twelve.
  test "the accessible description names the interest share of the first and final year's repayments" do
    loan = older_loan
    rows = loan.amortization_schedule.payments
    first = interest_percent(rows.first(12))
    last = interest_percent(rows.last(12))
    assert_operator first, :>, last, "a long loan front-loads interest, or this cannot tell first from last"

    description = Loan::PayoffChart.new(loan, as_of: @today, period: @all_time).payload[:aria_description]

    assert_includes description, I18n.t("UI.account.chart.loan.aria_interest_share", first: first, last: last)
  end

  # Under 24 payments the first and final years would overlap, so one clause
  # covers every payment. Both sides of the boundary.
  test "a schedule of 23 payments quotes one interest share, and one of 24 quotes two" do
    short = build_loan(term_months: 23)
    long = build_loan(term_months: 24)
    short_rows = short.amortization_schedule.payments
    long_rows = long.amortization_schedule.payments
    assert_equal [ 23, 24 ], [ short_rows.size, long_rows.size ]

    short_description = Loan::PayoffChart.new(short, as_of: @today, period: @all_time).payload[:aria_description]
    long_description = Loan::PayoffChart.new(long, as_of: @today, period: @all_time).payload[:aria_description]

    assert_includes short_description, I18n.t("UI.account.chart.loan.aria_interest_share_overall", share: interest_percent(short_rows))
    assert_not_includes short_description, I18n.t("UI.account.chart.loan.aria_interest_share", first: interest_percent(short_rows.first(12)), last: interest_percent(short_rows.last(12)))
    assert_includes long_description, I18n.t("UI.account.chart.loan.aria_interest_share", first: interest_percent(long_rows.first(12)), last: interest_percent(long_rows.last(12)))
    assert_not_includes long_description, I18n.t("UI.account.chart.loan.aria_interest_share_overall", share: interest_percent(long_rows))
  end

  # The shading reads the scheduled points the payload already carries; it
  # adds no series and no key.
  test "the payload's keys and series are unchanged by the shading" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today, period: @all_time).payload

    assert_equal %i[today locale currency currency_precision domain_start domain_end actual scheduled projected visible
                    scheduled_payoff_date projected_payoff_date months_saved interest_saved balloon labels aria_description],
                 payload.keys
    assert_equal %i[actual scheduled projected], Loan::PayoffChart::SERIES
  end

  test "the payload labels the two shares for the legend" do
    payload = Loan::PayoffChart.new(on_contract_loan, as_of: @today).payload

    assert_equal I18n.t("UI.account.chart.loan.interest_share"), payload[:labels][:interest_share]
    assert_equal I18n.t("UI.account.chart.loan.principal_share"), payload[:labels][:principal_share]
  end

  # The chart draws every point and clips to the window, so a series with one
  # point inside and its neighbour outside still crosses the plot: the final
  # repayment entering a window, or the opening point leaving one. Its legend
  # entries, the share swatches among them, follow it. Both sides of the
  # boundary: a segment that only touches the window's edge draws nothing.
  test "a series with one point inside and a neighbour outside is visible" do
    loan = build_loan
    rows = loan.amortization_schedule.payments
    final = rows.last.date
    assert_operator rows[-2].date, :<, final - 1, "the final payment's neighbour must sit outside the window"

    entering = Period.new(key: "current_month", start_date: final - 1, end_date: final + 5)
    payload = Loan::PayoffChart.new(loan, as_of: @today, period: entering).payload
    assert_equal [ final - 1, final ].map(&:iso8601), [ payload[:domain_start], payload[:scheduled].last[:date] ]
    assert_includes payload[:visible].map(&:to_s), "scheduled", "the final repayment enters the window and ends inside it"
    assert_includes payload[:aria_description], interest_share_clause(rows)

    touching = Period.new(key: "current_month", start_date: final, end_date: final + 5)
    payload = Loan::PayoffChart.new(loan, as_of: @today, period: touching).payload
    assert_not_includes payload[:visible].map(&:to_s), "scheduled", "a series that ends on the window's start draws nothing in it"
    assert_not_includes payload[:aria_description], interest_share_clause(rows)

    leaving = Period.new(key: "current_month", start_date: loan.origination_date, end_date: loan.origination_date + 10)
    payload = Loan::PayoffChart.new(loan, as_of: @today, period: leaving).payload
    assert_equal 1, payload[:scheduled].count { |point| Date.iso8601(point[:date]).between?(loan.origination_date, loan.origination_date + 10) }
    assert_includes payload[:visible].map(&:to_s), "scheduled", "the opening point starts inside the window and the line leaves it"
  end

  # One recorded balance strictly inside the domain, away from both edges, is
  # still one point, and the chart draws no line through it.
  test "a single point inside the domain is not visible" do
    account = travel_to(@today) do
      Account.create!(
        family: @family, name: "Loan #{SecureRandom.hex(4)}", balance: 500_000, currency: "USD",
        accountable: Loan.new(subtype: "mortgage", interest_rate: 6, term_months: 24,
                              rate_type: "fixed", start_date: @today - 10)
      )
    end
    account.balances.create!(date: @today, balance: 500_000, currency: "USD",
                             start_cash_balance: 500_000, flows_factor: -1)

    payload = Loan::PayoffChart.new(account.loan, as_of: @today, period: @all_time).payload

    assert_equal [ @today.iso8601 ], payload[:actual].map { |point| point[:date] }
    assert_operator payload[:domain_start], :<, @today.iso8601
    assert_operator payload[:domain_end], :>, @today.iso8601
    assert_not_includes payload[:visible].map(&:to_s), "actual", "one point is not a line"
  end

  private
    # The description's sentence saying each payment's tooltip splits it into
    # principal and interest.
    def composition_sentence
      I18n.t("UI.account.chart.loan.aria_composition",
             principal: I18n.t("loans.tabs.schedule.principal"),
             interest: I18n.t("loans.tabs.schedule.interest"))
    end

    def interest_share_clause(rows)
      I18n.t("UI.account.chart.loan.aria_interest_share",
             first: interest_percent(rows.first(12)), last: interest_percent(rows.last(12)))
    end

    def interest_percent(rows)
      interest = rows.sum { |row| row.interest.amount }
      (interest / (interest + rows.sum { |row| row.principal.amount }) * 100).round
    end

    # Each bounded window the loan chart offers, with the start date the
    # shared Period gives it on @today. All is handled by @all_time.
    def window_starts
      {
        "current_month" => @today.beginning_of_month,
        "last_90_days" => @today - 90,
        "current_year" => @today.beginning_of_year,
        "last_365_days" => @today - 365,
        "last_5_years" => @today - 5.years,
        "last_10_years" => @today - 10.years
      }
    end

    # Every statement, including those the query cache answers: a cached
    # lookup still builds its relation and records.
    def sql_statements(&block)
      count = 0
      counter = ->(*, payload) { count += 1 unless payload[:name] == "SCHEMA" }
      ActiveSupport::Notifications.subscribed(counter, "sql.active_record", &block)
      count
    end

    # A loan with no recorded start date, as a loan created before the field
    # existed has: origination falls back to the account's first valuation.
    def build_undated_loan(term_months:)
      account = Account.create!(
        family: @family, name: "Undated Loan #{SecureRandom.hex(4)}",
        balance: 500_000, currency: "USD",
        accountable: Loan.new(subtype: "mortgage", interest_rate: 6, term_months: term_months, rate_type: "fixed")
      )
      account.entries.create!(
        date: Date.new(2026, 1, 1), name: "Opening balance", amount: 500_000, currency: "USD",
        entryable: Valuation.new(kind: "opening_anchor")
      )
      record_balances(account)
      account.loan
    end

    def build_loan(rate_type: "fixed", term_months: 24)
      account = Account.create!(
        family: @family, name: "Loan #{SecureRandom.hex(4)}",
        balance: 500_000, currency: "USD",
        accountable: Loan.new(subtype: "mortgage", interest_rate: 6, term_months: term_months,
                              rate_type: rate_type, start_date: Date.new(2026, 1, 1))
      )
      # The opening valuation the account form records: Loan#original_balance
      # reads it, and without it the principal would follow whatever the
      # current balance is later set to.
      account.entries.create!(
        date: Date.new(2026, 1, 1), name: "Opening balance", amount: 500_000, currency: "USD",
        entryable: Valuation.new(kind: "opening_anchor")
      )
      record_balances(account)
      account.loan
    end

    # Loans exactly on contract at `@today`. The schedule then agrees with the
    # recorded history and the projection has somewhere to go.
    def on_contract_loan
      loan = build_loan
      scheduled = scheduled_balance_at(loan, @today)
      loan.account.update!(balance: scheduled)
      record_balances(loan.account, through: @today, closing: scheduled)
      loan.reload
    end

    # A thirty-year mortgage drawn down in 2016 and added to the app in 2024:
    # eight years of contract with no recorded balance, then history from
    # 2024 that follows the schedule, exactly on contract at `@today`.
    def older_loan
      origination = Date.new(2016, 1, 1)
      account = Account.create!(
        family: @family, name: "Old Loan #{SecureRandom.hex(4)}",
        balance: 500_000, currency: "USD",
        accountable: Loan.new(subtype: "mortgage", interest_rate: 6, term_months: 360,
                              rate_type: "fixed", start_date: origination)
      )
      account.entries.create!(
        date: origination, name: "Opening balance", amount: 500_000, currency: "USD",
        entryable: Valuation.new(kind: "opening_anchor")
      )
      scheduled = scheduled_balance_at(account.loan, @today)
      account.update!(balance: scheduled)
      record_balances(account, from: Date.new(2024, 1, 1), through: @today, closing: scheduled)
      account.loan.reload
    end

    def scheduled_balance_at(loan, date)
      loan.amortization_schedule.payments.select { |p| p.date <= date }.last.ending_balance.amount
    end

    # Materialised balance rows the way the balance calculator writes them for
    # a liability: the outstanding amount in start_cash_balance with a -1 flows
    # factor, which Balance::ChartSeriesBuilder reads back as a positive debt.
    # One row a month from `from`, following the schedule's balances so the
    # recorded history is a plausible loan rather than a flat line. The row on
    # `from` carries the schedule's balance on that day, the principal before
    # the first payment.
    def record_balances(account, from: Date.new(2026, 1, 1), through: Date.new(2026, 6, 30), closing: nil)
      account.balances.delete_all
      schedule_rows = account.loan.amortization_schedule&.payments || []
      opening = schedule_rows.select { |p| p.date <= from }.last&.ending_balance&.amount || 500_000
      rows = schedule_rows.select { |p| p.date > from && p.date <= through }
      account.balances.create!(date: from, balance: opening, currency: "USD",
                               start_cash_balance: opening, flows_factor: -1)
      rows.each do |row|
        account.balances.create!(date: row.date, balance: row.ending_balance.amount, currency: "USD",
                                 start_cash_balance: row.ending_balance.amount, flows_factor: -1)
      end
      return if closing.nil?

      account.balances.create!(date: through, balance: closing, currency: "USD",
                               start_cash_balance: closing, flows_factor: -1)
    end
end