fix(cli): use wss://:3001 for TV pairing and catch WebSocketException

- src/lg_smtv_mcp/cli.py: update _pair_tv() to connect via wss://<ip>:3001 matching TvManager and WebOS/4.1.0 TLS SSAP endpoint; catch WebSocketException in main() so handshake errors exit cleanly with code 1

- tests/test_cli.py: update pairing URL assertion to wss://192.0.2.20:3001 and add test_cli_pair_returns_nonzero_on_websocket_handshake_error
This commit is contained in:
kiskaadee committed 2026-11-19 00:41:47 -05:00
1 parent 2af3c99e4c
commit 85a5acad35
2 files changed
+23 -3

No files matched your search

+3 -2
View File
@@ -13,6 +13,7 @@ from pathlib import Path
from typing import Any, cast
from mcp.server.mcpserver import MCPServer
from websockets.exceptions import WebSocketException
from lg_smtv_mcp.auth import ASGIApp
from lg_smtv_mcp.config import TvConfig, load_config
@@ -73,7 +74,7 @@ async def _pair_tv(
existing_key: str | None,
connect_fn: WebSocketConnectFn,
) -> str:
ws = await connect_fn(f"ws://{ip}:3000")
ws = await connect_fn(f"wss://{ip}:3001")
try:
reg_payload = build_register_payload(existing_key)
await ws.send(json.dumps(reg_payload))
@@ -197,6 +198,6 @@ def main(
if args.command == "serve":
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)
except (ConfigError, TvError, OSError, ValueError) as exc:
except (ConfigError, TvError, WebSocketException, OSError, ValueError) as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
+20 -1
View File
@@ -8,6 +8,7 @@ from typing import Any
import httpx
import pytest
from mcp.server.mcpserver import MCPServer
from websockets.exceptions import InvalidMessage
from lg_smtv_mcp.auth import ASGIApp
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 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"))
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
@@ -196,3 +197,21 @@ def test_cli_pair_fetch_ssh_key_failure_preserves_client_key_and_exits_nonzero(
assert rc == 1
assert json.loads(key_file.read_text(encoding="utf-8")) == {"192.0.2.10": "tv-key-10"}
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