mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
192 lines
6.1 KiB
Python
192 lines
6.1 KiB
Python
"""插件市场索引同步与异步策略一致性测试。"""
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from app.adapters.external.market import PluginHelper
|
|
from app.adapters.external.plugin.client import PluginMarketClient
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sync_and_async_plugin_indexes_share_request_and_result(
|
|
monkeypatch,
|
|
) -> None:
|
|
"""同步与异步索引读取必须使用同一请求计划并返回相同三态结果。"""
|
|
helper = PluginHelper()
|
|
repo_url = "https://github.com/policy-owner/policy-repository"
|
|
response = SimpleNamespace(
|
|
status_code=200,
|
|
text='{"DemoPlugin": {"version": "1.2.3"}}',
|
|
)
|
|
sync_requests: list[tuple[str, dict]] = []
|
|
async_requests: list[tuple[str, dict]] = []
|
|
|
|
def sync_request(url: str, *, headers: dict):
|
|
"""记录同步入口收到的 URL 与请求头。"""
|
|
sync_requests.append((url, headers))
|
|
return response
|
|
|
|
async def async_request(url: str, *, headers: dict):
|
|
"""记录异步入口收到的 URL 与请求头。"""
|
|
async_requests.append((url, headers))
|
|
return response
|
|
|
|
monkeypatch.setattr(
|
|
helper,
|
|
"_PluginHelper__request_with_fallback",
|
|
sync_request,
|
|
)
|
|
monkeypatch.setattr(
|
|
helper,
|
|
"_PluginHelper__async_request_with_fallback",
|
|
async_request,
|
|
)
|
|
|
|
helper.get_plugins.cache_clear()
|
|
await helper.async_get_plugins.cache_clear()
|
|
sync_result = helper.get_plugins(repo_url, "v3")
|
|
# 同步与异步装饰器按设计共享缓存区;清除后再验证异步 I/O 入口本身。
|
|
await helper.async_get_plugins.cache_clear()
|
|
async_result = await helper.async_get_plugins(repo_url, "v3")
|
|
|
|
assert sync_result == async_result == {
|
|
"DemoPlugin": {"version": "1.2.3"},
|
|
}
|
|
assert sync_requests == async_requests
|
|
assert sync_requests[0][0] == (
|
|
"https://raw.githubusercontent.com/policy-owner/"
|
|
"policy-repository/main/package.v3.json"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("status_code", "content", "expected"),
|
|
[
|
|
(404, "404: Not Found", {}),
|
|
(500, "upstream failed", None),
|
|
(200, '{"DemoPlugin": {}}', {"DemoPlugin": {}}),
|
|
(200, "not-json", None),
|
|
(200, "[]", None),
|
|
],
|
|
)
|
|
def test_plugin_index_response_preserves_status_contract(
|
|
status_code: int,
|
|
content: str,
|
|
expected: dict | None,
|
|
) -> None:
|
|
"""统一响应解析必须保留 404 空索引、失败和有效字典的既有区别。"""
|
|
result = PluginHelper._resolve_plugin_index_response(status_code, content)
|
|
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("status_code", "content", "expected"),
|
|
[
|
|
(200, '{"DemoPlugin": {"version": "1.2.3"}}', {"DemoPlugin": {"version": "1.2.3"}}),
|
|
(404, "404: Not Found", None),
|
|
],
|
|
)
|
|
def test_plugin_index_result_preserves_read_state(
|
|
monkeypatch,
|
|
status_code: int,
|
|
content: str,
|
|
expected: dict | None,
|
|
) -> None:
|
|
"""只读入口以值和 None 区分真实索引与确定不存在。"""
|
|
helper = PluginHelper()
|
|
repo_url = f"https://github.com/policy-owner/policy-repository-{status_code}"
|
|
|
|
def request(_url: str, *, headers: dict):
|
|
return SimpleNamespace(status_code=status_code, text=content)
|
|
|
|
monkeypatch.setattr(helper, "_PluginHelper__request_with_fallback", request)
|
|
helper.get_plugin_index_result.cache_clear()
|
|
|
|
result = helper.get_plugin_index_result(repo_url, "v3")
|
|
|
|
assert result == expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("status_code", "content", "message"),
|
|
[
|
|
(500, "upstream failed", "插件索引请求失败:HTTP 500"),
|
|
(200, "not-json", "插件索引响应格式无效"),
|
|
],
|
|
)
|
|
def test_plugin_index_result_raises_for_unusable_reads(
|
|
monkeypatch,
|
|
status_code: int,
|
|
content: str,
|
|
message: str,
|
|
) -> None:
|
|
"""不可判定读取必须抛错,由应用库存统一记录失败事实。"""
|
|
helper = PluginHelper()
|
|
|
|
def request(_url: str, *, headers: dict):
|
|
return SimpleNamespace(status_code=status_code, text=content)
|
|
|
|
monkeypatch.setattr(helper, "_PluginHelper__request_with_fallback", request)
|
|
helper.get_plugin_index_result.cache_clear()
|
|
|
|
with pytest.raises(RuntimeError, match=message):
|
|
helper.get_plugin_index_result(
|
|
f"https://github.com/policy-owner/policy-failed-{status_code}",
|
|
"v3",
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_plugin_index_result_preserves_absent_state(monkeypatch) -> None:
|
|
"""异步只读入口也必须保留 404 不存在事实。"""
|
|
helper = PluginHelper()
|
|
|
|
async def request(_url: str, *, headers: dict):
|
|
return SimpleNamespace(status_code=404, text="404: Not Found")
|
|
|
|
monkeypatch.setattr(
|
|
helper,
|
|
"_PluginHelper__async_request_with_fallback",
|
|
request,
|
|
)
|
|
await helper.async_get_plugin_index_result.cache_clear()
|
|
|
|
result = await helper.async_get_plugin_index_result(
|
|
"https://github.com/policy-owner/policy-repository-async",
|
|
"v3",
|
|
)
|
|
|
|
assert result is None
|
|
|
|
|
|
def test_plugin_index_result_propagates_adapter_exception(monkeypatch) -> None:
|
|
"""请求异常必须传播给应用库存统一转换为失败事实。"""
|
|
helper = PluginHelper()
|
|
|
|
def request(_url: str, *, headers: dict):
|
|
raise OSError("socket closed")
|
|
|
|
monkeypatch.setattr(helper, "_PluginHelper__request_with_fallback", request)
|
|
helper.get_plugin_index_result.cache_clear()
|
|
|
|
with pytest.raises(OSError, match="socket closed"):
|
|
helper.get_plugin_index_result(
|
|
"https://github.com/policy-owner/policy-repository-exception",
|
|
"v3",
|
|
)
|
|
|
|
|
|
def test_plugin_market_client_exposes_index_result_port() -> None:
|
|
"""市场客户端应原样转发索引读取结果并保留只读边界。"""
|
|
expected = {"DemoPlugin": {"version": "1.2.3"}}
|
|
|
|
class FakeHelper:
|
|
def get_plugin_index_result(self, repo_url: str, package_version: str | None):
|
|
return expected
|
|
|
|
client = PluginMarketClient(FakeHelper())
|
|
|
|
assert client.get_plugin_index_result("https://github.com/example/repo", "v3") is expected
|