File size: 4,127 Bytes
2e10c7a
 
 
 
 
 
daf11c9
 
 
2e10c7a
 
 
 
daf11c9
2e10c7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Every test runs against fly_trader_test. DATABASE_URL is forced before any fly_trader import so
the refuse-production guard in fly_trader.db.connection can never see the real database."""
import os

os.environ["DATABASE_URL"] = "postgresql:///fly_trader_test"
os.environ["FLY_TRADER_TESTING"] = "1"           # fly_trader/netguard.py: no transaction, order or transfer leaves a test run
for _name in ("BOT_PRIVATE_KEY", "BOT_PRIVATE_KEY_FILE", "RH_BOT_PRIVATE_KEY", "SIGNER_SOCKET"):   # and the operator's signing keys
    os.environ[_name] = ""                                                         # (or signer) never load (empty = unset; .env never overrides)
for _name in ("RH_RPC_URL_TX", "ALCHEMY_API_KEY", "QUICKNODE_RH_API_URL", "QUICKNODE_RH_API_KEY"):   # no test reaches the real transaction endpoint
    os.environ[_name] = ""
for _name in ("LIVE_ENABLED", "RH_LIVE_ENABLED", "KALSHI_LIVE_ENABLED", "LIVE_WITHOUT_HANDOVER"):   # nor runs with the operator's
    os.environ[_name] = "0"                                                                   # live switches from .env
os.environ["RH_CAPITAL_ETH"] = ""      # empty = the code's pinned default, not the operator's wallet balance (load_dotenv never overrides)
os.environ["KELLY_FRACTION"] = ""      # and the code's default stake, not the operator's

import pytest  # noqa: E402

TEST_URL = "postgresql:///fly_trader_test"


@pytest.fixture(scope="session", autouse=True)
def _test_database():
    from fly_trader.db import schema
    schema.ensure_database(TEST_URL)
    schema.apply_schema(TEST_URL)
    yield


@pytest.fixture
def db_conn():
    from fly_trader.db.connection import connect
    conn = connect(TEST_URL)
    try:
        yield conn
    finally:
        conn.rollback()
        conn.close()


class FakeSupervisor:
    """The console's view of the Supervisor in tests: worker state you set, and every control call recorded."""

    def __init__(self):
        from fly_trader.ops.supervisor import WORKERS
        self.alive_set: set[str] = set()
        self.calls: list[tuple] = []
        self.pid = os.getpid()
        self.autostarted = True
        self.workers = WORKERS

    def status(self) -> dict:
        return {n: {"alive": n in self.alive_set, "started_at": None, "stopped_at": None, "error": None, "stopping": False} for n in self.workers}

    def alive(self, name: str) -> bool:
        return name in self.alive_set

    def external(self) -> dict:
        return {}

    def start(self, name: str, started_by: str = "console") -> bool:
        self.calls.append(("start", name, started_by)); self.alive_set.add(name); return True

    def request_stop(self, name: str, by: str = "console") -> bool:
        self.calls.append(("request_stop", name)); return name in self.alive_set

    def stop(self, name: str, grace_s: float = 90.0) -> bool:
        self.calls.append(("stop", name)); self.alive_set.discard(name); return True

    def log_tail(self, name: str, n: int = 60) -> str:
        return ""


@pytest.fixture
def fake_sup(monkeypatch):
    """Point the console (fly_trader.ui.runtime.sup) at a FakeSupervisor."""
    from fly_trader.ui import runtime
    s = FakeSupervisor()
    monkeypatch.setattr(runtime, "sup", lambda: s)
    return s


@pytest.fixture
def console(fake_sup):
    """A TestClient on the console app (no workers hosted). ``console.act(name, body)`` POSTs an action with the
    console's CSRF header; ``console.topic(name, **params)`` reads a topic."""
    from fastapi.testclient import TestClient
    from fly_trader.ui.server import create_app
    import json as _json
    from fly_trader.ui import runtime
    hosted = runtime.HOSTS_WORKERS
    app = create_app(hosts_workers=False)
    with TestClient(app, base_url="http://localhost") as c:
        c.act = lambda name, body=None: c.post(f"/api/action/{name}", json=body or {}, headers={"X-Fly-Console": "1"})
        c.topic = lambda topic_name, **params: c.get(f"/api/topic/{topic_name}", params={"params": _json.dumps(params)} if params else None)
        c.sup = fake_sup
        yield c
    runtime.HOSTS_WORKERS = hosted