-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpermissions.test.ts
More file actions
219 lines (169 loc) · 7.5 KB
/
Copy pathpermissions.test.ts
File metadata and controls
219 lines (169 loc) · 7.5 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { readFile, writeFile } from "fs/promises";
import { join } from "path";
import { describe, expect, it } from "vitest";
import type { PermissionRequest, PermissionRequestResult } from "../../src/index.js";
import { approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
describe("Permission callbacks", async () => {
const { copilotClient: client, workDir } = await createSdkTestContext();
it("should invoke permission handler for write operations", async () => {
const permissionRequests: PermissionRequest[] = [];
const session = await client.createSession({
onPermissionRequest: (request, invocation) => {
permissionRequests.push(request);
expect(invocation.sessionId).toBe(session.sessionId);
// Approve the permission
const result: PermissionRequestResult = { kind: "approved" };
return result;
},
});
await writeFile(join(workDir, "test.txt"), "original content");
await session.sendAndWait({
prompt: "Edit test.txt and replace 'original' with 'modified'",
});
// Should have received at least one permission request
expect(permissionRequests.length).toBeGreaterThan(0);
// Should include write permission request
const writeRequests = permissionRequests.filter((req) => req.kind === "write");
expect(writeRequests.length).toBeGreaterThan(0);
await session.destroy();
});
it("should deny permission when handler returns denied", async () => {
const session = await client.createSession({
onPermissionRequest: () => {
return { kind: "denied-interactively-by-user" };
},
});
const originalContent = "protected content";
const testFile = join(workDir, "protected.txt");
await writeFile(testFile, originalContent);
await session.sendAndWait({
prompt: "Edit protected.txt and replace 'protected' with 'hacked'.",
});
// Verify the file was NOT modified
const content = await readFile(testFile, "utf-8");
expect(content).toBe(originalContent);
await session.destroy();
});
it("should deny tool operations when handler explicitly denies", async () => {
let permissionDenied = false;
const session = await client.createSession({
onPermissionRequest: () => ({
kind: "denied-no-approval-rule-and-could-not-request-from-user",
}),
});
session.on((event) => {
if (
event.type === "tool.execution_complete" &&
!event.data.success &&
event.data.error?.message.includes("Permission denied")
) {
permissionDenied = true;
}
});
await session.sendAndWait({ prompt: "Run 'node --version'" });
expect(permissionDenied).toBe(true);
await session.destroy();
});
it("should deny tool operations when handler explicitly denies after resume", async () => {
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session1.sessionId;
await session1.sendAndWait({ prompt: "What is 1+1?" });
const session2 = await client.resumeSession(sessionId, {
onPermissionRequest: () => ({
kind: "denied-no-approval-rule-and-could-not-request-from-user",
}),
});
let permissionDenied = false;
session2.on((event) => {
if (
event.type === "tool.execution_complete" &&
!event.data.success &&
event.data.error?.message.includes("Permission denied")
) {
permissionDenied = true;
}
});
await session2.sendAndWait({ prompt: "Run 'node --version'" });
expect(permissionDenied).toBe(true);
await session2.destroy();
});
it("should work with approve-all permission handler", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const message = await session.sendAndWait({
prompt: "What is 2+2?",
});
expect(message?.data.content).toContain("4");
await session.destroy();
});
it("should handle async permission handler", async () => {
const permissionRequests: PermissionRequest[] = [];
const session = await client.createSession({
onPermissionRequest: async (request, _invocation) => {
permissionRequests.push(request);
// Simulate async permission check (e.g., user prompt)
await new Promise((resolve) => setTimeout(resolve, 10));
return { kind: "approved" };
},
});
await session.sendAndWait({
prompt: "Run 'echo test' and tell me what happens",
});
expect(permissionRequests.length).toBeGreaterThan(0);
await session.destroy();
});
it("should resume session with permission handler", async () => {
const permissionRequests: PermissionRequest[] = [];
// Create initial session
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session1.sessionId;
await session1.sendAndWait({ prompt: "What is 1+1?" });
// Resume with permission handler
const session2 = await client.resumeSession(sessionId, {
onPermissionRequest: (request) => {
permissionRequests.push(request);
return { kind: "approved" };
},
});
await session2.sendAndWait({
prompt: "Run 'echo resumed' for me",
});
// Should have permission requests from resumed session
expect(permissionRequests.length).toBeGreaterThan(0);
await session2.destroy();
});
it("should handle permission handler errors gracefully", async () => {
const session = await client.createSession({
onPermissionRequest: () => {
throw new Error("Handler error");
},
});
const message = await session.sendAndWait({
prompt: "Run 'echo test'. If you can't, say 'failed'.",
});
// Should handle the error and deny permission
expect(message?.data.content?.toLowerCase()).toMatch(/fail|cannot|unable|permission/);
await session.destroy();
});
it("should receive toolCallId in permission requests", async () => {
let receivedToolCallId = false;
const session = await client.createSession({
onPermissionRequest: (request) => {
if (request.toolCallId) {
receivedToolCallId = true;
expect(typeof request.toolCallId).toBe("string");
expect(request.toolCallId.length).toBeGreaterThan(0);
}
return { kind: "approved" };
},
});
await session.sendAndWait({
prompt: "Run 'echo test'",
});
expect(receivedToolCallId).toBe(true);
await session.destroy();
});
});