config 게이트 이중 경로(max_active_slots 설정 시 자산 중심, 미설정 시 legacy pool). 운영 데몬은 cutover(BAT-δ) 전까지 pool 무중단. - repository: find_slot_by_portfolio·count_active(holding+pending_buy+pending_sell)· reconcile_universe(생성 멱등/soft-retire/tombstone, allow_retire 가드) — protocol+in_memory+PG (PG: ON CONFLICT (instance_key, portfolio_id) WHERE portfolio_id IS NOT NULL) - snapshot_scheduler: run_once swap 직후 _reconcile_slots(빈 universe skip, ≥50%↓ retire 보류) - intents: _admit_buy 모드 분기 + _admit_buy_asset_centric(활성화/already_active/ capacity_full/unknown_portfolio/over-cap, 인스턴스 lock) - daemon: instance_key·max_active_slots·reconcile_enabled·trading_date_fn 배선 - 신규 테스트: test_universe_reconcile(6), test_asset_centric_admission(6), PG reconcile(1) 검증: 단위 387 passed / 35 skipped, PG-gated 17 passed, ruff clean. legacy 무파손. REQ-058/059/060 은 Approved 유지(자산 중심 경로 도입 검증; default 전환·cutover 는 BAT-δ). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
75 lines
2.9 KiB
Python
75 lines
2.9 KiB
Python
"""dcy-020 / REQ-058 (BAT-028): universe reconcile lifecycle (in-memory)."""
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
import pytest
|
|
|
|
from trex_core.domain.slot import SlotService, SlotState
|
|
from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository
|
|
|
|
DAY = date(2026, 6, 10)
|
|
INST = "inst-1"
|
|
|
|
|
|
@pytest.fixture
|
|
def repo():
|
|
return InMemorySlotRepository()
|
|
|
|
|
|
def test_creates_asset_slots_idempotently(repo):
|
|
r1 = repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
|
assert r1["created"] == 2
|
|
# idempotent — second pass creates nothing
|
|
r2 = repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
|
assert r2["created"] == 0
|
|
assert repo.find_slot_by_portfolio(INST, "pf-a").status is SlotState.IDLE
|
|
|
|
|
|
def test_soft_retire_inactive_asset_that_left_universe(repo):
|
|
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
|
r = repo.reconcile_universe(INST, DAY, {"pf-a"}) # pf-b left
|
|
assert r["retired"] == 1
|
|
slot_b = repo.find_slot_by_portfolio(INST, "pf-b")
|
|
assert slot_b.retired_at is not None # soft-retired, row preserved
|
|
|
|
|
|
def test_tombstone_holding_asset_that_left_universe(repo):
|
|
svc = SlotService(repo, instance_key=INST)
|
|
repo.reconcile_universe(INST, DAY, {"pf-a"})
|
|
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
|
svc.transit_slot(slot.slot_id, slot.status, SlotState.PENDING_BUY,
|
|
reason="r", actor="a", symbol="005930")
|
|
svc.transit_slot(slot.slot_id, SlotState.PENDING_BUY, SlotState.HOLDING,
|
|
reason="r", actor="a", avg_price=70_000)
|
|
r = repo.reconcile_universe(INST, DAY, set()) # pf-a left while holding
|
|
assert r["tombstoned"] == 1 and r["retired"] == 0
|
|
held = repo.find_slot_by_portfolio(INST, "pf-a")
|
|
assert held.status is SlotState.HOLDING and held.retired_at is None # protected
|
|
|
|
|
|
def test_allow_retire_false_skips_retire(repo):
|
|
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
|
r = repo.reconcile_universe(INST, DAY, {"pf-a"}, allow_retire=False)
|
|
assert r["retired"] == 0
|
|
assert repo.find_slot_by_portfolio(INST, "pf-b").retired_at is None
|
|
|
|
|
|
def test_count_active(repo):
|
|
svc = SlotService(repo, instance_key=INST)
|
|
repo.reconcile_universe(INST, DAY, {"pf-a", "pf-b"})
|
|
assert repo.count_active(INST) == 0
|
|
slot = repo.find_slot_by_portfolio(INST, "pf-a")
|
|
svc.transit_slot(slot.slot_id, slot.status, SlotState.PENDING_BUY,
|
|
reason="r", actor="a", symbol="005930")
|
|
assert repo.count_active(INST) == 1 # pending_buy counts as active
|
|
|
|
|
|
def test_instance_isolation(repo):
|
|
repo.reconcile_universe("inst-1", DAY, {"pf-a"})
|
|
repo.reconcile_universe("inst-2", DAY, {"pf-a"})
|
|
# same portfolio_id, different instances -> two distinct slots
|
|
assert repo.find_slot_by_portfolio("inst-1", "pf-a").slot_id != \
|
|
repo.find_slot_by_portfolio("inst-2", "pf-a").slot_id
|
|
assert repo.count_active("inst-1") == 0
|