import os
import json
import pytest
import importlib

PLUGINS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))

@pytest.mark.parametrize("plugin", ["auth", "admin", "plant", "cli"])
def test_plugin_metadata_and_init(plugin):
    plugin_path = os.path.join(PLUGINS_DIR, plugin)
    meta_path = os.path.join(plugin_path, 'plugin.json')
    assert os.path.isfile(meta_path), f"plugin.json missing for {plugin}"
    meta = json.loads(open(meta_path).read())
    for key in ("name", "version", "description"):
        assert key in meta, f"{key} missing in {plugin}/plugin.json"
    init_path = os.path.join(plugin_path, '__init__.py')
    if os.path.exists(init_path):
        spec = importlib.util.spec_from_file_location(f"plugins.{plugin}", init_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        assert hasattr(module, "register_cli"), f"register_cli missing in {plugin}/__init__.py"
