test(protocols): add Red-phase contract tests and stubs for tv_core.protocols
- Add tv_core package init and typed protocol/dataclass stubs raising NotImplementedError - Add xfail contract tests for WebSocketClosedError args/str invariants and frozen result dataclasses
This commit is contained in:
1 parent
1489d4e7da
commit
712aff354b
3 files changed
+194
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Core LG webOS TV transport, protocol, and state machine package."""
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Transport protocols, connection exceptions, and frozen result containers."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Literal, Protocol
|
||||
|
||||
|
||||
class WebSocketClosedError(Exception):
|
||||
"""Raised when a WebSocket transport closes unexpectedly or is already closed."""
|
||||
|
||||
code: int
|
||||
reason: str
|
||||
|
||||
def __init__(self, code: int, reason: str = "") -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class WebSocketTransport(Protocol):
|
||||
"""Minimal async WebSocket transport boundary for SSAP and pointer sockets."""
|
||||
|
||||
async def send(self, data: str) -> None: ...
|
||||
|
||||
async def recv(self) -> str: ...
|
||||
|
||||
async def close(self) -> None: ...
|
||||
|
||||
|
||||
type WebSocketConnectFn = Callable[[str], Awaitable[WebSocketTransport]]
|
||||
|
||||
|
||||
class CaptureTransport(Protocol):
|
||||
"""Minimal async SFTP transport boundary for Developer Mode screenshot retrieval."""
|
||||
|
||||
async def read_and_remove(
|
||||
self,
|
||||
remote_path: str,
|
||||
timeout: float = 6.0,
|
||||
cleanup_grace: float = 1.5,
|
||||
) -> bytes: ...
|
||||
|
||||
async def sweep_temp_files(self, prefix: str = "/tmp/lg_smtv_cap_") -> None: ...
|
||||
|
||||
async def close(self) -> None: ...
|
||||
|
||||
|
||||
class FrameCapturer(Protocol):
|
||||
"""Protocol for capturing screenshots via SSAP + SFTP."""
|
||||
|
||||
async def capture_frame(
|
||||
self,
|
||||
ssap_request_fn: Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]],
|
||||
) -> CaptureResult: ...
|
||||
|
||||
async def close(self) -> None: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class TvStatus:
|
||||
"""Result of a tv_wake operation."""
|
||||
|
||||
status: Literal["connected", "already_connected"]
|
||||
elapsed_ms: int
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class TvStateSnapshot:
|
||||
"""Point-in-time read-only snapshot of cached TV state."""
|
||||
|
||||
connected: bool
|
||||
stale: bool
|
||||
volume: int | None
|
||||
muted: bool | None
|
||||
sound_output: str | None
|
||||
foreground_app: str | None
|
||||
last_updated: float | None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CaptureResult:
|
||||
"""Result of a screenshot capture operation."""
|
||||
|
||||
image_bytes: bytes
|
||||
frame_sha256: str
|
||||
changed_since_last: bool
|
||||
size_bytes: int
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class KeysResult:
|
||||
"""Result of a batched pointer key sequence."""
|
||||
|
||||
keys_sent: bool
|
||||
count: int
|
||||
frame_sha256: str | None = None
|
||||
changed_since_last: bool | None = None
|
||||
size_bytes: int | None = None
|
||||
capture_error: str | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class TextResult:
|
||||
"""Result of an IME text-entry operation."""
|
||||
|
||||
text_sent: bool
|
||||
enter_sent: bool
|
||||
frame_sha256: str | None = None
|
||||
changed_since_last: bool | None = None
|
||||
size_bytes: int | None = None
|
||||
capture_error: str | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,69 @@
|
||||
from dataclasses import FrozenInstanceError
|
||||
|
||||
import pytest
|
||||
|
||||
from lg_smtv_mcp.tv_core.protocols import (
|
||||
CaptureResult,
|
||||
KeysResult,
|
||||
TextResult,
|
||||
TvStateSnapshot,
|
||||
TvStatus,
|
||||
WebSocketClosedError,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: protocols")
|
||||
def test_websocket_closed_error_preserves_args_and_str_representation() -> None:
|
||||
err = WebSocketClosedError(1008, "Try Again Later (EWS)")
|
||||
|
||||
assert isinstance(err, Exception)
|
||||
assert err.code == 1008
|
||||
assert err.reason == "Try Again Later (EWS)"
|
||||
assert err.args
|
||||
assert "1008" in str(err)
|
||||
assert "Try Again Later (EWS)" in str(err)
|
||||
|
||||
err_no_reason = WebSocketClosedError(1000)
|
||||
assert err_no_reason.code == 1000
|
||||
assert err_no_reason.reason == ""
|
||||
assert "1000" in str(err_no_reason)
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, reason="Red phase: protocols")
|
||||
def test_result_dataclasses_are_frozen_and_expose_expected_fields() -> None:
|
||||
status = TvStatus(status="connected", elapsed_ms=120)
|
||||
snapshot = TvStateSnapshot(
|
||||
connected=True,
|
||||
stale=False,
|
||||
volume=15,
|
||||
muted=False,
|
||||
sound_output="tv_speaker",
|
||||
foreground_app="youtube.leanback.v4",
|
||||
last_updated=1234.5,
|
||||
)
|
||||
capture = CaptureResult(
|
||||
image_bytes=b"\xff\xd8\xff",
|
||||
frame_sha256="abc123",
|
||||
changed_since_last=True,
|
||||
size_bytes=3,
|
||||
)
|
||||
keys_res = KeysResult(keys_sent=True, count=2, capture_error="SCREENSHOT_UNAVAILABLE: off")
|
||||
text_res = TextResult(text_sent=True, enter_sent=False)
|
||||
|
||||
assert status.status == "connected"
|
||||
assert snapshot.foreground_app == "youtube.leanback.v4"
|
||||
assert capture.size_bytes == 3
|
||||
assert keys_res.count == 2
|
||||
assert keys_res.capture_error == "SCREENSHOT_UNAVAILABLE: off"
|
||||
assert text_res.enter_sent is False
|
||||
|
||||
with pytest.raises(FrozenInstanceError):
|
||||
status.elapsed_ms = 0 # type: ignore[misc]
|
||||
with pytest.raises(FrozenInstanceError):
|
||||
snapshot.volume = 99 # type: ignore[misc]
|
||||
with pytest.raises(FrozenInstanceError):
|
||||
capture.size_bytes = 0 # type: ignore[misc]
|
||||
with pytest.raises(FrozenInstanceError):
|
||||
keys_res.count = 0 # type: ignore[misc]
|
||||
with pytest.raises(FrozenInstanceError):
|
||||
text_res.text_sent = False # type: ignore[misc]
|
||||
Reference in new issue
Block a user