Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions python/packages/core/tests/core/test_middleware_with_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,17 +1467,15 @@ async def test_decorator_and_type_mismatch(self, client: MockChatClient) -> None

# This will cause a type error at decoration time, so we need to test differently
# Should raise MiddlewareException due to mismatch during agent creation
with pytest.raises(MiddlewareException, match="MiddlewareTypes type mismatch"):

@agent_middleware # type: ignore[arg-type]
async def mismatched_middleware(
context: FunctionInvocationContext, # Wrong type for @agent_middleware
call_next: Any,
) -> None:
await call_next()
@agent_middleware # type: ignore[arg-type]
async def mismatched_middleware(
context: FunctionInvocationContext, # Wrong type for @agent_middleware
call_next: Any,
) -> None:
await call_next()

agent = Agent(client=client, middleware=[mismatched_middleware])
await agent.run([Message(role="user", contents=["test"])])
with pytest.raises(MiddlewareException, match="MiddlewareTypes type mismatch"):
Agent(client=client, middleware=[mismatched_middleware])

async def test_only_decorator_specified(self, chat_client_base: "MockBaseChatClient") -> None:
"""Only decorator specified - rely on decorator."""
Expand Down Expand Up @@ -1595,22 +1593,19 @@ async def no_info_middleware(context: Any, call_next: Any) -> None: # No decora

# Should raise MiddlewareException
with pytest.raises(MiddlewareException, match="Cannot determine middleware type"):
agent = Agent(client=client, middleware=[no_info_middleware])
await agent.run([Message(role="user", contents=["test"])])
Agent(client=client, middleware=[no_info_middleware])

async def test_insufficient_parameters_error(self, client: Any) -> None:
"""Test that middleware with insufficient parameters raises an error."""
from agent_framework import Agent, agent_middleware

# Should raise MiddlewareException about insufficient parameters
with pytest.raises(MiddlewareException, match="must have at least 2 parameters"):

@agent_middleware # type: ignore[arg-type]
async def insufficient_params_middleware(context: Any) -> None: # Missing 'next' parameter
pass
@agent_middleware # type: ignore[arg-type]
async def insufficient_params_middleware(context: Any) -> None: # Missing 'next' parameter
pass

agent = Agent(client=client, middleware=[insufficient_params_middleware])
await agent.run([Message(role="user", contents=["test"])])
with pytest.raises(MiddlewareException, match="must have at least 2 parameters"):
Agent(client=client, middleware=[insufficient_params_middleware])

async def test_decorator_markers_preserved(self) -> None:
"""Test that decorator markers are properly set on functions."""
Expand Down