forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_agent_and_compact_rpc.py
More file actions
193 lines (161 loc) · 6.96 KB
/
test_agent_and_compact_rpc.py
File metadata and controls
193 lines (161 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""E2E tests for Agent Selection and Session Compaction RPC APIs."""
import pytest
from copilot import CopilotClient, PermissionHandler
from copilot.generated.rpc import SessionAgentSelectParams
from .testharness import CLI_PATH, E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
class TestAgentSelectionRpc:
@pytest.mark.asyncio
async def test_should_list_available_custom_agents(self):
"""Test listing available custom agents via RPC."""
client = CopilotClient({"cli_path": CLI_PATH, "use_stdio": True})
try:
await client.start()
session = await client.create_session(
{
"on_permission_request": PermissionHandler.approve_all,
"custom_agents": [
{
"name": "test-agent",
"display_name": "Test Agent",
"description": "A test agent",
"prompt": "You are a test agent.",
},
{
"name": "another-agent",
"display_name": "Another Agent",
"description": "Another test agent",
"prompt": "You are another agent.",
},
],
}
)
result = await session.rpc.agent.list()
assert result.agents is not None
assert len(result.agents) == 2
assert result.agents[0].name == "test-agent"
assert result.agents[0].display_name == "Test Agent"
assert result.agents[0].description == "A test agent"
assert result.agents[1].name == "another-agent"
await session.disconnect()
await client.stop()
finally:
await client.force_stop()
@pytest.mark.asyncio
async def test_should_return_null_when_no_agent_is_selected(self):
"""Test getCurrent returns null when no agent is selected."""
client = CopilotClient({"cli_path": CLI_PATH, "use_stdio": True})
try:
await client.start()
session = await client.create_session(
{
"on_permission_request": PermissionHandler.approve_all,
"custom_agents": [
{
"name": "test-agent",
"display_name": "Test Agent",
"description": "A test agent",
"prompt": "You are a test agent.",
}
],
}
)
result = await session.rpc.agent.get_current()
assert result.agent is None
await session.disconnect()
await client.stop()
finally:
await client.force_stop()
@pytest.mark.asyncio
async def test_should_select_and_get_current_agent(self):
"""Test selecting an agent and verifying getCurrent returns it."""
client = CopilotClient({"cli_path": CLI_PATH, "use_stdio": True})
try:
await client.start()
session = await client.create_session(
{
"on_permission_request": PermissionHandler.approve_all,
"custom_agents": [
{
"name": "test-agent",
"display_name": "Test Agent",
"description": "A test agent",
"prompt": "You are a test agent.",
}
],
}
)
# Select the agent
select_result = await session.rpc.agent.select(
SessionAgentSelectParams(name="test-agent")
)
assert select_result.agent is not None
assert select_result.agent.name == "test-agent"
assert select_result.agent.display_name == "Test Agent"
# Verify getCurrent returns the selected agent
current_result = await session.rpc.agent.get_current()
assert current_result.agent is not None
assert current_result.agent.name == "test-agent"
await session.disconnect()
await client.stop()
finally:
await client.force_stop()
@pytest.mark.asyncio
async def test_should_deselect_current_agent(self):
"""Test deselecting the current agent."""
client = CopilotClient({"cli_path": CLI_PATH, "use_stdio": True})
try:
await client.start()
session = await client.create_session(
{
"on_permission_request": PermissionHandler.approve_all,
"custom_agents": [
{
"name": "test-agent",
"display_name": "Test Agent",
"description": "A test agent",
"prompt": "You are a test agent.",
}
],
}
)
# Select then deselect
await session.rpc.agent.select(SessionAgentSelectParams(name="test-agent"))
await session.rpc.agent.deselect()
# Verify no agent is selected
current_result = await session.rpc.agent.get_current()
assert current_result.agent is None
await session.disconnect()
await client.stop()
finally:
await client.force_stop()
@pytest.mark.asyncio
async def test_should_return_empty_list_when_no_custom_agents_configured(self):
"""Test listing agents returns empty when none configured."""
client = CopilotClient({"cli_path": CLI_PATH, "use_stdio": True})
try:
await client.start()
session = await client.create_session(
{"on_permission_request": PermissionHandler.approve_all}
)
result = await session.rpc.agent.list()
assert result.agents == []
await session.disconnect()
await client.stop()
finally:
await client.force_stop()
class TestSessionCompactionRpc:
@pytest.mark.asyncio
async def test_should_compact_session_history_after_messages(self, ctx: E2ETestContext):
"""Test compacting session history via RPC."""
session = await ctx.client.create_session(
{"on_permission_request": PermissionHandler.approve_all}
)
# Send a message to create some history
await session.send_and_wait({"prompt": "What is 2+2?"})
# Compact the session
result = await session.rpc.compaction.compact()
assert isinstance(result.success, bool)
assert isinstance(result.tokens_removed, (int, float))
assert isinstance(result.messages_removed, (int, float))
await session.disconnect()