@gotgenes/pi-permission-system 5.2.1 → 5.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,299 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5
+
6
+ import { getGlobalConfigPath } from "../src/config-paths";
7
+ import piPermissionSystemExtension from "../src/index";
8
+ import type {
9
+ PermissionDecisionEvent,
10
+ PermissionsCheckReplyData,
11
+ PermissionsCheckRequest,
12
+ PermissionsPromptReplyData,
13
+ PermissionsPromptRequest,
14
+ PermissionsReadyEvent,
15
+ PermissionsRpcReply,
16
+ } from "../src/permission-events";
17
+ import {
18
+ emitDecisionEvent,
19
+ emitReadyEvent,
20
+ PERMISSIONS_DECISION_CHANNEL,
21
+ PERMISSIONS_PROTOCOL_VERSION,
22
+ PERMISSIONS_READY_CHANNEL,
23
+ PERMISSIONS_RPC_CHECK_CHANNEL,
24
+ PERMISSIONS_RPC_PROMPT_CHANNEL,
25
+ } from "../src/permission-events";
26
+
27
+ // ── Minimal EventBus stub ──────────────────────────────────────────────────
28
+
29
+ function makeEventBus() {
30
+ return {
31
+ emit: vi.fn(),
32
+ on: vi.fn().mockReturnValue(() => undefined),
33
+ };
34
+ }
35
+
36
+ // ── Constants ──────────────────────────────────────────────────────────────
37
+
38
+ describe("constants", () => {
39
+ it("PERMISSIONS_PROTOCOL_VERSION is 1", () => {
40
+ expect(PERMISSIONS_PROTOCOL_VERSION).toBe(1);
41
+ });
42
+
43
+ it("channel names have the correct values", () => {
44
+ expect(PERMISSIONS_READY_CHANNEL).toBe("permissions:ready");
45
+ expect(PERMISSIONS_DECISION_CHANNEL).toBe("permissions:decision");
46
+ expect(PERMISSIONS_RPC_CHECK_CHANNEL).toBe("permissions:rpc:check");
47
+ expect(PERMISSIONS_RPC_PROMPT_CHANNEL).toBe("permissions:rpc:prompt");
48
+ });
49
+ });
50
+
51
+ // ── emitReadyEvent ─────────────────────────────────────────────────────────
52
+
53
+ describe("emitReadyEvent", () => {
54
+ it("emits on the permissions:ready channel with protocol version", () => {
55
+ const bus = makeEventBus();
56
+ emitReadyEvent(bus);
57
+ expect(bus.emit).toHaveBeenCalledOnce();
58
+ expect(bus.emit).toHaveBeenCalledWith("permissions:ready", {
59
+ protocolVersion: 1,
60
+ });
61
+ });
62
+
63
+ it("emitted payload satisfies PermissionsReadyEvent shape", () => {
64
+ const bus = makeEventBus();
65
+ emitReadyEvent(bus);
66
+ const payload = bus.emit.mock.calls[0][1] as PermissionsReadyEvent;
67
+ expect(typeof payload.protocolVersion).toBe("number");
68
+ });
69
+ });
70
+
71
+ // ── emitDecisionEvent ──────────────────────────────────────────────────────
72
+
73
+ describe("emitDecisionEvent", () => {
74
+ function makeDecisionEvent(
75
+ overrides: Partial<PermissionDecisionEvent> = {},
76
+ ): PermissionDecisionEvent {
77
+ return {
78
+ surface: "bash",
79
+ value: "git status",
80
+ result: "allow",
81
+ resolution: "policy_allow",
82
+ origin: "global",
83
+ agentName: null,
84
+ matchedPattern: "*",
85
+ ...overrides,
86
+ };
87
+ }
88
+
89
+ it("emits on the permissions:decision channel", () => {
90
+ const bus = makeEventBus();
91
+ emitDecisionEvent(bus, makeDecisionEvent());
92
+ expect(bus.emit).toHaveBeenCalledOnce();
93
+ expect(bus.emit.mock.calls[0][0]).toBe("permissions:decision");
94
+ });
95
+
96
+ it("forwards the full payload unchanged", () => {
97
+ const bus = makeEventBus();
98
+ const event = makeDecisionEvent({
99
+ surface: "mcp",
100
+ value: "exa:search",
101
+ result: "deny",
102
+ resolution: "policy_deny",
103
+ origin: "project",
104
+ agentName: "Worker",
105
+ matchedPattern: "exa:*",
106
+ });
107
+ emitDecisionEvent(bus, event);
108
+ expect(bus.emit.mock.calls[0][1]).toEqual(event);
109
+ });
110
+
111
+ it("accepts all defined resolution values", () => {
112
+ const resolutions: PermissionDecisionEvent["resolution"][] = [
113
+ "policy_allow",
114
+ "policy_deny",
115
+ "session_approved",
116
+ "infrastructure_auto_allowed",
117
+ "user_approved",
118
+ "user_approved_for_session",
119
+ "user_denied",
120
+ "auto_approved",
121
+ "confirmation_unavailable",
122
+ ];
123
+ const bus = makeEventBus();
124
+ for (const resolution of resolutions) {
125
+ emitDecisionEvent(bus, makeDecisionEvent({ resolution }));
126
+ }
127
+ expect(bus.emit).toHaveBeenCalledTimes(resolutions.length);
128
+ });
129
+
130
+ it("accepts null for optional fields", () => {
131
+ const bus = makeEventBus();
132
+ emitDecisionEvent(
133
+ bus,
134
+ makeDecisionEvent({
135
+ origin: null,
136
+ agentName: null,
137
+ matchedPattern: null,
138
+ }),
139
+ );
140
+ const payload = bus.emit.mock.calls[0][1] as PermissionDecisionEvent;
141
+ expect(payload.origin).toBeNull();
142
+ expect(payload.agentName).toBeNull();
143
+ expect(payload.matchedPattern).toBeNull();
144
+ });
145
+ });
146
+
147
+ // ── Type-shape compile-time checks (runtime assertions on literal values) ──
148
+
149
+ describe("type shapes (PermissionsRpcReply)", () => {
150
+ it("success reply has success=true and protocolVersion", () => {
151
+ const reply: PermissionsRpcReply<{ result: "allow" }> = {
152
+ success: true,
153
+ protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
154
+ data: { result: "allow" },
155
+ };
156
+ expect(reply.success).toBe(true);
157
+ expect(reply.protocolVersion).toBe(1);
158
+ });
159
+
160
+ it("error reply has success=false and error string", () => {
161
+ const reply: PermissionsRpcReply = {
162
+ success: false,
163
+ protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
164
+ error: "no_ui",
165
+ };
166
+ expect(reply.success).toBe(false);
167
+ if (!reply.success) {
168
+ expect(reply.error).toBe("no_ui");
169
+ }
170
+ });
171
+ });
172
+
173
+ describe("type shapes (PermissionsCheckRequest)", () => {
174
+ it("minimal request requires requestId and surface", () => {
175
+ const req: PermissionsCheckRequest = {
176
+ requestId: "abc-123",
177
+ surface: "bash",
178
+ };
179
+ expect(req.requestId).toBe("abc-123");
180
+ expect(req.surface).toBe("bash");
181
+ });
182
+
183
+ it("optional fields are accepted", () => {
184
+ const req: PermissionsCheckRequest = {
185
+ requestId: "abc-123",
186
+ surface: "bash",
187
+ value: "git status",
188
+ agentName: "Worker",
189
+ };
190
+ expect(req.value).toBe("git status");
191
+ expect(req.agentName).toBe("Worker");
192
+ });
193
+ });
194
+
195
+ describe("type shapes (PermissionsCheckReplyData)", () => {
196
+ it("has result, matchedPattern, origin", () => {
197
+ const data: PermissionsCheckReplyData = {
198
+ result: "ask",
199
+ matchedPattern: null,
200
+ origin: "builtin",
201
+ };
202
+ expect(data.result).toBe("ask");
203
+ });
204
+ });
205
+
206
+ describe("type shapes (PermissionsPromptRequest)", () => {
207
+ it("minimal request requires requestId, surface, value, message", () => {
208
+ const req: PermissionsPromptRequest = {
209
+ requestId: "def-456",
210
+ surface: "bash",
211
+ value: "rm -rf /tmp",
212
+ message: "Allow rm -rf /tmp?",
213
+ };
214
+ expect(req.requestId).toBe("def-456");
215
+ });
216
+
217
+ it("optional agentName and sessionLabel are accepted", () => {
218
+ const req: PermissionsPromptRequest = {
219
+ requestId: "def-456",
220
+ surface: "bash",
221
+ value: "rm -rf /tmp",
222
+ message: "Allow rm -rf /tmp?",
223
+ agentName: "Explore",
224
+ sessionLabel: "Allow rm *",
225
+ };
226
+ expect(req.agentName).toBe("Explore");
227
+ expect(req.sessionLabel).toBe("Allow rm *");
228
+ });
229
+ });
230
+
231
+ describe("type shapes (PermissionsPromptReplyData)", () => {
232
+ it("approved reply has approved=true and state", () => {
233
+ const data: PermissionsPromptReplyData = {
234
+ approved: true,
235
+ state: "approved_for_session",
236
+ };
237
+ expect(data.approved).toBe(true);
238
+ expect(data.state).toBe("approved_for_session");
239
+ });
240
+
241
+ it("denied reply may include denialReason", () => {
242
+ const data: PermissionsPromptReplyData = {
243
+ approved: false,
244
+ state: "denied_with_reason",
245
+ denialReason: "Too risky",
246
+ };
247
+ expect(data.denialReason).toBe("Too risky");
248
+ });
249
+ });
250
+
251
+ // ── piPermissionSystemExtension emits permissions:ready ────────────────────
252
+
253
+ describe("piPermissionSystemExtension ready event wiring", () => {
254
+ let baseDir: string;
255
+ let originalAgentDir: string | undefined;
256
+
257
+ beforeEach(() => {
258
+ baseDir = mkdtempSync(join(tmpdir(), "pi-perm-events-test-"));
259
+ originalAgentDir = process.env.PI_CODING_AGENT_DIR;
260
+ const globalConfigPath = getGlobalConfigPath(baseDir);
261
+ mkdirSync(dirname(globalConfigPath), { recursive: true });
262
+ mkdirSync(join(baseDir, "agents"), { recursive: true });
263
+ writeFileSync(
264
+ globalConfigPath,
265
+ JSON.stringify({ permission: { "*": "ask" } }) + "\n",
266
+ "utf8",
267
+ );
268
+ process.env.PI_CODING_AGENT_DIR = baseDir;
269
+ });
270
+
271
+ afterEach(() => {
272
+ if (originalAgentDir === undefined) {
273
+ delete process.env.PI_CODING_AGENT_DIR;
274
+ } else {
275
+ process.env.PI_CODING_AGENT_DIR = originalAgentDir;
276
+ }
277
+ rmSync(baseDir, { recursive: true, force: true });
278
+ });
279
+
280
+ it("emits permissions:ready with protocolVersion when extension loads", () => {
281
+ const emitSpy = vi.fn();
282
+ piPermissionSystemExtension({
283
+ on: vi.fn(),
284
+ registerCommand: vi.fn(),
285
+ getAllTools: vi.fn().mockReturnValue([]),
286
+ setActiveTools: vi.fn(),
287
+ registerProvider: vi.fn(),
288
+ events: { emit: emitSpy, on: vi.fn().mockReturnValue(() => undefined) },
289
+ } as never);
290
+
291
+ const readyCalls = emitSpy.mock.calls.filter(
292
+ ([channel]) => channel === PERMISSIONS_READY_CHANNEL,
293
+ );
294
+ expect(readyCalls).toHaveLength(1);
295
+ expect(readyCalls[0][1]).toEqual({
296
+ protocolVersion: PERMISSIONS_PROTOCOL_VERSION,
297
+ });
298
+ });
299
+ });
@@ -78,7 +78,11 @@ describe("PermissionPrompter", () => {
78
78
 
79
79
  const decision = await prompter.prompt(makeCtx(false), makeDetails());
80
80
 
81
- expect(decision).toEqual({ approved: true, state: "approved" });
81
+ expect(decision).toEqual({
82
+ approved: true,
83
+ state: "approved",
84
+ autoApproved: true,
85
+ });
82
86
  expect(mockConfirmPermission).not.toHaveBeenCalled();
83
87
  });
84
88
 
@@ -175,6 +175,7 @@ function createToolCallHarness(
175
175
  registerProvider: (): void => {},
176
176
  events: {
177
177
  emit: (): void => {},
178
+ on: (): (() => void) => () => undefined,
178
179
  },
179
180
  } as never);
180
181
  } finally {
@@ -60,6 +60,7 @@ describe("session_start handler consolidation", () => {
60
60
  registerProvider: (): void => {},
61
61
  events: {
62
62
  emit: (): void => {},
63
+ on: (): (() => void) => () => undefined,
63
64
  },
64
65
  } as never);
65
66
 
@@ -82,6 +83,7 @@ describe("session_start handler consolidation", () => {
82
83
  registerProvider: (): void => {},
83
84
  events: {
84
85
  emit: (): void => {},
86
+ on: (): (() => void) => () => undefined,
85
87
  },
86
88
  } as never);
87
89