# pylint: disable=redefined-outer-name from pathlib import Path import logging import importlib import pytest from shepherd.agent import core from shepherd.agent import plugin @pytest.fixture(autouse=True) def fresh_agent_state(): plugin.unload_plugins() importlib.reload(core) @pytest.fixture def basic_config(tmp_path): def_conf_file = tmp_path / "shepherd_default.toml" def_conf_file.write_text(""" [shepherd] name = "shepherd-test" """) return def_conf_file @pytest.fixture def custom_config(tmp_path): def_conf_file = tmp_path / "shepherd_default.toml" def_conf_file.write_text(""" [shepherd] name = "shepherd-test" custom_config_path = "shepherd_custom.toml" """) custom_conf_file = tmp_path / "shepherd_custom.toml" custom_conf_file.write_text(""" [shepherd] name = "shepherd-custom" """) return def_conf_file @pytest.fixture def plugin_config(tmp_path, request): plugin_dir = Path(request.fspath.dirname)/'assets' def_conf_file = tmp_path / "shepherd_default.toml" def_conf_file.write_text(F""" [shepherd] name = "shepherd-test" plugin_dir = "{plugin_dir}" [classtestplugin] spec1 = "asdf" """) return def_conf_file def test_local_agent(basic_config): core.Agent(basic_config) def test_local_compiled_conf(basic_config): core.Agent(basic_config) compiled_conf = (basic_config.parent / "compiled-config.toml").read_text() assert 'name = "shepherd-test"' in compiled_conf # Paths should be resolved to absolute assert 'plugin_dir = "/' in compiled_conf assert 'Compiled Shepherd config' in compiled_conf def test_custom_conf_load(custom_config): agent = core.Agent(custom_config) assert agent.core_config["name"] == "shepherd-custom" def test_new_device_trigger(custom_config, caplog): caplog.set_level(logging.INFO) (custom_config.parent / "shepherd.new").touch() core.Agent(custom_config) assert "'new device' mode enabled" in caplog.text assert (custom_config.parent / "shepherd.identity").exists() def test_local_agent_start(basic_config): agent = core.Agent(basic_config) agent.start() def test_local_agent_plugin_start(plugin_config): agent = core.Agent(plugin_config) agent.start() assert agent.interface_functions["classtestplugin"].instance_method( 3) == "instance method 3" def test_core_interface(plugin_config): agent = core.Agent(plugin_config) agent.start() plugin_interface = agent.plugin_interfaces["classtestplugin"] assert plugin_interface.plugins["shepherd"].device_name() == "shepherd-test" assert plugin_interface.plugins["shepherd"].root_dir() == str(plugin_config.parent)