Add an Extra repayments tab to the loan page

Merged
JA
2mo ago
+816-140

Summary

Closes #304

Before: the only way to ask "what if I paid an extra amount each month?" was a form halfway down the Schedule tab. On a loan that was on schedule there was no chart until you typed an amount. Entering one replaced the projection line instead of adding one. "Interest saved" was measured against the original contract, not against not paying extra. Weekly and yearly amounts were approximated as monthly.

After: a loan with something to pay off has an Extra repayments tab beside Schedule. It takes one amount, paid every month on top of the current repayment. The chart always shows the contracted schedule and the current projection. Entering an amount adds a blue fourth line for the new schedule, plus cards for the new payoff date, the months sooner, and the interest saved against not paying extra. The Schedule tab keeps its cards, tables and ahead/behind chart, and loses the form.

How: Loan::ExtraRepaymentComparison builds the baseline and extra projections once, on one as_of, and passes both to Loan#payoff_chart_payload. The payload has gained an optional extra_projection: series. Loan::PayoffProjection#interest_saved_versus and #months_sooner_than compare two projections. The contract-based interest_saved/months_saved are unchanged because the Schedule cards depend on them. The chart controller draws the extra series, with a legend and tooltip row, only when it is present.

Scope and dependencies

  • Issue: #304 (Gatekeeper review: APPROVED; both count corrections and the system-test revision are folded into the plan)
  • Parent PR/branch, if stacked: none, branched from main at b5d85e9. main was merged in at 8988992 to resolve a conflict in loans/en.yml: the merge keeps main's new tooltip sentence in aria_description and this branch's aria_description_extra.
  • Intentionally out of scope:
    • Saved scenarios (LoanScenario/LoanExtraRepayment stay unused).
    • Lump sums and non-monthly cadences.
    • Changing the repayment held on variable-rate loans (the existing notice is carried over).
    • The #100 account-page chart.
    • PayoffProjection.monthly_equivalent and EXTRA_PAYMENT_FREQUENCIES are kept, still covered by their tests, with no UI caller.

