fix(capture): read Dev Mode screenshots via asyncssh SFTP open()
Core Architecture & Invariant CI / validate (pull_request) Successful in 16s

- src/lg_smtv_mcp/tv_core/capture.py: replace the nonexistent sftp.read() with sftp.open(path, 'rb') and an async context manager; the live AC#2 smoke against asyncssh 2.24.0 raised AttributeError because SFTPClient exposes open(), not read()
- tests/test_capture.py: reshape the SFTP fake to the real asyncssh contract (regular open(), async file read(), async context manager) and add a regression test asserting the real class API, closing the mock gap that let offline tests pass while the live screenshot tool failed
- tests/test_server.py: align the capture-wiring SFTP fake with the same open()/async-file contract so the default transport test exercises the real call shape
This commit is contained in:
kiskaadee committed 2026-11-17 16:15:56 -05:00
1 parent 9195d91d7d
commit 5a8ebffad2
3 files changed
+87 -17

No files matched your search

+2 -1
View File
@@ -102,7 +102,8 @@ class AsyncSshCaptureTransport:
try: try:
async with asyncio.timeout(timeout): async with asyncio.timeout(timeout):
sftp = await self._ensure_sftp(exclude=remote_path) sftp = await self._ensure_sftp(exclude=remote_path)
data = await sftp.read(remote_path) async with sftp.open(remote_path, "rb") as handle:
data = await handle.read()
return bytes(data) return bytes(data)
except asyncssh.Error, OSError: except asyncssh.Error, OSError:
await self._close_handles() await self._close_handles()
+73 -14
View File
@@ -2,9 +2,11 @@
import asyncio import asyncio
import hashlib import hashlib
import inspect
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import asyncssh
import pytest import pytest
from lg_smtv_mcp.config import TvConfig from lg_smtv_mcp.config import TvConfig
@@ -12,6 +14,35 @@ from lg_smtv_mcp.errors import ErrorCode, TvError
from lg_smtv_mcp.tv_core.capture import AsyncSshCaptureTransport, CaptureClient from lg_smtv_mcp.tv_core.capture import AsyncSshCaptureTransport, CaptureClient
class FakeSftpFile:
"""Mirror asyncssh.SFTPClientFile: sync open(), async read(), async context manager."""
def __init__(
self,
data: bytes,
*,
error: Exception | None = None,
read_delay: float = 0.0,
) -> None:
self._data = data
self._error = error
self._read_delay = read_delay
self.closed = False
async def __aenter__(self) -> FakeSftpFile:
return self
async def __aexit__(self, *_exc: object) -> None:
self.closed = True
async def read(self) -> bytes:
if self._read_delay > 0:
await asyncio.sleep(self._read_delay)
if self._error is not None:
raise self._error
return self._data
class FakeSftpClient: class FakeSftpClient:
def __init__( def __init__(
self, self,
@@ -19,25 +50,34 @@ class FakeSftpClient:
files: dict[str, bytes] | None = None, files: dict[str, bytes] | None = None,
extra_names: list[str] | None = None, extra_names: list[str] | None = None,
read_error: Exception | None = None, read_error: Exception | None = None,
read_delay: float = 0.0,
remove_delay: float = 0.0, remove_delay: float = 0.0,
remove_error: Exception | None = None, remove_error: Exception | None = None,
) -> None: ) -> None:
self.files = dict(files or {}) self.files = dict(files or {})
self.extra_names = list(extra_names or []) self.extra_names = list(extra_names or [])
self.read_error = read_error self.read_error = read_error
self.read_delay = read_delay
self.remove_delay = remove_delay self.remove_delay = remove_delay
self.remove_error = remove_error self.remove_error = remove_error
self.read_calls: list[str] = [] self.open_calls: list[tuple[str, str]] = []
self.handles: list[FakeSftpFile] = []
self.remove_calls: list[str] = [] self.remove_calls: list[str] = []
self.closed = False self.closed = False
async def read(self, path: str) -> bytes: def open(self, path: str, mode: str = "r") -> FakeSftpFile:
self.read_calls.append(path) """Mirror asyncssh.SFTPClient.open: regular function returning an async file."""
if self.read_error is not None: self.open_calls.append((path, mode))
raise self.read_error error = self.read_error
if path not in self.files: if error is None and path not in self.files:
raise FileNotFoundError(path) error = FileNotFoundError(path)
return self.files[path] handle = FakeSftpFile(
self.files.get(path, b""),
error=error,
read_delay=self.read_delay,
)
self.handles.append(handle)
return handle
async def remove(self, path: str) -> None: async def remove(self, path: str) -> None:
if self.remove_delay > 0: if self.remove_delay > 0:
@@ -155,6 +195,30 @@ async def test_async_ssh_capture_transport_connect_uses_required_args(tmp_path:
assert calls[0]["passphrase"] == "secret" assert calls[0]["passphrase"] == "secret"
async def test_async_ssh_capture_transport_reads_via_real_sftp_open_api(tmp_path: Path) -> None:
"""Guard against reintroducing sftp.read(), which asyncssh 2.24 does not expose."""
assert not hasattr(asyncssh.SFTPClient, "read")
assert hasattr(asyncssh.SFTPClient, "open")
assert hasattr(asyncssh.SFTPClientFile, "__aenter__")
assert inspect.iscoroutinefunction(asyncssh.SFTPClientFile.read)
remote_path = "/tmp/lg_smtv_cap_1.jpg"
sftp = FakeSftpClient(files={remote_path: b"jpeg"})
conn = FakeSshConnection(sftp)
async def fake_connect(**kwargs: Any) -> FakeSshConnection:
_ = kwargs
return conn
transport = AsyncSshCaptureTransport(_make_config(tmp_path), connect_fn=fake_connect)
data = await transport.read_and_remove(remote_path)
assert data == b"jpeg"
assert sftp.open_calls == [(remote_path, "rb")]
assert len(sftp.handles) == 1
assert sftp.handles[0].closed is True
async def test_async_ssh_capture_transport_sweeps_on_initial_connect_and_reconnect( async def test_async_ssh_capture_transport_sweeps_on_initial_connect_and_reconnect(
tmp_path: Path, tmp_path: Path,
) -> None: ) -> None:
@@ -248,12 +312,7 @@ async def test_async_ssh_capture_transport_closes_connection_when_start_sftp_cli
async def test_async_ssh_capture_transport_enforces_read_timeout( async def test_async_ssh_capture_transport_enforces_read_timeout(
tmp_path: Path, tmp_path: Path,
) -> None: ) -> None:
class HangingSftp(FakeSftpClient): sftp = FakeSftpClient(files={"/tmp/lg_smtv_cap_1.jpg": b"bytes"}, read_delay=10.0)
async def read(self, path: str) -> bytes:
await asyncio.sleep(10.0)
return b"never"
sftp = HangingSftp(files={"/tmp/lg_smtv_cap_1.jpg": b"bytes"})
conn = FakeSshConnection(sftp) conn = FakeSshConnection(sftp)
async def fake_connect(**kwargs: Any) -> FakeSshConnection: async def fake_connect(**kwargs: Any) -> FakeSshConnection:
+12 -2
View File
@@ -487,8 +487,18 @@ async def test_server_create_mcp_server_wires_default_capture_client(
_ = path _ = path
return [] return []
async def read(self, path: str) -> bytes: def open(self, path: str, mode: str = "r") -> Any:
return remote_files[path] 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: async def remove(self, path: str) -> None:
removed_paths.append(path) removed_paths.append(path)