Loan balance chart on the account page: recorded balance, schedule and projection (#105, #106, #100)
Closes #105. Closes #106. Part of #107 (M0). PR-2 in docs/plans/loan-chart-upstream-delivery.md §7 — Fixes we-promise/sure#3332 when opened upstream. Stacked on #109 — base is feat/loan-amortisation-engine, so this diff shows only the projection and the chart.
Upstream PR: we-promise/sure#3474
Re-targeted 2026-09-08 (#100). The chart now lives on the account page, in UI::Account::Chart, replacing the single-series balance chart for loans only. The extra-payment what-if that this PR originally carried is removed (#100 decision 10), not left dormant: Loan::RepaymentPlan, the simulator's extra-repayment hook, the form, the controller parameters and their locale keys and tests are gone. Extra payments already made reach the chart through the recorded balance.
Summary
One chart on a loan account's page carrying three series on one axis:
| Series | What it is | Token | Style |
|---|---|---|---|
| Recorded balance | account.balance_series, origination → today, in the loan's own currency | --color-success | solid |
| Original schedule | the amortisation schedule, origination → contracted payoff, re-amortised at each recorded rate change | --color-destructive | dashed |
| Projected from today | today's recorded balance forward, paying the schedule's repayment period by period | --color-success | dashed |
Solid is fact, dashed is forecast; hue is secondary, so the chart reads in greyscale and under deuteranopia.
The projection pays the schedule's own repayment (#100 decision 1)
Loan::Simulator gains a :scheduled payment strategy whose per-period amount comes from a callable. Loan::PayoffProjection passes the schedule's payment for that position, holding the last scheduled amount past maturity. So a loan ahead of schedule keeps paying what the contract asks and finishes early; a future recorded rate change resizes the projected repayment exactly as it resizes the schedule; and a fixed loan's projection is byte-identical to the held-payment result. It supersedes the earlier "seed and re-amortise" fix to cubic's P1 on this PR, which re-amortised the lower actual balance back to the original maturity.
The period governs the x-domain (decision 4)
The payload carries domain_start / domain_end. Under All the domain runs origination → the later of the two payoffs; under any other period it is the period itself, so forward series are visible only when the window reaches past today. The recorded series is queried from the period start to the earlier of the period end and today, clipped at origination, so a period that opens before the loan does not draw a flat zero lead-in. Period is untouched.
What this means on first load. Periodable opens the page under the user's stored default period, usually 30 days, so the first view shows the recorded and scheduled lines over that window and no projected line, while the cards beside the chart still quote the projection. The projected line appears when the picker is set to All. This is decision 4 as the owner made it, stated here because upstream has no loan-scoped "All" and a reviewer will otherwise read the empty forward window as a bug.
On the account page (decisions 3, 5, 6, 7)
AccountsController#showcaptures one@as_ofand passes it to the payload, the component and the Schedule tab. Nothing underapp/models/loan/readsDate.currentfor itself once a caller has supplied a date. It also builds the payoff projection once and hands it to the payload and to the Schedule tab's forecast card, which no longer runs its own simulation.- The Projected Payoff and Interest Saved cards sit beside the chart inside the
chart_detailsframe; the period picker re-renders both. The four schedule summary cards stay on the Schedule tab, which no longer has a chart of its own. - Gate G6 (#57): the SVG is focusable; arrow keys, Home and End step the tooltip through the table's dates and Escape clears; the tooltip becomes a
role=statuslive region only under the keyboard, so a pointer sweep never announces; a<details>table with the same rows is associated to the SVG througharia-describedby; the description is real DOM text as well as thearia-label. - Colour comes from functional tokens resolved through
getComputedStyleon each draw, with aMutationObserverredraw on theme change. Lines carrydata-seriesso a test can name them.
Scope and dependencies
- Issue: #105, #106, #100 (decisions 1, 3, 4, 5, 6, 7, 10), we-promise/sure#3332
- Parent PR/branch, if stacked: #109 (
feat/loan-amortisation-engine), which now carries #100 decision 8 and is merged into this branch - Intentionally out of scope: the extra-payment what-if (decision 10; the fork keeps its own Schedule-tab form until a later tranche); the fork's Current Monthly Payment alignment (decision 9, fork PR-B); a
Rails.cachelayer and the p95 measurement; the G6 screen-reader transcript and greyscale/deuteranopia screenshots, which need a person at a screen and are listed under Risks below
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 — brief §7.4 rows map to
test/models/loan/simulator_test.rb,payoff_projection_test.rb,payoff_chart_test.rb,test/components/UI/account/chart_test.rb,test/controllers/loans_controller_test.rb,test/system/loan_payoff_chart_test.rb. - Any gate test has an observed failing mutation transcript attached — see the mutation table below.
- Fixtures used as evidence assert internal consistency — the chart fixtures now record the opening valuation the account form records; without it
Loan#original_balancefell back to the current balance and a mid-term loan appeared to have borrowed only what it still owed (schedule opening at 257,479 against a recorded 500,000). Found by the browser test. - Financial calculations use
BigDecimaland documented rounding points — the:scheduledamount is coerced throughBigDecimal(….to_s)at the simulator boundary; rounding is unchanged from #109. - De-identification uses token-shape allowlists; no personal data is included.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Validation
Commands and results (Ruby 3.3.6 locally; the Gemfile pins 3.4.9, which the sandbox cannot fetch — CI runs the pinned version):
| Check | Result |
|---|---|
Loan model, component, controller suites (test/models/loan, loan_test.rb, plaid_account, UI/account/chart_test.rb, loans_controller_test.rb, accounts_controller_test.rb) | 198 runs, 987 assertions, 0 failures |
System test test/system/loan_payoff_chart_test.rb (headless Chromium 141) | 3 runs, 26 assertions, 0 failures |
| Full suite, this head | 8542 runs, 34349 assertions, 4 failures, 2 errors |
Full suite, base feat/mvp-payoff-chart@74425f81 | 8534 runs, 34277 assertions, 4 failures, 2 errors |
bin/rubocop | clean (2544 files) |
bundle exec erb_lint --lint-all | clean (694 files) |
npm run lint | clean (125 files) |
bin/brakeman --no-pager | 0 warnings |
The six failing tests are identical on head and base (comm of the two failing sets is empty): three Settings::HostingsControllerTest cases, ActiveStorageAuthorizationTest, Provider::OpenaiTest (Ruby 3.4 Hash#inspect format) and Provider::RegistryTest (Anthropic endpoint configuration). None is loan-related. This branch adds 8 runs and 72 assertions and changes neither the count nor the set.
Observed-to-fail mutations
| Mutation | Test that failed |
|---|---|
:scheduled reverted to the seeded :reamortize path | payoff_projection_test "a future rate change moves the projected repayment by the schedule's amount" |
payment_amount nil for the projection | payoff_projection_test "a variable loan ahead of schedule pays the contract and finishes early" |
| Keyboard stops from every series point (weekly recorded points) | loan_payoff_chart_test (system) "the second press moves to the next date" — identical tooltip on consecutive presses |
parseDate out of scope in the keyboard block | system test "the chart must be focusable" — tabindex nil |
hide() leaving aria-live on the tooltip after Escape | system test "expected not to find css div[aria-live='polite']" |
loan_legend ignoring visible | chart_test "the legend lists only the series the payload marks visible" — 3 entries, expected 2 |
| Not-converged notice never rendered | chart_test "a projection with no payoff date shows the not-converged notice" |
| Decision 8 reverted (on #109) | loan_test "arm should read as variable"; mortgage_processor_test "expected false to be truthy" |
A WINDOWS key renamed off Period::PERIODS | payoff_chart_test "every loan window is a shared period key" |
PayoffChart ignores the projection the controller passes | accounts_controller_test "a loan account page builds its payoff projection once for the chart and the Schedule tab" |
| The Schedule tab builds its own projection | same test |
labels without interactive_chart | payoff_chart_test "the payload carries a localized interactive chart role description" |
Degradation matrix (brief §7.5)
| Case | Asserted in |
|---|---|
| Not schedulable (blank rate type) | chart_test fallback to the time-series chart; loans_controller_test "no schedule renders the page without a loan chart"; payoff_chart_test "no payload at all" |
| Provider rate type outside the known set | #109 (decision 8) — loan_test, mortgage_processor_test, amortization_schedule_test |
| Balance zero | payoff_chart_test "withheld only when it cannot run" — no projected series, not in visible |
| Not converged | payoff_chart_test description; chart_test notice in place of the cards |
| No divergence | payoff_chart_test "drawn when it overlaps the schedule" |
| Originated today / no balance rows | payoff_chart_test, two tests |
| Period other than All | payoff_chart_test "visible only when the domain reaches past today"; chart_test legend limited to visible |
Exit criterion: nothing of the what-if remains
git grep -n 'extra_payment\|RepaymentPlan\|accelerated\|extra_for' returns two hits, both tests asserting the removal: the controller test that an extra_payment parameter is ignored, and the payload test that no accelerated key exists.
Risks, blockers, and follow-up
- Owed evidence that needs a person: the G6 screen-reader transcript (tool and version named), the keyboard-only walkthrough, and greyscale/deuteranopia screenshots. The keyboard behaviour is asserted by the system test; the transcript and screenshots are not something a headless sandbox can produce honestly.
- Not measured: added server time on
showfor a 360-month schedule (#100 asks for a p95 under 150 ms). The payload is built once per request and memoised by account,as_ofand period. - Opening upstream is blocked on jjmata's ordering ruling on we-promise/sure#2984 (brief §8), and we-promise/sure#3332's body must be replaced with brief Appendix A first — the owner has to paste it.
- Two review threads on this PR (cubic on
repayment_plan.rb, CodeRabbit on the simulator's extras) concern code that decision 10 removed; they are answered on the threads. - Next unblocked issue: fork PR-B (brief §9) after the upstream merge and sync.
- Add a loan payoff chart with actual, scheduled, and projected balance series.
- Project payoff from recorded balances using scheduled repayment dates and variable-rate changes.
- Show projected payoff, interest-saved, and non-convergence details.
- Add accessible descriptions, keyboard navigation, responsive rendering, and light/dark theme support.
- Handle missing schedules, zero balances, matured loans, and non-converging projections.
- Add English and German translations and broad model, controller, component, and system test coverage.
| Author | Lines added | Lines removed |
|---|---|---|
| jaysbeekay | 10,991 | 2,861 |