"""Intent admission RPC handler + dedup tests (REQ-021/022/023, dcy-006). The handler calls IntentService.submit_*, so dedup uses the IntentStore (intent_log) gate (REQ-007). Two messages with the same correlation_id trigger exactly ONE slot transition (REQ-023 AC-1). """ from __future__ import annotations from datetime import date from trex_core.adapters.persistence.in_memory_intent_store import InMemoryIntentStore from trex_core.application.intents import IntentService from trex_core.application.messaging import RpcRequest from trex_core.application.messaging_handlers import make_intent_rpc_handler from trex_core.domain.slot import SlotService, SlotState from trex_core.domain.slot.in_memory_repository import InMemorySlotRepository DAY = date(2026, 6, 5) def _wire(): repo = InMemorySlotRepository() svc = SlotService(repo) intents = IntentService(svc, repo, InMemoryIntentStore()) svc.preallocate_daily_slots(DAY, 3) return repo, svc, intents def _buy_req(intent_id: str, symbol: str = "005930") -> RpcRequest: return RpcRequest( payload={ "intent_type": "buy", "intent_id": intent_id, "trading_date": DAY.isoformat(), "symbol": symbol, "qty": 10, }, correlation_id=intent_id, ) def test_buy_admission_returns_admitted_and_transitions_slot(): repo, _, intents = _wire() handler = make_intent_rpc_handler(intents) resp = handler(_buy_req("buy-1")) assert resp.payload["status"] == "admitted" assert resp.payload["slot_id"] from uuid import UUID slot = repo.fetch_slot(UUID(resp.payload["slot_id"])) assert slot.status is SlotState.PENDING_BUY and slot.symbol == "005930" def test_duplicate_correlation_id_dedups_via_intent_log(): """REQ-023 AC-1: same intent_id ⇒ slot transition exactly once.""" repo, _, intents = _wire() handler = make_intent_rpc_handler(intents) r1 = handler(_buy_req("buy-dup")) r2 = handler(_buy_req("buy-dup")) assert r1.payload == r2.payload pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY] assert len(pending) == 1 def test_buy_rejected_when_no_idle_slot(): repo, _, intents = _wire() handler = make_intent_rpc_handler(intents) for i in range(3): assert handler(_buy_req(f"buy-{i}")).payload["status"] == "admitted" resp = handler(_buy_req("overflow")) assert resp.payload["status"] == "rejected" assert resp.payload["reason"] == "no_idle_slot" def test_sell_admission_from_holding(): repo, svc, intents = _wire() handler = make_intent_rpc_handler(intents) sid = handler(_buy_req("buy-h")).payload["slot_id"] from uuid import UUID svc.transit_slot(UUID(sid), SlotState.PENDING_BUY, SlotState.HOLDING, reason="filled", actor="t") resp = handler(RpcRequest( payload={"intent_type": "sell", "intent_id": "sell-h", "slot_id": sid}, correlation_id="sell-h", )) assert resp.payload["status"] == "admitted" assert repo.fetch_slot(UUID(sid)).status is SlotState.PENDING_SELL def test_bad_payload_returns_rejected_not_exception(): """Handler must not raise — bad input ⇒ structured rejection.""" _, _, intents = _wire() handler = make_intent_rpc_handler(intents) # missing slot_id for sell resp = handler(RpcRequest( payload={"intent_type": "sell", "intent_id": "bad"}, correlation_id="bad", )) assert resp.payload["status"] == "rejected" assert resp.payload["reason"].startswith("bad_request:") def test_unknown_intent_type_rejected(): _, _, intents = _wire() handler = make_intent_rpc_handler(intents) resp = handler(RpcRequest( payload={"intent_type": "ping", "intent_id": "x"}, correlation_id="x", )) assert resp.payload["status"] == "rejected" assert "unknown_intent_type" in resp.payload["reason"] def test_correlation_id_is_used_as_intent_id_when_missing_in_payload(): """The transport's correlation_id is the canonical idempotency key.""" repo, _, intents = _wire() handler = make_intent_rpc_handler(intents) req = RpcRequest( payload={ "intent_type": "buy", "trading_date": DAY.isoformat(), "symbol": "005930", "qty": 5, }, correlation_id="cid-only", ) r1 = handler(req) r2 = handler(req) assert r1.payload["status"] == "admitted" assert r1.payload == r2.payload pending = [s for s in repo.list_slots(DAY) if s.status is SlotState.PENDING_BUY] assert len(pending) == 1