import httpx import pytest from moviepilot_client import ( MoviePilotAPIError, MoviePilotAuthenticationError, MoviePilotClient, MoviePilotConfigError, MoviePilotConnectionError, MoviePilotResponseError, ) def make_transport(handler): return httpx.MockTransport(handler) @pytest.mark.asyncio async def test_request_adds_api_header_and_unwraps_data() -> None: async def handler(request: httpx.Request) -> httpx.Response: assert request.headers["X-API-KEY"] == "secret" assert request.url.path == "/api/v1/media/search" assert request.url.params["title"] == "沙丘" return httpx.Response(200, json={"success": True, "data": [{"id": 1}]}) async with MoviePilotClient( "http://moviepilot:3000/", "secret", transport=make_transport(handler) ) as client: response = await client.get( "/api/v1/media/search", params={"title": "沙丘"} ) assert response.data == [{"id": 1}] assert response.success is True @pytest.mark.asyncio async def test_list_response_is_preserved() -> None: async def handler(request: httpx.Request) -> httpx.Response: return httpx.Response(200, json=[{"id": 1}]) async with MoviePilotClient( "http://moviepilot:3000", "secret", transport=make_transport(handler) ) as client: response = await client.get("/api/v1/subscribe/list") assert response.data == [{"id": 1}] assert response.success is None @pytest.mark.asyncio async def test_api_failure_and_auth_failure_are_normalized() -> None: async def api_error(request: httpx.Request) -> httpx.Response: return httpx.Response(400, json={"success": False, "message": "参数错误"}) async with MoviePilotClient( "http://moviepilot:3000", "secret", transport=make_transport(api_error) ) as client: with pytest.raises(MoviePilotAPIError, match="参数错误"): await client.get("/api/v1/media/search") async def auth_error(request: httpx.Request) -> httpx.Response: return httpx.Response(401, json={"detail": "Unauthorized"}) async with MoviePilotClient( "http://moviepilot:3000", "secret", transport=make_transport(auth_error) ) as client: with pytest.raises(MoviePilotAuthenticationError): await client.get("/api/v1/media/search") @pytest.mark.asyncio async def test_connection_and_json_errors_are_normalized() -> None: async def invalid_json(request: httpx.Request) -> httpx.Response: return httpx.Response(200, content=b"not-json") async with MoviePilotClient( "http://moviepilot:3000", "secret", transport=make_transport(invalid_json) ) as client: with pytest.raises(MoviePilotResponseError): await client.get("/api/v1/media/search") async def network_error(request: httpx.Request) -> httpx.Response: raise httpx.ConnectError("offline", request=request) async with MoviePilotClient( "http://moviepilot:3000", "secret", transport=make_transport(network_error) ) as client: with pytest.raises(MoviePilotConnectionError): await client.get("/api/v1/media/search") def test_invalid_configuration_is_rejected() -> None: with pytest.raises(MoviePilotConfigError): MoviePilotClient("", "secret") with pytest.raises(MoviePilotConfigError): MoviePilotClient("http://moviepilot:3000", "") with pytest.raises(MoviePilotConfigError): MoviePilotClient("moviepilot:3000", "secret")