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