Files
crypto_monitor/tests/test_hub_fund_history_lib.py
T
dekun d3d366d0ee Hide disabled exchanges from dashboard and fund overview.
Only aggregate and display exchanges with enabled monitoring in system settings.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 18:41:52 +08:00

114 lines
3.2 KiB
Python

"""hub_fund_history_lib:总资金、回撤与日快照。"""
from __future__ import annotations
from hub_fund_history_lib import (
account_total_usdt,
build_fund_overview,
compute_drawdown,
get_fund_history,
record_fund_snapshot,
)
def test_account_total_requires_both_sides():
assert account_total_usdt(10, 20) == 30.0
assert account_total_usdt(10, None) is None
assert account_total_usdt(None, 5) is None
def test_compute_drawdown():
dd = compute_drawdown([100, 120, 90, 110])
assert dd["peak_usdt"] == 120.0
assert dd["max_drawdown_u"] == 30.0
assert dd["max_drawdown_pct"] == 25.0
def test_build_fund_overview_skips_unmonitored(tmp_path, monkeypatch):
hist_path = tmp_path / "hub_fund_history.json"
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_PATH", hist_path)
record_fund_snapshot(
"2026-06-01",
[
{
"key": "binance",
"name": "Binance",
"funding_usdt": 10,
"trading_usdt": 20,
"monitored": True,
}
],
keep_days=180,
)
record_fund_snapshot(
"2026-06-02",
[
{
"key": "binance",
"name": "Binance",
"funding_usdt": 12,
"trading_usdt": 18,
"monitored": True,
}
],
keep_days=180,
)
exchanges = [
{"id": "0", "key": "binance", "name": "Binance", "enabled": True},
{"id": "3", "key": "gate_bot", "name": "Gate Bot", "enabled": False},
]
board_rows = [
{
"key": "binance",
"name": "Binance",
"account_ok": True,
"funding_usdt": 15,
"trading_usdt": 25,
}
]
out = build_fund_overview(
exchanges,
board_rows=board_rows,
trading_day="2026-06-02",
keep_days=180,
)
assert out["totals"]["total_usdt"] == 40.0
assert out["totals"]["monitored_count"] == 1
assert len(out["accounts"]) == 1
assert all(a["monitored"] for a in out["accounts"])
assert out["totals"]["drawdown"]["max_drawdown_u"] == 0.0
def test_history_start_day_filters_older(tmp_path, monkeypatch):
hist_path = tmp_path / "hub_fund_history.json"
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_PATH", hist_path)
monkeypatch.setattr("hub_fund_history_lib.FUND_HISTORY_START_DAY", "2026-06-09")
record_fund_snapshot(
"2026-06-01",
[
{
"key": "binance",
"name": "Binance",
"funding_usdt": 1,
"trading_usdt": 1,
"monitored": True,
}
],
keep_days=180,
)
record_fund_snapshot(
"2026-06-09",
[
{
"key": "binance",
"name": "Binance",
"funding_usdt": 10,
"trading_usdt": 20,
"monitored": True,
}
],
keep_days=180,
)
hist = get_fund_history(anchor_day="2026-06-10", keep_days=180)
assert "2026-06-01" not in hist
assert "2026-06-09" in hist