Files
LEDMatrix/test/test_api_v3_bool_coercion.py
T
ChuckandClaude Opus 5.5 7ab6fb1aff refactor(web): read plugins through a PluginCatalog; only the display runs them (#688)
The web process built its own PluginManager and loaded plugins into itself:
store installs and updates loaded or reloaded a web-side copy, and config
saves and enable/disable called on_config_change, on_enable and on_disable
on it. None of that reached the panel, and /plugins/installed reported
runtime state from those copies.

- Add PluginCatalog (src/plugin_system/plugin_catalog.py): manifests,
  directories, display modes, installed version, schema and config reads,
  with no way to run a plugin. app.py and both blueprints use it; the
  plugin_manager blueprint attribute is gone.
- Remove every lifecycle call from the web routes. Config changes already
  reach the display through ConfigService (on_config_change) and the
  enabled-set reconcile.
- Health and metrics readers move to api_v3.health_tracker /
  resource_monitor. /plugins/installed reports loaded/state/error_info as
  null (the display does not publish them) and enabled by the display's
  rule.
- Store install, update and uninstall answer restart_required when the
  running display will not pick the change up by itself
  (display_restart_required). The restart banner follows the flag via
  window.noteRestartRequired instead of the /config/main URL heuristic;
  /config/main now sends restart_required: true.
- The one remaining in-process import of plugin code (Starlark helper
  modules, oauth_flow action scripts) goes through
  _import_plugin_code_in_web_process() until a web-entry contract.
- /plugins/installed reports vegas_participation (from #682) from the
  user's setting or the manifest, with vegas_participation_source; when
  only the plugin's code decides it, null with source 'runtime', since the
  web process no longer has plugin instances to ask.
- Check & Update All keeps its restart flags when the final list refresh
  fails, and asks for a restart when an enabled plugin's first request got
  no answer and the re-sent one found it up to date.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 10:39:44 -04:00

77 lines
3.4 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
with 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