mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-04 06:15:09 +00:00
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>
165 lines
6.7 KiB
Python
165 lines
6.7 KiB
Python
"""Tests that api_v3 routes find installed plugins before anything else has.
|
|
|
|
The web process discovers plugins lazily (web_interface/app.py): nothing scans
|
|
the plugins directory at startup, and plugin_manifests stays empty until some
|
|
endpoint calls discover_plugins(). Routes that consulted plugin_manifests
|
|
without discovering therefore misbehaved for as long as nothing else had run.
|
|
Measured on a rig after restarting ledmatrix-web:
|
|
|
|
POST /api/v3/display/on-demand/start {"plugin_id": "ledmatrix-stocks"}
|
|
-> 404 "Plugin ledmatrix-stocks not found", for over three minutes,
|
|
until GET /api/v3/plugins/installed happened to discover plugins.
|
|
|
|
The browser UI loads the plugin list first, so people rarely saw it; API-only
|
|
callers (the Home Assistant MQTT bridge, scripts) saw it after every restart.
|
|
/plugins/toggle answered the same 404, and /config/main was worse: an
|
|
undiscovered plugin section skipped secret separation and wrote its API key
|
|
into config.json in plain text.
|
|
|
|
A real PluginCatalog over a temporary plugins directory, so "empty until
|
|
discovered" is the real behaviour rather than a mock's.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.config_manager import ConfigManager # noqa: E402
|
|
from src.plugin_system.plugin_catalog import PluginCatalog # noqa: E402
|
|
from src.plugin_system.schema_manager import SchemaManager # noqa: E402
|
|
from test._api_v3_test_helpers import api_v3_client, api_v3_module # noqa: F401,E402
|
|
|
|
PLUGIN_ID = 'ledmatrix-stocks'
|
|
MODE = 'stocks'
|
|
|
|
|
|
def _install(plugins_dir, plugin_id, modes, schema=None):
|
|
plugin_dir = plugins_dir / plugin_id
|
|
plugin_dir.mkdir(parents=True)
|
|
(plugin_dir / 'manifest.json').write_text(json.dumps({
|
|
'id': plugin_id, 'name': plugin_id, 'version': '1.0.0',
|
|
'entry_point': 'manager.py', 'class_name': 'Plugin',
|
|
'display_modes': modes,
|
|
}))
|
|
(plugin_dir / 'manager.py').write_text('')
|
|
if schema is not None:
|
|
(plugin_dir / 'config_schema.json').write_text(json.dumps(schema))
|
|
|
|
|
|
@pytest.fixture
|
|
def plugins_dir(tmp_path):
|
|
path = tmp_path / 'plugin-repos'
|
|
path.mkdir()
|
|
_install(path, PLUGIN_ID, [MODE], schema={
|
|
'type': 'object',
|
|
'properties': {
|
|
'enabled': {'type': 'boolean'},
|
|
'symbols': {'type': 'string'},
|
|
'api_key': {'type': 'string', 'x-secret': True},
|
|
},
|
|
})
|
|
return path
|
|
|
|
|
|
@pytest.fixture
|
|
def fresh_web_process(api_v3_module, plugins_dir):
|
|
"""The web process right after a restart: nothing discovered yet."""
|
|
catalog = PluginCatalog(plugins_dir=plugins_dir)
|
|
assert not catalog.plugin_manifests
|
|
api_v3_module.api_v3.plugin_catalog = catalog
|
|
return catalog
|
|
|
|
|
|
@pytest.fixture
|
|
def display_service(api_v3_module):
|
|
"""Keep on-demand start away from systemctl and the real cache."""
|
|
cache = api_v3_module.api_v3.cache_manager = MagicMock()
|
|
with patch('web_interface.blueprints.api_v3.display._get_display_service_status') as status:
|
|
status.return_value = {'active': True}
|
|
yield cache
|
|
|
|
|
|
def _start(client, **body):
|
|
body.setdefault('start_service', False)
|
|
return client.post('/api/v3/display/on-demand/start', json=body)
|
|
|
|
|
|
class TestOnDemandStart:
|
|
def test_by_plugin_id(self, api_v3_client, fresh_web_process, display_service):
|
|
response = _start(api_v3_client, plugin_id=PLUGIN_ID)
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
request = display_service.set.call_args.args[1]
|
|
assert (request['plugin_id'], request['mode']) == (PLUGIN_ID, MODE)
|
|
|
|
def test_by_mode_alone(self, api_v3_client, fresh_web_process, display_service):
|
|
response = _start(api_v3_client, mode=MODE)
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
assert display_service.set.call_args.args[1]['plugin_id'] == PLUGIN_ID
|
|
|
|
def test_a_plugin_installed_since_the_last_scan(
|
|
self, api_v3_client, fresh_web_process, plugins_dir, display_service):
|
|
fresh_web_process.discover_plugins()
|
|
_install(plugins_dir, 'ledmatrix-weather', ['weather'])
|
|
|
|
assert _start(api_v3_client, plugin_id='ledmatrix-weather').status_code == 200
|
|
assert _start(api_v3_client, mode=MODE).status_code == 200
|
|
|
|
def test_a_mode_installed_since_the_last_scan(
|
|
self, api_v3_client, fresh_web_process, plugins_dir, display_service):
|
|
fresh_web_process.discover_plugins()
|
|
_install(plugins_dir, 'ledmatrix-weather', ['weather'])
|
|
|
|
response = _start(api_v3_client, mode='weather')
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
|
|
def test_an_unknown_plugin_is_still_not_found(
|
|
self, api_v3_client, fresh_web_process, display_service):
|
|
assert _start(api_v3_client, plugin_id='no-such-plugin').status_code == 404
|
|
assert _start(api_v3_client, mode='no-such-mode').status_code == 404
|
|
display_service.set.assert_not_called()
|
|
|
|
|
|
def test_toggle_finds_the_plugin(api_v3_client, api_v3_module, fresh_web_process):
|
|
config_manager = api_v3_module.api_v3.config_manager
|
|
config_manager.load_config.return_value = {PLUGIN_ID: {'enabled': False}}
|
|
config_manager.save_config_atomic.return_value = MagicMock(
|
|
status=MagicMock(value='success'))
|
|
|
|
response = api_v3_client.post('/api/v3/plugins/toggle',
|
|
json={'plugin_id': PLUGIN_ID, 'enabled': True})
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
saved = config_manager.save_config_atomic.call_args.args[0]
|
|
assert saved[PLUGIN_ID]['enabled'] is True
|
|
|
|
|
|
def test_main_config_save_keeps_a_plugin_secret_out_of_config_json(
|
|
api_v3_client, api_v3_module, fresh_web_process, plugins_dir, tmp_path):
|
|
# /config/main validates a plugin section like POST /plugins/config does,
|
|
# so it needs a real schema manager rather than the helper's mock.
|
|
api_v3_module.api_v3.schema_manager = SchemaManager(plugins_dir=plugins_dir)
|
|
config_file = tmp_path / 'config.json'
|
|
config_file.write_text('{}')
|
|
secrets_file = tmp_path / 'config_secrets.json'
|
|
config_manager = ConfigManager(config_path=str(config_file),
|
|
secrets_path=str(secrets_file))
|
|
config_manager.template_path = str(tmp_path / 'no-template.json')
|
|
api_v3_module.api_v3.config_manager = config_manager
|
|
|
|
response = api_v3_client.post('/api/v3/config/main', json={
|
|
PLUGIN_ID: {'symbols': 'AAPL', 'api_key': 's3cret-key'},
|
|
})
|
|
|
|
assert response.status_code == 200, response.get_json()
|
|
assert 's3cret' not in config_file.read_text()
|
|
assert json.loads(secrets_file.read_text())[PLUGIN_ID]['api_key'] == 's3cret-key'
|
|
assert json.loads(config_file.read_text())[PLUGIN_ID]['symbols'] == 'AAPL'
|