Files
LEDMatrix/test/test_api_v3_bool_coercion.py
T
ChuckandClaude Opus 5.5 fd0a4b50f9 test: the on-demand routes' other callers ack over the socket, not the mailbox
test_api_v3_bool_coercion, test_api_v3_lazy_plugin_discovery and
test_web_api's stop test checked the mailbox write; they now get an ack
from a mocked control socket. The stop route touches no manager any more,
so it leaves the removed-catch-all sample. A request that was sent is
never 'not listening' (pins the sent check the mutation run let through).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 09:40:31 -04:00

80 lines
3.6 KiB
Python

"""Boolean request fields are coerced, not used raw.
``bool("false")`` is True. /plugins/toggle stored a JSON ``"enabled": "false"``
as-is in config.json (a truthy string the display then treats as enabled) and
passed it to the Starlark toggle the same way, and /display/on-demand/start
pinned the mode and restarted the service for ``"pinned": "false"`` /
``"start_service": "false"``.
"""
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
sys.path.insert(0, str(Path(__file__).parent.parent))
from test._api_v3_test_helpers import api_v3_client, api_v3_module # noqa: F401,E402
class TestPluginToggle:
@pytest.fixture
def saved(self, api_v3_module, monkeypatch):
monkeypatch.setattr(api_v3_module, '_discovered_plugin_manifests',
lambda *a, **k: {'clock': {}})
api_v3_module.api_v3.config_manager.load_config = MagicMock(return_value={})
captured = {}
def save(config_manager, config, create_backup=True):
captured.update(config)
return True, None
monkeypatch.setattr(api_v3_module, '_save_config_atomic', save)
return captured
@pytest.mark.parametrize("raw,expected", [
("false", False), ("true", True), (False, False), (True, True), (0, False),
])
def test_enabled_is_stored_as_a_real_bool(self, api_v3_client, saved, raw, expected):
response = api_v3_client.post('/api/v3/plugins/toggle',
json={'plugin_id': 'clock', 'enabled': raw})
assert response.status_code == 200, response.get_json()
assert saved['clock']['enabled'] is expected
def test_a_starlark_app_gets_the_coerced_value(self, api_v3_client, api_v3_module):
toggle = MagicMock(return_value=({'status': 'success'}, 200))
with patch('web_interface.blueprints.api_v3.plugins._toggle_starlark_app', toggle):
api_v3_client.post('/api/v3/plugins/toggle',
json={'plugin_id': 'starlark:clock', 'enabled': 'false'})
toggle.assert_called_once_with('clock', False)
class TestOnDemandStart:
@pytest.fixture
def service(self, api_v3_module):
api_v3_module.api_v3.plugin_catalog = None
api_v3_module.api_v3.config_manager = None
# The display answers the control socket (the only way in).
with patch("web_interface.blueprints.api_v3.display.control_client.on_demand_start",
side_effect=lambda request_id, *a: {"accepted": True}), \
patch("web_interface.blueprints.api_v3.display._get_display_service_status",
return_value={"active": True}), \
patch("web_interface.blueprints.api_v3.display._stop_display_service") as stop, \
patch("web_interface.blueprints.api_v3.display._ensure_display_service_running",
return_value={"active": True}) as ensure:
yield stop, ensure
def test_string_false_neither_pins_nor_restarts(self, api_v3_client, service):
stop, ensure = service
response = api_v3_client.post('/api/v3/display/on-demand/start', json={
'plugin_id': 'weather', 'pinned': 'false', 'start_service': 'false'})
assert response.status_code == 200, response.get_json()
assert response.get_json()['data']['pinned'] is False
stop.assert_not_called()
ensure.assert_not_called()
def test_string_true_pins(self, api_v3_client, service):
response = api_v3_client.post('/api/v3/display/on-demand/start', json={
'plugin_id': 'weather', 'pinned': 'true', 'start_service': False})
assert response.get_json()['data']['pinned'] is True