Files
auto-stock/backend/tests/test_main_ops_r9_20261012.py
shuim d140055e64 fix: 하드닝 루프 9회차 — 매수 원복·잔고 연속조회·결과 불명 매도·브레이커·경보 보강
- 8회차 HIGH 수정 16건 레드팀 재검증: 12 확인·3 부분·1 미수정 → 전부 보강
- trade_executor/fill_sweep: 늦게 확정된 0체결 BUY → CANCELLED + ActiveTrade close, 부분체결 → 수량 축소(멱등, 재기동 후 TH 재구성)
- korea_investment_api: get_balance_all_v2(tr_cont 연속조회, _partial), get_todays_orders_v2(실패 None), 브레이커 청산용 조회 면제(OPEN 중 미집계)
- swing/exit_service·mdd_manager: 매도 직전 상한·MDD 축소가 전체 잔고 페이지 사용(§8.5-37), 불명 대기 중 부분익절 skip·쿨다운·pre_hldg 차감, 대기 TTL 15:30+재매수 폐기(대조 1회 선행)(§8.5-38 해소)
- swing/scheduler: 청산 TIMED_OUT/SKIPPED/in-flight 시 MDD 축소·진입 보류(예외는 진입만), 3사이클 연속 시 error+텔레그램
- daytrading: 결과 불명 매도 재판정 v3(주문조회 v2 None→보류, 등록 이후 일치 행→accepted, 원장≤0→보류), 재기동 복원 min(주문가능,보유), 잔고 조회 후 일일손실한도 재확인, 단독 실행 Redis 접속 제거
- main: 스트림 무수신 경보 v4(희망 구독 기준), 첫 하트비트 없음 stale(HB_FIRST_BEAT_GRACE_SEC)
- 운영: Jenkinsfile health_watch.py 디렉터리 잔존 복구, prod_smoke 사이드카 확인, config 키 12개 선언(동작 무변경)
- 진입가 v3(max(AT,PA))는 KIS 이동평균 평단 전제 오류로 레드팀 반증 → 폐기
- CLAUDE.md §3.2 ③-d, §8.5-37/38 갱신, §8.5-39~48 신규(Crisis 손절 소급 §8.5-39 는 사용자 결정 대기)
- 유닛 스위트 2781 passed / 0 failed (소켓 차단), 네트워크 시도 0건

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-12 15:37:16 +09:00

112 lines
4.0 KiB
Python

"""R9 E-2/E-5/E-6: 첫 하트비트 유예 판정, config 키 선언 동일성, 소규모 수정."""
import asyncio
import pytest
import app.main as m
from app.core import heartbeat as hb
from app.core.config import settings, Settings
class _T:
def __init__(self, done=False):
self._d = done
def done(self):
return self._d
def _app(alive=True):
a = type("A", (), {})()
a.state = type("S", (), {})()
for attr in ("swing_task", "dt_task"):
setattr(a.state, attr, _T(not alive))
return a
@pytest.fixture(autouse=True)
def _reset_hb(monkeypatch):
monkeypatch.setattr(m, "_scheduler_tasks", {"swing": "swing_task", "daytrading": "dt_task"})
hb.reset()
yield
hb.reset()
def test_no_first_beat_after_grace_in_market_is_stale():
over = settings.HB_FIRST_BEAT_GRACE_SEC + 100
h = m._scheduler_health_v3(_app(), now=1.0e6, market_open=True, uptime_sec=over)
for name in m._HEARTBEAT_WATCHED:
assert h[name]["stale"] is True and h[name]["no_first_beat"] is True
def test_no_first_beat_within_grace_is_normal():
under = settings.HB_FIRST_BEAT_GRACE_SEC - 500
h = m._scheduler_health_v3(_app(), now=1.0e6, market_open=True, uptime_sec=under)
for name in m._HEARTBEAT_WATCHED:
assert h[name]["stale"] is False and h[name]["no_first_beat"] is False
def test_no_first_beat_off_hours_is_normal():
over = settings.HB_FIRST_BEAT_GRACE_SEC + 100
h = m._scheduler_health_v3(_app(), now=1.0e6, market_open=False, uptime_sec=over)
assert all(v["stale"] is False for v in h.values())
def test_beat_present_not_flagged_as_no_first_beat():
hb.beat("swing")
over = settings.HB_FIRST_BEAT_GRACE_SEC + 100
h = m._scheduler_health_v3(_app(), market_open=True, uptime_sec=over)
assert h["swing"]["no_first_beat"] is False and h["swing"]["stale"] is False
def test_v2_signature_and_behavior_unchanged():
h = m._scheduler_health_v2(_app(), now=1.0e6, market_open=True)
assert "no_first_beat" not in h["swing"] and h["swing"]["stale"] is False
# ── E-5: 선언된 config 키 값이 소비처 기본값과 동일 ──────────────────────────
def _mod_default(modname, const):
import importlib
return getattr(importlib.import_module(modname), const)
@pytest.mark.parametrize("key,const_mod,const", [
("NEWS_EMPTY_ALERT_THRESHOLD", "app.services.news_sentiment", "_EMPTY_ALERT_DEFAULT"),
])
def test_declared_keys_match_consumer_defaults(key, const_mod, const):
assert key in Settings.model_fields
assert getattr(settings, key) == _mod_default(const_mod, const)
@pytest.mark.parametrize("key,expected", [
("KIS_WS_MAX_REGISTRATIONS", 41), ("RISK_MAX_DAILY_LOSS_RATIO", 0.03), ("RISK_MAX_CONSECUTIVE_LOSSES", 3),
("SWING_ADAPTIVE_TRAILING_ENABLED", False), ("MEAN_REVERSION_RULE_MERGE_ENABLED", False),
("HB_FIRST_BEAT_GRACE_SEC", 600.0),
])
def test_declared_keys_literal_defaults(key, expected):
assert getattr(settings, key) == expected
@pytest.mark.parametrize("key", [
"KIS_WS_ORIGIN", "KIS_WS_SUBPROTOCOLS",
"BATCH_ANALYSIS_TIMEOUT_SEC", "ZERO_BUY_ALERT_HOURS",
])
def test_declared_none_keys_preserve_none_semantics(key):
assert key in Settings.model_fields
assert getattr(settings, key) is None
def test_cors_origins_stays_undeclared():
""".env 에 값이 있어 선언하면 CORS 동작이 바뀐다 — 선언 금지 회귀 가드."""
assert "CORS_ORIGINS" not in Settings.model_fields
# ── E-6 ────────────────────────────────────────────────────────────────────
def test_trailing_stop_uses_resolved_multiplier(monkeypatch):
import app.services.trade_helpers as th
monkeypatch.setattr(th, "resolve_swing_trailing_atr_mult_v2", lambda: 2.0)
import inspect
src = inspect.getsource(th)
assert "high_price - 1.5 * atr" not in src
assert "resolve_swing_trailing_atr_mult_v2() * atr" in src