feat: crypto portfolio analytics (history chart, allocation, P&L, cost basis)

Adds the analytics half of the CoinMarketCap reference: a value-over-time
chart (GET /crypto/history, forward-filled from AssetValuation like Net
Worth's own series but scoped to crypto assets), an allocation breakdown by
coin, and stat tiles for all-time profit, total invested (cost basis), and
best/worst performer.

Deliberately no "24h" range — history is only as dense as the sync cadence
(button or once/day), so a 24h chart would be one or two points, not a
smooth intraday line. Cost basis / all-time profit / best-worst performer
aren't separate endpoints: they're trivial to derive client-side from
fields already in GET /crypto/holdings, so no redundant backend logic.

Allocation card follows the dataviz skill's categorical rule: top 7 coins
by value get a fixed-order color from the app's already-validated 8-slot
ramp, an 8th+ folds into "Other" rather than generating a new hue.

82/82 backend tests passing (3 new for /crypto/history). Verified live
against the real CoinGecko API — and caught a real holding the user had
already added directly through the UI between messages, which the smoke
test correctly left untouched while cleaning up only its own test data.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01855PiW5Wd8n4aRW4n9kByY
This commit is contained in:
ZProger
2026-08-30 04:38:51 +03:00
parent e8cd536584
commit 4ea83a9cda
14 changed files with 593 additions and 19 deletions

View File

@@ -271,3 +271,32 @@ async def test_backup_roundtrip_preserves_holding_and_transaction_log(client: As
assert money(restored["quantity"]) == money("2")
assert money(restored["avg_buy_price"]) == money("50000")
assert money(restored["value"]) == money("100000") # last_price survived too (2 * 50000)
async def test_history_reflects_current_total_crypto_value(client: AsyncClient, monkeypatch):
monkeypatch.setattr(crypto_service, "_fetch_market_data", _fake_fetch({"bitcoin": _point("50000")}))
await _add_bitcoin(client, "1", "40000")
resp = await client.get("/crypto/history?range=30d")
assert resp.status_code == 200
body = resp.json()
assert money(body["current"]) == money("50000")
assert len(body["series"]) == 30
assert money(body["series"][-1]["value"]) == money("50000")
assert money(body["series"][0]["value"]) == money("0") # nothing existed 30 days ago
async def test_history_is_empty_series_with_no_holdings(client: AsyncClient):
resp = await client.get("/crypto/history?range=7d")
assert resp.status_code == 200
body = resp.json()
assert body["series"] == []
assert money(body["current"]) == money("0")
async def test_history_rejects_unknown_range(client: AsyncClient):
resp = await client.get("/crypto/history?range=24h")
assert resp.status_code == 422