Files
trex-core/tests/runtime/test_instance_config.py
T
genichinandClaude Opus 4.8 f0ad37747b feat(rel-007): dcy-025/024 + BAT-035/036 — slots.req 즉시 교체 + safety_sell_enabled
BAT-035 (dcy-025): trex.snapshot.req → trex.slots.req 즉시 교체
- topology: SNAPSHOT_REQ_QUEUE 제거, SLOTS_REQ_QUEUE = "trex.slots.req" 추가
- portfolio.py: View/build_snapshot_envelope/SnapshotEnvelope/_asset_to_dict 제거;
  slot_to_dict 시그니처 확장(snapshot_store/watchdog_store/pause_store/watchdog_timeout_sec)
  — asset_quantity(hub)/cash_balance/cash_estimated/warned/watchdog{}/paused 병합 (REQ-074)
- messaging_handlers.py: make_snapshot_rpc_handler 제거, make_slots_rpc_handler 추가
  — filter(all/inactive/pending/holding/error) + spy(bool) 처리; strategy 필드 제외 (REQ-073/074)
- daemon.py: _snapshot_rpc_loop → _slots_rpc_loop, make_slots_rpc_handler 연결
- cli.py: universe/slots → slots_queue + spy=True + server-side filter; buyable→inactive 매핑 (REQ-063)
- rabbitmq.py: snapshot_queue → slots_queue
- docs: interface-contract.md + runtime-flows.md slots.req 계약으로 업데이트 (REQ-075)
- REQ-028: Implemented → Deprecated (코드 삭제 완료)
- 테스트: test_snapshot_handler/selection/rpc_watchdog/messaging_snapshot_consumer 삭제;
  test_slots_handler.py(신규, 13 cases), test_killswitch_masking_resume 재작성,
  test_ledger_option_d._apply_cash_estimate → slots handler 경유, test_topology/cli_operator 갱신

BAT-036 (dcy-024): per-instance safety_sell_enabled 플래그
- config.py: DaemonInstanceConfig + DaemonConfig 에 safety_sell_enabled: bool = True 추가;
  from_toml 파싱, from_config 전파
- daemon.py: start() workers 조건부 — safety_sell_enabled=false 시 trex-safety-sell 미기동 + WARNING
- tests: test_instance_config.py 에 safety_sell_enabled 파싱 3개 케이스 추가

REQ: 073/074/075/021/034/063/048/005 Approved → Implemented

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-11 14:45:45 +09:00

118 lines
4.8 KiB
Python

"""Per-instance TOML config loader tests (REQ-048 / BAT-013)."""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
import pytest
from trex_core.runtime.config import DaemonConfig, DaemonInstanceConfig
_REPO = Path(__file__).resolve().parents[2]
def _write_toml(tmp_path, body: str) -> str:
p = tmp_path / "inst.toml"
p.write_text(body)
return str(p)
def test_from_toml_parses(tmp_path):
"""AC-1: from_toml reads instance_key + portfolio_hub_token."""
path = _write_toml(tmp_path, 'instance_key = "inst-1"\nportfolio_hub_token = "tok"\n')
cfg = DaemonInstanceConfig.from_toml(path)
assert cfg.instance_key == "inst-1"
assert cfg.portfolio_hub_token == "tok"
def test_from_toml_missing_instance_key_raises(tmp_path):
"""AC-4: missing required instance_key → error (no silent default)."""
path = _write_toml(tmp_path, 'portfolio_hub_token = "tok"\n')
with pytest.raises(RuntimeError):
DaemonInstanceConfig.from_toml(path)
def test_from_config_injects_token(tmp_path, monkeypatch):
"""AC-3 / S-5: TOML token is injected into PortfolioHubConfig when base_url is set,
even with TREX_PORTFOLIO_HUB_TOKEN absent from the environment."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
monkeypatch.setenv("TREX_PORTFOLIO_HUB_BASE_URL", "http://hub:18000")
monkeypatch.delenv("TREX_PORTFOLIO_HUB_TOKEN", raising=False)
path = _write_toml(tmp_path, 'instance_key = "inst-9"\nportfolio_hub_token = "toml-tok"\n')
cfg = DaemonConfig.from_config(path)
assert cfg.instance_key == "inst-9"
assert cfg.portfolio_hub is not None
assert cfg.portfolio_hub.token == "toml-tok"
def test_default_instance_key_when_from_env(monkeypatch):
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
assert DaemonConfig.from_env().instance_key == "default"
def test_trading_timezone_defaults_to_kst(tmp_path, monkeypatch):
"""dcy-014 / REQ-048: trading TZ defaults to KST (preallocation trigger
removed at BAT-030 with the pool model)."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
inst = DaemonInstanceConfig.from_toml(_write_toml(tmp_path, 'instance_key = "i1"\n'))
assert inst.trading_timezone == "Asia/Seoul"
assert DaemonConfig.from_env().trading_timezone == "Asia/Seoul"
def test_trading_timezone_from_toml_propagates(tmp_path, monkeypatch):
"""dcy-014 / REQ-048: TOML trading_timezone + trigger flow into DaemonConfig."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
path = _write_toml(
tmp_path,
'instance_key = "i2"\ntrading_timezone = "UTC"\n',
)
cfg = DaemonConfig.from_config(path)
assert cfg.trading_timezone == "UTC"
def test_config_required_cli_exits_nonzero():
"""AC-2: daemon entry without --config exits non-zero (argparse required)."""
proc = subprocess.run(
[sys.executable, "-m", "trex_core.runtime.daemon"],
capture_output=True, text=True, timeout=30, cwd=str(_REPO),
)
assert proc.returncode != 0
assert "config" in (proc.stderr + proc.stdout).lower()
def test_gitignore_excludes_instance_toml():
"""AC-5 (CHG-20260609-01): .gitignore ignores config/* but keeps example
templates (*.example.toml covers instance.example.toml) and .env.example."""
gi = (_REPO / ".gitignore").read_text()
assert "/config/*" in gi
assert "!/config/*.example.toml" in gi
assert "!/config/.env.example" in gi
# --- safety_sell_enabled (REQ-048 CHG-20260611-01, BAT-036) -----------------
def test_safety_sell_enabled_default_true(tmp_path, monkeypatch):
"""AC-10: absent from TOML → safety_sell_enabled=True (default on)."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
inst = DaemonInstanceConfig.from_toml(_write_toml(tmp_path, 'instance_key = "i3"\n'))
assert inst.safety_sell_enabled is True
cfg = DaemonConfig.from_config(_write_toml(tmp_path, 'instance_key = "i3"\n'))
assert cfg.safety_sell_enabled is True
def test_safety_sell_enabled_false_from_toml(tmp_path, monkeypatch):
"""AC-10: safety_sell_enabled = false in TOML → DaemonConfig.safety_sell_enabled=False."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
path = _write_toml(tmp_path, 'instance_key = "i4"\nsafety_sell_enabled = false\n')
cfg = DaemonConfig.from_config(path)
assert cfg.safety_sell_enabled is False
def test_safety_sell_enabled_true_explicit_from_toml(tmp_path, monkeypatch):
"""AC-10: safety_sell_enabled = true in TOML → DaemonConfig.safety_sell_enabled=True."""
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
path = _write_toml(tmp_path, 'instance_key = "i5"\nsafety_sell_enabled = true\n')
cfg = DaemonConfig.from_config(path)
assert cfg.safety_sell_enabled is True