"""Step definitions for network module coverage tests.""" import asyncio import tempfile from pathlib import Path import yaml from behave import given, then, when from cleveragents.network import AgentNetwork @given("I have an AgentNetwork instance") def step_have_network_instance(context): """Prepare for AgentNetwork creation.""" context.network = None @when("I initialize it without config files") def step_init_without_config(context): """Initialize AgentNetwork without config files.""" context.network = AgentNetwork() @then("the config_manager should be created") def step_config_manager_created(context): """Verify config_manager is created.""" assert context.network.config_manager is not None assert hasattr(context.network.config_manager, "load_files") @then("the agent_factory should be None") def step_agent_factory_none(context): """Verify agent_factory is None.""" assert context.network.agent_factory is None @given("I have config files available") def step_have_config_files(context): """Create temporary config files.""" context.temp_dir = tempfile.mkdtemp() context.config_file = Path(context.temp_dir) / "test_config.yaml" # Create a simple config file with agents as dictionary and routes config_data = { "agents": {"test_agent": {"type": "llm", "model": "test-model"}}, "routes": {"main": {"input": "test_agent", "output": "test_agent"}}, "cleveragents": {"default_router": "main"}, } with open(context.config_file, "w") as f: yaml.dump(config_data, f) @when("I initialize AgentNetwork with config files") def step_init_with_config(context): """Initialize AgentNetwork with config files.""" context.network = AgentNetwork(config_files=[context.config_file]) context.init_called = True @then("the config_manager should load the files") def step_config_loaded(context): """Verify config files are loaded.""" assert context.network.config_manager is not None # The config should have been loaded assert context.init_called @then("the config should be validated") def step_config_validated(context): """Verify config is validated.""" # If we got here without exception, validation passed assert context.network is not None @given("I have an initialized AgentNetwork") def step_initialized_network(context): """Create an initialized AgentNetwork.""" context.network = AgentNetwork() context.exception = None @when("I try to process a message") def step_try_process_message(context): """Try to process a message through the network.""" try: # Use asyncio to run the async method loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) result = loop.run_until_complete(context.network.process("test message")) context.result = result except NotImplementedError as e: context.exception = e except Exception as e: context.exception = e finally: loop.close() @when("I try to process a message with context") def step_try_process_with_context(context): """Try to process a message with context.""" try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) result = loop.run_until_complete(context.network.process("test message", context={"key": "value"})) context.result = result except NotImplementedError as e: context.exception = e except Exception as e: context.exception = e finally: loop.close() @then("a NotImplementedError should be raised") def step_not_implemented_raised(context): """Verify NotImplementedError was raised.""" assert isinstance( context.exception, NotImplementedError ), f"Expected NotImplementedError, got {type(context.exception)}: {context.exception}" @then("the error should indicate reactive system not implemented") def step_error_mentions_reactive(context): """Verify error message mentions reactive system.""" assert "reactive system" in str( context.exception ), f"Error message doesn't mention 'reactive system': {context.exception}" @then("the error message should be appropriate") def step_appropriate_error(context): """Verify error message is appropriate.""" assert context.exception is not None assert len(str(context.exception)) > 0 @given("I want verbose output") def step_want_verbose(context): """Set up for verbose initialization.""" context.verbose = True @when("I initialize AgentNetwork with verbose flag") def step_init_with_verbose(context): """Initialize with verbose flag.""" context.network = AgentNetwork(_verbose=True) @then("the network should be created successfully") def step_network_created(context): """Verify network was created.""" assert context.network is not None assert isinstance(context.network, AgentNetwork) @then("the verbose flag should be handled") def step_verbose_handled(context): """Verify verbose flag was handled.""" # The network should be created without errors assert context.network.config_manager is not None