diff --git a/src/lg_smtv_mcp/tv_core/capture.py b/src/lg_smtv_mcp/tv_core/capture.py index 2b92d01..25fb6bf 100644 --- a/src/lg_smtv_mcp/tv_core/capture.py +++ b/src/lg_smtv_mcp/tv_core/capture.py @@ -160,10 +160,10 @@ class CaptureClient: f"executeOneShot failed: {exc.message}", ) from exc - if response.get("returnValue") is False: + if response.get("returnValue") is not True: raise TvError( ErrorCode.SCREENSHOT_UNAVAILABLE, - "executeOneShot returned returnValue false", + "executeOneShot did not return returnValue true", ) try: diff --git a/tests/test_capture.py b/tests/test_capture.py index da69b41..41f8102 100644 --- a/tests/test_capture.py +++ b/tests/test_capture.py @@ -149,12 +149,25 @@ async def test_async_ssh_capture_transport_sweeps_on_initial_connect_and_reconne assert "/tmp/keep.txt" not in sftp2.remove_calls -async def test_async_ssh_capture_transport_shields_remove_on_read_error(tmp_path: Path) -> None: - sftp = FakeSftpClient( - files={"/tmp/lg_smtv_cap_1.jpg": b"jpeg"}, - read_error=OSError("read failed"), - remove_delay=0.05, - ) +async def test_async_ssh_capture_transport_finishes_remove_after_cancellation( + tmp_path: Path, +) -> None: + remote_path = "/tmp/lg_smtv_cap_1.jpg" + + class BlockingRemoveSftp(FakeSftpClient): + def __init__(self) -> None: + super().__init__(files={remote_path: b"jpeg"}) + self.remove_started = asyncio.Event() + self.allow_remove = asyncio.Event() + self.remove_finished = asyncio.Event() + + async def remove(self, path: str) -> None: + self.remove_started.set() + await self.allow_remove.wait() + await super().remove(path) + self.remove_finished.set() + + sftp = BlockingRemoveSftp() conn = FakeSshConnection(sftp) async def fake_connect(**kwargs: Any) -> FakeSshConnection: @@ -162,10 +175,20 @@ async def test_async_ssh_capture_transport_shields_remove_on_read_error(tmp_path return conn transport = AsyncSshCaptureTransport(_make_config(tmp_path), connect_fn=fake_connect) - with pytest.raises(OSError, match="read failed"): - await transport.read_and_remove("/tmp/lg_smtv_cap_1.jpg", timeout=6.0, cleanup_grace=1.5) + read_task = asyncio.create_task(transport.read_and_remove(remote_path)) + try: + await asyncio.wait_for(sftp.remove_started.wait(), timeout=1.0) + read_task.cancel() + with pytest.raises(asyncio.CancelledError): + await read_task + finally: + sftp.allow_remove.set() + if not read_task.done(): + read_task.cancel() + await asyncio.gather(read_task, return_exceptions=True) - assert "/tmp/lg_smtv_cap_1.jpg" in sftp.remove_calls + await asyncio.wait_for(sftp.remove_finished.wait(), timeout=1.0) + assert remote_path in sftp.remove_calls async def test_capture_client_sends_execute_one_shot_and_returns_metadata(tmp_path: Path) -> None: @@ -246,8 +269,13 @@ async def test_capture_client_changed_since_last_and_close_resets(tmp_path: Path assert third.changed_since_last is True -async def test_capture_client_maps_execute_one_shot_failure_without_sftp_read( - tmp_path: Path, +@pytest.mark.parametrize( + "response", + [{"returnValue": False}, {}, {"returnValue": None}], + ids=["false", "missing", "null"], +) +async def test_capture_client_rejects_non_true_return_value_without_sftp_read( + tmp_path: Path, response: dict[str, Any] ) -> None: _ = tmp_path @@ -273,7 +301,7 @@ async def test_capture_client_maps_execute_one_shot_failure_without_sftp_read( async def ssap_request(uri: str, payload: dict[str, Any]) -> dict[str, Any]: _ = (uri, payload) - return {"returnValue": False} + return response with pytest.raises(TvError) as exc_info: await client.capture_frame(ssap_request) @@ -402,3 +430,40 @@ async def test_capture_client_maps_timeout_to_screenshot_unavailable(tmp_path: P with pytest.raises(TvError) as exc_info: await client.capture_frame(ssap_request) assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE + + +async def test_capture_client_maps_sftp_timeout_and_cancels_read(tmp_path: Path) -> None: + _ = tmp_path + + class BlockingTransport: + def __init__(self) -> None: + self.read_cancelled = asyncio.Event() + + async def read_and_remove( + self, remote_path: str, timeout: float = 6.0, cleanup_grace: float = 1.5 + ) -> bytes: + _ = (remote_path, timeout, cleanup_grace) + try: + await asyncio.Event().wait() + finally: + self.read_cancelled.set() + raise AssertionError("capture read should be cancelled by the timeout") + + async def sweep_temp_files(self, prefix: str = "/tmp/lg_smtv_cap_") -> None: + _ = prefix + + async def close(self) -> None: + return None + + transport = BlockingTransport() + client = CaptureClient(transport) + + async def ssap_request(uri: str, payload: dict[str, Any]) -> dict[str, Any]: + _ = (uri, payload) + return {"returnValue": True} + + with pytest.raises(TvError) as exc_info: + await asyncio.wait_for(client.capture_frame(ssap_request), timeout=7.0) + + assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE + assert transport.read_cancelled.is_set() diff --git a/tests/test_server.py b/tests/test_server.py index 805f054..c758518 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -10,6 +10,7 @@ from contextlib import asynccontextmanager from pathlib import Path from typing import Any +import asyncssh import httpx import pytest from mcp.server.mcpserver.exceptions import ToolError @@ -459,13 +460,17 @@ async def test_server_http_app_allows_authenticated_mcp_initialize_request( await asyncio.wait_for(lifespan_task, timeout=2.0) -async def test_server_create_mcp_server_wires_default_capture_client(tmp_path: Path) -> None: +async def test_server_create_mcp_server_wires_default_capture_client( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: import lg_smtv_mcp.server as server_mod - from lg_smtv_mcp.tv_core.capture import AsyncSshCaptureTransport, CaptureClient + from lg_smtv_mcp.tv_core.capture import CaptureClient config = _make_config(tmp_path) captured: dict[str, Any] = {} - original_tv_manager = server_mod.TvManager + remote_files: dict[str, bytes] = {} + removed_paths: list[str] = [] + connect_calls: list[dict[str, Any]] = [] class DummyManager: async def start(self) -> None: @@ -474,20 +479,70 @@ async def test_server_create_mcp_server_wires_default_capture_client(tmp_path: P async def close(self) -> None: return None + class FakeSftp: + def __init__(self) -> None: + self.closed = False + + async def listdir(self, path: str) -> list[str]: + _ = path + return [] + + async def read(self, path: str) -> bytes: + return remote_files[path] + + async def remove(self, path: str) -> None: + removed_paths.append(path) + remote_files.pop(path, None) + + async def exit(self) -> None: + self.closed = True + + sftp_sessions: list[FakeSftp] = [] + + class FakeSshConnection: + async def start_sftp_client(self) -> FakeSftp: + sftp = FakeSftp() + sftp_sessions.append(sftp) + return sftp + + def close(self) -> None: + return None + + async def wait_closed(self) -> None: + return None + def fake_tv_manager(cfg: Any, **kwargs: Any) -> DummyManager: captured["config"] = cfg captured["kwargs"] = kwargs return DummyManager() - server_mod.TvManager = fake_tv_manager # type: ignore[assignment] - try: - server_mod.create_mcp_server(config) - finally: - server_mod.TvManager = original_tv_manager # type: ignore[assignment] + async def fake_connect(**kwargs: Any) -> FakeSshConnection: + connect_calls.append(kwargs) + return FakeSshConnection() + + monkeypatch.setattr(server_mod, "TvManager", fake_tv_manager) + monkeypatch.setattr(asyncssh, "connect", fake_connect) + server_mod.create_mcp_server(config) assert captured["config"] is config assert "capture_client" in captured["kwargs"] capture_client = captured["kwargs"]["capture_client"] assert isinstance(capture_client, CaptureClient) - transport = capture_client._transport # pyright: ignore[reportPrivateUsage] - assert isinstance(transport, AsyncSshCaptureTransport) + + async def ssap_request(uri: str, payload: dict[str, Any]) -> dict[str, Any]: + assert uri == "ssap://com.webos.service.capture/executeOneShot" + remote_files[payload["path"]] = _TEST_JPEG + return {"returnValue": True} + + try: + result = await capture_client.capture_frame(ssap_request) + finally: + await capture_client.close() + + assert result.image_bytes == _TEST_JPEG + assert len(connect_calls) == 1 + assert connect_calls[0]["host"] == config.tv_ip + assert connect_calls[0]["port"] == 9922 + assert removed_paths == ["/tmp/lg_smtv_cap_1.jpg"] + assert len(sftp_sessions) == 1 + assert sftp_sessions[0].closed is True