From 120d3ecf329834578b345025a1e428d2777c1177 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 19 Nov 2026 10:30:52 -0500 Subject: [PATCH] feat(config): add optional capture downscaling and quality settings - Add capture_max_width (64..3840) and capture_jpeg_quality (1..95) fields to TvConfig - Validate both fields via _parse_bounded_int in mode="before" validators allowing None - Parse CAPTURE_MAX_WIDTH and CAPTURE_JPEG_QUALITY in load_config() - Add unit tests in tests/test_config.py covering defaults, valid bounds, and ConfigError rejections --- src/lg_smtv_mcp/config.py | 22 +++++++++++++++ tests/test_config.py | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/lg_smtv_mcp/config.py b/src/lg_smtv_mcp/config.py index e89da69..b23133f 100644 --- a/src/lg_smtv_mcp/config.py +++ b/src/lg_smtv_mcp/config.py @@ -72,6 +72,8 @@ class TvConfig(BaseModel): mcp_bearer_token: str | None = None max_volume: int = 40 enable_raw_ssap: bool = False + capture_max_width: int | None = None + capture_jpeg_quality: int | None = None def __init__(self, **data: Any) -> None: try: @@ -100,6 +102,20 @@ class TvConfig(BaseModel): def _validate_max_volume(cls, value: Any) -> int: return _parse_bounded_int("MAX_VOLUME", value, min_val=0, max_val=100) + @field_validator("capture_max_width", mode="before") + @classmethod + def _validate_capture_max_width(cls, value: Any) -> int | None: + if value is None: + return None + return _parse_bounded_int("CAPTURE_MAX_WIDTH", value, min_val=64, max_val=3840) + + @field_validator("capture_jpeg_quality", mode="before") + @classmethod + def _validate_capture_jpeg_quality(cls, value: Any) -> int | None: + if value is None: + return None + return _parse_bounded_int("CAPTURE_JPEG_QUALITY", value, min_val=1, max_val=95) + def _load_dotenv(env_path: Path) -> None: """Populate unset os.environ keys from a .env file using stdlib only.""" @@ -149,6 +165,8 @@ def load_config(env_path: Path | None = Path(".env")) -> TvConfig: tv_ip = _optional_str("TV_IP") or "" port_raw = _optional_str("PORT") max_volume_raw = _optional_str("MAX_VOLUME") + capture_max_width_raw = _optional_str("CAPTURE_MAX_WIDTH") + capture_jpeg_quality_raw = _optional_str("CAPTURE_JPEG_QUALITY") host = _optional_str("HOST") or "127.0.0.1" wol_broadcast = _optional_str("WOL_BROADCAST") or "255.255.255.255" @@ -177,6 +195,10 @@ def load_config(env_path: Path | None = Path(".env")) -> TvConfig: kwargs["port"] = port_raw if max_volume_raw is not None: kwargs["max_volume"] = max_volume_raw + if capture_max_width_raw is not None: + kwargs["capture_max_width"] = capture_max_width_raw + if capture_jpeg_quality_raw is not None: + kwargs["capture_jpeg_quality"] = capture_jpeg_quality_raw return TvConfig(**kwargs) diff --git a/tests/test_config.py b/tests/test_config.py index dc2f2bb..47fc60f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -22,6 +22,8 @@ def _clear_tv_env(monkeypatch: pytest.MonkeyPatch) -> None: "MCP_BEARER_TOKEN", "MAX_VOLUME", "ENABLE_RAW_SSAP", + "CAPTURE_MAX_WIDTH", + "CAPTURE_JPEG_QUALITY", ): monkeypatch.delenv(key, raising=False) @@ -74,6 +76,8 @@ def test_load_config_uses_expected_defaults_when_optional_vars_unset( assert cfg.allowed_hosts == () assert cfg.tv_mac is None assert cfg.mcp_bearer_token is None + assert cfg.capture_max_width is None + assert cfg.capture_jpeg_quality is None def test_dotenv_fallback_does_not_overwrite_existing_env( @@ -108,6 +112,12 @@ def test_dotenv_fallback_does_not_overwrite_existing_env( ({"TV_IP": "192.0.2.10", "PORT": "0"}, "PORT"), ({"TV_IP": "192.0.2.10", "PORT": "65536"}, "PORT"), ({"TV_IP": "192.0.2.10", "PORT": "abc"}, "PORT"), + ({"TV_IP": "192.0.2.10", "CAPTURE_MAX_WIDTH": "63"}, "CAPTURE_MAX_WIDTH"), + ({"TV_IP": "192.0.2.10", "CAPTURE_MAX_WIDTH": "3841"}, "CAPTURE_MAX_WIDTH"), + ({"TV_IP": "192.0.2.10", "CAPTURE_MAX_WIDTH": "wide"}, "CAPTURE_MAX_WIDTH"), + ({"TV_IP": "192.0.2.10", "CAPTURE_JPEG_QUALITY": "0"}, "CAPTURE_JPEG_QUALITY"), + ({"TV_IP": "192.0.2.10", "CAPTURE_JPEG_QUALITY": "96"}, "CAPTURE_JPEG_QUALITY"), + ({"TV_IP": "192.0.2.10", "CAPTURE_JPEG_QUALITY": "high"}, "CAPTURE_JPEG_QUALITY"), ], ) def test_load_config_rejects_invalid_env_values_with_config_error( @@ -136,6 +146,14 @@ def test_load_config_rejects_invalid_env_values_with_config_error( ({"tv_ip": "192.0.2.10", "max_volume": 150}, "MAX_VOLUME"), ({"tv_ip": "192.0.2.10", "max_volume": 12.5}, "MAX_VOLUME"), ({"tv_ip": "192.0.2.10", "wol_broadcast": "bad-ip"}, "WOL_BROADCAST"), + ({"tv_ip": "192.0.2.10", "capture_max_width": 63}, "CAPTURE_MAX_WIDTH"), + ({"tv_ip": "192.0.2.10", "capture_max_width": 3841}, "CAPTURE_MAX_WIDTH"), + ({"tv_ip": "192.0.2.10", "capture_max_width": 1280.5}, "CAPTURE_MAX_WIDTH"), + ({"tv_ip": "192.0.2.10", "capture_max_width": True}, "CAPTURE_MAX_WIDTH"), + ({"tv_ip": "192.0.2.10", "capture_jpeg_quality": 0}, "CAPTURE_JPEG_QUALITY"), + ({"tv_ip": "192.0.2.10", "capture_jpeg_quality": 96}, "CAPTURE_JPEG_QUALITY"), + ({"tv_ip": "192.0.2.10", "capture_jpeg_quality": 75.5}, "CAPTURE_JPEG_QUALITY"), + ({"tv_ip": "192.0.2.10", "capture_jpeg_quality": False}, "CAPTURE_JPEG_QUALITY"), ], ) def test_tv_config_rejects_invalid_direct_arguments_with_config_error( @@ -210,3 +228,41 @@ def test_require_mac_returns_trimmed_value(monkeypatch: pytest.MonkeyPatch, tmp_ cfg = load_config(env_path=tmp_path / "missing.env") assert require_mac(cfg) == "AA:BB:CC:DD:EE:FF" + + +@pytest.mark.parametrize( + ("width_env", "quality_env", "expected_width", "expected_quality"), + [ + ("64", "1", 64, 1), + ("3840", "95", 3840, 95), + (" 1280 ", " 75 ", 1280, 75), + ("", " ", None, None), + ], +) +def test_load_config_parses_capture_max_width_and_jpeg_quality( + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, + width_env: str, + quality_env: str, + expected_width: int | None, + expected_quality: int | None, +) -> None: + _clear_tv_env(monkeypatch) + monkeypatch.setenv("TV_IP", "192.0.2.10") + monkeypatch.setenv("CAPTURE_MAX_WIDTH", width_env) + monkeypatch.setenv("CAPTURE_JPEG_QUALITY", quality_env) + + cfg = load_config(env_path=tmp_path / "missing.env") + + assert cfg.capture_max_width == expected_width + assert cfg.capture_jpeg_quality == expected_quality + + +def test_tv_config_accepts_explicit_none_and_valid_capture_bounds() -> None: + cfg_none = TvConfig(tv_ip="192.0.2.10", capture_max_width=None, capture_jpeg_quality=None) + assert cfg_none.capture_max_width is None + assert cfg_none.capture_jpeg_quality is None + + cfg_bounds = TvConfig(tv_ip="192.0.2.10", capture_max_width=1280, capture_jpeg_quality=80) + assert cfg_bounds.capture_max_width == 1280 + assert cfg_bounds.capture_jpeg_quality == 80