Compare commits
2
Commits
2af3c99e4c
...
aac00356a1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aac00356a1 | ||
|
|
85a5acad35 |
No files matched your search
@@ -13,6 +13,7 @@ from pathlib import Path
|
|||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
from mcp.server.mcpserver import MCPServer
|
from mcp.server.mcpserver import MCPServer
|
||||||
|
from websockets.exceptions import WebSocketException
|
||||||
|
|
||||||
from lg_smtv_mcp.auth import ASGIApp
|
from lg_smtv_mcp.auth import ASGIApp
|
||||||
from lg_smtv_mcp.config import TvConfig, load_config
|
from lg_smtv_mcp.config import TvConfig, load_config
|
||||||
@@ -73,7 +74,7 @@ async def _pair_tv(
|
|||||||
existing_key: str | None,
|
existing_key: str | None,
|
||||||
connect_fn: WebSocketConnectFn,
|
connect_fn: WebSocketConnectFn,
|
||||||
) -> str:
|
) -> str:
|
||||||
ws = await connect_fn(f"ws://{ip}:3000")
|
ws = await connect_fn(f"wss://{ip}:3001")
|
||||||
try:
|
try:
|
||||||
reg_payload = build_register_payload(existing_key)
|
reg_payload = build_register_payload(existing_key)
|
||||||
await ws.send(json.dumps(reg_payload))
|
await ws.send(json.dumps(reg_payload))
|
||||||
@@ -197,6 +198,6 @@ def main(
|
|||||||
if args.command == "serve":
|
if args.command == "serve":
|
||||||
return _handle_serve(args, config, uvicorn_run=uvicorn_run, stdio_run=stdio_run)
|
return _handle_serve(args, config, uvicorn_run=uvicorn_run, stdio_run=stdio_run)
|
||||||
return _handle_pair(args, config, connect_fn=connect_fn, fetch_url=fetch_url)
|
return _handle_pair(args, config, connect_fn=connect_fn, fetch_url=fetch_url)
|
||||||
except (ConfigError, TvError, OSError, ValueError) as exc:
|
except (ConfigError, TvError, WebSocketException, OSError, ValueError) as exc:
|
||||||
print(f"error: {exc}", file=sys.stderr)
|
print(f"error: {exc}", file=sys.stderr)
|
||||||
return 1
|
return 1
|
||||||
@@ -105,7 +105,7 @@ class AsyncSshCaptureTransport:
|
|||||||
async with sftp.open(remote_path, "rb") as handle:
|
async with sftp.open(remote_path, "rb") as handle:
|
||||||
data = await handle.read()
|
data = await handle.read()
|
||||||
return bytes(data)
|
return bytes(data)
|
||||||
except asyncssh.Error, OSError:
|
except asyncssh.Error, asyncssh.KeyImportError, OSError:
|
||||||
await self._close_handles()
|
await self._close_handles()
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
@@ -192,7 +192,7 @@ class CaptureClient:
|
|||||||
)
|
)
|
||||||
except TvError:
|
except TvError:
|
||||||
raise
|
raise
|
||||||
except (asyncssh.Error, OSError) as exc:
|
except (asyncssh.Error, asyncssh.KeyImportError, OSError) as exc:
|
||||||
raise TvError(
|
raise TvError(
|
||||||
ErrorCode.SCREENSHOT_UNAVAILABLE,
|
ErrorCode.SCREENSHOT_UNAVAILABLE,
|
||||||
f"Failed to retrieve screenshot over SSH/SFTP: {exc}",
|
f"Failed to retrieve screenshot over SSH/SFTP: {exc}",
|
||||||
|
|||||||
@@ -504,3 +504,21 @@ async def test_capture_client_maps_sftp_timeout_and_cancels_read() -> None:
|
|||||||
|
|
||||||
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
||||||
assert transport.read_cancelled.is_set()
|
assert transport.read_cancelled.is_set()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_capture_client_maps_key_import_error_to_screenshot_unavailable() -> None:
|
||||||
|
transport = FakeCaptureTransport(
|
||||||
|
read_error=asyncssh.KeyImportError(
|
||||||
|
"Passphrase must be specified to import encrypted private keys"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
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 client.capture_frame(ssap_request)
|
||||||
|
assert exc_info.value.code is ErrorCode.SCREENSHOT_UNAVAILABLE
|
||||||
|
assert "Passphrase must be specified" in exc_info.value.message
|
||||||
+20
-1
@@ -8,6 +8,7 @@ from typing import Any
|
|||||||
import httpx
|
import httpx
|
||||||
import pytest
|
import pytest
|
||||||
from mcp.server.mcpserver import MCPServer
|
from mcp.server.mcpserver import MCPServer
|
||||||
|
from websockets.exceptions import InvalidMessage
|
||||||
|
|
||||||
from lg_smtv_mcp.auth import ASGIApp
|
from lg_smtv_mcp.auth import ASGIApp
|
||||||
from lg_smtv_mcp.cli import main
|
from lg_smtv_mcp.cli import main
|
||||||
@@ -122,7 +123,7 @@ def test_cli_pair_merges_existing_client_keys_and_writes_0600(tmp_path: Path) ->
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert rc == 0
|
assert rc == 0
|
||||||
assert connected_urls == ["ws://192.0.2.20:3000"]
|
assert connected_urls == ["wss://192.0.2.20:3001"]
|
||||||
saved = json.loads(key_file.read_text(encoding="utf-8"))
|
saved = json.loads(key_file.read_text(encoding="utf-8"))
|
||||||
assert saved == {"192.0.2.10": "old-key", "192.0.2.20": "new-key-20"}
|
assert saved == {"192.0.2.10": "old-key", "192.0.2.20": "new-key-20"}
|
||||||
assert stat.S_IMODE(key_file.stat().st_mode) == 0o600
|
assert stat.S_IMODE(key_file.stat().st_mode) == 0o600
|
||||||
@@ -196,3 +197,21 @@ def test_cli_pair_fetch_ssh_key_failure_preserves_client_key_and_exits_nonzero(
|
|||||||
assert rc == 1
|
assert rc == 1
|
||||||
assert json.loads(key_file.read_text(encoding="utf-8")) == {"192.0.2.10": "tv-key-10"}
|
assert json.loads(key_file.read_text(encoding="utf-8")) == {"192.0.2.10": "tv-key-10"}
|
||||||
assert not ssh_dest.exists()
|
assert not ssh_dest.exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_pair_returns_nonzero_on_websocket_handshake_error(
|
||||||
|
tmp_path: Path,
|
||||||
|
capsys: pytest.CaptureFixture[str],
|
||||||
|
) -> None:
|
||||||
|
async def failing_connect(_url: str) -> FakeWebSocketTransport:
|
||||||
|
raise InvalidMessage("did not receive a valid HTTP response")
|
||||||
|
|
||||||
|
rc = main(
|
||||||
|
["pair"],
|
||||||
|
config_loader=lambda: _make_config(tmp_path),
|
||||||
|
connect_fn=failing_connect,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert rc == 1
|
||||||
|
captured = capsys.readouterr()
|
||||||
|
assert "error: did not receive a valid HTTP response" in captured.err
|
||||||
Reference in new issue
Block a user