@@ -2619,3 +2619,73 @@ def untyped_callback(event):
26192619
26202620 with pytest .raises (ValueError , match = "cannot infer event type" ):
26212621 agent .add_hook (untyped_callback )
2622+
2623+
2624+ def test_agent_plugins_sync_initialization ():
2625+ """Test that plugins with sync init_plugin are initialized correctly."""
2626+ plugin_mock = unittest .mock .Mock ()
2627+ plugin_mock .name = "test-plugin"
2628+ plugin_mock .init_plugin = unittest .mock .Mock ()
2629+
2630+ agent = Agent (
2631+ model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "response" }]}]),
2632+ plugins = [plugin_mock ],
2633+ )
2634+
2635+ plugin_mock .init_plugin .assert_called_once_with (agent )
2636+
2637+
2638+ def test_agent_plugins_async_initialization ():
2639+ """Test that plugins with async init_plugin are initialized correctly."""
2640+ plugin_mock = unittest .mock .Mock ()
2641+ plugin_mock .name = "async-plugin"
2642+ plugin_mock .init_plugin = unittest .mock .AsyncMock ()
2643+
2644+ agent = Agent (
2645+ model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "response" }]}]),
2646+ plugins = [plugin_mock ],
2647+ )
2648+
2649+ plugin_mock .init_plugin .assert_called_once_with (agent )
2650+
2651+
2652+ def test_agent_plugins_multiple_in_order ():
2653+ """Test that multiple plugins are initialized in order."""
2654+ call_order = []
2655+
2656+ plugin1 = unittest .mock .Mock ()
2657+ plugin1 .name = "plugin1"
2658+ plugin1 .init_plugin = unittest .mock .Mock (side_effect = lambda agent : call_order .append ("plugin1" ))
2659+
2660+ plugin2 = unittest .mock .Mock ()
2661+ plugin2 .name = "plugin2"
2662+ plugin2 .init_plugin = unittest .mock .Mock (side_effect = lambda agent : call_order .append ("plugin2" ))
2663+
2664+ Agent (
2665+ model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "response" }]}]),
2666+ plugins = [plugin1 , plugin2 ],
2667+ )
2668+
2669+ assert call_order == ["plugin1" , "plugin2" ]
2670+
2671+
2672+ def test_agent_plugins_can_register_hooks ():
2673+ """Test that plugins can register hooks during initialization."""
2674+ hook_called = []
2675+
2676+ class TestPlugin :
2677+ name = "hook-plugin"
2678+
2679+ def init_plugin (self , agent ):
2680+ def hook_callback (event : BeforeModelCallEvent ):
2681+ hook_called .append (True )
2682+
2683+ agent .add_hook (hook_callback )
2684+
2685+ agent = Agent (
2686+ model = MockedModelProvider ([{"role" : "assistant" , "content" : [{"text" : "response" }]}]),
2687+ plugins = [TestPlugin ()],
2688+ )
2689+
2690+ agent ("test" )
2691+ assert len (hook_called ) == 1
0 commit comments