mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-06-28 11:12:00 +08:00
feat: sync plugin markets from wiki
This commit is contained in:
@@ -1,71 +1,103 @@
|
||||
import asyncio
|
||||
from unittest import TestCase
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from unittest.mock import ANY, AsyncMock, MagicMock, patch
|
||||
|
||||
from app import schemas
|
||||
from app.api.endpoints.plugin import plugin_history
|
||||
from app.api.endpoints.system import sync_plugin_market_from_wiki
|
||||
|
||||
|
||||
class PluginEndpointTest(TestCase):
|
||||
def test_plugin_history_merges_remote_metadata():
|
||||
"""
|
||||
已安装插件点击更新说明时,接口会按需合并远端仓库中的更新记录。
|
||||
"""
|
||||
installed_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
plugin_name="Demo Plugin",
|
||||
plugin_version="1.0.0",
|
||||
installed=True,
|
||||
history={},
|
||||
)
|
||||
market_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
repo_url="https://github.com/demo/plugins",
|
||||
history={"v1.1.0": "- 新增更新说明"},
|
||||
system_version=">=2.0.0",
|
||||
system_version_compatible=True,
|
||||
has_update=True,
|
||||
)
|
||||
plugin_manager = MagicMock()
|
||||
plugin_manager.get_local_plugins.return_value = [installed_plugin]
|
||||
plugin_manager.get_local_repo_plugins.return_value = []
|
||||
plugin_manager.async_get_online_plugins = AsyncMock(return_value=[market_plugin])
|
||||
|
||||
def test_plugin_history_merges_remote_metadata(self):
|
||||
"""
|
||||
已安装插件点击更新说明时,接口会按需合并远端仓库中的更新记录。
|
||||
"""
|
||||
try:
|
||||
from app import schemas
|
||||
from app.api.endpoints.plugin import plugin_history
|
||||
except ModuleNotFoundError as exc:
|
||||
self.skipTest(f"missing dependency: {exc}")
|
||||
with patch("app.api.endpoints.plugin.PluginManager", return_value=plugin_manager):
|
||||
result = asyncio.run(plugin_history("DemoPlugin", None, True))
|
||||
|
||||
installed_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
plugin_name="Demo Plugin",
|
||||
plugin_version="1.0.0",
|
||||
installed=True,
|
||||
history={},
|
||||
)
|
||||
market_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
repo_url="https://github.com/demo/plugins",
|
||||
history={"v1.1.0": "- 新增更新说明"},
|
||||
system_version=">=2.0.0",
|
||||
system_version_compatible=True,
|
||||
has_update=True,
|
||||
)
|
||||
plugin_manager = MagicMock()
|
||||
plugin_manager.get_local_plugins.return_value = [installed_plugin]
|
||||
plugin_manager.get_local_repo_plugins.return_value = []
|
||||
plugin_manager.async_get_online_plugins = AsyncMock(return_value=[market_plugin])
|
||||
assert result.repo_url == "https://github.com/demo/plugins"
|
||||
assert result.history == {"v1.1.0": "- 新增更新说明"}
|
||||
assert result.system_version == ">=2.0.0"
|
||||
assert result.has_update
|
||||
|
||||
with patch("app.api.endpoints.plugin.PluginManager", return_value=plugin_manager):
|
||||
result = asyncio.run(plugin_history("DemoPlugin", None, True))
|
||||
|
||||
self.assertEqual("https://github.com/demo/plugins", result.repo_url)
|
||||
self.assertEqual({"v1.1.0": "- 新增更新说明"}, result.history)
|
||||
self.assertEqual(">=2.0.0", result.system_version)
|
||||
self.assertTrue(result.has_update)
|
||||
def test_plugin_history_returns_installed_plugin_when_remote_missing():
|
||||
"""
|
||||
远端仓库不可用时,接口仍返回本地已安装插件信息,前端可继续展示兜底状态。
|
||||
"""
|
||||
installed_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
plugin_name="Demo Plugin",
|
||||
plugin_version="1.0.0",
|
||||
installed=True,
|
||||
)
|
||||
plugin_manager = MagicMock()
|
||||
plugin_manager.get_local_plugins.return_value = [installed_plugin]
|
||||
plugin_manager.get_local_repo_plugins.return_value = []
|
||||
plugin_manager.async_get_online_plugins = AsyncMock(return_value=[])
|
||||
|
||||
def test_plugin_history_returns_installed_plugin_when_remote_missing(self):
|
||||
"""
|
||||
远端仓库不可用时,接口仍返回本地已安装插件信息,前端可继续展示兜底状态。
|
||||
"""
|
||||
try:
|
||||
from app import schemas
|
||||
from app.api.endpoints.plugin import plugin_history
|
||||
except ModuleNotFoundError as exc:
|
||||
self.skipTest(f"missing dependency: {exc}")
|
||||
with patch("app.api.endpoints.plugin.PluginManager", return_value=plugin_manager):
|
||||
result = asyncio.run(plugin_history("DemoPlugin", None, True))
|
||||
|
||||
installed_plugin = schemas.Plugin(
|
||||
id="DemoPlugin",
|
||||
plugin_name="Demo Plugin",
|
||||
plugin_version="1.0.0",
|
||||
installed=True,
|
||||
)
|
||||
plugin_manager = MagicMock()
|
||||
plugin_manager.get_local_plugins.return_value = [installed_plugin]
|
||||
plugin_manager.get_local_repo_plugins.return_value = []
|
||||
plugin_manager.async_get_online_plugins = AsyncMock(return_value=[])
|
||||
assert result.id == "DemoPlugin"
|
||||
assert result.history == {}
|
||||
|
||||
with patch("app.api.endpoints.plugin.PluginManager", return_value=plugin_manager):
|
||||
result = asyncio.run(plugin_history("DemoPlugin", None, True))
|
||||
|
||||
self.assertEqual("DemoPlugin", result.id)
|
||||
self.assertEqual({}, result.history)
|
||||
def test_sync_plugin_market_from_wiki_merges_and_deduplicates_repos():
|
||||
"""
|
||||
Wiki 同步会提取标记区域内的 GitHub 仓库地址,并与本地配置合并去重后写入。
|
||||
"""
|
||||
markdown = """
|
||||
<!-- plugin-market-repos:start -->
|
||||
- https://github.com/local/existing/
|
||||
- https://github.com/wiki/new-repo/
|
||||
- https://github.com/wiki/new-repo
|
||||
<!-- plugin-market-repos:end -->
|
||||
- https://github.com/wiki/ignored-outside-marker
|
||||
"""
|
||||
response = MagicMock(status_code=200, text=markdown)
|
||||
request_utils = MagicMock()
|
||||
request_utils.get_res = AsyncMock(return_value=response)
|
||||
with (
|
||||
patch("app.api.endpoints.system.AsyncRequestUtils", return_value=request_utils),
|
||||
patch("app.api.endpoints.system.settings.PLUGIN_MARKET", "https://github.com/local/existing"),
|
||||
patch(
|
||||
"app.core.config.Settings.update_setting",
|
||||
autospec=True,
|
||||
return_value=(True, ""),
|
||||
) as update_setting,
|
||||
patch("app.api.endpoints.system.eventmanager.async_send_event", new=AsyncMock()) as send_event,
|
||||
):
|
||||
result = asyncio.run(sync_plugin_market_from_wiki(None, None))
|
||||
|
||||
assert result.success
|
||||
assert result.data["repos"] == [
|
||||
"https://github.com/local/existing",
|
||||
"https://github.com/wiki/new-repo",
|
||||
]
|
||||
assert result.data["added_count"] == 1
|
||||
assert result.data["total_count"] == 2
|
||||
update_setting.assert_called_once_with(
|
||||
ANY,
|
||||
"PLUGIN_MARKET",
|
||||
"https://github.com/local/existing,https://github.com/wiki/new-repo",
|
||||
)
|
||||
send_event.assert_awaited_once()
|
||||
|
||||
Reference in New Issue
Block a user