mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-10-31 12:24:39 +08:00
115 lines
3.7 KiB
Python
115 lines
3.7 KiB
Python
"""
|
|
验证系统多语言文本的选择和接口输出。
|
|
"""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from unittest.mock import patch
|
|
|
|
from app.api.endpoints import module as system_endpoint
|
|
from app.runtime.localization import LocaleHelper
|
|
|
|
|
|
class _FakeModuleManager:
|
|
"""
|
|
提供 system 模块接口测试所需的最小模块管理器。
|
|
"""
|
|
|
|
def list_enabled_specs(self) -> tuple:
|
|
"""
|
|
返回当前配置下的 manifest 元数据视图。
|
|
"""
|
|
return (
|
|
SimpleNamespace(id="DoubanModule", metadata={"name": "豆瓣"}),
|
|
)
|
|
|
|
def list_switchable_specs(self) -> tuple:
|
|
"""
|
|
返回后端声明的可手动开关模块。
|
|
"""
|
|
return (
|
|
SimpleNamespace(id="DoubanModule", metadata={"name": "豆瓣"}),
|
|
)
|
|
|
|
def test(self, moduleid: str) -> tuple[bool, str]:
|
|
"""
|
|
返回模块测试结果
|
|
"""
|
|
return False, "模块不支持测试"
|
|
|
|
|
|
def test_system_modulelist_keeps_chinese_name_and_adds_i18n_name():
|
|
"""
|
|
模块列表接口应保留旧中文字段,并提供前端可用的多语言字段。
|
|
"""
|
|
token = LocaleHelper.set_current_locale("en-US")
|
|
with patch.object(system_endpoint, "get_module_manager", return_value=_FakeModuleManager()):
|
|
try:
|
|
response = system_endpoint.modulelist(_="token")
|
|
finally:
|
|
LocaleHelper.reset_current_locale(token)
|
|
|
|
module = response.data["modules"][0]
|
|
assert module["id"] == "DoubanModule"
|
|
assert module["name"] == "豆瓣"
|
|
assert module["name_i18n"] == "Douban"
|
|
assert module["name_key"] == "system.modules.DoubanModule.name"
|
|
|
|
|
|
def test_system_moduletest_localizes_message():
|
|
"""
|
|
模块测试接口应按当前请求语言直接返回翻译后的 message。
|
|
"""
|
|
token = LocaleHelper.set_current_locale("en-US")
|
|
with patch.object(system_endpoint, "get_module_manager", return_value=_FakeModuleManager()):
|
|
try:
|
|
response = system_endpoint.moduletest("DoubanModule", _="token")
|
|
finally:
|
|
LocaleHelper.reset_current_locale(token)
|
|
|
|
assert response.success is False
|
|
assert response.message == "Module does not support testing"
|
|
assert not hasattr(response, "message_i18n")
|
|
|
|
|
|
def test_system_module_settings_uses_backend_catalog_and_persisted_switch():
|
|
"""
|
|
模块设置接口应返回后端目录,并按持久化开关计算状态。
|
|
"""
|
|
token = LocaleHelper.set_current_locale("en-US")
|
|
|
|
class _FakeRuntimeSettings:
|
|
"""
|
|
提供模块设置接口所需的最小运行配置读取器。
|
|
"""
|
|
|
|
@staticmethod
|
|
def get(key: str, default=None):
|
|
"""
|
|
返回测试用的模块开关映射。
|
|
"""
|
|
if key == "MODULE_ENABLE":
|
|
return {"DoubanModule": False}
|
|
return default
|
|
|
|
with (
|
|
patch.object(system_endpoint, "get_module_manager", return_value=_FakeModuleManager()),
|
|
patch.object(system_endpoint, "get_runtime_settings", return_value=_FakeRuntimeSettings()),
|
|
):
|
|
try:
|
|
response = asyncio.run(system_endpoint.module_settings(_="admin"))
|
|
finally:
|
|
LocaleHelper.reset_current_locale(token)
|
|
|
|
assert response.data["modules"] == [
|
|
{
|
|
"id": "DoubanModule",
|
|
"name": "豆瓣",
|
|
"name_i18n": "Douban",
|
|
"name_key": "system.modules.DoubanModule.name",
|
|
"description_i18n": "Douban data source for movie, TV, and music identification, search, and discovery lists.",
|
|
"description_key": "system.modules.DoubanModule.description",
|
|
"enabled": False,
|
|
}
|
|
]
|