forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermissionTests.cs
More file actions
192 lines (158 loc) · 6.53 KB
/
Copy pathPermissionTests.cs
File metadata and controls
192 lines (158 loc) · 6.53 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Test.Harness;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public class PermissionTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "permissions", output)
{
[Fact]
public async Task Should_Invoke_Permission_Handler_For_Write_Operations()
{
var permissionRequests = new List<PermissionRequest>();
CopilotSession? session = null;
session = await Client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
permissionRequests.Add(request);
Assert.Equal(session!.SessionId, invocation.SessionId);
return Task.FromResult(new PermissionRequestResult { Kind = "approved" });
}
});
await File.WriteAllTextAsync(Path.Combine(Ctx.WorkDir, "test.txt"), "original content");
await session.SendAsync(new MessageOptions
{
Prompt = "Edit test.txt and replace 'original' with 'modified'"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
// Should have received at least one permission request
Assert.NotEmpty(permissionRequests);
// Should include write permission request
Assert.Contains(permissionRequests, r => r.Kind == "write");
}
[Fact]
public async Task Should_Deny_Permission_When_Handler_Returns_Denied()
{
var session = await Client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
return Task.FromResult(new PermissionRequestResult
{
Kind = "denied-interactively-by-user"
});
}
});
var testFilePath = Path.Combine(Ctx.WorkDir, "protected.txt");
await File.WriteAllTextAsync(testFilePath, "protected content");
await session.SendAsync(new MessageOptions
{
Prompt = "Edit protected.txt and replace 'protected' with 'hacked'."
});
await TestHelper.GetFinalAssistantMessageAsync(session);
// Verify the file was NOT modified
var content = await File.ReadAllTextAsync(testFilePath);
Assert.Equal("protected content", content);
}
[Fact]
public async Task Should_Work_Without_Permission_Handler__Default_Behavior_()
{
// Create session without permission handler
var session = await Client.CreateSessionAsync(new SessionConfig());
await session.SendAsync(new MessageOptions
{
Prompt = "What is 2+2?"
});
var message = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.Contains("4", message?.Data.Content ?? string.Empty);
}
[Fact]
public async Task Should_Handle_Async_Permission_Handler()
{
var permissionRequestReceived = false;
var session = await Client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = async (request, invocation) =>
{
permissionRequestReceived = true;
// Simulate async permission check
await Task.Delay(10);
return new PermissionRequestResult { Kind = "approved" };
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test' and tell me what happens"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.True(permissionRequestReceived, "Permission request should have been received");
}
[Fact]
public async Task Should_Resume_Session_With_Permission_Handler()
{
var permissionRequestReceived = false;
// Create session without permission handler
var session1 = await Client.CreateSessionAsync();
var sessionId = session1.SessionId;
await session1.SendAsync(new MessageOptions { Prompt = "What is 1+1?" });
await TestHelper.GetFinalAssistantMessageAsync(session1);
// Resume with permission handler
var session2 = await Client.ResumeSessionAsync(sessionId, new ResumeSessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
permissionRequestReceived = true;
return Task.FromResult(new PermissionRequestResult { Kind = "approved" });
}
});
await session2.SendAsync(new MessageOptions
{
Prompt = "Run 'echo resumed' for me"
});
await TestHelper.GetFinalAssistantMessageAsync(session2);
Assert.True(permissionRequestReceived, "Permission request should have been received");
}
[Fact]
public async Task Should_Handle_Permission_Handler_Errors_Gracefully()
{
var session = await Client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
// Simulate an error in the handler
throw new InvalidOperationException("Handler error");
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test'. If you can't, say 'failed'."
});
var message = await TestHelper.GetFinalAssistantMessageAsync(session);
// Should handle the error and deny permission
Assert.Matches("fail|cannot|unable|permission", message?.Data.Content?.ToLowerInvariant() ?? string.Empty);
}
[Fact]
public async Task Should_Receive_ToolCallId_In_Permission_Requests()
{
var receivedToolCallId = false;
var session = await Client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
if (!string.IsNullOrEmpty(request.ToolCallId))
{
receivedToolCallId = true;
}
return Task.FromResult(new PermissionRequestResult { Kind = "approved" });
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test'"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.True(receivedToolCallId, "Should have received toolCallId in permission request");
}
}