Skip to content

Commit 69c406f

Browse files
fix: test_agent_instructions_signature
1 parent db50380 commit 69c406f

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed
Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,109 @@
11
import pytest
2-
from src.agents.agent import Agent # adjust if needed
2+
from unittest.mock import Mock
3+
# Adjust import based on actual repo structure
4+
from src.agents.agent import Agent, RunContextWrapper
35

4-
class DummyContext:
5-
pass
6-
7-
class DummyAgent(Agent):
8-
def __init__(self, instructions):
9-
super().__init__(instructions=instructions)
10-
11-
@pytest.mark.asyncio
12-
async def test_valid_signature():
13-
async def good_instructions(ctx, agent):
14-
return "valid"
15-
a = DummyAgent(good_instructions)
16-
result = await a.get_system_prompt(DummyContext())
17-
assert result == "valid"
18-
19-
@pytest.mark.asyncio
20-
async def test_invalid_signature_raises():
21-
async def bad_instructions(ctx):
22-
return "invalid"
23-
a = DummyAgent(bad_instructions)
24-
import pytest
25-
with pytest.raises(TypeError):
26-
await a.get_system_prompt(DummyContext())
6+
class TestInstructionsSignatureValidation:
7+
"""Test suite for instructions function signature validation"""
8+
9+
@pytest.fixture
10+
def mock_run_context(self):
11+
"""Create a mock RunContextWrapper for testing"""
12+
return Mock(spec=RunContextWrapper)
13+
14+
@pytest.mark.asyncio
15+
async def test_valid_async_signature_passes(self, mock_run_context):
16+
"""Test that async function with correct signature works"""
17+
async def valid_instructions(context, agent):
18+
return "Valid async instructions"
19+
20+
agent = Agent(instructions=valid_instructions)
21+
result = await agent.get_system_prompt(mock_run_context)
22+
assert result == "Valid async instructions"
23+
24+
@pytest.mark.asyncio
25+
async def test_valid_sync_signature_passes(self, mock_run_context):
26+
"""Test that sync function with correct signature works"""
27+
def valid_instructions(context, agent):
28+
return "Valid sync instructions"
29+
30+
agent = Agent(instructions=valid_instructions)
31+
result = await agent.get_system_prompt(mock_run_context)
32+
assert result == "Valid sync instructions"
33+
34+
@pytest.mark.asyncio
35+
async def test_one_parameter_raises_error(self, mock_run_context):
36+
"""Test that function with only one parameter raises TypeError"""
37+
def invalid_instructions(context):
38+
return "Should fail"
39+
40+
agent = Agent(instructions=invalid_instructions)
41+
42+
with pytest.raises(TypeError) as exc_info:
43+
await agent.get_system_prompt(mock_run_context)
44+
45+
assert "must accept exactly 2 arguments" in str(exc_info.value)
46+
assert "but got 1" in str(exc_info.value)
47+
48+
@pytest.mark.asyncio
49+
async def test_three_parameters_raises_error(self, mock_run_context):
50+
"""Test that function with three parameters raises TypeError"""
51+
def invalid_instructions(context, agent, extra):
52+
return "Should fail"
53+
54+
agent = Agent(instructions=invalid_instructions)
55+
56+
with pytest.raises(TypeError) as exc_info:
57+
await agent.get_system_prompt(mock_run_context)
58+
59+
assert "must accept exactly 2 arguments" in str(exc_info.value)
60+
assert "but got 3" in str(exc_info.value)
61+
62+
@pytest.mark.asyncio
63+
async def test_zero_parameters_raises_error(self, mock_run_context):
64+
"""Test that function with no parameters raises TypeError"""
65+
def invalid_instructions():
66+
return "Should fail"
67+
68+
agent = Agent(instructions=invalid_instructions)
69+
70+
with pytest.raises(TypeError) as exc_info:
71+
await agent.get_system_prompt(mock_run_context)
72+
73+
assert "must accept exactly 2 arguments" in str(exc_info.value)
74+
assert "but got 0" in str(exc_info.value)
75+
76+
@pytest.mark.asyncio
77+
async def test_function_with_args_kwargs_passes(self, mock_run_context):
78+
"""Test that function with *args/**kwargs still works (edge case)"""
79+
def flexible_instructions(context, agent, *args, **kwargs):
80+
return "Flexible instructions"
81+
82+
agent = Agent(instructions=flexible_instructions)
83+
# This should potentially pass as it can accept the 2 required args
84+
# Adjust this test based on your desired behavior
85+
result = await agent.get_system_prompt(mock_run_context)
86+
assert result == "Flexible instructions"
87+
88+
@pytest.mark.asyncio
89+
async def test_string_instructions_still_work(self, mock_run_context):
90+
"""Test that string instructions continue to work"""
91+
agent = Agent(instructions="Static string instructions")
92+
result = await agent.get_system_prompt(mock_run_context)
93+
assert result == "Static string instructions"
94+
95+
@pytest.mark.asyncio
96+
async def test_none_instructions_return_none(self, mock_run_context):
97+
"""Test that None instructions return None"""
98+
agent = Agent(instructions=None)
99+
result = await agent.get_system_prompt(mock_run_context)
100+
assert result is None
101+
102+
@pytest.mark.asyncio
103+
async def test_non_callable_instructions_log_error(self, mock_run_context, caplog):
104+
"""Test that non-callable instructions log an error"""
105+
agent = Agent(instructions=123) # Invalid type
106+
result = await agent.get_system_prompt(mock_run_context)
107+
assert result is None
108+
# Check that error was logged (adjust based on actual logging setup)
109+
# assert "Instructions must be a string or a function" in caplog.text

0 commit comments

Comments
 (0)