ADR-013 Phase 0: plugin lifecycle groundwork (validate, apply-profile, dependency fixes)

This commit is contained in:
2026-07-18 18:08:48 -04:00
parent 61514b61a8
commit 41f5dbf5e0
7 changed files with 533 additions and 39 deletions

View File

@@ -149,6 +149,143 @@ def test_enable_seeds_plugin_permissions(manager, app, db):
assert Permission.query.filter_by(name='alpha.view').count() == 1
# --- ADR-013 Phase 0: reverse-deps from disk, cycles, upgrade-all, profiles --
def test_disable_blocked_by_unloaded_dependent(manager):
"""alpha cannot be disabled while an enabled dependent exists, even when
that dependent was never loaded as an instance (manifest read from disk)."""
# Mark both enabled directly in the registry WITHOUT loading beta.
manager.registry.enable('alpha')
manager.registry.enable('beta')
assert manager.loader.get_loaded_plugin('beta') is None
# The old loaded-instance-only check would wrongly allow this.
assert manager.disable_plugin('alpha') is False
assert manager.registry.is_enabled('alpha')
def test_uninstall_blocked_by_disabled_dependent(manager):
"""An installed-but-DISABLED dependent still blocks uninstalling its
dependency (uninstall reads all installed manifests, not enabled instances)."""
assert manager.registry.is_installed('beta')
assert not manager.registry.is_enabled('beta')
assert manager.uninstall_plugin('alpha') is False
assert manager.registry.is_installed('alpha')
def test_dependency_cycle_detected(tmp_path):
"""A -> B -> A raises instead of looping or silently dropping a plugin."""
from shopdb.exceptions import PluginDependencyError
plugins_dir = tmp_path / 'plugins'
plugins_dir.mkdir()
_write_plugin(plugins_dir, 'aa', deps=['bb'])
_write_plugin(plugins_dir, 'bb', deps=['aa'])
loader = PluginLoader(plugins_dir, PluginRegistry(tmp_path / 'plugins.json'))
with pytest.raises(PluginDependencyError):
loader._sort_by_dependencies(['aa', 'bb'])
def test_upgrade_all_only_touches_registered(manager, tmp_path):
"""upgrade-all migrates only ADOPTED plugins; an unregistered folder on
disk is never migrated as a side effect."""
_write_plugin(tmp_path / 'plugins', 'gamma') # on disk, not registered
class StubMigrationManager:
def has_pending_migrations(self, name):
return False
manager.migration_manager = StubMigrationManager()
results = manager.upgrade_all_plugins()
assert set(results) == {'alpha', 'beta'}
assert 'gamma' not in results
def test_apply_profile_enables_dependency_closure(manager):
"""Naming only the dependent enables it AND its dependency, in order."""
result = manager.apply_profile(['beta'])
assert manager.registry.is_enabled('alpha')
assert manager.registry.is_enabled('beta')
assert 'alpha' in result['enabled']
assert 'beta' in result['enabled']
def test_apply_profile_installs_and_enables_fresh_plugin(manager, tmp_path):
"""A plugin on disk but not yet registered is installed AND enabled."""
_write_plugin(tmp_path / 'plugins', 'gamma')
class StubMigrationManager:
def has_pending_migrations(self, name):
return False
def run_plugin_migrations(self, name, revision='head'):
return True
def has_unapplied_migrations(self, name):
return False
manager.migration_manager = StubMigrationManager()
result = manager.apply_profile(['gamma'])
assert manager.registry.is_installed('gamma')
assert manager.registry.is_enabled('gamma')
assert 'gamma' in result['installed']
def test_apply_profile_rejects_unknown_plugin(manager):
"""A profile naming a plugin that is not on disk fails loudly."""
from shopdb.exceptions import PluginError
with pytest.raises(PluginError):
manager.apply_profile(['doesnotexist'])
def test_manifest_schema_check():
"""The no-dependency manifest schema check flags missing/mistyped fields
and accepts a valid manifest (with unknown extra fields)."""
from shopdb.plugins.cli import _check_against_schema, _load_manifest_schema
schema = _load_manifest_schema()
good = {'name': 'x', 'version': '1.0.0', 'description': 'd',
'dependencies': [], 'tier': 'optional', 'somefuture': 42}
assert _check_against_schema(good, schema) == []
bad = {'name': 'x', 'version': '1.0.0', 'description': 'd',
'dependencies': 'notalist', 'tier': 'bogus',
'default_enabled': 'yes'}
errors = _check_against_schema(bad, schema)
assert any('dependencies' in e for e in errors)
assert any('tier' in e for e in errors)
assert any('default_enabled' in e for e in errors)
assert any('version' in e for e in _check_against_schema({'name': 'x'}, schema))
def test_all_bundled_manifests_match_schema(app):
"""Every bundled plugin's manifest passes the schema - the schema must
describe reality, not an idealized shape."""
from shopdb.plugins.cli import _check_against_schema, _load_manifest_schema
schema = _load_manifest_schema()
pm = app.extensions['plugin_manager']
for name in pm.loader.discover_plugins():
manifest = pm.loader.load_manifest(name)
assert _check_against_schema(manifest, schema) == [], name
def test_validate_bundled_plugin_passes(app):
"""`flask plugin validate <bundled>` exits clean on a real plugin."""
runner = app.test_cli_runner()
result = runner.invoke(args=['plugin', 'validate', 'printers'])
assert result.exit_code in (0, None), result.output
assert 'valid' in result.output
# --- Seed idempotency -------------------------------------------------------
def test_seed_settings_is_idempotent(app, db):