fix(capture): handle DecompressionBombError and deduplicate server test SSH scaffolding
- src/lg_smtv_mcp/tv_core/capture.py: catch Image.DecompressionBombError alongside UnidentifiedImageError/OSError/ValueError/EOFError so oversized/malformed image headers map to TvError(ErrorCode.SCREENSHOT_UNAVAILABLE) (CR-01) - tests/test_capture.py: add Red-Green regression test verifying DecompressionBombError maps to SCREENSHOT_UNAVAILABLE - tests/test_server.py: extract _wire_capture_client_with_fake_ssh helper to eliminate duplicated fake SSH/SFTP scaffolding across server capture wiring tests (CR-02)
This commit was merged in pull request #24.
This commit is contained in:
1 parent
09618bef3d
commit
93a572cbb5
3 files changed
+46
-67
No files matched your search
@@ -44,7 +44,13 @@ def _recompress_jpeg(
|
|||||||
out = BytesIO()
|
out = BytesIO()
|
||||||
img.save(out, format="JPEG", quality=quality)
|
img.save(out, format="JPEG", quality=quality)
|
||||||
return out.getvalue()
|
return out.getvalue()
|
||||||
except (UnidentifiedImageError, OSError, ValueError, EOFError) as exc:
|
except (
|
||||||
|
UnidentifiedImageError,
|
||||||
|
Image.DecompressionBombError,
|
||||||
|
OSError,
|
||||||
|
ValueError,
|
||||||
|
EOFError,
|
||||||
|
) as exc:
|
||||||
raise TvError(
|
raise TvError(
|
||||||
ErrorCode.SCREENSHOT_UNAVAILABLE,
|
ErrorCode.SCREENSHOT_UNAVAILABLE,
|
||||||
f"Failed to process screenshot image: {exc}",
|
f"Failed to process screenshot image: {exc}",
|
||||||
|
|||||||
@@ -653,3 +653,20 @@ async def test_capture_client_raises_screenshot_unavailable_on_corrupt_image_byt
|
|||||||
await client.capture_frame(ssap_request)
|
await client.capture_frame(ssap_request)
|
||||||
|
|
||||||
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
|
async def test_capture_client_maps_decompression_bomb_error_to_screenshot_unavailable(
|
||||||
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
|
) -> None:
|
||||||
|
raw = _make_jpeg(32, 32, quality=95)
|
||||||
|
monkeypatch.setattr(Image, "MAX_IMAGE_PIXELS", 100)
|
||||||
|
client = CaptureClient(FakeCaptureTransport(image_bytes=raw), max_width=16)
|
||||||
|
|
||||||
|
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 client.capture_frame(ssap_request)
|
||||||
|
|
||||||
|
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
||||||
+22
-66
@@ -460,17 +460,17 @@ async def test_server_http_app_allows_authenticated_mcp_initialize_request(
|
|||||||
await asyncio.wait_for(lifespan_task, timeout=2.0)
|
await asyncio.wait_for(lifespan_task, timeout=2.0)
|
||||||
|
|
||||||
|
|
||||||
async def test_server_create_mcp_server_wires_default_capture_client(
|
def _wire_capture_client_with_fake_ssh(
|
||||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
config: TvConfig,
|
||||||
|
) -> tuple[dict[str, Any], dict[str, bytes], list[str], list[dict[str, Any]], list[Any]]:
|
||||||
import lg_smtv_mcp.server as server_mod
|
import lg_smtv_mcp.server as server_mod
|
||||||
from lg_smtv_mcp.tv_core.capture import CaptureClient
|
|
||||||
|
|
||||||
config = _make_config(tmp_path)
|
|
||||||
captured: dict[str, Any] = {}
|
captured: dict[str, Any] = {}
|
||||||
remote_files: dict[str, bytes] = {}
|
remote_files: dict[str, bytes] = {}
|
||||||
removed_paths: list[str] = []
|
removed_paths: list[str] = []
|
||||||
connect_calls: list[dict[str, Any]] = []
|
connect_calls: list[dict[str, Any]] = []
|
||||||
|
sftp_sessions: list[Any] = []
|
||||||
|
|
||||||
class DummyManager:
|
class DummyManager:
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
@@ -488,6 +488,8 @@ async def test_server_create_mcp_server_wires_default_capture_client(
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
def open(self, path: str, mode: str = "r") -> Any:
|
def open(self, path: str, mode: str = "r") -> Any:
|
||||||
|
_ = mode
|
||||||
|
|
||||||
class FakeFile:
|
class FakeFile:
|
||||||
async def __aenter__(self) -> FakeFile:
|
async def __aenter__(self) -> FakeFile:
|
||||||
return self
|
return self
|
||||||
@@ -507,8 +509,6 @@ async def test_server_create_mcp_server_wires_default_capture_client(
|
|||||||
async def exit(self) -> None:
|
async def exit(self) -> None:
|
||||||
self.closed = True
|
self.closed = True
|
||||||
|
|
||||||
sftp_sessions: list[FakeSftp] = []
|
|
||||||
|
|
||||||
class FakeSshConnection:
|
class FakeSshConnection:
|
||||||
async def start_sftp_client(self) -> FakeSftp:
|
async def start_sftp_client(self) -> FakeSftp:
|
||||||
sftp = FakeSftp()
|
sftp = FakeSftp()
|
||||||
@@ -533,6 +533,18 @@ async def test_server_create_mcp_server_wires_default_capture_client(
|
|||||||
monkeypatch.setattr(server_mod, "TvManager", fake_tv_manager)
|
monkeypatch.setattr(server_mod, "TvManager", fake_tv_manager)
|
||||||
monkeypatch.setattr(asyncssh, "connect", fake_connect)
|
monkeypatch.setattr(asyncssh, "connect", fake_connect)
|
||||||
server_mod.create_mcp_server(config)
|
server_mod.create_mcp_server(config)
|
||||||
|
return captured, remote_files, removed_paths, connect_calls, sftp_sessions
|
||||||
|
|
||||||
|
|
||||||
|
async def test_server_create_mcp_server_wires_default_capture_client(
|
||||||
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||||
|
) -> None:
|
||||||
|
from lg_smtv_mcp.tv_core.capture import CaptureClient
|
||||||
|
|
||||||
|
config = _make_config(tmp_path)
|
||||||
|
captured, remote_files, removed_paths, connect_calls, sftp_sessions = (
|
||||||
|
_wire_capture_client_with_fake_ssh(monkeypatch, config)
|
||||||
|
)
|
||||||
|
|
||||||
assert captured["config"] is config
|
assert captured["config"] is config
|
||||||
assert "capture_client" in captured["kwargs"]
|
assert "capture_client" in captured["kwargs"]
|
||||||
@@ -565,7 +577,6 @@ async def test_server_create_mcp_server_wires_capture_downscaling_config(
|
|||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import lg_smtv_mcp.server as server_mod
|
|
||||||
from lg_smtv_mcp.tv_core.capture import CaptureClient
|
from lg_smtv_mcp.tv_core.capture import CaptureClient
|
||||||
|
|
||||||
config = TvConfig(
|
config = TvConfig(
|
||||||
@@ -574,64 +585,9 @@ async def test_server_create_mcp_server_wires_capture_downscaling_config(
|
|||||||
capture_max_width=640,
|
capture_max_width=640,
|
||||||
capture_jpeg_quality=60,
|
capture_jpeg_quality=60,
|
||||||
)
|
)
|
||||||
captured: dict[str, Any] = {}
|
captured, remote_files, _removed_paths, _connect_calls, _sftp_sessions = (
|
||||||
remote_files: dict[str, bytes] = {}
|
_wire_capture_client_with_fake_ssh(monkeypatch, config)
|
||||||
|
)
|
||||||
class DummyManager:
|
|
||||||
async def start(self) -> None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
class FakeSftp:
|
|
||||||
async def listdir(self, path: str) -> list[str]:
|
|
||||||
_ = path
|
|
||||||
return []
|
|
||||||
|
|
||||||
def open(self, path: str, mode: str = "r") -> Any:
|
|
||||||
_ = mode
|
|
||||||
|
|
||||||
class FakeFile:
|
|
||||||
async def __aenter__(self) -> FakeFile:
|
|
||||||
return self
|
|
||||||
|
|
||||||
async def __aexit__(self, *_exc: object) -> None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def read(self) -> bytes:
|
|
||||||
return remote_files[path]
|
|
||||||
|
|
||||||
return FakeFile()
|
|
||||||
|
|
||||||
async def remove(self, path: str) -> None:
|
|
||||||
remote_files.pop(path, None)
|
|
||||||
|
|
||||||
async def exit(self) -> None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
class FakeSshConnection:
|
|
||||||
async def start_sftp_client(self) -> FakeSftp:
|
|
||||||
return FakeSftp()
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
async def fake_connect(**kwargs: Any) -> FakeSshConnection:
|
|
||||||
_ = kwargs
|
|
||||||
return FakeSshConnection()
|
|
||||||
|
|
||||||
monkeypatch.setattr(server_mod, "TvManager", fake_tv_manager)
|
|
||||||
monkeypatch.setattr(asyncssh, "connect", fake_connect)
|
|
||||||
server_mod.create_mcp_server(config)
|
|
||||||
|
|
||||||
capture_client = captured["kwargs"]["capture_client"]
|
capture_client = captured["kwargs"]["capture_client"]
|
||||||
assert isinstance(capture_client, CaptureClient)
|
assert isinstance(capture_client, CaptureClient)
|
||||||
|
|||||||
Reference in new issue
Block a user