-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_e2e.py
More file actions
268 lines (217 loc) · 8.77 KB
/
test_e2e.py
File metadata and controls
268 lines (217 loc) · 8.77 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""End-to-end tests for RuntimeUseClient against a local runtimeuse server
with the deterministic echo handler."""
import json
import pytest
from src.runtimeuse_client import (
RuntimeUseClient,
QueryOptions,
QueryResult,
TextResult,
StructuredOutputResult,
AssistantMessageInterface,
AgentRuntimeError,
CancelledException,
CommandInterface,
)
pytestmark = [pytest.mark.e2e, pytest.mark.asyncio]
class TestTextResult:
async def test_echo_text(
self, client: RuntimeUseClient, query_options: QueryOptions
):
result = await client.query(prompt="ECHO:hello world", options=query_options)
assert isinstance(result, QueryResult)
assert isinstance(result.data, TextResult)
assert result.data.text == "hello world"
async def test_plain_prompt_echoed(
self, client: RuntimeUseClient, query_options: QueryOptions
):
result = await client.query(prompt="just a plain prompt", options=query_options)
assert isinstance(result.data, TextResult)
assert result.data.text == "just a plain prompt"
class TestStructuredOutputResult:
async def test_structured_output(
self, client: RuntimeUseClient, make_query_options
):
payload = {"answer": 42, "nested": {"key": "value"}}
result = await client.query(
prompt=f"STRUCTURED:{json.dumps(payload)}",
options=make_query_options(
output_format_json_schema_str=json.dumps({"type": "object"}),
),
)
assert isinstance(result.data, StructuredOutputResult)
assert result.data.structured_output == payload
class TestAssistantStreaming:
async def test_assistant_messages_streamed(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
result = await client.query(
prompt="STREAM:3",
options=make_query_options(on_assistant_message=on_msg),
)
assert isinstance(result.data, TextResult)
assert result.data.text == "streamed 3 messages"
assert len(received) == 3
assert received[0].text_blocks == ["message 1 of 3"]
assert received[1].text_blocks == ["message 2 of 3"]
assert received[2].text_blocks == ["message 3 of 3"]
class TestErrorFromHandler:
async def test_error_raises(
self, client: RuntimeUseClient, query_options: QueryOptions
):
with pytest.raises(AgentRuntimeError, match="something broke"):
await client.query(prompt="ERROR:something broke", options=query_options)
class TestTimeout:
async def test_timeout_raises(self, client: RuntimeUseClient, make_query_options):
with pytest.raises(TimeoutError):
await client.query(
prompt="SLOW:30000",
options=make_query_options(timeout=0.5),
)
class TestCancellation:
async def test_abort_during_streaming(self, ws_url: str, make_query_options):
client = RuntimeUseClient(ws_url=ws_url)
async def abort_on_first(msg: AssistantMessageInterface):
client.abort()
with pytest.raises(CancelledException):
await client.query(
prompt="STREAM:5",
options=make_query_options(on_assistant_message=abort_on_first),
)
class TestPrePostCommands:
async def test_pre_command_output_streamed(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
result = await client.query(
prompt="ECHO:hello",
options=make_query_options(
pre_agent_invocation_commands=[
CommandInterface(command="echo pre-sentinel")
],
on_assistant_message=on_msg,
),
)
assert isinstance(result.data, TextResult)
assert result.data.text == "hello"
all_text = [block for msg in received for block in msg.text_blocks]
assert any("pre-sentinel" in t for t in all_text)
async def test_post_command_output_streamed(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
result = await client.query(
prompt="ECHO:hello",
options=make_query_options(
post_agent_invocation_commands=[
CommandInterface(command="echo post-sentinel")
],
on_assistant_message=on_msg,
),
)
assert isinstance(result.data, TextResult)
assert result.data.text == "hello"
all_text = [block for msg in received for block in msg.text_blocks]
assert any("post-sentinel" in t for t in all_text)
async def test_pre_and_post_commands_both_run(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
result = await client.query(
prompt="ECHO:hello",
options=make_query_options(
pre_agent_invocation_commands=[
CommandInterface(command="echo pre-sentinel")
],
post_agent_invocation_commands=[
CommandInterface(command="echo post-sentinel")
],
on_assistant_message=on_msg,
),
)
assert isinstance(result.data, TextResult)
assert result.data.text == "hello"
all_text = [block for msg in received for block in msg.text_blocks]
assert any("pre-sentinel" in t for t in all_text)
assert any("post-sentinel" in t for t in all_text)
async def test_pre_command_with_cwd(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
await client.query(
prompt="ECHO:ok",
options=make_query_options(
pre_agent_invocation_commands=[
CommandInterface(command="pwd", cwd="/tmp")
],
on_assistant_message=on_msg,
),
)
all_text = [block for msg in received for block in msg.text_blocks]
assert any("/tmp" in t for t in all_text)
async def test_post_command_with_cwd(
self, client: RuntimeUseClient, make_query_options
):
received: list[AssistantMessageInterface] = []
async def on_msg(msg: AssistantMessageInterface):
received.append(msg)
await client.query(
prompt="ECHO:ok",
options=make_query_options(
post_agent_invocation_commands=[
CommandInterface(command="pwd", cwd="/tmp")
],
on_assistant_message=on_msg,
),
)
all_text = [block for msg in received for block in msg.text_blocks]
assert any("/tmp" in t for t in all_text)
async def test_failed_pre_command_raises_error(
self, client: RuntimeUseClient, make_query_options
):
with pytest.raises(AgentRuntimeError, match="failed with exit code"):
await client.query(
prompt="ECHO:should not reach",
options=make_query_options(
pre_agent_invocation_commands=[
CommandInterface(command="exit 1")
],
),
)
async def test_failed_post_command_raises_error(
self, client: RuntimeUseClient, make_query_options
):
with pytest.raises(AgentRuntimeError, match="failed with exit code"):
await client.query(
prompt="ECHO:hello",
options=make_query_options(
post_agent_invocation_commands=[
CommandInterface(command="exit 1")
],
),
)
class TestInvocationFieldsForwarded:
async def test_fields_round_trip(
self, client: RuntimeUseClient, make_query_options
):
result = await client.query(
prompt="ECHO:field test",
options=make_query_options(
system_prompt="Custom system prompt.",
model="test-model",
source_id="e2e-source",
),
)
assert isinstance(result.data, TextResult)
assert result.data.text == "field test"