mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-04 14:25:08 +00:00
fix(composer): register composer_bp and close a second empty-block gap
composer_bp defined the whole Plugin Composer feature -- the /composer/ page and all its API routes -- but web_interface/app.py never imported or registered it, so every composer URL 404'd in the running app (every composer test builds its own minimal Flask app and registers the blueprint directly, which is why this went unnoticed). Wire it up the same way pages_v3/api_v3 are: import, set config_manager/plugin_manager/ plugins_dir/project_root, register_blueprint(url_prefix='/composer') -- matching the prefix composer.html and composer-app.js already hardcode. Also closes the other still-open half of a CodeRabbit finding: manager.py.j2 already guards element types the template has no branch for, but a dynamic_text element with binding.source other than 'config' hit the same empty-if-block bug one level deeper (its own inner if produced nothing). Added the same pass fallback. Verified both against current code before fixing; the other 8 findings from that review were already fixed in earlier commits on this branch. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""composer_bp must actually be registered on the Flask app.
|
||||
|
||||
web_interface/blueprints/composer.py defines a full blueprint -- the
|
||||
/composer/ page, /composer/api/generate, /composer/api/install, etc. -- but
|
||||
nothing in web_interface/app.py imported or registered it. pages_v3 and
|
||||
api_v3 both go through the same "import, wire managers, register_blueprint"
|
||||
sequence; composer_bp had none of the three. The routes existed and worked
|
||||
in isolation (every other composer test builds its own minimal Flask app
|
||||
and registers composer_bp directly), but in the real running app every
|
||||
composer URL -- including the page itself -- was a 404, and the frontend's
|
||||
hardcoded '/composer/api/...' fetches (composer-app.js) had nothing to
|
||||
reach.
|
||||
|
||||
app.py has import-time side effects (it loads config, builds managers, and
|
||||
in some configurations starts background threads), so this scans the source
|
||||
text rather than importing the module -- the same approach already used for
|
||||
test_composer_js_contracts.py's structural checks.
|
||||
"""
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
APP_PY = Path(__file__).resolve().parent.parent / "web_interface" / "app.py"
|
||||
|
||||
|
||||
def _source() -> str:
|
||||
return APP_PY.read_text()
|
||||
|
||||
|
||||
def test_composer_blueprint_is_imported():
|
||||
src = _source()
|
||||
assert re.search(
|
||||
r"from web_interface\.blueprints\.composer import composer_bp", src
|
||||
), "app.py never imports composer_bp"
|
||||
|
||||
|
||||
def test_composer_blueprint_is_registered_under_composer_prefix():
|
||||
src = _source()
|
||||
match = re.search(
|
||||
r"app\.register_blueprint\(\s*composer_bp\s*,\s*url_prefix\s*=\s*"
|
||||
r"['\"](?P<prefix>[^'\"]*)['\"]",
|
||||
src,
|
||||
)
|
||||
assert match, "app.py never calls app.register_blueprint(composer_bp, ...)"
|
||||
# composer.html and composer-app.js hardcode '/composer/api/...' and
|
||||
# '/composer/api/fonts/...' -- any other prefix serves a page whose own
|
||||
# fetches and @font-face all 404.
|
||||
assert match.group("prefix") == "/composer"
|
||||
|
||||
|
||||
def test_composer_blueprint_managers_are_wired_before_registration():
|
||||
"""Registered with no managers set means every route 503s or crashes.
|
||||
|
||||
composer.py initialises config_manager/plugin_manager/plugins_dir/
|
||||
project_root to None at import time specifically so app.py can set them
|
||||
-- the same pattern api_v3 and pages_v3 use.
|
||||
"""
|
||||
src = _source()
|
||||
register_match = re.search(r"app\.register_blueprint\(\s*composer_bp", src)
|
||||
assert register_match, "app.py never registers composer_bp"
|
||||
before_registration = src[: register_match.start()]
|
||||
for attr in ("config_manager", "plugin_manager", "plugins_dir", "project_root"):
|
||||
assert re.search(rf"composer_bp\.{attr}\s*=", before_registration), (
|
||||
f"composer_bp.{attr} is never set before registration"
|
||||
)
|
||||
@@ -78,3 +78,24 @@ def test_template_closes_the_branch_chain_with_a_fallback():
|
||||
text = TEMPLATE.read_text()
|
||||
assert "{% else %}" in text
|
||||
assert "pass # element type" in text
|
||||
|
||||
|
||||
@pytest.mark.parametrize("wrapper", [
|
||||
{"minWidth": 64},
|
||||
{"blink": True},
|
||||
{"minWidth": 64, "blink": True},
|
||||
])
|
||||
def test_dynamic_text_with_non_config_binding_does_not_break_generation(wrapper):
|
||||
"""dynamic_text only renders a body for binding.source == 'config'.
|
||||
|
||||
_preprocess_elements accepts any string as binding.source (it just
|
||||
defaults a missing one to 'config'), so a 'live' or 'sensor' source --
|
||||
anything a client sends that isn't literally 'config' -- hit the same
|
||||
empty-if-block bug as an undrawable element type, just one level deeper:
|
||||
the branch is taken, but its own inner `if` produced nothing.
|
||||
"""
|
||||
element = {"type": "dynamic_text", "x": 0, "y": 0, "color": "#ffffff",
|
||||
"binding": {"source": "live", "key": "temperature"}, **wrapper}
|
||||
files = generate(element) # must not raise ComposerInputError
|
||||
assert "manager.py" in files
|
||||
assert 'binding_source "live" draws nothing' in files["manager.py"]
|
||||
|
||||
Reference in New Issue
Block a user