77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
# test_config.py - 配置模組測試
|
|
"""
|
|
Tests for Config class.
|
|
"""
|
|
|
|
import os
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
import sys
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.config import Config
|
|
|
|
|
|
class TestConfig:
|
|
"""Config 測試套件"""
|
|
|
|
def test_default_values(self):
|
|
"""測試默認值"""
|
|
assert Config.DEVICE_ID == os.getenv("MATRIX_DEVICE_ID", "PROJECT_M_BOT")
|
|
assert Config.STORE_PATH == os.getenv("STORE_PATH", "store")
|
|
|
|
def test_token_json_key(self):
|
|
"""測試 Token JSON Key 路徑"""
|
|
assert Config.TOKEN_JSON_KEY == "channels.matrix.accessToken"
|
|
|
|
def test_validate_missing_config(self):
|
|
"""測試配置驗證(缺少配置時)"""
|
|
# 臨時清空環境變量
|
|
original_server = os.environ.get("MATRIX_SERVER")
|
|
original_user = os.environ.get("MATRIX_USER_ID")
|
|
original_password = os.environ.get("MATRIX_PASSWORD")
|
|
|
|
try:
|
|
os.environ["MATRIX_SERVER"] = ""
|
|
os.environ["MATRIX_USER_ID"] = ""
|
|
os.environ["MATRIX_PASSWORD"] = ""
|
|
|
|
# 重新載入配置類的值
|
|
Config.MATRIX_SERVER = ""
|
|
Config.MATRIX_USER_ID = ""
|
|
Config.MATRIX_PASSWORD = ""
|
|
|
|
is_valid, missing = Config.validate()
|
|
|
|
assert is_valid is False
|
|
assert "MATRIX_SERVER" in missing
|
|
assert "MATRIX_USER_ID" in missing
|
|
assert "MATRIX_PASSWORD" in missing
|
|
finally:
|
|
# 恢復原值
|
|
if original_server:
|
|
os.environ["MATRIX_SERVER"] = original_server
|
|
if original_user:
|
|
os.environ["MATRIX_USER_ID"] = original_user
|
|
if original_password:
|
|
os.environ["MATRIX_PASSWORD"] = original_password
|
|
|
|
def test_get_openclaw_path_with_tilde(self):
|
|
"""測試 ~ 路徑展開"""
|
|
Config.OPENCLAW_JSON_PATH = "~/.openclaw/openclaw.json"
|
|
|
|
path = Config.get_openclaw_path()
|
|
|
|
assert isinstance(path, Path)
|
|
assert str(path).startswith(str(Path.home()))
|
|
assert "~" not in str(path)
|
|
|
|
def test_get_openclaw_path_absolute(self):
|
|
"""測試絕對路徑"""
|
|
Config.OPENCLAW_JSON_PATH = "/tmp/test/openclaw.json"
|
|
|
|
path = Config.get_openclaw_path()
|
|
|
|
assert path == Path("/tmp/test/openclaw.json")
|