Files
trex-core/tests/application/test_killswitch_execution.py
T
genichinandClaude Opus 4.8 7d727824dc feat(slot): BAT-030 — 자산 중심 cutover 완성 (dcy-020 BAT-δ, REQ-056·057·062·064·065)
M1 literal inactive 전면화: SlotState 5-state(idle/closed 별칭 제거),
마이그레이션 010(CHECK 교체+행 rewrite), ops_checks 를 transition_log 기반 재작성.
M2 집행 매도 수량 브로커 정본(D3): dispatch 매도 qty=broker hldg_qty,
미러 fallback 없음, 브로커 0 → skip+WARN, 리스트 기반 dispatch(head-block 방지).
M3 컬럼 drop + legacy pool 제거: 마이그레이션 011(trading_date·entry_count DROP),
repo 계약 instance 스코프 개편(집행 큐 포함), preallocation 폐기(데몬 7스레드),
admission 자산 중심 단일 경로(slot_id 정본), capacity 재정의(available=M−active),
daily_symbol_entries 코드 경로 제거.
M4 cutover 도구(D1·D4): trex-core-cutover(전체 rebuild, preflight/브로커 carry-over/
--allow-unmapped/--promote-unique/dry-run) + PG 리허설 테스트 8건.
M5 baseline 마감: data-model/runtime-flows/project-structure/interface-contract
자산 중심 최종 갱신 + run-trex-core-daemon 스킬 정합.

검증: 단위 367 passed + ruff clean; PG-gated 29 passed(010/011 round-trip·드리프트
가드·cutover 리허설); RabbitMQ 21 passed; run-the-app e2e(test DB+실 MQ, M=1):
reconcile 슬롯 생성→slot_id 매수→dedup→capacity_full→fill→already_active→
매도→inactive(자산 바인딩 보존)→영구 슬롯 재활성 전 구간 green.
실 cutover 는 운영자 실행(REL 단계, D4).

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

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})
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