test(tv-core): harden boundary coverage, inject wait/sleep seams, and replace polling sleeps
Core Architecture & Invariant CI / validate (pull_request) Successful in 17s
Core Architecture & Invariant CI / validate (pull_request) Successful in 17s
- Inject sleep_fn in PointerClient and wait_fn (EventWaitFn) in TvManager to eliminate monkeypatching asyncio.sleep/wait_for in tests - Add inclusive upper/lower boundary tests for max_volume (0, 35), send_keys (20 keys, delay_ms=500, settle_ms=2000), and type_text (1, 500 chars) - Replace blind polling loops and fixed sleeps with _wait_for/_wait_for_state and an _started async context manager guaranteeing mgr.close() cleanup - Split multi-behavior tests into focused test_<unit>_<behavior> functions and parametrize capture partial-success cases
This commit is contained in:
1 parent
f579ac37bf
commit
8574132baf
4 files changed
+493
-476
No files matched your search
@@ -5,7 +5,7 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
from collections.abc import AsyncGenerator, Callable
|
from collections.abc import AsyncGenerator, Awaitable, Callable
|
||||||
from contextlib import asynccontextmanager, suppress
|
from contextlib import asynccontextmanager, suppress
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
@@ -27,6 +27,14 @@ from lg_smtv_mcp.tv_core.ssap import SsapClient, websockets_connect
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type EventWaitFn = Callable[[asyncio.Event, float], Awaitable[None]]
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_event_or_timeout(event: asyncio.Event, timeout: float) -> None:
|
||||||
|
"""Wait until event is set or timeout seconds elapse, whichever comes first."""
|
||||||
|
with suppress(TimeoutError):
|
||||||
|
await asyncio.wait_for(event.wait(), timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
class ConnectionState(StrEnum):
|
class ConnectionState(StrEnum):
|
||||||
"""Lifecycle states of the single-owner TvManager connection loop."""
|
"""Lifecycle states of the single-owner TvManager connection loop."""
|
||||||
@@ -81,11 +89,13 @@ class TvManager:
|
|||||||
wake_poll_interval: float = 1.0,
|
wake_poll_interval: float = 1.0,
|
||||||
wake_timeout: float = 45.0,
|
wake_timeout: float = 45.0,
|
||||||
ui_lock_timeout: float = 25.0,
|
ui_lock_timeout: float = 25.0,
|
||||||
|
wait_fn: EventWaitFn = wait_event_or_timeout,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._config = config
|
self._config = config
|
||||||
self._connect_fn = connect_fn
|
self._connect_fn = connect_fn
|
||||||
self._capture_client = capture_client
|
self._capture_client = capture_client
|
||||||
self._wol_fn = wol_fn
|
self._wol_fn = wol_fn
|
||||||
|
self._wait_fn = wait_fn
|
||||||
self._backoff_steps = backoff_steps
|
self._backoff_steps = backoff_steps
|
||||||
self._wake_poll_interval = wake_poll_interval
|
self._wake_poll_interval = wake_poll_interval
|
||||||
self._wake_timeout = wake_timeout
|
self._wake_timeout = wake_timeout
|
||||||
@@ -284,10 +294,7 @@ class TvManager:
|
|||||||
self._reload_requested = False
|
self._reload_requested = False
|
||||||
return current_idx
|
return current_idx
|
||||||
|
|
||||||
try:
|
await self._wait_fn(self._wake_or_reload_event, delay)
|
||||||
await asyncio.wait_for(self._wake_or_reload_event.wait(), timeout=delay)
|
|
||||||
except TimeoutError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if self.state is ConnectionState.PAIRING_REQUIRED and not self._wake_requested:
|
if self.state is ConnectionState.PAIRING_REQUIRED and not self._wake_requested:
|
||||||
self._reload_requested = False
|
self._reload_requested = False
|
||||||
|
|||||||
@@ -81,9 +81,11 @@ class PointerClient:
|
|||||||
self,
|
self,
|
||||||
ssap_request_fn: Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]],
|
ssap_request_fn: Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]],
|
||||||
connect_fn: WebSocketConnectFn = websockets_connect,
|
connect_fn: WebSocketConnectFn = websockets_connect,
|
||||||
|
sleep_fn: Callable[[float], Awaitable[None]] = asyncio.sleep,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._ssap_request_fn = ssap_request_fn
|
self._ssap_request_fn = ssap_request_fn
|
||||||
self._connect_fn = connect_fn
|
self._connect_fn = connect_fn
|
||||||
|
self._sleep_fn = sleep_fn
|
||||||
self._ws: WebSocketTransport | None = None
|
self._ws: WebSocketTransport | None = None
|
||||||
|
|
||||||
async def _ensure_socket(self) -> WebSocketTransport:
|
async def _ensure_socket(self) -> WebSocketTransport:
|
||||||
@@ -115,7 +117,7 @@ class PointerClient:
|
|||||||
frame = f"type:button\nname:{key}\n\n"
|
frame = f"type:button\nname:{key}\n\n"
|
||||||
await self._send_frame_with_retry(frame)
|
await self._send_frame_with_retry(frame)
|
||||||
if delay_ms > 0 and index < len(normalized) - 1:
|
if delay_ms > 0 and index < len(normalized) - 1:
|
||||||
await asyncio.sleep(delay_ms / 1000.0)
|
await self._sleep_fn(delay_ms / 1000.0)
|
||||||
return len(normalized)
|
return len(normalized)
|
||||||
|
|
||||||
async def _send_frame_with_retry(self, frame: str) -> None:
|
async def _send_frame_with_retry(self, frame: str) -> None:
|
||||||
|
|||||||
+476
-465
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,3 @@
|
|||||||
import asyncio
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -53,22 +52,20 @@ async def test_pointer_rejects_unknown_keys_and_newlines_with_invalid_argument(
|
|||||||
assert ws.sent == []
|
assert ws.sent == []
|
||||||
|
|
||||||
|
|
||||||
async def test_pointer_paces_buttons_by_delay_ms(monkeypatch: pytest.MonkeyPatch) -> None:
|
async def test_pointer_paces_buttons_by_delay_ms() -> None:
|
||||||
ws = FakeWebSocketTransport()
|
ws = FakeWebSocketTransport()
|
||||||
sleeps: list[float] = []
|
sleeps: list[float] = []
|
||||||
|
|
||||||
async def fake_sleep(seconds: float) -> None:
|
async def fake_sleep(seconds: float) -> None:
|
||||||
sleeps.append(seconds)
|
sleeps.append(seconds)
|
||||||
|
|
||||||
monkeypatch.setattr(asyncio, "sleep", fake_sleep)
|
|
||||||
|
|
||||||
async def fake_ssap(_uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
async def fake_ssap(_uri: str, _payload: dict[str, Any]) -> dict[str, Any]:
|
||||||
return {"socketPath": "wss://192.0.2.10:3001/pointer/1"}
|
return {"socketPath": "wss://192.0.2.10:3001/pointer/1"}
|
||||||
|
|
||||||
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
async def fake_connect(_url: str) -> FakeWebSocketTransport:
|
||||||
return ws
|
return ws
|
||||||
|
|
||||||
client = PointerClient(fake_ssap, connect_fn=fake_connect)
|
client = PointerClient(fake_ssap, connect_fn=fake_connect, sleep_fn=fake_sleep)
|
||||||
await client.send_buttons(["LEFT", "RIGHT", "ENTER"], delay_ms=150)
|
await client.send_buttons(["LEFT", "RIGHT", "ENTER"], delay_ms=150)
|
||||||
await client.close()
|
await client.close()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user