fix: close broker uncertainty audit gaps

This commit is contained in:
jordan
2026-07-29 17:53:39 +09:00
parent de1e3eb44c
commit 611805dcec
19 changed files with 236 additions and 42 deletions
+36 -1
View File
@@ -11,7 +11,7 @@ import os
import httpx
import pytest
from trex_core.adapters.broker.kis import KISBroker
from trex_core.adapters.broker.kis import KISBroker, _balance_http_error_code
from trex_core.application.broker_port import (
BrokerBalanceError,
CancelState,
@@ -31,6 +31,41 @@ def test_account_split_and_mock_tr_ids():
assert "12345678" not in b.account_scope_hash()
def test_account_scope_hash_separates_endpoint_and_application_identity():
mock_a = KISBroker(
"12345678-01", app_key="app-a", app_secret="s", base_url=_MOCK_URL
)
mock_b = KISBroker(
"12345678-01", app_key="app-b", app_secret="s", base_url=_MOCK_URL
)
live_a = KISBroker(
"12345678-01",
app_key="app-a",
app_secret="s",
base_url="https://openapi.koreainvestment.com:9443",
allow_live=True,
)
assert len({mock_a.account_scope_hash(), mock_b.account_scope_hash(),
live_a.account_scope_hash()}) == 3
@pytest.mark.parametrize(
("status", "code"),
[
(400, "broker_rejected"),
(401, "authentication_failed"),
(403, "authorization_failed"),
(422, "broker_rejected"),
(429, "transport_failed"),
(500, "transport_failed"),
(None, "broker_rejected"),
],
)
def test_balance_http_status_classification(status, code):
assert _balance_http_error_code(status) == code
def test_live_base_url_requires_allow_live():
with pytest.raises(RuntimeError):
KISBroker(
@@ -9,7 +9,8 @@ from __future__ import annotations
import psycopg
from trex_core.adapters.persistence.migrate import apply_migrations, migration_files
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS
from trex_core.adapters.persistence.postgresql import PostgreSQLSlotRepository
from trex_core.domain.slot import EXPECTED_SLOT_FIELDS, SlotService, SlotState
_EXPECTED_TABLES = {
"daily_slot",
@@ -61,3 +62,22 @@ def test_apply_migrations_is_idempotent(pg_dsn):
# applying twice must not error (CREATE ... IF NOT EXISTS)
apply_migrations(pg_dsn)
apply_migrations(pg_dsn)
def test_migration_replay_accepts_existing_auto_venue_row(pg_clean):
repo = PostgreSQLSlotRepository(pg_clean)
service = SlotService(repo, instance_key="migration-auto")
repo.reconcile_universe("migration-auto", {"pf-auto": "005930"})
slot = repo.find_slot_by_portfolio("migration-auto", "pf-auto")
service.transit_slot(
slot.slot_id,
SlotState.INACTIVE,
SlotState.PENDING_BUY,
reason="migration_replay",
actor="test",
venue="AUTO",
)
apply_migrations(pg_clean)
assert repo.fetch_slot(slot.slot_id).venue == "AUTO"
@@ -132,6 +132,29 @@ def test_dispatch_queue_instance_scoped(pg_clean):
assert repo_a.list_dispatched_pending("inst-b") == []
def test_cancel_attempt_reservation_is_compare_and_set(pg_clean):
repo, _, intents = _wire(pg_clean)
repo.reconcile_universe(INST, {"PF-1": "005930"})
decision = intents.submit_buy(
BuyIntent(intent_id="pg-cancel-cas", qty=1, portfolio_id="PF-1")
)
assert repo.mark_dispatched(decision.slot_id, "ord-cas")
assert repo.mark_cancel_outcome(
decision.slot_id,
"ord-cas",
"unknown",
only_if_unset=True,
)
assert not repo.mark_cancel_outcome(
decision.slot_id,
"ord-cas",
"unknown",
only_if_unset=True,
)
assert repo.mark_cancel_outcome(decision.slot_id, "ord-cas", "requested")
assert repo.fetch_slot(decision.slot_id).cancel_state == "requested"
# dcy-028 / REQ-079·081 (BAT-042): strategy-state store + occupancy_seq column
def test_strategy_state_store_pg(pg_clean):
from trex_core.adapters.persistence.postgresql import PgStrategyStateStore