reconcile_universe 시그니처를 set[str] → dict[str, str] (portfolio_id→symbol)로 변경. 기존 symbol=NULL 슬롯은 다음 reconcile 실행 시 자동 backfill. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
105 lines
3.8 KiB
Python
105 lines
3.8 KiB
Python
"""Kill-switch execution service tests (REQ-045 / BAT-012)."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from trex_core.application.killswitch import KillSwitchService
|
|
from trex_core.application.portfolio import GLOBAL_KILLSWITCH, InMemoryKillSwitchStore
|
|
from trex_core.domain.slot import SlotService, SlotState
|
|
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
|
|
|
|
_DAY = date(2026, 6, 7)
|
|
_NOW = datetime(2026, 6, 7, 6, 0, tzinfo=timezone.utc) # UTC date == _DAY
|
|
|
|
|
|
@pytest.fixture
|
|
def repo():
|
|
r = InMemorySlotRepository()
|
|
return r
|
|
|
|
|
|
@pytest.fixture
|
|
def service(repo):
|
|
return SlotService(repo)
|
|
|
|
|
|
@pytest.fixture
|
|
def store():
|
|
return InMemoryKillSwitchStore()
|
|
|
|
|
|
@pytest.fixture
|
|
def ks(repo, service, store):
|
|
return KillSwitchService(slot_repo=repo, slot_service=service, killswitch_store=store)
|
|
|
|
|
|
def _holding(service, repo, *, portfolio_id, symbol="005930"):
|
|
repo.reconcile_universe("default", {portfolio_id: "SYM"})
|
|
slot = repo.find_slot_by_portfolio("default", portfolio_id)
|
|
pending = service.transit_slot(
|
|
slot.slot_id, SlotState.INACTIVE, SlotState.PENDING_BUY,
|
|
reason="buy", actor="test", symbol=symbol,
|
|
)
|
|
return service.transit_slot(
|
|
pending.slot_id, SlotState.PENDING_BUY, SlotState.HOLDING,
|
|
reason="filled", actor="test", avg_price=50000.0, qty=1.0,
|
|
)
|
|
|
|
|
|
def test_global_liquidates_all(ks, repo, service, store):
|
|
"""AC-1: global scope sells all holding slots."""
|
|
a = _holding(service, repo, portfolio_id="pf-A")
|
|
b = _holding(service, repo, portfolio_id="pf-B")
|
|
n = ks.fire(scope="global", now=_NOW)
|
|
assert n == 2
|
|
assert repo.fetch_slot(a.slot_id).status is SlotState.PENDING_SELL
|
|
assert repo.fetch_slot(b.slot_id).status is SlotState.PENDING_SELL
|
|
assert GLOBAL_KILLSWITCH in store.list_excluded(_DAY)
|
|
|
|
|
|
def test_portfolio_scope_limits(ks, repo, service, store):
|
|
"""AC-2: portfolio scope only sells that portfolio's slot (1:1 asset slot)."""
|
|
a1 = _holding(service, repo, portfolio_id="pf-A", symbol="A1")
|
|
b = _holding(service, repo, portfolio_id="pf-B")
|
|
n = ks.fire(scope="portfolio", portfolio_id="pf-A", now=_NOW)
|
|
assert n == 1
|
|
assert repo.fetch_slot(a1.slot_id).status is SlotState.PENDING_SELL
|
|
assert repo.fetch_slot(b.slot_id).status is SlotState.HOLDING
|
|
assert store.list_excluded(_DAY) == {"pf-A"}
|
|
|
|
|
|
def test_idempotent_second_fire(ks, repo, service):
|
|
"""AC-3: a second fire does not re-transition already pending_sell slots."""
|
|
a = _holding(service, repo, portfolio_id="pf-A")
|
|
ks.fire(scope="global", now=_NOW)
|
|
n2 = ks.fire(scope="global", now=_NOW)
|
|
assert n2 == 0 # nothing still holding
|
|
log = repo.transition_log_for(a.slot_id)
|
|
assert len([e for e in log if e.to_state is SlotState.PENDING_SELL]) == 1
|
|
|
|
|
|
def test_marks_day_exclusion(ks, repo, service, store):
|
|
"""AC-5: scope marked day-excluded in the store."""
|
|
_holding(service, repo, portfolio_id="pf-A")
|
|
ks.fire(scope="portfolio", portfolio_id="pf-A", now=_NOW)
|
|
assert "pf-A" in store.list_excluded(_DAY)
|
|
|
|
|
|
def test_resume_clears_exclusion_not_slots(ks, repo, service, store):
|
|
"""AC-8: resume clears day-exclusion but sold slots stay sold."""
|
|
a = _holding(service, repo, portfolio_id="pf-A")
|
|
ks.fire(scope="portfolio", portfolio_id="pf-A", now=_NOW)
|
|
assert repo.fetch_slot(a.slot_id).status is SlotState.PENDING_SELL
|
|
ks.resume(scope="portfolio", portfolio_id="pf-A", now=_NOW)
|
|
assert "pf-A" not in store.list_excluded(_DAY)
|
|
# sold slot is NOT reverted
|
|
assert repo.fetch_slot(a.slot_id).status is SlotState.PENDING_SELL
|
|
|
|
|
|
def test_no_holding_returns_zero(ks, store):
|
|
n = ks.fire(scope="global", now=_NOW)
|
|
assert n == 0
|
|
assert GLOBAL_KILLSWITCH in store.list_excluded(_DAY) # still marks exclusion
|