forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHooksTest.java
More file actions
226 lines (184 loc) · 9.66 KB
/
HooksTest.java
File metadata and controls
226 lines (184 loc) · 9.66 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.PostToolUseHookInput;
import com.github.copilot.sdk.json.PreToolUseHookInput;
import com.github.copilot.sdk.json.PreToolUseHookOutput;
import com.github.copilot.sdk.json.SessionConfig;
import com.github.copilot.sdk.json.SessionHooks;
/**
* Tests for hooks functionality (pre-tool-use and post-tool-use hooks).
*
* <p>
* These tests use the shared CapiProxy infrastructure for deterministic API
* response replay. Snapshots are stored in test/snapshots/hooks/.
* </p>
*
* <p>
* Note: Tests for userPromptSubmitted, sessionStart, and sessionEnd hooks are
* not included as they are not tested in the upstream .NET or Node.js SDKs and
* require test harness updates to properly invoke these hooks.
* </p>
*/
public class HooksTest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
/**
* Verifies that pre-tool-use hook is invoked when model runs a tool.
*
* @see Snapshot: hooks/invoke_pre_tool_use_hook_when_model_runs_a_tool
*/
@Test
void testInvokePreToolUseHookWhenModelRunsATool() throws Exception {
ctx.configureForTest("hooks", "invoke_pre_tool_use_hook_when_model_runs_a_tool");
var preToolUseInputs = new ArrayList<PreToolUseHookInput>();
final String[] sessionIdHolder = new String[1];
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setHooks(new SessionHooks().setOnPreToolUse((input, invocation) -> {
preToolUseInputs.add(input);
assertEquals(sessionIdHolder[0], invocation.getSessionId());
return CompletableFuture.completedFuture(PreToolUseHookOutput.allow());
}));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
sessionIdHolder[0] = session.getSessionId();
// Create a file for the model to read
Path testFile = ctx.getWorkDir().resolve("hello.txt");
Files.writeString(testFile, "Hello from the test!");
session.sendAndWait(
new MessageOptions().setPrompt("Read the contents of hello.txt and tell me what it says"))
.get(60, TimeUnit.SECONDS);
// Should have received at least one preToolUse hook call
assertFalse(preToolUseInputs.isEmpty(), "Should have received preToolUse hook calls");
// Should have received the tool name
assertTrue(preToolUseInputs.stream().anyMatch(i -> i.getToolName() != null && !i.getToolName().isEmpty()),
"Should have received tool name in preToolUse hook");
}
}
/**
* Verifies that post-tool-use hook is invoked after model runs a tool.
*
* @see Snapshot: hooks/invoke_post_tool_use_hook_after_model_runs_a_tool
*/
@Test
void testInvokePostToolUseHookAfterModelRunsATool() throws Exception {
ctx.configureForTest("hooks", "invoke_post_tool_use_hook_after_model_runs_a_tool");
var postToolUseInputs = new ArrayList<PostToolUseHookInput>();
final String[] sessionIdHolder = new String[1];
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setHooks(new SessionHooks().setOnPostToolUse((input, invocation) -> {
postToolUseInputs.add(input);
assertEquals(sessionIdHolder[0], invocation.getSessionId());
return CompletableFuture.completedFuture(null);
}));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
sessionIdHolder[0] = session.getSessionId();
// Create a file for the model to read
Path testFile = ctx.getWorkDir().resolve("world.txt");
Files.writeString(testFile, "World from the test!");
session.sendAndWait(
new MessageOptions().setPrompt("Read the contents of world.txt and tell me what it says"))
.get(60, TimeUnit.SECONDS);
// Should have received at least one postToolUse hook call
assertFalse(postToolUseInputs.isEmpty(), "Should have received postToolUse hook calls");
// Should have received the tool name and result
assertTrue(postToolUseInputs.stream().anyMatch(i -> i.getToolName() != null && !i.getToolName().isEmpty()),
"Should have received tool name in postToolUse hook");
assertTrue(postToolUseInputs.stream().anyMatch(i -> i.getToolResult() != null),
"Should have received tool result in postToolUse hook");
}
}
/**
* Verifies that both hooks are invoked for a single tool call.
*
* @see Snapshot: hooks/invoke_both_hooks_for_single_tool_call
*/
@Test
void testInvokeBothHooksForSingleToolCall() throws Exception {
ctx.configureForTest("hooks", "invoke_both_hooks_for_single_tool_call");
var preToolUseInputs = new ArrayList<PreToolUseHookInput>();
var postToolUseInputs = new ArrayList<PostToolUseHookInput>();
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setHooks(new SessionHooks().setOnPreToolUse((input, invocation) -> {
preToolUseInputs.add(input);
return CompletableFuture.completedFuture(PreToolUseHookOutput.allow());
}).setOnPostToolUse((input, invocation) -> {
postToolUseInputs.add(input);
return CompletableFuture.completedFuture(null);
}));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
// Create a file for the model to read
Path testFile = ctx.getWorkDir().resolve("both.txt");
Files.writeString(testFile, "Testing both hooks!");
session.sendAndWait(new MessageOptions().setPrompt("Read the contents of both.txt")).get(60,
TimeUnit.SECONDS);
// Both hooks should have been called
assertFalse(preToolUseInputs.isEmpty(), "Should have received preToolUse hook calls");
assertFalse(postToolUseInputs.isEmpty(), "Should have received postToolUse hook calls");
// The same tool should appear in both
Set<String> preToolNames = preToolUseInputs.stream().map(PreToolUseHookInput::getToolName)
.filter(n -> n != null && !n.isEmpty()).collect(Collectors.toSet());
Set<String> postToolNames = postToolUseInputs.stream().map(PostToolUseHookInput::getToolName)
.filter(n -> n != null && !n.isEmpty()).collect(Collectors.toSet());
// Check if there's any overlap
boolean hasOverlap = preToolNames.stream().anyMatch(postToolNames::contains);
assertTrue(hasOverlap, "Expected the same tool to appear in both pre and post hooks");
}
}
/**
* Verifies that tool execution is denied when pre-tool-use returns deny.
*
* @see Snapshot: hooks/deny_tool_execution_when_pre_tool_use_returns_deny
*/
@Test
void testDenyToolExecutionWhenPreToolUseReturnsDeny() throws Exception {
ctx.configureForTest("hooks", "deny_tool_execution_when_pre_tool_use_returns_deny");
var preToolUseInputs = new ArrayList<PreToolUseHookInput>();
var config = new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
.setHooks(new SessionHooks().setOnPreToolUse((input, invocation) -> {
preToolUseInputs.add(input);
// Deny all tool calls
return CompletableFuture.completedFuture(PreToolUseHookOutput.deny());
}));
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(config).get();
// Create a file
Path testFile = ctx.getWorkDir().resolve("protected.txt");
String originalContent = "Original content that should not be modified";
Files.writeString(testFile, originalContent);
var response = session
.sendAndWait(
new MessageOptions().setPrompt("Edit protected.txt and replace 'Original' with 'Modified'"))
.get(60, TimeUnit.SECONDS);
// The hook should have been called
assertFalse(preToolUseInputs.isEmpty(), "Should have received preToolUse hook calls");
// The response should be defined
assertNotNull(response, "Response should not be null");
}
}
}