Add BalanceSheet::NetWorthVelocity for net worth pace

Merged
JA
1mo ago
+346-0

Part of #4079

Fork pre-flight: jaysbeekay/sure#360 (same branch, same commit; its CI and review history live there). Model layer of jaysbeekay/sure#129, drop 10.2.

Summary

Adds BalanceSheet::NetWorthVelocity, a read model for how fast net worth is moving over a period (velocity, per month) and how that pace compares with the equally long period just before it (momentum). Model only: no view, controller, component or locale string.

What calls this? Nothing yet, deliberately. The dashboard figure that reads it (a small component beside the net worth headline) is a separate UI PR that follows once this one lands, per the "small, cohesive changes" guidance. Merging this changes no behaviour a user can see.

What changed

  • app/models/balance_sheet/net_worth_velocity.rb (new, 103 lines including comments)
    • velocity: (last point - first point) / days between them x 365.2425 / 12, as Money in the family currency.
    • momentum: velocity minus the velocity of prior_period, the window of the same length ending the day before period starts.
    • Both read BalanceSheet::NetWorthBreakdownSeriesBuilder#breakdown_series(period:), the series the Reports page draws, which already applies the viewer's account visibility and is cached per period.
    • Both are nil when the history of every account the series is built from (BalanceSheet::HistoricalAccountScope#account_ids) does not cover the window. An account starts at its first non-pending entry or its first Balance row, whichever is earlier (the unlinked branch of Account#history_start_date); the series reads Balance rows, so an account with balances but no entries counts. Of the accounts that have started by the window's end, the latest start must be on or before the window's start. One long-lived account does not vouch for one opened inside the window; an account opened after the window ends reads zero throughout it and does not hold it back; accounts with neither entries nor balances are ignored.
    • The reference date is the period: passed in; nothing in the class reads Date.current.
  • test/models/balance_sheet/net_worth_velocity_test.rb (new, 21 tests).

Demo data

None added. Nothing renders the figures yet; the tests build their own history.

Migration notes

None. No schema change.

Verification

  • bin/rails test test/models/balance_sheet/net_worth_velocity_test.rb: 21 runs, 39 assertions, 0 failures.
  • Full suite bin/rails test on d76ba7038 (upstream main at 275f2f994): 11371 runs, 48419 assertions, 0 failures, 0 errors, 34 skips.
  • The three tests added from review failed on 6757a5957 first: velocity is withheld when an account with only balances starts inside the period, an account opened after the prior period does not withhold momentum, an account opened after a past period does not withhold its figures.
  • bin/rubocop -f github on the changed files: no offences. bin/brakeman --no-pager -q: 0 warnings. No ERB or JS changed, so erb_lint and Biome have nothing to check.
  • Each behaviour was broken on purpose and the named test failed:
MutationCaught by
history check always passes5 tests, incl. velocity is withheld when history starts inside the period, momentum is withheld when history starts inside the prior period, there is no velocity when the family has no history
earliest instead of latest account startvelocity is withheld when any account's history starts inside the period
pending transactions count as historya pending transaction dated before the history does not count as history
account start from entries only, ignoring Balance rowsvelocity is withheld when an account with only balances starts inside the period
no filter on accounts started after the window endsan account opened after the prior period does not withhold momentum, an account opened after a past period does not withhold its figures
momentum sign reversed4 tests, incl. momentum flips sign when growth slows
prior period shifted by a daythe prior period is the same length, ending the day before this one starts, reads the period it is given, not today's date
30-day month instead of 365.2425 / 12velocity is the change over the period, per month, and 2 more

Removing either single-point guard (points.size < 2, days.positive?) on its own is not caught, because the other still holds; they are belt and braces for a one-day window, covered by a single-day period has no velocity.

Blast radius

  • Who else hits this line, and what do they pass? No existing line changes. The class calls NetWorthBreakdownSeriesBuilder.new(family, user:) and HistoricalAccountScope.new(family, user:) with the balance sheet's own family and user, exactly as BalanceSheet and the Reports page do, and Period.custom(start_date:, end_date:).
  • What else reads what I just changed the reading of? Nothing: this adds a reader and changes no reader. grep -rn "NetWorthBreakdownSeriesBuilder.new\|HistoricalAccountScope.new" app lists net_worth_series_builder.rb, net_worth_breakdown_series_builder.rb and this file; the first two are untouched.
  • Cost when a caller exists: two cached breakdown_series calls (this window and the prior one) and two grouped queries for the history guard, one on entries and one on balances.

Summary by CodeRabbit

  • New Features
    • Added net-worth velocity, showing the average monthly change over a selected period.
    • Added net-worth momentum, comparing that change with the preceding period of equal length.
    • These figures are unavailable when account history does not cover the comparison period or there is not enough date history to calculate a change.