test(pointer): add Red-phase contract tests and stubs for PointerClient
- Add typed PointerClient and BUTTON_ALLOWLIST stubs - Add xfail contract tests for key normalization, allowlist/newline rejection, delay_ms pacing, and mid-batch reconnect retry
This commit is contained in:
1 parent
372235e9d5
commit
41d197ee59
2 files changed
+175
No files matched your search
@@ -0,0 +1,67 @@
|
||||
"""Pointer input WebSocket client for LG webOS D-pad and remote buttons."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any
|
||||
|
||||
from lg_smtv_mcp.tv_core.protocols import WebSocketConnectFn
|
||||
from lg_smtv_mcp.tv_core.ssap import websockets_connect
|
||||
|
||||
BUTTON_ALLOWLIST: frozenset[str] = frozenset(
|
||||
{
|
||||
"UP",
|
||||
"DOWN",
|
||||
"LEFT",
|
||||
"RIGHT",
|
||||
"ENTER",
|
||||
"BACK",
|
||||
"HOME",
|
||||
"EXIT",
|
||||
"PLAY",
|
||||
"PAUSE",
|
||||
"STOP",
|
||||
"REWIND",
|
||||
"FASTFORWARD",
|
||||
"RED",
|
||||
"GREEN",
|
||||
"YELLOW",
|
||||
"BLUE",
|
||||
"VOLUMEUP",
|
||||
"VOLUMEDOWN",
|
||||
"MUTE",
|
||||
"CHANNELUP",
|
||||
"CHANNELDOWN",
|
||||
"INFO",
|
||||
"GUIDE",
|
||||
"MENU",
|
||||
"CC",
|
||||
"0",
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8",
|
||||
"9",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class PointerClient:
|
||||
"""Manages the persistent webOS pointer input socket and button frame formatting."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
ssap_request_fn: Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]],
|
||||
connect_fn: WebSocketConnectFn = websockets_connect,
|
||||
) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
async def send_buttons(self, keys: list[str], delay_ms: int = 250) -> int:
|
||||
"""Validate, normalize, and send a sequence of button frames."""
|
||||
raise NotImplementedError
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Close the pointer WebSocket transport if open."""
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,108 @@
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from lg_smtv_mcp.errors import ErrorCode, TvError
|
||||
from lg_smtv_mcp.tv_core.pointer import BUTTON_ALLOWLIST, PointerClient
|
||||
from tests.conftest import FakeWebSocketTransport
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: pointer")
|
||||
async def test_pointer_normalizes_keys_to_uppercase_and_formats_frames() -> None:
|
||||
ws = FakeWebSocketTransport()
|
||||
ssap_calls: list[str] = []
|
||||
|
||||
async def fake_ssap(uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
||||
ssap_calls.append(uri)
|
||||
return {"socketPath": "wss://192.0.2.10:3001/pointer/1"}
|
||||
|
||||
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
||||
return ws
|
||||
|
||||
client = PointerClient(fake_ssap, connect_fn=fake_connect)
|
||||
count = await client.send_buttons(["up", "Enter", "home"], delay_ms=0)
|
||||
await client.close()
|
||||
|
||||
assert count == 3
|
||||
assert ssap_calls == ["ssap://com.webos.service.networkinput/getPointerInputSocket"]
|
||||
assert ws.sent == [
|
||||
"type:button\nname:UP\n\n",
|
||||
"type:button\nname:ENTER\n\n",
|
||||
"type:button\nname:HOME\n\n",
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: pointer")
|
||||
@pytest.mark.parametrize("bad_key", ["INVALID_KEY", "UP\nname:HOME", "ENTER\r", ""])
|
||||
async def test_pointer_rejects_unknown_keys_and_newlines_with_invalid_argument(
|
||||
bad_key: str,
|
||||
) -> None:
|
||||
assert "UP" in BUTTON_ALLOWLIST
|
||||
ws = FakeWebSocketTransport()
|
||||
|
||||
async def fake_ssap(_uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"socketPath": "wss://192.0.2.10:3001/pointer/1"}
|
||||
|
||||
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
||||
return ws
|
||||
|
||||
client = PointerClient(fake_ssap, connect_fn=fake_connect)
|
||||
with pytest.raises(TvError) as exc_info:
|
||||
await client.send_buttons(["UP", bad_key], delay_ms=0)
|
||||
|
||||
assert exc_info.value.code is ErrorCode.INVALID_ARGUMENT
|
||||
assert ws.sent == []
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: pointer")
|
||||
async def test_pointer_paces_buttons_by_delay_ms(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
ws = FakeWebSocketTransport()
|
||||
sleeps: list[float] = []
|
||||
|
||||
async def fake_sleep(seconds: float) -> None:
|
||||
sleeps.append(seconds)
|
||||
|
||||
monkeypatch.setattr(asyncio, "sleep", fake_sleep)
|
||||
|
||||
async def fake_ssap(_uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"socketPath": "wss://192.0.2.10:3001/pointer/1"}
|
||||
|
||||
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
||||
return ws
|
||||
|
||||
client = PointerClient(fake_ssap, connect_fn=fake_connect)
|
||||
await client.send_buttons(["LEFT", "RIGHT", "ENTER"], delay_ms=150)
|
||||
await client.close()
|
||||
|
||||
assert sleeps == [pytest.approx(0.15), pytest.approx(0.15)]
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: pointer")
|
||||
async def test_pointer_retries_once_on_mid_batch_socket_drop() -> None:
|
||||
ws1 = FakeWebSocketTransport()
|
||||
ws2 = FakeWebSocketTransport()
|
||||
connects = 0
|
||||
|
||||
def drop_after_first(data: str, transport: FakeWebSocketTransport) -> None:
|
||||
if "LEFT" in data:
|
||||
transport.closed = True
|
||||
|
||||
ws1._on_send = drop_after_first # pyright: ignore[reportPrivateUsage]
|
||||
|
||||
async def fake_ssap(_uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {"socketPath": f"wss://192.0.2.10:3001/pointer/{connects + 1}"}
|
||||
|
||||
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
||||
nonlocal connects
|
||||
connects += 1
|
||||
return ws1 if connects == 1 else ws2
|
||||
|
||||
client = PointerClient(fake_ssap, connect_fn=fake_connect)
|
||||
count = await client.send_buttons(["LEFT", "ENTER"], delay_ms=0)
|
||||
await client.close()
|
||||
|
||||
assert count == 2
|
||||
assert connects == 2
|
||||
assert ws1.sent == ["type:button\nname:LEFT\n\n"]
|
||||
assert ws2.sent == ["type:button\nname:ENTER\n\n"]
|
||||
Reference in new issue
Block a user