2025-06-10 08:40:47 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
測試配置和共用 fixtures
|
|
|
|
"""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import os
|
2025-06-11 03:25:08 +08:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
from collections.abc import Generator
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2025-06-11 06:11:29 +08:00
|
|
|
# 使用正確的模組導入,不手動修改 sys.path
|
|
|
|
from mcp_feedback_enhanced.i18n import get_i18n_manager
|
|
|
|
from mcp_feedback_enhanced.web.main import WebUIManager
|
2025-06-10 08:40:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def event_loop():
|
|
|
|
"""創建事件循環 fixture"""
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
yield loop
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def temp_dir() -> Generator[Path, None, None]:
|
|
|
|
"""創建臨時目錄 fixture"""
|
|
|
|
temp_path = Path(tempfile.mkdtemp())
|
|
|
|
yield temp_path
|
|
|
|
shutil.rmtree(temp_path, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_project_dir(temp_dir: Path) -> Path:
|
|
|
|
"""創建測試專案目錄"""
|
|
|
|
project_dir = temp_dir / "test_project"
|
|
|
|
project_dir.mkdir()
|
2025-06-11 03:25:08 +08:00
|
|
|
|
2025-06-10 08:40:47 +08:00
|
|
|
# 創建一些測試文件
|
|
|
|
(project_dir / "README.md").write_text("# Test Project")
|
|
|
|
(project_dir / "main.py").write_text("print('Hello World')")
|
2025-06-11 03:25:08 +08:00
|
|
|
|
2025-06-10 08:40:47 +08:00
|
|
|
return project_dir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def web_ui_manager() -> Generator[WebUIManager, None, None]:
|
|
|
|
"""創建 WebUIManager fixture"""
|
2025-06-11 14:59:27 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
# 設置測試模式環境變數
|
|
|
|
original_test_mode = os.environ.get("MCP_TEST_MODE")
|
2025-06-18 13:54:25 +00:00
|
|
|
original_web_host = os.environ.get("MCP_WEB_HOST")
|
2025-06-11 14:59:27 +08:00
|
|
|
original_web_port = os.environ.get("MCP_WEB_PORT")
|
|
|
|
|
|
|
|
os.environ["MCP_TEST_MODE"] = "true"
|
2025-06-18 13:54:25 +00:00
|
|
|
os.environ["MCP_WEB_HOST"] = "127.0.0.1" # 確保測試使用本地主機
|
2025-06-11 14:59:27 +08:00
|
|
|
# 使用動態端口範圍避免衝突
|
|
|
|
os.environ["MCP_WEB_PORT"] = "0" # 讓系統自動分配端口
|
|
|
|
|
|
|
|
try:
|
2025-06-18 13:54:25 +00:00
|
|
|
manager = WebUIManager() # 使用環境變數控制主機和端口
|
2025-06-11 14:59:27 +08:00
|
|
|
yield manager
|
|
|
|
finally:
|
|
|
|
# 恢復原始環境變數
|
|
|
|
if original_test_mode is not None:
|
|
|
|
os.environ["MCP_TEST_MODE"] = original_test_mode
|
|
|
|
else:
|
|
|
|
os.environ.pop("MCP_TEST_MODE", None)
|
|
|
|
|
2025-06-18 13:54:25 +00:00
|
|
|
if original_web_host is not None:
|
|
|
|
os.environ["MCP_WEB_HOST"] = original_web_host
|
|
|
|
else:
|
|
|
|
os.environ.pop("MCP_WEB_HOST", None)
|
|
|
|
|
2025-06-11 14:59:27 +08:00
|
|
|
if original_web_port is not None:
|
|
|
|
os.environ["MCP_WEB_PORT"] = original_web_port
|
|
|
|
else:
|
|
|
|
os.environ.pop("MCP_WEB_PORT", None)
|
|
|
|
|
|
|
|
# 清理
|
|
|
|
if manager.server_thread and manager.server_thread.is_alive():
|
|
|
|
# 這裡可以添加服務器停止邏輯
|
|
|
|
pass
|
2025-06-10 08:40:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def i18n_manager():
|
|
|
|
"""創建 I18N 管理器 fixture"""
|
|
|
|
return get_i18n_manager()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2025-06-11 03:25:08 +08:00
|
|
|
def test_config() -> dict[str, Any]:
|
2025-06-10 08:40:47 +08:00
|
|
|
"""測試配置 fixture"""
|
|
|
|
return {
|
|
|
|
"timeout": 30,
|
|
|
|
"debug": True,
|
|
|
|
"web_port": 8765,
|
|
|
|
"test_summary": "測試摘要 - 這是一個自動化測試",
|
2025-06-11 03:25:08 +08:00
|
|
|
"test_feedback": "這是測試回饋內容",
|
2025-06-10 08:40:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_test_env():
|
|
|
|
"""自動設置測試環境"""
|
|
|
|
# 設置測試環境變數
|
|
|
|
original_debug = os.environ.get("MCP_DEBUG")
|
|
|
|
os.environ["MCP_DEBUG"] = "true"
|
2025-06-11 03:25:08 +08:00
|
|
|
|
2025-06-10 08:40:47 +08:00
|
|
|
yield
|
2025-06-11 03:25:08 +08:00
|
|
|
|
2025-06-10 08:40:47 +08:00
|
|
|
# 恢復原始環境
|
|
|
|
if original_debug is not None:
|
|
|
|
os.environ["MCP_DEBUG"] = original_debug
|
|
|
|
else:
|
|
|
|
os.environ.pop("MCP_DEBUG", None)
|