-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_session_config_e2e.py
More file actions
432 lines (344 loc) · 16.4 KB
/
test_session_config_e2e.py
File metadata and controls
432 lines (344 loc) · 16.4 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""E2E tests for session configuration including model capabilities overrides."""
import base64
import os
import uuid
import pytest
from copilot import ModelCapabilitiesOverride, ModelSupportsOverride
from copilot.session import PermissionHandler
from .testharness import E2ETestContext
pytestmark = pytest.mark.asyncio(loop_scope="module")
PROVIDER_HEADER_NAME = "x-copilot-sdk-provider-header"
CLIENT_NAME = "python-public-surface-client"
def has_image_url_content(exchanges: list[dict]) -> bool:
"""Check if any exchange contains an image_url content part in user messages."""
for ex in exchanges:
for msg in ex.get("request", {}).get("messages", []):
if msg.get("role") == "user" and isinstance(msg.get("content"), list):
if any(p.get("type") == "image_url" for p in msg["content"]):
return True
return False
def _make_proxy_provider(proxy_url: str, header_value: str) -> dict:
return {
"type": "openai",
"base_url": proxy_url,
"api_key": "test-provider-key",
"headers": {PROVIDER_HEADER_NAME: header_value},
}
def _normalize_headers(headers) -> dict[str, str]:
if isinstance(headers, list):
flat: dict[str, str] = {}
for entry in headers:
if isinstance(entry, dict):
key = entry.get("name") or entry.get("key")
value = entry.get("value")
if key is not None:
flat[str(key).lower()] = str(value)
return flat
if isinstance(headers, dict):
flat = {}
for key, value in headers.items():
if isinstance(value, list):
flat[str(key).lower()] = ", ".join(str(v) for v in value)
else:
flat[str(key).lower()] = str(value)
return flat
return {}
def _assert_header_contains(headers, name: str, expected: str) -> None:
flat = _normalize_headers(headers)
actual = flat.get(name.lower(), "")
assert expected in actual, (
f"Expected header {name!r} to contain {expected!r}; got {actual!r}. All headers: {flat!r}"
)
def _get_system_message(exchange: dict) -> str:
for msg in exchange.get("request", {}).get("messages", []):
if msg.get("role") == "system":
value = msg.get("content")
if isinstance(value, str):
return value
return ""
def _get_tool_names(exchange: dict) -> list[str]:
tools = exchange.get("request", {}).get("tools") or []
names: list[str] = []
for tool in tools:
function = tool.get("function") if isinstance(tool, dict) else None
if isinstance(function, dict):
name = function.get("name")
if isinstance(name, str):
names.append(name)
return names
PNG_1X1 = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
)
VIEW_IMAGE_PROMPT = "Use the view tool to look at the file test.png and describe what you see"
class TestSessionConfig:
"""Tests for session configuration including model capabilities overrides."""
async def test_vision_disabled_then_enabled_via_setmodel(self, ctx: E2ETestContext):
png_path = os.path.join(ctx.work_dir, "test.png")
with open(png_path, "wb") as f:
f.write(PNG_1X1)
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
model_capabilities=ModelCapabilitiesOverride(
supports=ModelSupportsOverride(vision=False)
),
)
# Turn 1: vision off — no image_url expected
await session.send_and_wait(VIEW_IMAGE_PROMPT)
traffic_after_t1 = await ctx.get_exchanges()
assert not has_image_url_content(traffic_after_t1)
# Switch vision on
await session.set_model(
"claude-sonnet-4.5",
model_capabilities=ModelCapabilitiesOverride(
supports=ModelSupportsOverride(vision=True)
),
)
# Turn 2: vision on — image_url expected in new exchanges
await session.send_and_wait(VIEW_IMAGE_PROMPT)
traffic_after_t2 = await ctx.get_exchanges()
new_exchanges = traffic_after_t2[len(traffic_after_t1) :]
assert has_image_url_content(new_exchanges)
await session.disconnect()
async def test_vision_enabled_then_disabled_via_setmodel(self, ctx: E2ETestContext):
png_path = os.path.join(ctx.work_dir, "test.png")
with open(png_path, "wb") as f:
f.write(PNG_1X1)
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
model_capabilities=ModelCapabilitiesOverride(
supports=ModelSupportsOverride(vision=True)
),
)
# Turn 1: vision on — image_url expected
await session.send_and_wait(VIEW_IMAGE_PROMPT)
traffic_after_t1 = await ctx.get_exchanges()
assert has_image_url_content(traffic_after_t1)
# Switch vision off
await session.set_model(
"claude-sonnet-4.5",
model_capabilities=ModelCapabilitiesOverride(
supports=ModelSupportsOverride(vision=False)
),
)
# Turn 2: vision off — no image_url expected in new exchanges
await session.send_and_wait(VIEW_IMAGE_PROMPT)
traffic_after_t2 = await ctx.get_exchanges()
new_exchanges = traffic_after_t2[len(traffic_after_t1) :]
assert not has_image_url_content(new_exchanges)
await session.disconnect()
async def test_should_use_custom_sessionid(self, ctx: E2ETestContext):
from copilot.generated.session_events import SessionStartData
requested_session_id = str(uuid.uuid4())
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
session_id=requested_session_id,
)
assert session.session_id == requested_session_id
messages = await session.get_events()
assert messages
start_event = messages[0]
assert isinstance(start_event.data, SessionStartData)
assert start_event.data.session_id == requested_session_id
await session.disconnect()
async def test_should_forward_clientname_in_useragent(self, ctx: E2ETestContext):
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
client_name=CLIENT_NAME,
)
await session.send_and_wait("What is 1+1?")
exchanges = await ctx.get_exchanges()
assert exchanges
_assert_header_contains(exchanges[-1].get("requestHeaders"), "user-agent", CLIENT_NAME)
await session.disconnect()
async def test_should_forward_custom_provider_headers_on_create(self, ctx: E2ETestContext):
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
model="claude-sonnet-4.5",
provider=_make_proxy_provider(ctx.proxy_url, "create-provider-header"),
)
message = await session.send_and_wait("What is 1+1?")
assert "2" in (message.data.content or "")
exchanges = await ctx.get_exchanges()
assert exchanges
headers = exchanges[-1].get("requestHeaders")
_assert_header_contains(headers, "authorization", "Bearer test-provider-key")
_assert_header_contains(headers, PROVIDER_HEADER_NAME, "create-provider-header")
await session.disconnect()
async def test_should_forward_custom_provider_headers_on_resume(self, ctx: E2ETestContext):
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
)
session_id = session1.session_id
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
model="claude-sonnet-4.5",
provider=_make_proxy_provider(ctx.proxy_url, "resume-provider-header"),
)
message = await session2.send_and_wait("What is 2+2?")
assert "4" in (message.data.content or "")
exchanges = await ctx.get_exchanges()
assert exchanges
headers = exchanges[-1].get("requestHeaders")
_assert_header_contains(headers, "authorization", "Bearer test-provider-key")
_assert_header_contains(headers, PROVIDER_HEADER_NAME, "resume-provider-header")
await session2.disconnect()
await session1.disconnect()
async def test_should_forward_provider_wire_model(self, ctx: E2ETestContext):
# Verifies that ProviderConfig.wire_model overrides the model name sent
# to the provider API, while SessionConfig.model still drives runtime
# configuration lookup (capabilities, prompts, reasoning behavior).
# max_output_tokens is also set here to confirm the SDK accepts it
# without serialization errors; the CLI does not echo it as
# `max_tokens` on the OpenAI-style wire request, so we don't assert on
# it directly (see unit tests for serialization coverage).
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
model="claude-sonnet-4.5",
provider={
"type": "openai",
"base_url": ctx.proxy_url,
"api_key": "test-provider-key",
"wire_model": "test-wire-model",
"max_output_tokens": 1024,
},
)
await session.send_and_wait("What is 1+1?")
exchanges = await ctx.get_exchanges()
assert len(exchanges) == 1
request = exchanges[0]["request"]
assert request["model"] == "test-wire-model"
await session.disconnect()
async def test_should_use_provider_model_id_as_wire_model(self, ctx: E2ETestContext):
# ProviderConfig.model_id drives both the runtime resolved model AND the wire
# model when wire_model is not specified. SessionConfig.model is intentionally
# omitted so that model_id is the only model source.
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
provider={
"type": "openai",
"base_url": ctx.proxy_url,
"api_key": "test-provider-key",
"model_id": "claude-sonnet-4.5",
},
)
await session.send_and_wait("What is 1+1?")
exchanges = await ctx.get_exchanges()
assert len(exchanges) == 1
assert exchanges[0]["request"]["model"] == "claude-sonnet-4.5"
await session.disconnect()
async def test_should_use_workingdirectory_for_tool_execution(self, ctx: E2ETestContext):
sub_dir = os.path.join(ctx.work_dir, "subproject")
os.makedirs(sub_dir, exist_ok=True)
with open(os.path.join(sub_dir, "marker.txt"), "w", encoding="utf-8") as f:
f.write("I am in the subdirectory")
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
working_directory=sub_dir,
)
message = await session.send_and_wait("Read the file marker.txt and tell me what it says")
assert "subdirectory" in (message.data.content or "")
await session.disconnect()
async def test_should_apply_workingdirectory_on_session_resume(self, ctx: E2ETestContext):
sub_dir = os.path.join(ctx.work_dir, "resume-subproject")
os.makedirs(sub_dir, exist_ok=True)
with open(os.path.join(sub_dir, "resume-marker.txt"), "w", encoding="utf-8") as f:
f.write("I am in the resume working directory")
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
)
session_id = session1.session_id
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
working_directory=sub_dir,
)
message = await session2.send_and_wait(
"Read the file resume-marker.txt and tell me what it says"
)
assert "resume working directory" in (message.data.content or "")
await session2.disconnect()
await session1.disconnect()
async def test_should_apply_systemmessage_on_session_resume(self, ctx: E2ETestContext):
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
)
session_id = session1.session_id
resume_instruction = "End the response with RESUME_SYSTEM_MESSAGE_SENTINEL."
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
system_message={"mode": "append", "content": resume_instruction},
)
message = await session2.send_and_wait("What is 1+1?")
assert "RESUME_SYSTEM_MESSAGE_SENTINEL" in (message.data.content or "")
exchanges = await ctx.get_exchanges()
assert exchanges
assert resume_instruction in _get_system_message(exchanges[-1])
await session2.disconnect()
await session1.disconnect()
async def test_should_apply_instruction_directories_on_create(self, ctx: E2ETestContext):
project_dir = os.path.join(ctx.work_dir, "instruction-create-project")
instruction_dir = os.path.join(ctx.work_dir, "extra-create-instructions")
instruction_files_dir = os.path.join(instruction_dir, ".github", "instructions")
sentinel = "PY_CREATE_INSTRUCTION_DIRECTORIES_SENTINEL"
os.makedirs(project_dir, exist_ok=True)
os.makedirs(instruction_files_dir, exist_ok=True)
with open(
os.path.join(instruction_files_dir, "extra.instructions.md"), "w", encoding="utf-8"
) as f:
f.write(f"Always include {sentinel}.")
session = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
working_directory=project_dir,
instruction_directories=[instruction_dir],
)
await session.send_and_wait("What is 1+1?")
exchanges = await ctx.get_exchanges()
assert exchanges
assert sentinel in _get_system_message(exchanges[-1])
await session.disconnect()
async def test_should_apply_instruction_directories_on_resume(self, ctx: E2ETestContext):
project_dir = os.path.join(ctx.work_dir, "instruction-resume-project")
instruction_dir = os.path.join(ctx.work_dir, "extra-resume-instructions")
instruction_files_dir = os.path.join(instruction_dir, ".github", "instructions")
sentinel = "PY_RESUME_INSTRUCTION_DIRECTORIES_SENTINEL"
os.makedirs(project_dir, exist_ok=True)
os.makedirs(instruction_files_dir, exist_ok=True)
with open(
os.path.join(instruction_files_dir, "extra.instructions.md"), "w", encoding="utf-8"
) as f:
f.write(f"Always include {sentinel}.")
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
working_directory=project_dir,
)
session2 = await ctx.client.resume_session(
session1.session_id,
on_permission_request=PermissionHandler.approve_all,
working_directory=project_dir,
instruction_directories=[instruction_dir],
)
await session2.send_and_wait("What is 1+1?")
exchanges = await ctx.get_exchanges()
assert exchanges
assert sentinel in _get_system_message(exchanges[-1])
await session2.disconnect()
await session1.disconnect()
async def test_should_apply_availabletools_on_session_resume(self, ctx: E2ETestContext):
session1 = await ctx.client.create_session(
on_permission_request=PermissionHandler.approve_all,
)
session_id = session1.session_id
session2 = await ctx.client.resume_session(
session_id,
on_permission_request=PermissionHandler.approve_all,
available_tools=["view"],
)
try:
await session2.send("What is 1+1?")
exchanges = await ctx.wait_for_exchanges()
assert _get_tool_names(exchanges[-1]) == ["view"]
finally:
await session2.disconnect()
await session1.disconnect()