This repository was archived by the owner on Apr 3, 2026. It is now read-only.
forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermissions.test.ts
More file actions
166 lines (126 loc) · 5.71 KB
/
permissions.test.ts
File metadata and controls
166 lines (126 loc) · 5.71 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
/*---------------------------------------------------------------------------------------------
* 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 { 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 work without permission handler (default behavior)", async () => {
// Create session without onPermissionRequest handler
const session = await client.createSession();
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 session without permission handler
const session1 = await client.createSession();
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();
});
});