Pin today's cost-basis figures before lot storage
Summary
PR 0 of the per-lot cost basis work: characterisation tests that pin today's average-cost figures before anything moves, so PR 1 (lot storage, average only) can be held to byte-identical output. Tests only, no production code changes. Every test passes on main by design; each one is shown to be non-vacuous by the mutation table below.
Part of jaysbeekay/sure#551
Stack: PR 0 of 6. PR 1 will be cut from this branch (feat/551-lot-basis-pr0-characterisation) and target it; when this merges, PR 1 is retargeted to main (owner decision 5 on jaysbeekay/sure#551).
Upstream: the paired upstream enhancement is we-promise/sure#4449. Upstream delivery happens later, so there is no upstream PR or fork pre-flight for this slice yet.
Blast radius
Who else hits these lines, and what do they pass? Nothing. The diff adds tests only, in test/models/holding_test.rb and test/models/investment_statement_test.rb, plus two private helpers in the statement test (materialize_two_parcel_example, cost_basis_on). No production file, fixture or shared helper changes, so no caller sees a different value. The tests read the per-date snapshot through the real sync path (Holding::Materializer with the forward strategy), which is the path PR 1 replaces.
What is pinned
| Test | Figure pinned | Code it pins |
|---|---|---|
remap_security! merge pins the weighted cost basis (holding_test.rb:458) | existing 5 @ 100 merged with moved 10 @ 40 → cost_basis 100 before, 60 after; qty 15 | app/models/holding.rb:252-253 (weighted merge) |
remap_security! merge with an unknown side keeps the existing basis (holding_test.rb:478) | known 100 + unknown moved side → 100 over 15 units; unknown existing + moved 40 → nil | app/models/holding.rb:254-255 (else branch) |
investment statement figures on the two-parcel fixture (investment_statement_test.rb:2369) | 60 @ $12 (2024-01-10) + 40 @ $20 (2024-03-05), sell 40 @ $25 (2024-06-01), price $30 on 2024-06-02. Snapshot basis 12.00 on 03-04, 15.20 on 05-31 and on 06-01 (the sale leaves it unchanged); table row qty 60, avg_cost $15.20, unrealized $1,800 vs $912; unrealized_gains 888; sale basis $608, proceeds $1,000, gain $392; realized_gains net 392, 1 trade, none excluded | holding/cost_basis_tracker.rb:38 (buy) and :49 (sell relief at the average), via holding/forward_calculator.rb and holding/materializer.rb:122; investment_statement.rb:860 (build_holdings_table_row avg cost), :379-382 (unrealized_gains), :639 (realized_gains); trade.rb:261 (calculate_realized_gain_loss) |
investment statement figures on the two-parcel fixture with the first parcel transferred in (investment_statement_test.rb:2403) | same fixture, first parcel labelled Transfer: snapshot basis nil on 03-04, 05-31 (not the $20 parcel's price) and 06-02; table row avg_cost nil, unrealized nil, missing_cost_basis true; unrealized_gains 0; sale gain nil with reason :missing_cost_basis; realized_gains 0 trades, { missing_cost_basis: 1 } excluded | holding/forward_calculator.rb:109 (transfer-in marks the position unknown) and :131 (cost_basis_for); holding.rb:421 (calculate_avg_cost fallback) and :119 (preload_avg_costs) |
Mutation table
Each mutation was applied to a throwaway copy of the committed tree in a Docker volume (never to the worktree), the four tests were run, and the file was restored. Every mutation was an exact single-occurrence replacement.
| # | Mutation | Test that failed | Observed |
|---|---|---|---|
| M1 | holding.rb:253 weighted merge → plain mean (a + b) / 2 | weighted cost basis | expected 60, got 70 |
| M2 | holding.rb:255 else branch keeps holding.cost_basis instead of existing.cost_basis | unknown side keeps | expected 100, got nil |
| M3 | cost_basis_tracker.rb:49 sell relieves no cost | two-parcel fixture | expected 15.20, got 25.33 |
| M4 | cost_basis_tracker.rb:38 buy replaces the total with the latest price (no weighting) | two-parcel fixture | expected 15.20, got 20 |
| M5 | trade.rb:261 basis uses holding.qty instead of qty.abs | two-parcel fixture | sale basis assertion ($608) fails |
| M6 | investment_statement.rb:382 sums trend.current instead of trend.value | two-parcel fixture | expected 888, got 1800 |
| M7 | forward_calculator.rb:109 transfer-in applied to the tracker like a buy | transferred-in fixture | expected nil, got 12 |
| M8 | forward_calculator.rb:131 transfer guard removed from cost_basis_for | transferred-in fixture | expected nil, got 20 |
| M9 | holding.rb:421 internal-movement guard removed from calculate_avg_cost | transferred-in fixture | sale gain expected nil, got $1,000 - $800 |
| M10 | holding.rb:119 has_transfer dropped from preload_avg_costs | transferred-in fixture | row avg_cost expected nil, got $20 |
| M11 | materializer.rb:122 writes nil instead of the reconciled basis | two-parcel fixture | expected 12, got nil |
Validation
- Full suite on the exact head
f666272b9(beastpc, Postgres 16,PARALLEL_WORKERS=6): 14310 runs, 66519 assertions, 0 failures, 0 errors, 38 skips. bin/rubocop test/models/holding_test.rb test/models/investment_statement_test.rb: no offenses.- No ERB, JavaScript, migration, API or UI change, so erb_lint, Biome, rswag and screenshots do not apply; brakeman is unaffected by a test-only diff.
- Checked against upstream
AGENTS.mdanddocs/llm-guides/testing.md: Minitest only, edge cases built in the test, no new fixtures, comments describe current behaviour rather than history, subject at most 72 characters.
Notes on the PR 0 definition in jaysbeekay/sure#551
- The plan's PR 0 line lists three things to pin ("the remap merge
cost_basis,InvestmentStatementunrealized and realized on the Example fixture, and the transfer-in nil"), but the named PR 0 tests cover only the first two. The transfer-in nil is added as a fourth test on the Example's own transfer case (the body's "Transfer case" paragraph), named after the fixture test it mirrors. Nothing else was added. - "Realized gain of the 40-unit sale == $392" is pinned at two levels,
Trade#realized_gain_lossandInvestmentStatement#realized_gains, because PR 2 changes what both read. - The issue's Example has no fees, so no fee mutation is meaningful against this fixture; the fee path is already covered by
forward_calculator_test.rb("an acquisition fee is part of the cost basis") and is not repeated here.
Summary by CodeRabbit
- Tests
- Expanded coverage for cost-basis handling when holdings are merged, including cases where one or both holdings have unknown basis.
- Added checks for investment statement calculations across multiple purchase dates, transferred-in holdings, sales, and realized and unrealized gains.