|
| 1 | +"""Tests for the plugin system.""" |
| 2 | + |
| 3 | +import unittest.mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from strands.plugins import Plugin |
| 8 | +from strands.plugins.registry import _PluginRegistry |
| 9 | + |
| 10 | +# Plugin Protocol Tests |
| 11 | + |
| 12 | + |
| 13 | +def test_plugin_protocol_is_runtime_checkable(): |
| 14 | + """Test that Plugin Protocol is runtime checkable with isinstance.""" |
| 15 | + |
| 16 | + class MyPlugin: |
| 17 | + name = "my-plugin" |
| 18 | + |
| 19 | + def init_plugin(self, agent): |
| 20 | + pass |
| 21 | + |
| 22 | + plugin = MyPlugin() |
| 23 | + assert isinstance(plugin, Plugin) |
| 24 | + |
| 25 | + |
| 26 | +def test_plugin_protocol_sync_implementation(): |
| 27 | + """Test Plugin Protocol works with synchronous init_plugin.""" |
| 28 | + |
| 29 | + class SyncPlugin: |
| 30 | + name = "sync-plugin" |
| 31 | + |
| 32 | + def init_plugin(self, agent): |
| 33 | + agent.custom_attribute = "initialized by plugin" |
| 34 | + |
| 35 | + plugin = SyncPlugin() |
| 36 | + mock_agent = unittest.mock.Mock() |
| 37 | + |
| 38 | + # Verify the plugin matches the protocol |
| 39 | + assert isinstance(plugin, Plugin) |
| 40 | + assert plugin.name == "sync-plugin" |
| 41 | + |
| 42 | + # Execute init_plugin synchronously |
| 43 | + plugin.init_plugin(mock_agent) |
| 44 | + assert mock_agent.custom_attribute == "initialized by plugin" |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_plugin_protocol_async_implementation(): |
| 49 | + """Test Plugin Protocol works with asynchronous init_plugin.""" |
| 50 | + |
| 51 | + class AsyncPlugin: |
| 52 | + name = "async-plugin" |
| 53 | + |
| 54 | + async def init_plugin(self, agent): |
| 55 | + agent.custom_attribute = "initialized by async plugin" |
| 56 | + |
| 57 | + plugin = AsyncPlugin() |
| 58 | + mock_agent = unittest.mock.Mock() |
| 59 | + |
| 60 | + # Verify the plugin matches the protocol |
| 61 | + assert isinstance(plugin, Plugin) |
| 62 | + assert plugin.name == "async-plugin" |
| 63 | + |
| 64 | + # Execute init_plugin asynchronously |
| 65 | + await plugin.init_plugin(mock_agent) |
| 66 | + assert mock_agent.custom_attribute == "initialized by async plugin" |
| 67 | + |
| 68 | + |
| 69 | +def test_plugin_protocol_requires_name(): |
| 70 | + """Test that Plugin Protocol requires a name property.""" |
| 71 | + |
| 72 | + class PluginWithoutName: |
| 73 | + def init_plugin(self, agent): |
| 74 | + pass |
| 75 | + |
| 76 | + plugin = PluginWithoutName() |
| 77 | + # A class without 'name' should not pass isinstance check |
| 78 | + assert not isinstance(plugin, Plugin) |
| 79 | + |
| 80 | + |
| 81 | +def test_plugin_protocol_requires_init_plugin_method(): |
| 82 | + """Test that Plugin Protocol requires an init_plugin method.""" |
| 83 | + |
| 84 | + class PluginWithoutInitPlugin: |
| 85 | + name = "incomplete-plugin" |
| 86 | + |
| 87 | + plugin = PluginWithoutInitPlugin() |
| 88 | + # A class without 'init_plugin' should not pass isinstance check |
| 89 | + assert not isinstance(plugin, Plugin) |
| 90 | + |
| 91 | + |
| 92 | +def test_plugin_protocol_with_class_attribute_name(): |
| 93 | + """Test Plugin Protocol works when name is a class attribute.""" |
| 94 | + |
| 95 | + class PluginWithClassAttribute: |
| 96 | + name: str = "class-attr-plugin" |
| 97 | + |
| 98 | + def init_plugin(self, agent): |
| 99 | + pass |
| 100 | + |
| 101 | + plugin = PluginWithClassAttribute() |
| 102 | + assert isinstance(plugin, Plugin) |
| 103 | + assert plugin.name == "class-attr-plugin" |
| 104 | + |
| 105 | + |
| 106 | +def test_plugin_protocol_with_property_name(): |
| 107 | + """Test Plugin Protocol works when name is a property.""" |
| 108 | + |
| 109 | + class PluginWithProperty: |
| 110 | + @property |
| 111 | + def name(self): |
| 112 | + return "property-plugin" |
| 113 | + |
| 114 | + def init_plugin(self, agent): |
| 115 | + pass |
| 116 | + |
| 117 | + plugin = PluginWithProperty() |
| 118 | + assert isinstance(plugin, Plugin) |
| 119 | + assert plugin.name == "property-plugin" |
| 120 | + |
| 121 | + |
| 122 | +# _PluginRegistry Tests |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture |
| 126 | +def mock_agent(): |
| 127 | + """Create a mock agent for testing.""" |
| 128 | + return unittest.mock.Mock() |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def registry(mock_agent): |
| 133 | + """Create a fresh _PluginRegistry for each test.""" |
| 134 | + return _PluginRegistry(mock_agent) |
| 135 | + |
| 136 | + |
| 137 | +def test_plugin_registry_add_and_init_calls_init_plugin(registry, mock_agent): |
| 138 | + """Test adding a plugin calls its init_plugin method.""" |
| 139 | + |
| 140 | + class TestPlugin: |
| 141 | + name = "test-plugin" |
| 142 | + |
| 143 | + def __init__(self): |
| 144 | + self.initialized = False |
| 145 | + |
| 146 | + def init_plugin(self, agent): |
| 147 | + self.initialized = True |
| 148 | + agent.plugin_initialized = True |
| 149 | + |
| 150 | + plugin = TestPlugin() |
| 151 | + registry.add_and_init(plugin) |
| 152 | + |
| 153 | + assert plugin.initialized |
| 154 | + assert mock_agent.plugin_initialized |
| 155 | + |
| 156 | + |
| 157 | +def test_plugin_registry_add_duplicate_raises_error(registry, mock_agent): |
| 158 | + """Test that adding a duplicate plugin raises an error.""" |
| 159 | + |
| 160 | + class TestPlugin: |
| 161 | + name = "test-plugin" |
| 162 | + |
| 163 | + def init_plugin(self, agent): |
| 164 | + pass |
| 165 | + |
| 166 | + plugin1 = TestPlugin() |
| 167 | + plugin2 = TestPlugin() |
| 168 | + |
| 169 | + registry.add_and_init(plugin1) |
| 170 | + |
| 171 | + with pytest.raises(ValueError, match="plugin_name=<test-plugin> | plugin already registered"): |
| 172 | + registry.add_and_init(plugin2) |
| 173 | + |
| 174 | + |
| 175 | +def test_plugin_registry_add_and_init_with_async_plugin(registry, mock_agent): |
| 176 | + """Test that add_and_init handles async plugins using run_async.""" |
| 177 | + |
| 178 | + class AsyncPlugin: |
| 179 | + name = "async-plugin" |
| 180 | + |
| 181 | + def __init__(self): |
| 182 | + self.initialized = False |
| 183 | + |
| 184 | + async def init_plugin(self, agent): |
| 185 | + self.initialized = True |
| 186 | + agent.async_plugin_initialized = True |
| 187 | + |
| 188 | + plugin = AsyncPlugin() |
| 189 | + registry.add_and_init(plugin) |
| 190 | + |
| 191 | + assert plugin.initialized |
| 192 | + assert mock_agent.async_plugin_initialized |
0 commit comments