File size: 2,007 Bytes
b068b76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest
from fastapi.testclient import TestClient

import hf_unified_server

client = TestClient(hf_unified_server.app)


@pytest.mark.fallback
def test_local_resource_service_exposes_assets():
    """Loader should expose all symbols from the canonical registry."""
    service = hf_unified_server.local_resource_service
    service.refresh()
    symbols = service.get_supported_symbols()
    assert "BTC" in symbols
    assert len(symbols) >= 5


@pytest.mark.fallback
def test_top_prices_endpoint_uses_local_fallback(monkeypatch):
    """/api/crypto/prices/top should gracefully fall back to the local registry."""

    async def fail_get_top_coins(*_args, **_kwargs):
        raise hf_unified_server.CollectorError("coingecko unavailable")

    monkeypatch.setattr(hf_unified_server.market_collector, "get_top_coins", fail_get_top_coins)
    hf_unified_server.local_resource_service.refresh()

    response = client.get("/api/crypto/prices/top?limit=4")
    assert response.status_code == 200

    payload = response.json()
    assert payload["source"] == "local-fallback"
    assert payload["count"] == 4


@pytest.mark.api_health
def test_market_prices_endpoint_survives_provider_failure(monkeypatch):
    """Critical market endpoints must respond even when live providers fail."""

    async def fail_coin_details(*_args, **_kwargs):
        raise hf_unified_server.CollectorError("binance unavailable")

    async def fail_top_coins(*_args, **_kwargs):
        raise hf_unified_server.CollectorError("coingecko unavailable")

    monkeypatch.setattr(hf_unified_server.market_collector, "get_coin_details", fail_coin_details)
    monkeypatch.setattr(hf_unified_server.market_collector, "get_top_coins", fail_top_coins)
    hf_unified_server.local_resource_service.refresh()

    response = client.get("/api/market/prices?symbols=BTC,ETH,SOL")
    assert response.status_code == 200

    payload = response.json()
    assert payload["source"] == "local-fallback"
    assert payload["count"] == 3