|
| 1 | +"""Tests for experimental config_to_agent function.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from strands.experimental import config_to_agent |
| 10 | + |
| 11 | + |
| 12 | +def test_config_to_agent_with_dict(): |
| 13 | + """Test config_to_agent can be created with dict config.""" |
| 14 | + config = {"model": "test-model"} |
| 15 | + agent = config_to_agent(config) |
| 16 | + assert agent.model.config["model_id"] == "test-model" |
| 17 | + |
| 18 | + |
| 19 | +def test_config_to_agent_with_system_prompt(): |
| 20 | + """Test config_to_agent handles system prompt correctly.""" |
| 21 | + config = {"model": "test-model", "prompt": "Test prompt"} |
| 22 | + agent = config_to_agent(config) |
| 23 | + assert agent.system_prompt == "Test prompt" |
| 24 | + |
| 25 | + |
| 26 | +def test_config_to_agent_with_tools_list(): |
| 27 | + """Test config_to_agent handles tools list without failing.""" |
| 28 | + # Use a simple test that doesn't require actual tool loading |
| 29 | + config = {"model": "test-model", "tools": []} |
| 30 | + agent = config_to_agent(config) |
| 31 | + assert agent.model.config["model_id"] == "test-model" |
| 32 | + |
| 33 | + |
| 34 | +def test_config_to_agent_with_kwargs_override(): |
| 35 | + """Test that kwargs can override config values.""" |
| 36 | + config = {"model": "test-model", "prompt": "Config prompt"} |
| 37 | + agent = config_to_agent(config, system_prompt="Override prompt") |
| 38 | + assert agent.system_prompt == "Override prompt" |
| 39 | + |
| 40 | + |
| 41 | +def test_config_to_agent_file_prefix_required(): |
| 42 | + """Test that file paths without file:// prefix work.""" |
| 43 | + import json |
| 44 | + import tempfile |
| 45 | + |
| 46 | + config_data = {"model": "test-model"} |
| 47 | + temp_path = "" |
| 48 | + |
| 49 | + # We need to create files like this for windows compatibility |
| 50 | + try: |
| 51 | + with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False) as f: |
| 52 | + json.dump(config_data, f) |
| 53 | + f.flush() |
| 54 | + temp_path = f.name |
| 55 | + |
| 56 | + agent = config_to_agent(temp_path) |
| 57 | + assert agent.model.config["model_id"] == "test-model" |
| 58 | + finally: |
| 59 | + # Clean up the temporary file |
| 60 | + if os.path.exists(temp_path): |
| 61 | + os.remove(temp_path) |
| 62 | + |
| 63 | + |
| 64 | +def test_config_to_agent_file_prefix_valid(): |
| 65 | + """Test that file:// prefix is properly handled.""" |
| 66 | + config_data = {"model": "test-model", "prompt": "Test prompt"} |
| 67 | + temp_path = "" |
| 68 | + |
| 69 | + try: |
| 70 | + with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False) as f: |
| 71 | + json.dump(config_data, f) |
| 72 | + f.flush() |
| 73 | + temp_path = f.name |
| 74 | + |
| 75 | + agent = config_to_agent(f"file://{temp_path}") |
| 76 | + assert agent.model.config["model_id"] == "test-model" |
| 77 | + assert agent.system_prompt == "Test prompt" |
| 78 | + finally: |
| 79 | + # Clean up the temporary file |
| 80 | + if os.path.exists(temp_path): |
| 81 | + os.remove(temp_path) |
| 82 | + |
| 83 | + |
| 84 | +def test_config_to_agent_file_not_found(): |
| 85 | + """Test that FileNotFoundError is raised for missing files.""" |
| 86 | + with pytest.raises(FileNotFoundError, match="Configuration file not found"): |
| 87 | + config_to_agent("/nonexistent/path/config.json") |
| 88 | + |
| 89 | + |
| 90 | +def test_config_to_agent_invalid_json(): |
| 91 | + """Test that JSONDecodeError is raised for invalid JSON.""" |
| 92 | + try: |
| 93 | + with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False) as f: |
| 94 | + f.write("invalid json content") |
| 95 | + temp_path = f.name |
| 96 | + |
| 97 | + with pytest.raises(json.JSONDecodeError): |
| 98 | + config_to_agent(temp_path) |
| 99 | + finally: |
| 100 | + # Clean up the temporary file |
| 101 | + if os.path.exists(temp_path): |
| 102 | + os.remove(temp_path) |
| 103 | + |
| 104 | + |
| 105 | +def test_config_to_agent_invalid_config_type(): |
| 106 | + """Test that ValueError is raised for invalid config types.""" |
| 107 | + with pytest.raises(ValueError, match="Config must be a file path string or dictionary"): |
| 108 | + config_to_agent(123) |
| 109 | + |
| 110 | + |
| 111 | +def test_config_to_agent_with_name(): |
| 112 | + """Test config_to_agent handles agent name.""" |
| 113 | + config = {"model": "test-model", "name": "TestAgent"} |
| 114 | + agent = config_to_agent(config) |
| 115 | + assert agent.name == "TestAgent" |
| 116 | + |
| 117 | + |
| 118 | +def test_config_to_agent_ignores_none_values(): |
| 119 | + """Test that None values in config are ignored.""" |
| 120 | + config = {"model": "test-model", "prompt": None, "name": None} |
| 121 | + agent = config_to_agent(config) |
| 122 | + assert agent.model.config["model_id"] == "test-model" |
| 123 | + # Agent should use its defaults for None values |
| 124 | + |
| 125 | + |
| 126 | +def test_config_to_agent_validation_error_invalid_field(): |
| 127 | + """Test that invalid fields raise validation errors.""" |
| 128 | + config = {"model": "test-model", "invalid_field": "value"} |
| 129 | + with pytest.raises(ValueError, match="Configuration validation error"): |
| 130 | + config_to_agent(config) |
| 131 | + |
| 132 | + |
| 133 | +def test_config_to_agent_validation_error_wrong_type(): |
| 134 | + """Test that wrong field types raise validation errors.""" |
| 135 | + config = {"model": "test-model", "tools": "not-a-list"} |
| 136 | + with pytest.raises(ValueError, match="Configuration validation error"): |
| 137 | + config_to_agent(config) |
| 138 | + |
| 139 | + |
| 140 | +def test_config_to_agent_validation_error_invalid_tool_item(): |
| 141 | + """Test that invalid tool items raise validation errors.""" |
| 142 | + config = {"model": "test-model", "tools": ["valid-tool", 123]} |
| 143 | + with pytest.raises(ValueError, match="Configuration validation error"): |
| 144 | + config_to_agent(config) |
| 145 | + |
| 146 | + |
| 147 | +def test_config_to_agent_validation_error_invalid_tool(): |
| 148 | + """Test that invalid tools raise helpful error messages.""" |
| 149 | + config = {"model": "test-model", "tools": ["nonexistent_tool"]} |
| 150 | + with pytest.raises(ValueError, match="Failed to load tool nonexistent_tool"): |
| 151 | + config_to_agent(config) |
| 152 | + |
| 153 | + |
| 154 | +def test_config_to_agent_validation_error_missing_module(): |
| 155 | + """Test that missing modules raise helpful error messages.""" |
| 156 | + config = {"model": "test-model", "tools": ["nonexistent.module.tool"]} |
| 157 | + with pytest.raises(ValueError, match="Failed to load tool nonexistent.module.tool"): |
| 158 | + config_to_agent(config) |
| 159 | + |
| 160 | + |
| 161 | +def test_config_to_agent_validation_error_missing_function(): |
| 162 | + """Test that missing functions in existing modules raise helpful error messages.""" |
| 163 | + config = {"model": "test-model", "tools": ["json.nonexistent_function"]} |
| 164 | + with pytest.raises(ValueError, match="Failed to load tool json.nonexistent_function"): |
| 165 | + config_to_agent(config) |
| 166 | + |
| 167 | + |
| 168 | +def test_config_to_agent_with_tool(): |
| 169 | + """Test that missing functions in existing modules raise helpful error messages.""" |
| 170 | + config = {"model": "test-model", "tools": ["tests.fixtures.say_tool:say"]} |
| 171 | + agent = config_to_agent(config) |
| 172 | + assert "say" in agent.tool_names |
0 commit comments