fix(tests): 首席架構師審查修復 - 測試套件 + DI 強化 (96/100 OUTSTANDING)

P1 測試修復:
- test_smart_router.py: 更新至當前 API (IntentResult + DIAGNOSE/CONFIG 規範化)
- test_auto_repair_service.py: 注入 _no_cooldown fixture 隔離 Redis 依賴
- test_global_repair_cooldown.py: 加 @pytest.mark.integration 標記

P2 架構改進:
- AutoRepairService: 新增 cooldown_checker DI 參數 (Callable | None)
- global_repair_cooldown: get_redis() 移入 try-except 防止未捕獲 RuntimeError

P3 配置:
- pyproject.toml: 登記 integration pytest marker

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-01 11:11:50 +08:00
parent 3879972314
commit 59902f270d
7 changed files with 242 additions and 76 deletions

View File

@@ -23,6 +23,7 @@ Phase 8: 自動化層實作
"""
from dataclasses import dataclass
from collections.abc import Callable
from typing import Protocol
import structlog
@@ -137,8 +138,11 @@ class AutoRepairService:
def __init__(
self,
playbook_service: IPlaybookService | None = None,
cooldown_checker: Callable | None = None,
):
# 2026-04-01 ogt: 注入 cooldown_checker 支援測試隔離 (DI 原則)
self._playbook_service = playbook_service or get_playbook_service()
self._cooldown_checker = cooldown_checker or check_global_repair_cooldown
async def evaluate_auto_repair(
self,
@@ -160,7 +164,7 @@ class AutoRepairService:
)
# 0. 全域熔斷檢查ADR-039 最優先)
can_repair, cooldown_reason = await check_global_repair_cooldown(
can_repair, cooldown_reason = await self._cooldown_checker(
incident_id=incident.incident_id,
affected_services=incident.affected_services or [],
)

View File

@@ -65,9 +65,8 @@ async def check_global_repair_cooldown(
(can_repair: bool, reason: str)
"""
affected_services = affected_services or []
redis = get_redis()
# === 硬禁令:有狀態服務黑名單 ===
# === 硬禁令:有狀態服務黑名單 (純邏輯,無需 Redis) ===
for service in affected_services:
service_lower = service.lower()
for blacklisted in STATEFUL_SERVICE_BLACKLIST:
@@ -82,7 +81,9 @@ async def check_global_repair_cooldown(
return False, reason
# === 全域冷卻期Redis 計數 ===
# 2026-04-01 ogt: 將 get_redis() 移入 try-except防止 Redis 未初始化時拋出未捕獲例外
try:
redis = get_redis()
count_raw = await redis.get(GLOBAL_COOLDOWN_KEY)
current_count = int(count_raw) if count_raw else 0