fidelity: push per-fund manual snapshot instead of gains-offset DEPOSIT
PlanViewer's DisplayValuation.action JSON already gives us current fund units + unit price; we were parsing it and throwing it away, emitting only a single 'unrealised-gains-offset' DEPOSIT to make Wealthfolio's totals match the dashboard. That hack double-counted the gain as a cash contribution, hiding £35k of pension growth from every contribution/growth/ROI panel. New flow: - FidelityPlanViewerProvider exposes last_holdings + last_total_contribution after fetch() drains. - fidelity-ingest CLI converts to a ManualSnapshotPayload (cost basis allocated proportionally by current fund value share) and posts to WF /api/v1/snapshots/import. WF auto-creates unknown fund symbols with kind=INVESTMENT, quoteMode=MANUAL, quoteCcy=GBP. - The gains-offset emission is removed entirely. Historical offset rows already in WF are corrected at the dashboard layer by the dav_corrected view shipped in infra@2841347e. WealthfolioSink gains push_manual_snapshots() + ManualSnapshotPayload / SnapshotPosition wire types. 11 sink tests (3 new) + 9 fidelity provider tests (2 changed, 1 new) all green; mypy + ruff clean.
This commit is contained in:
parent
5adc4a7ba4
commit
cb159e17d9
5 changed files with 339 additions and 60 deletions
|
|
@ -1,19 +1,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, date, datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from broker_sync.models import Account, AccountType, ActivityType
|
||||
from broker_sync.models import Account, AccountType
|
||||
from broker_sync.providers.fidelity_planviewer import (
|
||||
ACCOUNT_ID,
|
||||
FidelityCreds,
|
||||
FidelityPlanViewerProvider,
|
||||
FidelityProviderConfigError,
|
||||
_gains_offset_activity,
|
||||
fidelity_holdings_to_snapshot,
|
||||
)
|
||||
from broker_sync.providers.parsers.fidelity import (
|
||||
parse_transactions_html,
|
||||
|
|
@ -96,21 +96,53 @@ def test_parse_valuation_fixture() -> None:
|
|||
assert set(h.units_by_source.keys()) >= {"SASC", "ERXS"}
|
||||
|
||||
|
||||
def test_gains_offset_emits_deposit_when_pot_exceeds_contributions() -> None:
|
||||
def test_holdings_to_snapshot_real_fixture() -> None:
|
||||
html = (_FIXTURES / "transactions-full.html").read_text()
|
||||
valuation = json.loads((_FIXTURES / "valuation.json").read_text())
|
||||
txs = parse_transactions_html(html)
|
||||
holdings = parse_valuation_json(valuation)
|
||||
as_of = datetime(2026, 4, 18, tzinfo=UTC)
|
||||
offset = _gains_offset_activity(holdings, txs, as_of)
|
||||
assert offset is not None
|
||||
assert offset.activity_type in (ActivityType.DEPOSIT, ActivityType.WITHDRAWAL)
|
||||
assert offset.amount is not None and offset.amount > 0
|
||||
assert offset.external_id == "fidelity:gains:2026-04-18"
|
||||
total_contrib = sum((tx.amount for tx in parse_transactions_html(html)),
|
||||
Decimal(0))
|
||||
|
||||
snapshot = fidelity_holdings_to_snapshot(
|
||||
holdings=holdings,
|
||||
total_real_contribution=total_contrib,
|
||||
as_of=date(2026, 4, 18),
|
||||
)
|
||||
assert snapshot is not None
|
||||
assert snapshot.date == date(2026, 4, 18)
|
||||
assert snapshot.currency == "GBP"
|
||||
# Cost basis sums to the cash contributions (allocated by fund value share)
|
||||
sum_cost = sum((p.total_cost_basis for p in snapshot.positions), Decimal(0))
|
||||
assert abs(sum_cost - total_contrib) < Decimal("1")
|
||||
# Meta scheme had KDOA + LAFC + one other at fixture time; the
|
||||
# dominant fund must be KDOA.
|
||||
symbols = [p.symbol for p in snapshot.positions]
|
||||
assert "KDOA" in symbols
|
||||
kdoa = next(p for p in snapshot.positions if p.symbol == "KDOA")
|
||||
assert kdoa.quantity > 0
|
||||
# Proportional cost-basis allocation: KDOA holds nearly the whole pot
|
||||
# so it should get the lion's share of cost
|
||||
kdoa_share = kdoa.total_cost_basis / sum_cost
|
||||
assert kdoa_share > Decimal("0.9")
|
||||
# cashBalances zero — pension contributions flow straight into funds
|
||||
assert snapshot.cash_balances == {"GBP": Decimal(0)}
|
||||
|
||||
|
||||
def test_gains_offset_none_when_no_holdings() -> None:
|
||||
assert _gains_offset_activity(
|
||||
holdings=[], transactions=[],
|
||||
as_of=datetime(2026, 4, 18, tzinfo=UTC),
|
||||
def test_holdings_to_snapshot_none_when_no_holdings() -> None:
|
||||
assert fidelity_holdings_to_snapshot(
|
||||
holdings=[], total_real_contribution=Decimal("100"),
|
||||
as_of=date(2026, 4, 18),
|
||||
) is None
|
||||
|
||||
|
||||
def test_provider_caches_holdings_for_cli_snapshot_push() -> None:
|
||||
"""The CLI reads `last_holdings` after fetch() drains to push the
|
||||
manual snapshot. This guards the contract that fetch() populates the
|
||||
attribute even when no Activity is yielded (e.g., backfill window
|
||||
cut-off)."""
|
||||
prov = FidelityPlanViewerProvider(FidelityCreds(
|
||||
storage_state_path="/tmp/x", plan_id="META",
|
||||
))
|
||||
# Pre-fetch state: empty
|
||||
assert prov.last_holdings == []
|
||||
assert prov.last_total_contribution == Decimal(0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue