FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
copilot-sdk/python/test_tool_set.py at main · github/copilot-sdk · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
github
/
copilot-sdk
Public
Notifications
You must be signed in to change notification settings
Fork
1.4k
Star
10.4k
Code
Issues
231
Pull requests
41
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
copilot-sdk
/
python
/
test_tool_set.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
388 lines (316 loc) · 14 KB
Breadcrumbs
copilot-sdk
/
python
/
test_tool_set.py
Copy path
File metadata and controls
388 lines (316 loc) · 14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Unit tests for the ``ToolSet`` builder and empty-mode helpers."""
from
__future__
import
annotations
import
pytest
from
copilot
import
BUILTIN_TOOLS_ISOLATED
,
CopilotClient
,
ToolSet
,
UriRuntimeConnection
from
copilot
.
_mode
import
(
_custom_agents_local_only_default
,
_embedding_cache_storage_default
,
_enable_file_hooks_default
,
_enable_host_git_operations_default
,
_enable_on_demand_instruction_discovery_default
,
_enable_session_store_default
,
_enable_session_telemetry_default
,
_enable_skills_default
,
_post_create_options_patch
,
_require_available_tools_for_empty_mode
,
_require_storage_for_empty_mode
,
_skip_embedding_retrieval_default
,
_system_message_for_mode
,
_validate_tool_filter_list
,
)
class
TestToolSet
:
def
test_add_builtin_string
(
self
):
ts
=
ToolSet
().
add_builtin
(
"bash"
)
assert
ts
.
to_list
()
==
[
"builtin:bash"
]
def
test_add_builtin_wildcard
(
self
):
ts
=
ToolSet
().
add_builtin
(
"*"
)
assert
ts
.
to_list
()
==
[
"builtin:*"
]
def
test_add_builtin_iterable
(
self
):
ts
=
ToolSet
().
add_builtin
([
"bash"
,
"edit"
])
assert
ts
.
to_list
()
==
[
"builtin:bash"
,
"builtin:edit"
]
def
test_add_builtin_isolated
(
self
):
ts
=
ToolSet
().
add_builtin
(
BUILTIN_TOOLS_ISOLATED
)
assert
ts
.
to_list
()
==
[
f"builtin:
{
name
}
"
for
name
in
BUILTIN_TOOLS_ISOLATED
]
def
test_add_mcp
(
self
):
ts
=
ToolSet
().
add_mcp
(
"github-list_issues"
)
assert
ts
.
to_list
()
==
[
"mcp:github-list_issues"
]
def
test_add_mcp_wildcard
(
self
):
assert
ToolSet
().
add_mcp
(
"*"
).
to_list
()
==
[
"mcp:*"
]
def
test_add_custom
(
self
):
assert
ToolSet
().
add_custom
(
"my_tool"
).
to_list
()
==
[
"custom:my_tool"
]
def
test_chained
(
self
):
ts
=
ToolSet
().
add_builtin
(
BUILTIN_TOOLS_ISOLATED
).
add_mcp
(
"*"
).
add_custom
(
"*"
)
assert
ts
.
to_list
()[
-
2
:]
==
[
"mcp:*"
,
"custom:*"
]
def
test_rejects_bad_name
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"tool names must match"
):
ToolSet
().
add_builtin
(
"has space"
)
def
test_rejects_empty
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"must not be empty"
):
ToolSet
().
add_custom
(
""
)
def
test_rejects_colon
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"tool names must match"
):
ToolSet
().
add_mcp
(
"server:tool"
)
def
test_iterable_protocol
(
self
):
ts
=
ToolSet
().
add_builtin
(
"bash"
).
add_mcp
(
"*"
)
assert
list
(
ts
)
==
[
"builtin:bash"
,
"mcp:*"
]
assert
len
(
ts
)
==
2
class
TestEmptyModeValidation
:
def
test_empty_mode_requires_storage
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"requires base_directory"
):
_require_storage_for_empty_mode
(
mode
=
"empty"
,
base_directory
=
None
,
session_fs_set
=
False
,
is_uri_connection
=
False
,
)
def
test_empty_mode_accepts_base_directory
(
self
):
_require_storage_for_empty_mode
(
mode
=
"empty"
,
base_directory
=
"/tmp/x"
,
session_fs_set
=
False
,
is_uri_connection
=
False
,
)
def
test_empty_mode_accepts_session_fs
(
self
):
_require_storage_for_empty_mode
(
mode
=
"empty"
,
base_directory
=
None
,
session_fs_set
=
True
,
is_uri_connection
=
False
,
)
def
test_empty_mode_accepts_uri_connection
(
self
):
_require_storage_for_empty_mode
(
mode
=
"empty"
,
base_directory
=
None
,
session_fs_set
=
False
,
is_uri_connection
=
True
,
)
def
test_copilot_cli_mode_no_storage_required
(
self
):
_require_storage_for_empty_mode
(
mode
=
"copilot-cli"
,
base_directory
=
None
,
session_fs_set
=
False
,
is_uri_connection
=
False
,
)
def
test_empty_mode_requires_available_tools
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"available_tools"
):
_require_available_tools_for_empty_mode
(
"empty"
,
None
)
def
test_empty_mode_accepts_available_tools
(
self
):
_require_available_tools_for_empty_mode
(
"empty"
, [
"builtin:bash"
])
def
test_copilot_cli_mode_no_tool_filter_required
(
self
):
_require_available_tools_for_empty_mode
(
"copilot-cli"
,
None
)
class
TestToolFilterListValidation
:
def
test_rejects_bare_wildcard
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"bare wildcard"
):
_validate_tool_filter_list
(
"available_tools"
, [
"*"
])
def
test_accepts_source_qualified_wildcard
(
self
):
_validate_tool_filter_list
(
"available_tools"
, [
"builtin:*"
,
"mcp:*"
])
def
test_accepts_none
(
self
):
_validate_tool_filter_list
(
"available_tools"
,
None
)
class
TestSystemMessageForMode
:
def
test_copilot_cli_pass_through
(
self
):
assert
_system_message_for_mode
(
"copilot-cli"
,
None
)
is
None
msg
=
{
"mode"
:
"append"
,
"content"
:
"hi"
}
assert
_system_message_for_mode
(
"copilot-cli"
,
msg
)
is
msg
def
test_empty_mode_none_supplied
(
self
):
out
=
_system_message_for_mode
(
"empty"
,
None
)
assert
out
==
{
"mode"
:
"customize"
,
"sections"
: {
"environment_context"
: {
"action"
:
"remove"
}},
}
def
test_empty_mode_replace_pass_through
(
self
):
msg
=
{
"mode"
:
"replace"
,
"content"
:
"verbatim"
}
assert
_system_message_for_mode
(
"empty"
,
msg
)
is
msg
def
test_empty_mode_customize_adds_section
(
self
):
msg
=
{
"mode"
:
"customize"
,
"sections"
: {
"identity"
: {
"action"
:
"remove"
}}}
out
=
_system_message_for_mode
(
"empty"
,
msg
)
assert
out
[
"sections"
][
"environment_context"
]
==
{
"action"
:
"remove"
}
assert
out
[
"sections"
][
"identity"
]
==
{
"action"
:
"remove"
}
def
test_empty_mode_customize_does_not_overwrite_existing
(
self
):
msg
=
{
"mode"
:
"customize"
,
"sections"
: {
"environment_context"
: {
"action"
:
"replace"
,
"content"
:
"X"
}},
}
assert
_system_message_for_mode
(
"empty"
,
msg
)
is
msg
def
test_empty_mode_append_promoted_to_customize
(
self
):
msg
=
{
"mode"
:
"append"
,
"content"
:
"tip"
}
out
=
_system_message_for_mode
(
"empty"
,
msg
)
assert
out
[
"mode"
]
==
"customize"
assert
out
[
"content"
]
==
"tip"
assert
out
[
"sections"
][
"environment_context"
]
==
{
"action"
:
"remove"
}
class
TestEmptyModeEmbeddingCacheStorageDefaults
:
def
test_empty_mode_defaults_to_in_memory
(
self
):
assert
_embedding_cache_storage_default
(
"empty"
,
None
)
==
"in-memory"
def
test_caller_wins
(
self
):
assert
_embedding_cache_storage_default
(
"empty"
,
"persistent"
)
==
"persistent"
assert
_embedding_cache_storage_default
(
"empty"
,
"in-memory"
)
==
"in-memory"
def
test_copilot_cli_does_not_change
(
self
):
assert
_embedding_cache_storage_default
(
"copilot-cli"
,
None
)
is
None
assert
_embedding_cache_storage_default
(
"copilot-cli"
,
"persistent"
)
==
"persistent"
class
TestEmptyModeBooleanDefaults
:
@
pytest
.
mark
.
parametrize
(
(
"helper"
,
"empty_default"
),
[
(
_enable_session_telemetry_default
,
False
),
(
_skip_embedding_retrieval_default
,
True
),
(
_enable_on_demand_instruction_discovery_default
,
False
),
(
_enable_file_hooks_default
,
False
),
(
_enable_host_git_operations_default
,
False
),
(
_enable_session_store_default
,
False
),
(
_enable_skills_default
,
False
),
(
_custom_agents_local_only_default
,
True
),
],
)
def
test_empty_mode_defaults
(
self
,
helper
,
empty_default
):
assert
helper
(
"empty"
,
None
)
is
empty_default
@
pytest
.
mark
.
parametrize
(
"helper"
,
[
_enable_session_telemetry_default
,
_skip_embedding_retrieval_default
,
_enable_on_demand_instruction_discovery_default
,
_enable_file_hooks_default
,
_enable_host_git_operations_default
,
_enable_session_store_default
,
_enable_skills_default
,
_custom_agents_local_only_default
,
],
)
def
test_caller_wins
(
self
,
helper
):
assert
helper
(
"empty"
,
True
)
is
True
assert
helper
(
"empty"
,
False
)
is
False
@
pytest
.
mark
.
parametrize
(
"helper"
,
[
_enable_session_telemetry_default
,
_skip_embedding_retrieval_default
,
_enable_on_demand_instruction_discovery_default
,
_enable_file_hooks_default
,
_enable_host_git_operations_default
,
_enable_session_store_default
,
_enable_skills_default
,
_custom_agents_local_only_default
,
],
)
def
test_copilot_cli_does_not_change
(
self
,
helper
):
assert
helper
(
"copilot-cli"
,
None
)
is
None
class
TestPostCreatePatch
:
def
test_empty_mode_defaults
(
self
):
patch
=
_post_create_options_patch
(
"empty"
,
None
,
None
,
None
,
None
)
assert
patch
==
{
"skipCustomInstructions"
:
True
,
"customAgentsLocalOnly"
:
True
,
"coauthorEnabled"
:
False
,
"manageScheduleEnabled"
:
False
,
"installedPlugins"
: [],
"includedBuiltinSkills"
: [],
}
def
test_empty_mode_caller_wins
(
self
):
patch
=
_post_create_options_patch
(
"empty"
,
False
,
False
,
True
,
True
)
assert
patch
==
{
"skipCustomInstructions"
:
False
,
"customAgentsLocalOnly"
:
False
,
"coauthorEnabled"
:
True
,
"manageScheduleEnabled"
:
True
,
"installedPlugins"
: [],
"includedBuiltinSkills"
: [],
}
def
test_empty_mode_preserves_explicit_builtin_skill_allowlist
(
self
):
patch
=
_post_create_options_patch
(
"empty"
,
None
,
None
,
None
,
None
, [
"code-review"
])
assert
patch
is
not
None
assert
patch
[
"includedBuiltinSkills"
]
==
[
"code-review"
]
def
test_copilot_cli_returns_none_when_unset
(
self
):
assert
_post_create_options_patch
(
"copilot-cli"
,
None
,
None
,
None
,
None
)
is
None
# Non-empty mode never injects the built-in skill restriction.
assert
"includedBuiltinSkills"
not
in
(
_post_create_options_patch
(
"copilot-cli"
,
True
,
None
,
False
,
None
)
or
{}
)
def
test_copilot_cli_passes_through_explicit_values
(
self
):
patch
=
_post_create_options_patch
(
"copilot-cli"
,
True
,
None
,
False
,
None
, [
"code-review"
])
assert
patch
==
{
"skipCustomInstructions"
:
True
,
"coauthorEnabled"
:
False
,
"includedBuiltinSkills"
: [
"code-review"
],
}
class
_CapturingOptions
:
"""Captures the params passed to ``session.rpc.options.update``."""
def
__init__
(
self
)
->
None
:
self
.
captured
:
list
=
[]
async
def
update
(
self
,
params
)
->
None
:
self
.
captured
.
append
(
params
)
class
_CapturingRpc
:
def
__init__
(
self
)
->
None
:
self
.
options
=
_CapturingOptions
()
class
_FakeSession
:
def
__init__
(
self
,
session_id
:
str
=
"sid-1"
)
->
None
:
self
.
session_id
=
session_id
self
.
rpc
=
_CapturingRpc
()
async
def
disconnect
(
self
)
->
None
:
pass
class
TestApplyPostCreateOptionsPatch
:
"""Guards the translation from the patch dict to ``SessionUpdateOptionsParams``.
This covers the wire request emitted on both create and resume, which both
funnel through ``_apply_post_create_options_patch``.
"""
def
_make_client
(
self
):
return
CopilotClient
(
mode
=
"empty"
,
connection
=
UriRuntimeConnection
(
url
=
"http://localhost:1234"
),
)
async
def
test_empty_mode_sends_included_builtin_skills_empty
(
self
):
client
=
self
.
_make_client
()
session
=
_FakeSession
()
await
client
.
_apply_post_create_options_patch
(
session
,
"empty"
,
None
,
None
,
None
,
None
)
assert
len
(
session
.
rpc
.
options
.
captured
)
==
1
params
=
session
.
rpc
.
options
.
captured
[
0
]
# The Empty post-patch must reach the wire request as an empty list, not
# be silently dropped during translation.
assert
params
.
included_builtin_skills
==
[]
assert
params
.
installed_plugins
==
[]
async
def
test_empty_mode_explicit_allowlist_reaches_wire
(
self
):
client
=
self
.
_make_client
()
session
=
_FakeSession
()
await
client
.
_apply_post_create_options_patch
(
session
,
"empty"
,
False
,
False
,
True
,
True
, [
"code-review"
]
)
params
=
session
.
rpc
.
options
.
captured
[
0
]
assert
params
.
included_builtin_skills
==
[
"code-review"
]
async
def
test_copilot_cli_mode_omits_included_builtin_skills
(
self
):
client
=
self
.
_make_client
()
session
=
_FakeSession
()
# Non-empty mode with no overrides sends no patch at all.
await
client
.
_apply_post_create_options_patch
(
session
,
"copilot-cli"
,
None
,
None
,
None
,
None
)
assert
session
.
rpc
.
options
.
captured
==
[]
async
def
test_copilot_cli_mode_never_sets_included_builtin_skills
(
self
):
client
=
self
.
_make_client
()
session
=
_FakeSession
()
await
client
.
_apply_post_create_options_patch
(
session
,
"copilot-cli"
,
True
,
None
,
False
,
None
)
params
=
session
.
rpc
.
options
.
captured
[
0
]
# copilot-cli mode must not inject the built-in skill restriction.
assert
params
.
included_builtin_skills
is
None
class
TestClientConstruction
:
def
test_empty_mode_without_storage_raises
(
self
):
with
pytest
.
raises
(
ValueError
,
match
=
"requires base_directory"
):
CopilotClient
(
mode
=
"empty"
)
def
test_empty_mode_with_base_directory_ok
(
self
,
tmp_path
):
# Use URI connection to skip bundled-CLI discovery.
client
=
CopilotClient
(
mode
=
"empty"
,
base_directory
=
str
(
tmp_path
),
connection
=
UriRuntimeConnection
(
url
=
"http://localhost:1234"
),
)
assert
client
.
_options
.
mode
==
"empty"
def
test_empty_mode_with_uri_connection_ok
(
self
):
client
=
CopilotClient
(
mode
=
"empty"
,
connection
=
UriRuntimeConnection
(
url
=
"http://localhost:1234"
),
)
assert
client
.
_options
.
mode
==
"empty"
def
test_default_mode_copilot_cli
(
self
):
client
=
CopilotClient
(
connection
=
UriRuntimeConnection
(
url
=
"http://localhost:1234"
),
)
assert
client
.
_options
.
mode
==
"copilot-cli"
Back
|
FazBrowse Home
|
New Git URL