44import os
55import textwrap
66import unittest .mock
7- from unittest .mock import call
87
98import pytest
109from pydantic import BaseModel
1413from strands .agent import AgentResult
1514from strands .agent .conversation_manager .null_conversation_manager import NullConversationManager
1615from strands .agent .conversation_manager .sliding_window_conversation_manager import SlidingWindowConversationManager
17- from strands .experimental .hooks import AgentInitializedEvent , EndRequestEvent , StartRequestEvent
1816from strands .handlers .callback_handler import PrintingCallbackHandler , null_callback_handler
1917from strands .models .bedrock import DEFAULT_BEDROCK_MODEL_ID , BedrockModel
2018from strands .types .content import Messages
2119from strands .types .exceptions import ContextWindowOverflowException , EventLoopException
22- from tests .fixtures .mock_hook_provider import MockHookProvider
2320
2421
2522@pytest .fixture
@@ -40,38 +37,6 @@ def converse(*args, **kwargs):
4037 return mock
4138
4239
43- @pytest .fixture
44- def mock_hook_messages (mock_model , tool , agenerator ):
45- """Fixture which returns a standard set of events for verifying hooks."""
46- mock_model .mock_converse .side_effect = [
47- agenerator (
48- [
49- {
50- "contentBlockStart" : {
51- "start" : {
52- "toolUse" : {
53- "toolUseId" : "t1" ,
54- "name" : tool .tool_spec ["name" ],
55- },
56- },
57- },
58- },
59- {"contentBlockDelta" : {"delta" : {"toolUse" : {"input" : '{"random_string": "abcdEfghI123"}' }}}},
60- {"contentBlockStop" : {}},
61- {"messageStop" : {"stopReason" : "tool_use" }},
62- ],
63- ),
64- agenerator (
65- [
66- {"contentBlockDelta" : {"delta" : {"text" : "test text" }}},
67- {"contentBlockStop" : {}},
68- ],
69- ),
70- ]
71-
72- return mock_model .mock_converse
73-
74-
7540@pytest .fixture
7641def system_prompt (request ):
7742 return request .param if hasattr (request , "param" ) else "You are a helpful assistant."
@@ -166,11 +131,6 @@ def tools(request, tool):
166131 return request .param if hasattr (request , "param" ) else [tool_decorated ]
167132
168133
169- @pytest .fixture
170- def hook_provider ():
171- return MockHookProvider ([AgentInitializedEvent , StartRequestEvent , EndRequestEvent ])
172-
173-
174134@pytest .fixture
175135def agent (
176136 mock_model ,
@@ -182,7 +142,6 @@ def agent(
182142 tool_registry ,
183143 tool_decorated ,
184144 request ,
185- hook_provider ,
186145):
187146 agent = Agent (
188147 model = mock_model ,
@@ -192,9 +151,6 @@ def agent(
192151 tools = tools ,
193152 )
194153
195- # for now, hooks are private
196- agent ._hooks .add_hook (hook_provider )
197-
198154 # Only register the tool directly if tools wasn't parameterized
199155 if not hasattr (request , "param" ) or request .param is None :
200156 # Create a new function tool directly from the decorated function
@@ -789,38 +745,6 @@ async def test_agent_invoke_async(mock_model, agent, agenerator):
789745 assert tru_message == exp_message
790746
791747
792- @unittest .mock .patch ("strands.experimental.hooks.registry.HookRegistry.invoke_callbacks" )
793- def test_agent_hooks__init__ (mock_invoke_callbacks ):
794- """Verify that the AgentInitializedEvent is emitted on Agent construction."""
795- agent = Agent ()
796-
797- # Verify AgentInitialized event was invoked
798- mock_invoke_callbacks .assert_called_once ()
799- assert mock_invoke_callbacks .call_args == call (AgentInitializedEvent (agent = agent ))
800-
801-
802- def test_agent_hooks__call__ (agent , mock_hook_messages , hook_provider ):
803- """Verify that the correct hook events are emitted as part of __call__."""
804-
805- agent ("test message" )
806-
807- assert hook_provider .events_received == [StartRequestEvent (agent = agent ), EndRequestEvent (agent = agent )]
808-
809-
810- @pytest .mark .asyncio
811- async def test_agent_hooks_stream_async (agent , mock_hook_messages , hook_provider ):
812- """Verify that the correct hook events are emitted as part of stream_async."""
813- iterator = agent .stream_async ("test message" )
814- await anext (iterator )
815- assert hook_provider .events_received == [StartRequestEvent (agent = agent )]
816-
817- # iterate the rest
818- async for _ in iterator :
819- pass
820-
821- assert hook_provider .events_received == [StartRequestEvent (agent = agent ), EndRequestEvent (agent = agent )]
822-
823-
824748def test_agent_tool (mock_randint , agent ):
825749 conversation_manager_spy = unittest .mock .Mock (wraps = agent .conversation_manager )
826750 agent .conversation_manager = conversation_manager_spy
@@ -1051,13 +975,6 @@ async def test_agent_structured_output_async(agent, user, agenerator):
1051975 agent .model .structured_output .assert_called_once_with (type (user ), [{"role" : "user" , "content" : [{"text" : prompt }]}])
1052976
1053977
1054- def test_agent_hooks_structured_output (agent , user , mock_hook_messages , hook_provider , agenerator ):
1055- agent .model .structured_output = unittest .mock .Mock (return_value = agenerator ([{"output" : user }]))
1056- agent .structured_output (type (user ), "example prompt" )
1057-
1058- assert hook_provider .events_received == [StartRequestEvent (agent = agent ), EndRequestEvent (agent = agent )]
1059-
1060-
1061978@pytest .mark .asyncio
1062979async def test_stream_async_returns_all_events (mock_event_loop_cycle , alist ):
1063980 agent = Agent ()
0 commit comments