Files
trex-core/tests/runtime/test_cli_operator.py
T
genichinandClaude Opus 4.8 6d7bca1460 feat(safety-sell): dcy-017 워치독 소유권 가시화 — snapshot 상태 + 발동 로깅 (BAT-021)
전략 이탈로 슬롯이 내부 청산 루틴(워치독) 소유로 넘어간 상태가 무성이라
운영자가 인지할 수 없던 문제(issue #12)를 가시화한다.

거버넌스: 전부 change-req (신규 REQ 없음, dcy-017 OQ-3; Redis 미채택).
- REQ-034/021: WatchdogStore.last_seen + snapshot RPC 응답에 holding 슬롯
  워치독 상태(expired/last_seen) read-only 동봉(best-effort)
- REQ-032: trex-core slots 가 expired 슬롯을 WATCHDOG-OWNED(전략 이탈) 표시
- REQ-037/038/039: safety-sell 워치독 소유 진입(WARN)/이탈(INFO) + 발동
  (forced_close·stop_loss=WARN, take_profit·market_close=INFO) 로그 +
  in-process 카운터(SafetySellService.counters), 구조화 필드(slot_id/
  portfolio_id/reason/phase/instance_key). per-tick 스킵은 카운터만
- REQ-042: runtime-flows/interface-contract 정합

best-effort 격리(NFR-1): 모든 가시화는 예외 격리 — 워치독 판정·청산 불변.
dcy-016 억제 읽기로 관측이 워치독을 리셋하지 않음(비침습).

검증: 단위 335 passed/34 skipped, 인프라 게이트(PG+RabbitMQ) 369 passed, ruff clean.
신규 test_safety_sell_visibility(6) + snapshot watchdog(1) + CLI(1). BAT-021 rc.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 17:10:26 +09:00

158 lines
5.7 KiB
Python

"""Operator CLI tests (dcy-016 / REQ-032) — RPC injected, no broker/DB needed."""
from __future__ import annotations
import json
import pytest
from trex_core.runtime import cli
@pytest.fixture(autouse=True)
def _env(monkeypatch):
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
monkeypatch.setenv("TREX_BROKER", "paper")
monkeypatch.delenv("TREX_KIS_ALLOW_LIVE", raising=False)
class _RpcRecorder:
def __init__(self, response):
self.response = response
self.calls = []
def __call__(self, queue, payload, *, correlation_id, timeout):
self.calls.append({"queue": queue, "payload": payload, "cid": correlation_id})
return self.response
_SNAPSHOT = {
"assets": [{"asset_id": "PF-1", "symbol": "005930", "asset_quantity": 0,
"active_strategy_id": "orb"}],
"view": "all", "available_slots": 9, "degraded": False, "reason": None,
}
_ADMITTED = {"intent_id": "i", "status": "admitted", "slot_id": "S-1", "reason": None}
_REJECTED = {"intent_id": "i", "status": "rejected", "slot_id": None,
"reason": "unknown_portfolio"}
# --- universe / slots (snapshot RPC + watchdog suppression) ------------------
def test_universe_uses_snapshot_queue_with_operator_suppression(capsys):
rpc = _RpcRecorder(_SNAPSHOT)
rc = cli.main(["universe"], rpc=rpc)
assert rc == 0
call = rpc.calls[0]
assert call["queue"] == "trex.snapshot.req" # default instance
assert call["payload"]["actor"] == "operator"
assert call["payload"]["touch_watchdog"] is False # FR-2a / R-4
assert "PF-1" in capsys.readouterr().out
def test_slots_status_holding_maps_to_held_view():
rpc = _RpcRecorder(_SNAPSHOT)
cli.main(["slots", "--status", "holding"], rpc=rpc)
assert rpc.calls[0]["payload"]["view"] == "held"
def test_slots_marks_watchdog_owned(capsys):
"""dcy-017: slots flags strategy-absent (watchdog-owned) positions."""
resp = {
"assets": [{"asset_id": "PF-1", "symbol": "005930", "asset_quantity": 10,
"active_strategy_id": "orb"}],
"view": "held", "available_slots": 9, "degraded": False, "reason": None,
"watchdog": [{"slot_id": "S-1", "portfolio_id": "PF-1", "symbol": "005930",
"expired": True, "last_seen": None}],
}
cli.main(["slots", "--status", "held"], rpc=_RpcRecorder(resp))
out = capsys.readouterr().out
assert "watchdog_owned=1" in out and "WATCHDOG-OWNED" in out
def test_instance_shortcut_routes_to_instance_queue():
rpc = _RpcRecorder(_SNAPSHOT)
cli.main(["universe", "--instance", "inst-2"], rpc=rpc)
assert rpc.calls[0]["queue"] == "trex.snapshot.req.inst-2"
def test_json_output_is_valid_json(capsys):
rpc = _RpcRecorder(_SNAPSHOT)
cli.main(["universe", "--json"], rpc=rpc)
parsed = json.loads(capsys.readouterr().out)
assert parsed["assets"][0]["asset_id"] == "PF-1"
# --- enter (buy intent RPC) + safeguards ------------------------------------
def test_enter_aborts_without_confirmation_no_rpc():
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--capital", "100000"],
rpc=rpc, confirm=lambda _p: False)
assert rc == 1 and rpc.calls == [] # aborted -> no order
def test_enter_with_yes_submits_buy_intent():
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--capital", "100000", "--yes"], rpc=rpc)
assert rc == 0
p = rpc.calls[0]["payload"]
assert rpc.calls[0]["queue"] == "trex.intent.req"
assert p["intent_type"] == "buy" and p["portfolio_id"] == "PF-1"
assert p["allocated_capital"] == 100000
assert "symbol" not in p # server-resolved (dcy-011)
def test_enter_rejected_returns_nonzero():
rpc = _RpcRecorder(_REJECTED)
rc = cli.main(["enter", "PF-x", "--yes"], rpc=rpc)
assert rc == 1
def test_enter_dry_run_does_not_submit(capsys):
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--capital", "5", "--dry-run"], rpc=rpc)
assert rc == 0 and rpc.calls == []
assert "dry-run" in capsys.readouterr().out
def test_enter_json_requires_yes():
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--json"], rpc=rpc)
assert rc == 2 and rpc.calls == [] # no auto-submit (FR-7)
# --- exit (sell intent RPC) -------------------------------------------------
def test_exit_with_yes_submits_sell_with_limit_price():
rpc = _RpcRecorder(_ADMITTED)
cli.main(["exit", "S-1", "--price", "70000", "--yes"], rpc=rpc)
p = rpc.calls[0]["payload"]
assert p["intent_type"] == "sell" and p["slot_id"] == "S-1"
assert p["limit_price"] == 70000
def test_exit_market_when_no_price():
rpc = _RpcRecorder(_ADMITTED)
cli.main(["exit", "S-1", "--yes"], rpc=rpc)
assert rpc.calls[0]["payload"]["limit_price"] is None
# --- live guard (OQ-2 double ack) -------------------------------------------
def test_live_requires_live_flag(monkeypatch):
monkeypatch.setattr(cli, "_broker_mode", lambda cfg: ("kis", True))
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--yes"], rpc=rpc) # --yes but no --live
assert rc == 2 and rpc.calls == []
def test_live_proceeds_with_yes_live(monkeypatch):
monkeypatch.setattr(cli, "_broker_mode", lambda cfg: ("kis", True))
rpc = _RpcRecorder(_ADMITTED)
rc = cli.main(["enter", "PF-1", "--yes", "--live"], rpc=rpc)
assert rc == 0 and len(rpc.calls) == 1
# --- transport failure surfaces as a clear error ----------------------------
def test_rpc_failure_surfaces_error(capsys):
def boom(*a, **k):
raise RuntimeError("daemon unreachable")
rc = cli.main(["universe"], rpc=boom)
assert rc == 3
assert "daemon unreachable" in capsys.readouterr().err