Where this differs from the plan

  • New PORO Loan::ExtraRepaymentComparison. The plan built the two projections "once per request" without naming where. Checking against upstream AGENTS.md (keep domain logic out of ERB, skinny controllers) put that in a model rather than the view.
  • payoff_chart_payload also gains require_divergence: and as_of:. The new tab must chart an on-schedule loan, which the divergence gate hid, and the CLAUDE.md reference-date rule needs one today for both projections. Both default to the old behaviour. The Schedule tab passes neither, and a test pins its key list.
  • Locales. Only 5 of the 19 config/locales/views/loans/*.yml files carried the old what-if keys (en, de, ru, tr, uk); those keys are removed. The new strings are in en and de; other locales fall back to English, as the rest of the loan UI does. The tab label lives in views/accounts/{en,de}.yml, where the other tab labels are.

Blast radius

  • payoff_chart_payload: the callers are _schedule.html.erb (passes only projection:, payload unchanged) and the new comparison.
  • payoff_projection_with_extra: its only caller was the Schedule what-if; now it is the comparison only. frequency: was dropped and all 4 test call sites were updated.
  • extra_payment_params: now permits only amount. It is read only by AccountsController#show.
  • Chart controller: with no extra_projection in the payload, nothing new is drawn. The Schedule system test still passes.
  • Preview gate: not gated, by the owner's decision. The loan tabs and the what-if they replace were never gated in code.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests. The 30 payoff_chart_payload references in test/models/loan_test.rb stay green, and a new test pins the payload's key list when no extra is given.
  • Any gate test has an observed failing mutation transcript attached (below).
  • Fixtures used as evidence assert internal consistency. Tests build their loan on the spot, and the ahead-of-schedule case also asserts that the contract-based figure differs from the new one, so it cannot pass on the old method.
  • Financial calculations use BigDecimal and documented rounding points. The comparisons subtract Money#amount/BigDecimal and add no rounding.
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Tests

Plan itemTest
Eligible loan has the tab; frame renders the formAccountsControllerTest "an eligible loan has an Extra repayments tab whose frame renders a monthly-only form"
Payload carries extra_projection ending earlier than baselineLoanTest "an extra projection adds its own series beside the baseline projection"
Interest saved is baseline minus extra, on an ahead-of-schedule loan (delta, not presence)Loan::PayoffProjectionTest "interest saved versus a baseline is what the extra buys, not the distance from the contract"
Chart endpoints equal the cards for one todayAccountsControllerTest "an amount draws the extra line beside the baseline, with cards measured against not paying extra"; Loan::ExtraRepaymentComparisonTest
System: extra line by its own marker, other series unchangedLoanPayoffChartTest "the Extra repayments tab draws the extra line beside the existing series only when an amount is entered"
Blank / 0 / negative / non-numeric / NaN / InfinityAccountsControllerTest "malformed amounts fall back to the baseline chart without an error" (also 1e400)
Amount larger than the balanceLoan::PayoffProjectionTest "an extra larger than the balance pays the loan off at the next payment"
Mutates nothingAccountsControllerTest "modelling an extra payment mutates nothing" (balance and loan_amortizations count)
No tab at zero balance / no scheduleAccountsControllerTest "no Extra repayments tab when the loan has nothing to pay off or no schedule"
Schedule tab has no form; payload unchanged"the Schedule tab no longer carries the extra-payment form"; LoanTest key-list test
Non-loan account tabs unchanged"a non-loan account has no Extra repayments tab"
Loan the current repayment never clears: notice follows the amount (added in review)Loan::ExtraRepaymentComparisonTest "the non-convergence notice follows the amount entered"; AccountsControllerTest "a loan the current repayment never clears says what the entered extra does, not to enter one"

Mutation transcripts

Each fix line was reverted on its own, and the named tests were run (DISABLE_PARALLELIZATION=true):

== M1: interest_saved_versus returns the contract-based interest_saved
Loan::PayoffProjectionTest#test_interest_saved_versus_a_baseline_is_what_the_extra_buys,_not_the_distance_from_the_contract [payoff_projection_test.rb:490]:
Expected: 0.3119613e5
  Actual: 0.11241583e6
Loan::PayoffProjectionTest#test_a_comparison_against_itself_saves_nothing [payoff_projection_test.rb:503]:
Expected: 0
  Actual: -0.131e1
46 runs, 139 assertions, 2 failures, 0 errors, 0 skips

== M2: extra series built from the baseline's payments
LoanTest#test_an_extra_projection_adds_its_own_series_beside_the_baseline_projection [loan_test.rb:746]
Loan::ExtraRepaymentComparisonTest#test_with_an_amount_both_projections_share_one_as_of_and_the_figures_compare_them [extra_repayment_comparison_test.rb:47]
63 runs, 916 assertions, 2 failures, 0 errors, 0 skips

== M3: :extra_repayments never added to loan_tabs
Expected exactly 1 element matching "button[data-id='extra_repayments']", found 0.
(5 controller failures)

== Unmutated (the four files above)
223 runs, 1500 assertions, 0 failures, 0 errors, 0 skips

The review fixes in 6ade1f5 have their own mutation transcripts in the review answer in the PR conversation.

Validation

Commands and results:

  • bin/rails test: green apart from 5 failures that are environment-only. They come from this sandbox: ANTHROPIC_BASE_URL is set, and one test depends on the Ruby version. None touches loans or accounts, and CI is the check on them.
  • DISABLE_PARALLELIZATION=true bin/rails test test/system/loan_payoff_chart_test.rb: 3 runs, 0 failures.
  • bin/rubocop: no offenses.
  • erb_lint on the changed templates: no errors.
  • npm run lint (Biome lint): clean. biome format already fails on main's copy of loan_payoff_chart_controller.js, so the file was not reformatted here.
  • bin/brakeman --no-pager: no warnings.

After review (6ade1f5, see the review answer in the PR conversation):

  • Loan model and account controller tests: 226 runs, 0 failures.
  • test/system/loan_payoff_chart_test.rb: 3 runs, 0 failures. It was re-run on the merge commit as well.
  • CI: 32/32 green on 6ade1f5 and on 8988992.

Upstream AGENTS.md check

This was checked against AGENTS.md on mirror/upstream-main:

  • Domain logic lives in Loan::ExtraRepaymentComparison, not in ERB, and the controller only routes the frame.
  • Strings go through t(). Buttons use DS::Button. There are no raw Tailwind palette classes or hex literals.
  • The new chart line and its legend swatch use var(--color-blue-600). This follows how the existing chart colours its lines (var(--color-green-600)/var(--color-yellow-600)); the design system has no chart tokens. The same pattern appears 51 times across app/ on main, including in DS::Pill and DS::ProgressRing.
  • The legend markup mirrors the Schedule tab's legend and appears once in this diff. Sharing it, as a partial or a DS::* primitive, is a follow-up for when this goes upstream. Doing it here would change the Schedule tab's output, which this PR keeps unchanged.

Screenshots

Deferred at the owner's request. Six were captured (light and dark: no amount, $500, and the Schedule tab without the form) and are held in the project thread. They will be needed when this goes upstream, because upstream AGENTS.md asks for screenshots on UI changes.

Risks, blockers, and follow-up

  • The new strings exist in en and de only; other locales show English.
  • Not behind the preview gate, by the owner's decision.
  • Screenshots are deferred (see above).
  • Nothing is persisted. Every figure is computed live from the current balance, like the existing projection.