Files
auto-stock/backend/tests/test_sector_mapper_cache_health_r3.py
shuim a264561576 fix: 하드닝 루프 8회차 — 체결 보정·고아 감지·청산 경보·실시간/차트·리스크 가드 일괄 수정
- 스윙 체결가 확인 전 executed_price NULL + fill_sweep, 진입가 resolve(ActiveTrade↔Portfolio), 0체결/부분체결 매수 장부 원복
- DT 고아 감지(근거 한정)·0체결 매도 복원·stale 점검 확대, 시간손절 -0.3% 원복
- 폴백 손절 ATR 캐시, 거래량부족-차단 신호 매수 차단, 현금 5% 유보 재적용, MDD peak/reset 수정
- KIS 서킷브레이커 청산 우회·경보, db_worker 재큐잉 순서, heartbeat·health-watch 사이드카
- 실시간 WS 재구독·41건 우선순위·다건 프레임 분할, 차트 샘플 제거·당일봉 합성, WS 서브프로토콜 인증
- 프론트: 미관리 그룹·확인 모달·차트 실시간 반영·예수금 표시 정정
- 전문가 에이전트 6종 추가(.claude/agents)
- 유닛 스위트 2667 passed (소켓 차단 하), tsc OK

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y83Za98XsNw9zuA4FTUytN
2026-10-10 12:17:49 +09:00

69 lines
2.3 KiB
Python

"""R3: 섹터 캐시 건강도·자동 갱신 회귀 테스트 (네트워크는 예외 발생형 목)."""
import asyncio
import json
import logging
from datetime import datetime, timedelta, timezone
import pytest
from app.services import sector_mapper as sm
@pytest.fixture(autouse=True)
def _iso(monkeypatch, tmp_path):
monkeypatch.setenv("AUTOSTOCK_SECTOR_CACHE_PATH", str(tmp_path / "sector_map.json"))
monkeypatch.setattr(sm, "_cache_payload", {})
monkeypatch.setattr(sm, "_cache_sectors", {})
monkeypatch.setattr(sm, "_cache_industries", {})
monkeypatch.setattr(sm, "_cache_mtime", -1.0)
def _boom(*a, **k):
raise AssertionError("network blocked")
monkeypatch.setattr(sm, "_fetch_json", _boom)
def _write(tmp_path, days_old):
ts = (datetime.now(timezone.utc) - timedelta(days=days_old)).isoformat()
(tmp_path / "sector_map.json").write_text(
json.dumps({"updated_at": ts, "sectors": {"000001": "finance"}}), encoding="utf-8")
def test_missing_cache_is_unhealthy_and_logs_error(caplog):
with caplog.at_level(logging.ERROR):
h = sm.log_cache_health_v2()
assert not h["ok"] and h["reason"] == "missing"
assert any("섹터 캐시" in r.message for r in caplog.records)
def test_fresh_cache_ok(tmp_path):
_write(tmp_path, 1)
h = sm.get_cache_health_v2()
assert h["ok"] and not h["stale"] and h["stocks"] == 1
def test_stale_cache_flagged(tmp_path):
_write(tmp_path, sm.SECTOR_CACHE_MAX_AGE_DAYS + 2)
h = sm.get_cache_health_v2()
assert not h["ok"] and h["reason"] == "stale"
def test_next_delay_skips_weekend():
kst = timezone(timedelta(hours=9))
fri_after = datetime(2026, 10, 9, 9, 0, tzinfo=kst) # 금요일 09:00
d = sm._next_refresh_delay_v2(fri_after)
target = fri_after + timedelta(seconds=d)
assert target.weekday() == 0 and (target.hour, target.minute) == (8, 30)
def test_scheduler_refreshes_immediately_when_missing(monkeypatch):
calls = []
async def fake_refresh(**kw):
calls.append(1)
raise asyncio.CancelledError # 1회 호출 후 루프 종료
monkeypatch.setattr(sm, "refresh_sector_map", fake_refresh)
with pytest.raises(asyncio.CancelledError):
asyncio.run(sm.sector_map_refresh_scheduler())
assert calls == [1]