@komarspn/pi-permission-system 16.0.2
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.
- package/CHANGELOG.md +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
suggestBashPattern,
|
|
4
|
+
suggestMcpPattern,
|
|
5
|
+
suggestSessionPattern,
|
|
6
|
+
} from "#src/pattern-suggest";
|
|
7
|
+
|
|
8
|
+
describe("suggestBashPattern", () => {
|
|
9
|
+
it("returns <command> <subcommand> * using the arity table", () => {
|
|
10
|
+
// git arity=2: include the subcommand in the prefix.
|
|
11
|
+
expect(suggestBashPattern("git status --short")).toBe("git status *");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("appends trailing * when arity covers all tokens (multi-word script name)", () => {
|
|
15
|
+
// npm run arity=3: prefix covers all three tokens → trailing wildcard.
|
|
16
|
+
expect(suggestBashPattern("npm run build")).toBe("npm run build*");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("returns the exact command when there are no arguments", () => {
|
|
20
|
+
expect(suggestBashPattern("ls")).toBe("ls");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("trims leading and trailing whitespace before lookup", () => {
|
|
24
|
+
// git arity=2, tokens=["git","log"], prefix covers all → trailing wildcard.
|
|
25
|
+
expect(suggestBashPattern(" git log ")).toBe("git log*");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("handles empty string gracefully", () => {
|
|
29
|
+
expect(suggestBashPattern("")).toBe("");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("falls back to first-word prefix for unknown commands", () => {
|
|
33
|
+
expect(suggestBashPattern("mytool --verbose run")).toBe("mytool *");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("returns first-word * for known arity-1 commands with args", () => {
|
|
37
|
+
expect(suggestBashPattern("rm -rf node_modules")).toBe("rm *");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("produces tighter pattern for docker compose than plain docker", () => {
|
|
41
|
+
expect(suggestBashPattern("docker compose up --build")).toBe(
|
|
42
|
+
"docker compose up *",
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("strips leading comment lines and suggests based on the actual command", () => {
|
|
47
|
+
expect(
|
|
48
|
+
suggestBashPattern(
|
|
49
|
+
"# Check debug logs\nfind /home -path '*debug*' -type f",
|
|
50
|
+
),
|
|
51
|
+
).toBe("find *");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("strips multiple leading comment lines", () => {
|
|
55
|
+
expect(suggestBashPattern("# Step 1\n# Step 2\ngit status --short")).toBe(
|
|
56
|
+
"git status *",
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("returns empty for comment-only input", () => {
|
|
61
|
+
expect(suggestBashPattern("# just a comment")).toBe("");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("handles mixed comment and command lines", () => {
|
|
65
|
+
expect(suggestBashPattern("# description\nrm -rf ./build; echo done")).toBe(
|
|
66
|
+
"rm *",
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("suggestMcpPattern", () => {
|
|
72
|
+
it("suggests server:* for a qualified target (colon-separated)", () => {
|
|
73
|
+
expect(suggestMcpPattern("exa:search")).toBe("exa:*");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("suggests server_* for a munged target (underscore-separated)", () => {
|
|
77
|
+
expect(suggestMcpPattern("exa_search")).toBe("exa_*");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("suggests * for a bare 'mcp' target", () => {
|
|
81
|
+
expect(suggestMcpPattern("mcp")).toBe("*");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("suggests * for a plain tool name with no server prefix", () => {
|
|
85
|
+
expect(suggestMcpPattern("search")).toBe("*");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("prefers colon over underscore when both are present", () => {
|
|
89
|
+
// Qualified names contain ':'; the colon check runs first.
|
|
90
|
+
expect(suggestMcpPattern("my-server:some_tool")).toBe("my-server:*");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("suggestSessionPattern", () => {
|
|
95
|
+
describe("bash surface", () => {
|
|
96
|
+
it("returns arity-aware subcommand pattern for multi-word command", () => {
|
|
97
|
+
// git arity=2: include the subcommand token in the prefix.
|
|
98
|
+
const result = suggestSessionPattern("bash", "git status --short");
|
|
99
|
+
expect(result).toMatchObject({
|
|
100
|
+
surface: "bash",
|
|
101
|
+
pattern: "git status *",
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("returns exact command for single-word bash command", () => {
|
|
106
|
+
const result = suggestSessionPattern("bash", "ls");
|
|
107
|
+
expect(result).toMatchObject({ surface: "bash", pattern: "ls" });
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("mcp surface", () => {
|
|
112
|
+
it("returns mcp surface with server:* for qualified target", () => {
|
|
113
|
+
const result = suggestSessionPattern("mcp", "exa:search");
|
|
114
|
+
expect(result).toMatchObject({ surface: "mcp", pattern: "exa:*" });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("returns mcp surface with server_* for munged target", () => {
|
|
118
|
+
const result = suggestSessionPattern("mcp", "exa_search");
|
|
119
|
+
expect(result).toMatchObject({ surface: "mcp", pattern: "exa_*" });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("returns * for bare mcp target", () => {
|
|
123
|
+
const result = suggestSessionPattern("mcp", "mcp");
|
|
124
|
+
expect(result).toMatchObject({ surface: "mcp", pattern: "*" });
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe("skill surface", () => {
|
|
129
|
+
it("returns exact skill name as pattern", () => {
|
|
130
|
+
const result = suggestSessionPattern("skill", "librarian");
|
|
131
|
+
expect(result).toMatchObject({ surface: "skill", pattern: "librarian" });
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe("external_directory surface", () => {
|
|
136
|
+
it("returns parent-directory glob from deriveApprovalPattern", () => {
|
|
137
|
+
const result = suggestSessionPattern(
|
|
138
|
+
"external_directory",
|
|
139
|
+
"/tmp/foo.txt",
|
|
140
|
+
);
|
|
141
|
+
expect(result).toMatchObject({
|
|
142
|
+
surface: "external_directory",
|
|
143
|
+
pattern: "/tmp/*",
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("path surface", () => {
|
|
149
|
+
it("returns directory-scoped pattern for a file path", () => {
|
|
150
|
+
const result = suggestSessionPattern("path", "src/.env");
|
|
151
|
+
expect(result).toMatchObject({
|
|
152
|
+
surface: "path",
|
|
153
|
+
pattern: "src/*",
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("label includes path pattern", () => {
|
|
158
|
+
const result = suggestSessionPattern("path", "src/.env");
|
|
159
|
+
expect(result.label).toBe('Yes, allow path "src/*" for this session');
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("path-bearing tool surfaces", () => {
|
|
164
|
+
it("returns directory-scoped pattern for read with a file path", () => {
|
|
165
|
+
const result = suggestSessionPattern("read", "/outside/project/file.ts");
|
|
166
|
+
expect(result).toMatchObject({
|
|
167
|
+
surface: "read",
|
|
168
|
+
pattern: "/outside/project/*",
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("returns directory-scoped pattern for write with a file path", () => {
|
|
173
|
+
const result = suggestSessionPattern("write", "src/main.ts");
|
|
174
|
+
expect(result).toMatchObject({
|
|
175
|
+
surface: "write",
|
|
176
|
+
pattern: "src/*",
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("returns * when value is '*' (fallback)", () => {
|
|
181
|
+
const result = suggestSessionPattern("read", "*");
|
|
182
|
+
expect(result).toMatchObject({ surface: "read", pattern: "*" });
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("label includes the path pattern for path-bearing tools", () => {
|
|
186
|
+
const result = suggestSessionPattern("read", "/tmp/data/file.txt");
|
|
187
|
+
expect(result.label).toBe(
|
|
188
|
+
'Yes, allow read "/tmp/data/*" for this session',
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("label shows tool name when pattern is *", () => {
|
|
193
|
+
const result = suggestSessionPattern("find", "*");
|
|
194
|
+
expect(result.label).toBe('Yes, allow tool "find" for this session');
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe("non-path-bearing tool surfaces", () => {
|
|
199
|
+
it("returns * for extension tools", () => {
|
|
200
|
+
const result = suggestSessionPattern("my_extension_tool", "*");
|
|
201
|
+
expect(result).toMatchObject({
|
|
202
|
+
surface: "my_extension_tool",
|
|
203
|
+
pattern: "*",
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe("label field", () => {
|
|
209
|
+
it("bash label includes surface prefix and pattern", () => {
|
|
210
|
+
const result = suggestSessionPattern("bash", "git status");
|
|
211
|
+
expect(result.label).toBe(
|
|
212
|
+
'Yes, allow bash "git status*" for this session',
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("mcp label includes surface prefix and pattern", () => {
|
|
217
|
+
const result = suggestSessionPattern("mcp", "exa:search");
|
|
218
|
+
expect(result.label).toBe('Yes, allow mcp tool "exa:*" for this session');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("skill label includes surface prefix", () => {
|
|
222
|
+
const result = suggestSessionPattern("skill", "librarian");
|
|
223
|
+
expect(result.label).toBe(
|
|
224
|
+
'Yes, allow skill "librarian" for this session',
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("external_directory label includes surface prefix", () => {
|
|
229
|
+
const result = suggestSessionPattern(
|
|
230
|
+
"external_directory",
|
|
231
|
+
"/tmp/foo.txt",
|
|
232
|
+
);
|
|
233
|
+
expect(result.label).toBe(
|
|
234
|
+
'Yes, allow access to external directory "/tmp/*" for this session',
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("path-bearing tool label includes path pattern", () => {
|
|
239
|
+
const result = suggestSessionPattern("edit", "src/file.ts");
|
|
240
|
+
expect(result.label).toBe('Yes, allow edit "src/*" for this session');
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("tool label shows tool name when value is *", () => {
|
|
244
|
+
const result = suggestSessionPattern("edit", "*");
|
|
245
|
+
expect(result.label).toBe('Yes, allow tool "edit" for this session');
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
});
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
createDeniedPermissionDecision,
|
|
4
|
+
isPermissionDecisionState,
|
|
5
|
+
normalizePermissionDenialReason,
|
|
6
|
+
type PermissionDecisionUi,
|
|
7
|
+
requestPermissionDecisionFromUi,
|
|
8
|
+
} from "#src/permission-dialog";
|
|
9
|
+
|
|
10
|
+
describe("isPermissionDecisionState", () => {
|
|
11
|
+
it("accepts approved", () => {
|
|
12
|
+
expect(isPermissionDecisionState("approved")).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("accepts denied", () => {
|
|
16
|
+
expect(isPermissionDecisionState("denied")).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("accepts denied_with_reason", () => {
|
|
20
|
+
expect(isPermissionDecisionState("denied_with_reason")).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("accepts approved_for_session", () => {
|
|
24
|
+
expect(isPermissionDecisionState("approved_for_session")).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("accepts approved_for_project", () => {
|
|
28
|
+
expect(isPermissionDecisionState("approved_for_project")).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("accepts approved_globally", () => {
|
|
32
|
+
expect(isPermissionDecisionState("approved_globally")).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("rejects unknown strings", () => {
|
|
36
|
+
expect(isPermissionDecisionState("unknown")).toBe(false);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("rejects non-strings", () => {
|
|
40
|
+
expect(isPermissionDecisionState(42)).toBe(false);
|
|
41
|
+
expect(isPermissionDecisionState(null)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("requestPermissionDecisionFromUi", () => {
|
|
46
|
+
it("returns approved when user selects Yes", async () => {
|
|
47
|
+
const ui: PermissionDecisionUi = {
|
|
48
|
+
select: vi.fn().mockResolvedValue("Yes"),
|
|
49
|
+
input: vi.fn(),
|
|
50
|
+
};
|
|
51
|
+
const result = await requestPermissionDecisionFromUi(
|
|
52
|
+
ui,
|
|
53
|
+
"Title",
|
|
54
|
+
"Message",
|
|
55
|
+
);
|
|
56
|
+
expect(result).toEqual({ approved: true, state: "approved" });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns approved_for_session when user selects session option", async () => {
|
|
60
|
+
const ui: PermissionDecisionUi = {
|
|
61
|
+
select: vi.fn().mockResolvedValue("Yes, for this session"),
|
|
62
|
+
input: vi.fn(),
|
|
63
|
+
};
|
|
64
|
+
const result = await requestPermissionDecisionFromUi(
|
|
65
|
+
ui,
|
|
66
|
+
"Title",
|
|
67
|
+
"Message",
|
|
68
|
+
);
|
|
69
|
+
expect(result).toEqual({ approved: true, state: "approved_for_session" });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns approved_for_project when user selects project option", async () => {
|
|
73
|
+
const ui: PermissionDecisionUi = {
|
|
74
|
+
select: vi.fn().mockResolvedValue("Yes, always for this project"),
|
|
75
|
+
input: vi.fn(),
|
|
76
|
+
};
|
|
77
|
+
const result = await requestPermissionDecisionFromUi(
|
|
78
|
+
ui,
|
|
79
|
+
"Title",
|
|
80
|
+
"Message",
|
|
81
|
+
);
|
|
82
|
+
expect(result).toEqual({ approved: true, state: "approved_for_project" });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("returns approved_globally when user selects all-projects option", async () => {
|
|
86
|
+
const ui: PermissionDecisionUi = {
|
|
87
|
+
select: vi.fn().mockResolvedValue("Yes, always for all projects"),
|
|
88
|
+
input: vi.fn(),
|
|
89
|
+
};
|
|
90
|
+
const result = await requestPermissionDecisionFromUi(
|
|
91
|
+
ui,
|
|
92
|
+
"Title",
|
|
93
|
+
"Message",
|
|
94
|
+
);
|
|
95
|
+
expect(result).toEqual({ approved: true, state: "approved_globally" });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("returns denied when user selects No", async () => {
|
|
99
|
+
const ui: PermissionDecisionUi = {
|
|
100
|
+
select: vi.fn().mockResolvedValue("No"),
|
|
101
|
+
input: vi.fn(),
|
|
102
|
+
};
|
|
103
|
+
const result = await requestPermissionDecisionFromUi(
|
|
104
|
+
ui,
|
|
105
|
+
"Title",
|
|
106
|
+
"Message",
|
|
107
|
+
);
|
|
108
|
+
expect(result).toEqual({ approved: false, state: "denied" });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("returns denied_with_reason when user provides reason", async () => {
|
|
112
|
+
const ui: PermissionDecisionUi = {
|
|
113
|
+
select: vi.fn().mockResolvedValue("No, provide reason"),
|
|
114
|
+
input: vi.fn().mockResolvedValue("not now"),
|
|
115
|
+
};
|
|
116
|
+
const result = await requestPermissionDecisionFromUi(
|
|
117
|
+
ui,
|
|
118
|
+
"Title",
|
|
119
|
+
"Message",
|
|
120
|
+
);
|
|
121
|
+
expect(result).toEqual({
|
|
122
|
+
approved: false,
|
|
123
|
+
state: "denied_with_reason",
|
|
124
|
+
denialReason: "not now",
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("returns denied when user selects deny-with-reason but gives empty input", async () => {
|
|
129
|
+
const ui: PermissionDecisionUi = {
|
|
130
|
+
select: vi.fn().mockResolvedValue("No, provide reason"),
|
|
131
|
+
input: vi.fn().mockResolvedValue(""),
|
|
132
|
+
};
|
|
133
|
+
const result = await requestPermissionDecisionFromUi(
|
|
134
|
+
ui,
|
|
135
|
+
"Title",
|
|
136
|
+
"Message",
|
|
137
|
+
);
|
|
138
|
+
expect(result).toEqual({ approved: false, state: "denied" });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("returns denied when user dismisses dialog (undefined)", async () => {
|
|
142
|
+
const ui: PermissionDecisionUi = {
|
|
143
|
+
select: vi.fn().mockResolvedValue(undefined),
|
|
144
|
+
input: vi.fn(),
|
|
145
|
+
};
|
|
146
|
+
const result = await requestPermissionDecisionFromUi(
|
|
147
|
+
ui,
|
|
148
|
+
"Title",
|
|
149
|
+
"Message",
|
|
150
|
+
);
|
|
151
|
+
expect(result).toEqual({ approved: false, state: "denied" });
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("passes six options to ui.select", async () => {
|
|
155
|
+
const selectFn = vi.fn().mockResolvedValue("Yes");
|
|
156
|
+
const ui: PermissionDecisionUi = {
|
|
157
|
+
select: selectFn,
|
|
158
|
+
input: vi.fn(),
|
|
159
|
+
};
|
|
160
|
+
await requestPermissionDecisionFromUi(ui, "Title", "Message");
|
|
161
|
+
const options = selectFn.mock.calls[0][1] as string[];
|
|
162
|
+
expect(options).toEqual([
|
|
163
|
+
"Yes",
|
|
164
|
+
"Yes, for this session",
|
|
165
|
+
"Yes, always for this project",
|
|
166
|
+
"Yes, always for all projects",
|
|
167
|
+
"No",
|
|
168
|
+
"No, provide reason",
|
|
169
|
+
]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("uses custom sessionLabel when provided", async () => {
|
|
173
|
+
const selectFn = vi.fn().mockResolvedValue("Yes");
|
|
174
|
+
const ui: PermissionDecisionUi = {
|
|
175
|
+
select: selectFn,
|
|
176
|
+
input: vi.fn(),
|
|
177
|
+
};
|
|
178
|
+
await requestPermissionDecisionFromUi(ui, "Title", "Message", {
|
|
179
|
+
sessionLabel: 'Yes, allow "git *" for this session',
|
|
180
|
+
});
|
|
181
|
+
const options = selectFn.mock.calls[0][1] as string[];
|
|
182
|
+
expect(options[1]).toBe('Yes, allow "git *" for this session');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("still returns approved_for_session when user selects the custom session label", async () => {
|
|
186
|
+
const customLabel = 'Yes, allow "git *" for this session';
|
|
187
|
+
const ui: PermissionDecisionUi = {
|
|
188
|
+
select: vi.fn().mockResolvedValue(customLabel),
|
|
189
|
+
input: vi.fn(),
|
|
190
|
+
};
|
|
191
|
+
const result = await requestPermissionDecisionFromUi(
|
|
192
|
+
ui,
|
|
193
|
+
"Title",
|
|
194
|
+
"Message",
|
|
195
|
+
{ sessionLabel: customLabel },
|
|
196
|
+
);
|
|
197
|
+
expect(result).toEqual({ approved: true, state: "approved_for_session" });
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("falls back to default session label when no options provided", async () => {
|
|
201
|
+
const selectFn = vi.fn().mockResolvedValue("Yes");
|
|
202
|
+
const ui: PermissionDecisionUi = {
|
|
203
|
+
select: selectFn,
|
|
204
|
+
input: vi.fn(),
|
|
205
|
+
};
|
|
206
|
+
await requestPermissionDecisionFromUi(ui, "Title", "Message");
|
|
207
|
+
const options = selectFn.mock.calls[0][1] as string[];
|
|
208
|
+
expect(options[1]).toBe("Yes, for this session");
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("normalizePermissionDenialReason", () => {
|
|
213
|
+
it("returns trimmed string for non-empty input", () => {
|
|
214
|
+
expect(normalizePermissionDenialReason(" reason ")).toBe("reason");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("returns undefined for empty string", () => {
|
|
218
|
+
expect(normalizePermissionDenialReason("")).toBeUndefined();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("returns undefined for non-string", () => {
|
|
222
|
+
expect(normalizePermissionDenialReason(42)).toBeUndefined();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("createDeniedPermissionDecision", () => {
|
|
227
|
+
it("returns denied_with_reason when reason provided", () => {
|
|
228
|
+
expect(createDeniedPermissionDecision("nope")).toEqual({
|
|
229
|
+
approved: false,
|
|
230
|
+
state: "denied_with_reason",
|
|
231
|
+
denialReason: "nope",
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("returns denied when no reason", () => {
|
|
236
|
+
expect(createDeniedPermissionDecision()).toEqual({
|
|
237
|
+
approved: false,
|
|
238
|
+
state: "denied",
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
});
|