From 648e100e3c0f3a7c6295fdf663fd0c549d610b8b Mon Sep 17 00:00:00 2001 From: OG T Date: Thu, 26 Mar 2026 10:37:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(tests):=20=E4=BF=AE=E5=BE=A9=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6=20lint=20=E9=8C=AF=E8=AA=A4=20+=20TelegramGateway=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=91=BC=E5=8F=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修復項目: 1. 新增 conftest.py 確保環境變數在 settings 前載入 2. test_github_webhook.py 移除重複的 os.environ 設定 (E402) 3. test_smart_router.py 排序 import (I001) 4. github_webhook.py 修正 send_message → send_notification Phase 13.1 首席架構師審查修復 Co-Authored-By: Claude Opus 4.5 --- apps/api/src/api/v1/github_webhook.py | 4 ++-- apps/api/tests/conftest.py | 25 +++++++++++++++++++++++++ apps/api/tests/test_github_webhook.py | 9 ++------- apps/api/tests/test_smart_router.py | 14 +++++++------- 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 apps/api/tests/conftest.py diff --git a/apps/api/src/api/v1/github_webhook.py b/apps/api/src/api/v1/github_webhook.py index 92466df8..d552b977 100644 --- a/apps/api/src/api/v1/github_webhook.py +++ b/apps/api/src/api/v1/github_webhook.py @@ -936,8 +936,8 @@ async def send_github_telegram_alert( message = "\n".join(message_lines) - # 發送訊息 - await telegram.send_message(message) + # 發送訊息 (使用 send_notification 而非 send_message) + await telegram.send_notification(message) logger.info( "github_telegram_sent", diff --git a/apps/api/tests/conftest.py b/apps/api/tests/conftest.py new file mode 100644 index 00000000..27cb23c2 --- /dev/null +++ b/apps/api/tests/conftest.py @@ -0,0 +1,25 @@ +""" +Test Configuration +================== +pytest conftest.py - 確保測試環境變數在所有模組載入前生效 + +Phase 13.1: GitHub Webhook 測試需要這些環境變數 +""" + +import os + +# ============================================================================= +# 關鍵: 在任何 import 之前設定環境變數 +# ============================================================================= +# pytest 會在所有 test_ 模組之前載入 conftest.py +# 這確保 settings singleton 建立時能讀到正確的測試值 + +os.environ.setdefault("MOCK_MODE", "true") +os.environ.setdefault("ENVIRONMENT", "dev") +os.environ.setdefault("GITHUB_WEBHOOK_SECRET", "test-secret-key-12345") +os.environ.setdefault("GITHUB_ALLOWED_REPOS", "test-owner/test-repo,wooo-ai/awoooi") + +# ============================================================================= +# Pytest Plugins +# ============================================================================= +pytest_plugins = ["pytest_asyncio"] diff --git a/apps/api/tests/test_github_webhook.py b/apps/api/tests/test_github_webhook.py index cb8fbf58..0ade6935 100644 --- a/apps/api/tests/test_github_webhook.py +++ b/apps/api/tests/test_github_webhook.py @@ -14,13 +14,6 @@ Phase 13.1: GitHub Webhook 整合測試 import hashlib import hmac import json -import os - -# 設定測試環境變數 (在 import main 之前) -os.environ.setdefault("MOCK_MODE", "true") -os.environ.setdefault("ENVIRONMENT", "dev") -os.environ.setdefault("GITHUB_WEBHOOK_SECRET", "test-secret-key-12345") -os.environ.setdefault("GITHUB_ALLOWED_REPOS", "test-owner/test-repo,wooo-ai/awoooi") import httpx import pytest @@ -28,6 +21,8 @@ from httpx import ASGITransport from src.main import app +# 環境變數設定已移至 conftest.py (解決 E402) + # ============================================================================= # Test Fixtures diff --git a/apps/api/tests/test_smart_router.py b/apps/api/tests/test_smart_router.py index fcabf37a..908180f1 100644 --- a/apps/api/tests/test_smart_router.py +++ b/apps/api/tests/test_smart_router.py @@ -4,18 +4,18 @@ Smart Router Tests - Phase 13.3 測試意圖分類、複雜度評分、AI 路由 """ -from src.services.intent_classifier import ( - IntentClassifier, - IntentType, - get_intent_classifier, +from src.services.ai_router import ( + AIRouter, + get_ai_router, ) from src.services.complexity_scorer import ( ComplexityScorer, get_complexity_scorer, ) -from src.services.ai_router import ( - AIRouter, - get_ai_router, +from src.services.intent_classifier import ( + IntentClassifier, + IntentType, + get_intent_classifier, )