@gotgenes/pi-permission-system 18.1.1 → 18.2.0

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.
Files changed (133) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/docs/configuration.md +11 -0
  3. package/package.json +1 -2
  4. package/src/access-intent/access-path.ts +29 -4
  5. package/src/access-intent/bash/bash-path-resolver.ts +39 -18
  6. package/src/access-intent/bash/msys-bash-tokens.ts +64 -0
  7. package/src/extension-config.ts +7 -0
  8. package/src/forwarded-permissions/permission-forwarder.ts +6 -3
  9. package/src/handlers/gates/helpers.ts +1 -1
  10. package/src/handlers/gates/runner.ts +22 -0
  11. package/src/index.ts +10 -6
  12. package/src/path-normalizer.ts +105 -2
  13. package/src/permission-manager.ts +21 -1
  14. package/src/permission-prompter.ts +10 -16
  15. package/src/prompting-gateway.ts +13 -17
  16. package/src/rule.ts +21 -1
  17. package/src/status.ts +1 -1
  18. package/src/yolo-mode.ts +0 -30
  19. package/test/access-intent/access-path.test.ts +0 -277
  20. package/test/access-intent/bash/node-text.test.ts +0 -148
  21. package/test/access-intent/bash/parser.test.ts +0 -19
  22. package/test/access-intent/bash/program.test.ts +0 -673
  23. package/test/access-intent/bash/token-classification.test.ts +0 -363
  24. package/test/access-intent/bash/token-collection.test.ts +0 -300
  25. package/test/active-agent.test.ts +0 -155
  26. package/test/async-cache.test.ts +0 -48
  27. package/test/bash-arity.test.ts +0 -144
  28. package/test/bash-external-directory.test.ts +0 -1022
  29. package/test/builtin-tool-input-formatters.test.ts +0 -109
  30. package/test/canonicalize-path.test.ts +0 -119
  31. package/test/composition-root.test.ts +0 -698
  32. package/test/config-loader.test.ts +0 -740
  33. package/test/config-modal.test.ts +0 -320
  34. package/test/config-paths.test.ts +0 -83
  35. package/test/config-pipeline.test.ts +0 -90
  36. package/test/config-reporter.test.ts +0 -147
  37. package/test/config-store.test.ts +0 -466
  38. package/test/decision-audit.test.ts +0 -72
  39. package/test/decision-reporter.test.ts +0 -112
  40. package/test/denial-messages.test.ts +0 -714
  41. package/test/detect-permissive-bash-fallback.test.ts +0 -56
  42. package/test/expand-home.test.ts +0 -93
  43. package/test/extension-config.test.ts +0 -129
  44. package/test/extension-paths.test.ts +0 -108
  45. package/test/forwarded-permissions/io.test.ts +0 -251
  46. package/test/forwarding-manager.test.ts +0 -200
  47. package/test/handlers/before-agent-start.test.ts +0 -314
  48. package/test/handlers/external-directory-integration.test.ts +0 -515
  49. package/test/handlers/external-directory-session-dedup.test.ts +0 -175
  50. package/test/handlers/external-directory-symlink-acceptance.test.ts +0 -167
  51. package/test/handlers/gates/bash-command-metamorphic.test.ts +0 -88
  52. package/test/handlers/gates/bash-command.test.ts +0 -257
  53. package/test/handlers/gates/bash-external-directory.test.ts +0 -268
  54. package/test/handlers/gates/bash-path.test.ts +0 -346
  55. package/test/handlers/gates/candidate-check.test.ts +0 -52
  56. package/test/handlers/gates/external-directory-messages.test.ts +0 -85
  57. package/test/handlers/gates/external-directory-policy.test.ts +0 -134
  58. package/test/handlers/gates/external-directory.test.ts +0 -267
  59. package/test/handlers/gates/helpers.test.ts +0 -165
  60. package/test/handlers/gates/path.test.ts +0 -369
  61. package/test/handlers/gates/runner.test.ts +0 -408
  62. package/test/handlers/gates/skill-input-gate-pipeline.test.ts +0 -176
  63. package/test/handlers/gates/skill-input.test.ts +0 -128
  64. package/test/handlers/gates/skill-read.test.ts +0 -161
  65. package/test/handlers/gates/tool-call-gate-pipeline.test.ts +0 -356
  66. package/test/handlers/gates/tool.test.ts +0 -244
  67. package/test/handlers/input-events.test.ts +0 -168
  68. package/test/handlers/input.test.ts +0 -199
  69. package/test/handlers/lifecycle.test.ts +0 -221
  70. package/test/handlers/tool-call-boundary.test.ts +0 -145
  71. package/test/handlers/tool-call-events.test.ts +0 -277
  72. package/test/handlers/tool-call.test.ts +0 -395
  73. package/test/handlers/validate-requested-tool.test.ts +0 -92
  74. package/test/helpers/external-directory-fixtures.ts +0 -269
  75. package/test/helpers/gate-fixtures.ts +0 -316
  76. package/test/helpers/handler-fixtures.ts +0 -335
  77. package/test/helpers/make-fake-pi.ts +0 -100
  78. package/test/helpers/manager-harness.ts +0 -112
  79. package/test/helpers/session-fixtures.ts +0 -199
  80. package/test/input-normalizer.test.ts +0 -325
  81. package/test/logging.test.ts +0 -51
  82. package/test/mcp-targets.test.ts +0 -233
  83. package/test/node-modules-discovery.test.ts +0 -97
  84. package/test/normalize.test.ts +0 -247
  85. package/test/path-containment.test.ts +0 -161
  86. package/test/path-normalization.test.ts +0 -233
  87. package/test/path-normalizer.test.ts +0 -196
  88. package/test/path-surfaces.test.ts +0 -55
  89. package/test/pattern-suggest.test.ts +0 -248
  90. package/test/permission-dialog.test.ts +0 -205
  91. package/test/permission-event-rpc.test.ts +0 -560
  92. package/test/permission-events.test.ts +0 -400
  93. package/test/permission-forwarder.test.ts +0 -370
  94. package/test/permission-forwarding.test.ts +0 -315
  95. package/test/permission-gate.test.ts +0 -269
  96. package/test/permission-manager-unified.test.ts +0 -3714
  97. package/test/permission-merge.test.ts +0 -61
  98. package/test/permission-prompter.test.ts +0 -518
  99. package/test/permission-prompts.test.ts +0 -363
  100. package/test/permission-resolver.test.ts +0 -277
  101. package/test/permission-session.test.ts +0 -404
  102. package/test/permission-ui-prompt.test.ts +0 -146
  103. package/test/permissions-service.test.ts +0 -192
  104. package/test/pi-infrastructure-read.test.ts +0 -432
  105. package/test/policy-loader.test.ts +0 -561
  106. package/test/prompting-gateway.test.ts +0 -231
  107. package/test/rule.test.ts +0 -650
  108. package/test/safe-system-paths.test.ts +0 -46
  109. package/test/scope-merge.test.ts +0 -116
  110. package/test/service-lifecycle.test.ts +0 -163
  111. package/test/service.test.ts +0 -261
  112. package/test/session-approval.test.ts +0 -75
  113. package/test/session-logger.test.ts +0 -200
  114. package/test/session-rules.test.ts +0 -321
  115. package/test/session-start.test.ts +0 -112
  116. package/test/skill-prompt-sanitizer.test.ts +0 -418
  117. package/test/status.test.ts +0 -10
  118. package/test/subagent-context.test.ts +0 -372
  119. package/test/subagent-lifecycle-events.test.ts +0 -132
  120. package/test/subagent-registry.test.ts +0 -145
  121. package/test/synthesize.test.ts +0 -302
  122. package/test/system-prompt-sanitizer.test.ts +0 -382
  123. package/test/tool-access-extractor-registry.test.ts +0 -77
  124. package/test/tool-input-formatter-registry.test.ts +0 -75
  125. package/test/tool-input-path.test.ts +0 -84
  126. package/test/tool-input-preview.test.ts +0 -129
  127. package/test/tool-input-prompt-formatters.test.ts +0 -115
  128. package/test/tool-preview-formatter.test.ts +0 -458
  129. package/test/tool-registry.test.ts +0 -197
  130. package/test/value-guards.test.ts +0 -193
  131. package/test/wildcard-matcher.test.ts +0 -424
  132. package/test/yaml-frontmatter.test.ts +0 -91
  133. package/test/yolo-mode.test.ts +0 -188
@@ -1,458 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2
-
3
- import type { ToolInputFormatterLookup } from "#src/tool-input-formatter-registry";
4
-
5
- // Mock logging collaborator before importing the module under test.
6
- vi.mock("../src/logging.js", () => ({
7
- safeJsonStringify: vi.fn((value: unknown) => JSON.stringify(value)),
8
- }));
9
-
10
- import { safeJsonStringify } from "#src/logging";
11
- import {
12
- TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
13
- TOOL_INPUT_PREVIEW_MAX_LENGTH,
14
- TOOL_TEXT_SUMMARY_MAX_LENGTH,
15
- } from "#src/tool-input-preview";
16
- import {
17
- resolveToolPreviewLimits,
18
- ToolPreviewFormatter,
19
- type ToolPreviewFormatterOptions,
20
- } from "#src/tool-preview-formatter";
21
- import type { PermissionCheckResult } from "#src/types";
22
-
23
- const mockedStringify = vi.mocked(safeJsonStringify);
24
-
25
- function makeFormatter(
26
- overrides: Partial<ToolPreviewFormatterOptions> = {},
27
- ): ToolPreviewFormatter {
28
- return new ToolPreviewFormatter({
29
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
30
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
31
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
32
- ...overrides,
33
- });
34
- }
35
-
36
- function makeResult(
37
- toolName: string,
38
- overrides: Partial<PermissionCheckResult> = {},
39
- ): PermissionCheckResult {
40
- return {
41
- toolName,
42
- state: "allow",
43
- source: "tool",
44
- origin: "builtin",
45
- ...overrides,
46
- };
47
- }
48
-
49
- beforeEach(() => {
50
- mockedStringify.mockReset();
51
- });
52
-
53
- afterEach(() => {
54
- vi.restoreAllMocks();
55
- });
56
-
57
- // ── sanitizeInlineText ────────────────────────────────────────────────────
58
-
59
- describe("ToolPreviewFormatter.sanitizeInlineText", () => {
60
- test("collapses whitespace and trims", () => {
61
- const f = makeFormatter();
62
- expect(f.sanitizeInlineText(" hello world ")).toBe("hello world");
63
- });
64
-
65
- test("returns 'empty text' for blank string", () => {
66
- const f = makeFormatter();
67
- expect(f.sanitizeInlineText("")).toBe("empty text");
68
- expect(f.sanitizeInlineText(" ")).toBe("empty text");
69
- });
70
-
71
- test("truncates at constructor toolTextSummaryMaxLength", () => {
72
- const f = makeFormatter({ toolTextSummaryMaxLength: 5 });
73
- const result = f.sanitizeInlineText("hello world");
74
- expect(result).toBe("hello…");
75
- });
76
-
77
- test("explicit maxLength override takes precedence over constructor default", () => {
78
- const f = makeFormatter({ toolTextSummaryMaxLength: 80 });
79
- const result = f.sanitizeInlineText("hello world", 5);
80
- expect(result).toBe("hello…");
81
- });
82
- });
83
-
84
- // ── formatJsonInputForPrompt ──────────────────────────────────────────────
85
-
86
- describe("ToolPreviewFormatter.formatJsonInputForPrompt", () => {
87
- test("returns empty string when serialization yields empty", () => {
88
- mockedStringify.mockReturnValue(undefined);
89
- const f = makeFormatter();
90
- expect(f.formatJsonInputForPrompt({})).toBe("");
91
- });
92
-
93
- test("returns prefixed JSON with 'with input' prefix", () => {
94
- mockedStringify.mockReturnValue('{"k":"v"}');
95
- const f = makeFormatter();
96
- expect(f.formatJsonInputForPrompt({ k: "v" })).toBe('with input {"k":"v"}');
97
- });
98
-
99
- test("truncates at constructor toolInputPreviewMaxLength", () => {
100
- const longJson = `"${"x".repeat(20)}"`;
101
- mockedStringify.mockReturnValue(longJson);
102
- const f = makeFormatter({ toolInputPreviewMaxLength: 10 });
103
- const result = f.formatJsonInputForPrompt({});
104
- // "with input " + 10 chars + ellipsis
105
- const preview = result.slice("with input ".length);
106
- expect(preview.length).toBe(11); // 10 + 1 for "…"
107
- expect(preview.endsWith("…")).toBe(true);
108
- });
109
-
110
- test("does not truncate when within toolInputPreviewMaxLength", () => {
111
- mockedStringify.mockReturnValue('{"k":"v"}');
112
- const f = makeFormatter({ toolInputPreviewMaxLength: 200 });
113
- expect(f.formatJsonInputForPrompt({ k: "v" })).toBe('with input {"k":"v"}');
114
- });
115
- });
116
-
117
- // ── formatSearchInputForPrompt ────────────────────────────────────────────
118
-
119
- describe("ToolPreviewFormatter.formatSearchInputForPrompt", () => {
120
- test("includes pattern and path", () => {
121
- const f = makeFormatter();
122
- const result = f.formatSearchInputForPrompt("grep", {
123
- pattern: "TODO",
124
- path: "/src",
125
- });
126
- expect(result).toContain("pattern 'TODO'");
127
- expect(result).toContain("path '/src'");
128
- });
129
-
130
- test("truncates pattern at toolTextSummaryMaxLength", () => {
131
- const f = makeFormatter({ toolTextSummaryMaxLength: 5 });
132
- const result = f.formatSearchInputForPrompt("grep", {
133
- pattern: "abcdefgh",
134
- });
135
- expect(result).toContain("abcde…");
136
- });
137
-
138
- test("uses 'current working directory' for find/grep/ls without path", () => {
139
- const f = makeFormatter();
140
- for (const toolName of ["find", "grep", "ls"]) {
141
- const result = f.formatSearchInputForPrompt(toolName, {});
142
- expect(result).toContain("current working directory");
143
- }
144
- });
145
-
146
- test("returns empty string for unknown tool with no input", () => {
147
- const f = makeFormatter();
148
- expect(f.formatSearchInputForPrompt("other", {})).toBe("");
149
- });
150
- });
151
-
152
- // ── formatToolInputForPrompt ──────────────────────────────────────────────
153
-
154
- describe("ToolPreviewFormatter.formatToolInputForPrompt", () => {
155
- test("dispatches 'edit' to standalone formatEditInputForPrompt", () => {
156
- mockedStringify.mockReturnValue(undefined);
157
- const f = makeFormatter();
158
- const result = f.formatToolInputForPrompt("edit", {
159
- path: "/foo.ts",
160
- edits: [],
161
- });
162
- expect(result).toContain("for '/foo.ts'");
163
- });
164
-
165
- test("dispatches 'write' to standalone formatWriteInputForPrompt", () => {
166
- const f = makeFormatter();
167
- const result = f.formatToolInputForPrompt("write", {
168
- path: "/out.ts",
169
- content: "hi",
170
- });
171
- expect(result).toContain("for '/out.ts'");
172
- });
173
-
174
- test("dispatches 'read' to standalone formatReadInputForPrompt", () => {
175
- const f = makeFormatter();
176
- const result = f.formatToolInputForPrompt("read", { path: "/src/x.ts" });
177
- expect(result).toContain("path '/src/x.ts'");
178
- });
179
-
180
- test("dispatches 'find'/'grep'/'ls' to formatSearchInputForPrompt", () => {
181
- const f = makeFormatter();
182
- for (const tool of ["find", "grep", "ls"]) {
183
- const result = f.formatToolInputForPrompt(tool, {});
184
- expect(result).toContain("current working directory");
185
- }
186
- });
187
-
188
- test("falls back to formatJsonInputForPrompt for unknown tools", () => {
189
- mockedStringify.mockReturnValue('{"x":1}');
190
- const f = makeFormatter();
191
- const result = f.formatToolInputForPrompt("unknown", { x: 1 });
192
- expect(result).toContain('{"x":1}');
193
- });
194
-
195
- test("unknown tool truncates at constructor toolInputPreviewMaxLength", () => {
196
- const longJson = `{"k":"${"x".repeat(50)}"}`;
197
- mockedStringify.mockReturnValue(longJson);
198
- const f = makeFormatter({ toolInputPreviewMaxLength: 10 });
199
- const result = f.formatToolInputForPrompt("custom", {});
200
- const preview = result.slice("with input ".length);
201
- expect(preview.endsWith("…")).toBe(true);
202
- expect(preview.length).toBe(11); // 10 + "…"
203
- });
204
- });
205
-
206
- // ── formatToolInputForPrompt (custom formatter seam) ───────────────────────
207
-
208
- describe("ToolPreviewFormatter.formatToolInputForPrompt — custom formatter seam", () => {
209
- function makeLookup(
210
- toolName: string,
211
- result: string | undefined,
212
- ): ToolInputFormatterLookup {
213
- return {
214
- get: (name) => (name === toolName ? () => result : undefined),
215
- };
216
- }
217
-
218
- test("uses a custom formatter's string result verbatim, bypassing the switch", () => {
219
- const lookup = makeLookup("my-tool", "custom preview");
220
- const f = new ToolPreviewFormatter(
221
- {
222
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
223
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
224
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
225
- },
226
- lookup,
227
- );
228
- expect(f.formatToolInputForPrompt("my-tool", {})).toBe("custom preview");
229
- });
230
-
231
- test("falls through to the built-in switch when custom formatter returns undefined", () => {
232
- mockedStringify.mockReturnValue('{"x":1}');
233
- const lookup = makeLookup("unknown-tool", undefined);
234
- const f = new ToolPreviewFormatter(
235
- {
236
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
237
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
238
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
239
- },
240
- lookup,
241
- );
242
- // Falls through to JSON default for unknown tools
243
- expect(f.formatToolInputForPrompt("unknown-tool", { x: 1 })).toContain(
244
- '{"x":1}',
245
- );
246
- });
247
-
248
- test("custom formatter for a built-in tool overrides the built-in preview", () => {
249
- const lookup = makeLookup("read", "custom read summary");
250
- const f = new ToolPreviewFormatter(
251
- {
252
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
253
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
254
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
255
- },
256
- lookup,
257
- );
258
- // Would normally use formatReadInputForPrompt; custom overrides it
259
- expect(f.formatToolInputForPrompt("read", { path: "/foo.ts" })).toBe(
260
- "custom read summary",
261
- );
262
- });
263
-
264
- test("absent lookup preserves current behaviour for all tool types", () => {
265
- const f = new ToolPreviewFormatter({
266
- toolInputPreviewMaxLength: TOOL_INPUT_PREVIEW_MAX_LENGTH,
267
- toolTextSummaryMaxLength: TOOL_TEXT_SUMMARY_MAX_LENGTH,
268
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
269
- });
270
- // Built-in path still works
271
- expect(f.formatToolInputForPrompt("read", { path: "/foo.ts" })).toContain(
272
- "/foo.ts",
273
- );
274
- });
275
- });
276
-
277
- // ── formatGenericToolInputForLog ──────────────────────────────────────────
278
-
279
- describe("ToolPreviewFormatter.formatGenericToolInputForLog", () => {
280
- test("returns undefined when serialization yields empty string", () => {
281
- mockedStringify.mockReturnValue(undefined);
282
- const f = makeFormatter();
283
- expect(f.formatGenericToolInputForLog({})).toBeUndefined();
284
- });
285
-
286
- test("returns prefixed input preview", () => {
287
- mockedStringify.mockReturnValue('{"k":"v"}');
288
- const f = makeFormatter();
289
- expect(f.formatGenericToolInputForLog({ k: "v" })).toBe('input {"k":"v"}');
290
- });
291
-
292
- test("truncates at constructor toolInputLogPreviewMaxLength", () => {
293
- const longJson = `{"k":"${"x".repeat(50)}"}`;
294
- mockedStringify.mockReturnValue(longJson);
295
- const f = makeFormatter({ toolInputLogPreviewMaxLength: 10 });
296
- const result = f.formatGenericToolInputForLog({});
297
- expect(result).toBeDefined();
298
- const preview = result!.slice("input ".length);
299
- expect(preview.length).toBe(11); // 10 + "…"
300
- expect(preview.endsWith("…")).toBe(true);
301
- });
302
- });
303
-
304
- // ── getToolInputPreviewForLog ─────────────────────────────────────────────
305
-
306
- describe("ToolPreviewFormatter.getToolInputPreviewForLog", () => {
307
- const pathBearingTools = new Set(["read", "write", "edit"]);
308
-
309
- test("returns undefined for bash tool", () => {
310
- const f = makeFormatter();
311
- expect(
312
- f.getToolInputPreviewForLog(
313
- makeResult("bash"),
314
- { command: "ls" },
315
- pathBearingTools,
316
- ),
317
- ).toBeUndefined();
318
- });
319
-
320
- test("returns undefined for mcp tool", () => {
321
- const f = makeFormatter();
322
- expect(
323
- f.getToolInputPreviewForLog(makeResult("mcp"), {}, pathBearingTools),
324
- ).toBeUndefined();
325
- });
326
-
327
- test("returns undefined for mcp source", () => {
328
- const f = makeFormatter();
329
- const result = makeResult("some-server:some-tool", { source: "mcp" });
330
- expect(
331
- f.getToolInputPreviewForLog(result, {}, pathBearingTools),
332
- ).toBeUndefined();
333
- });
334
-
335
- test("returns path-based preview for path-bearing tools", () => {
336
- const f = makeFormatter();
337
- const preview = f.getToolInputPreviewForLog(
338
- makeResult("read"),
339
- { path: "/src/foo.ts" },
340
- pathBearingTools,
341
- );
342
- expect(preview).toContain("/src/foo.ts");
343
- });
344
-
345
- test("truncates path preview at toolInputLogPreviewMaxLength", () => {
346
- const f = makeFormatter({ toolInputLogPreviewMaxLength: 15 });
347
- const longPath = `/src/${"a".repeat(50)}.ts`;
348
- const preview = f.getToolInputPreviewForLog(
349
- makeResult("read"),
350
- { path: longPath },
351
- pathBearingTools,
352
- );
353
- expect(preview).toBeDefined();
354
- expect(preview!.length).toBeLessThanOrEqual(16); // 15 + "…"
355
- });
356
-
357
- test("returns generic JSON preview for non-path-bearing tools", () => {
358
- mockedStringify.mockReturnValue('{"n":1}');
359
- const f = makeFormatter();
360
- const preview = f.getToolInputPreviewForLog(
361
- makeResult("task"),
362
- { n: 1 },
363
- pathBearingTools,
364
- );
365
- expect(preview).toContain('{"n":1}');
366
- });
367
- });
368
-
369
- // ── getPermissionLogContext ───────────────────────────────────────────────
370
-
371
- describe("ToolPreviewFormatter.getPermissionLogContext", () => {
372
- const pathBearingTools = new Set(["read", "write", "edit"]);
373
-
374
- test("returns command, target, toolInputPreview, and origin fields", () => {
375
- const f = makeFormatter();
376
- const result = makeResult("bash", { command: "ls -la" });
377
- const ctx = f.getPermissionLogContext(result, {}, pathBearingTools);
378
- expect(ctx.command).toBe("ls -la");
379
- expect(ctx.target).toBeUndefined();
380
- expect(ctx.toolInputPreview).toBeUndefined();
381
- });
382
-
383
- test("includes toolInputPreview for non-bash path-bearing tools", () => {
384
- const f = makeFormatter();
385
- const result = makeResult("read");
386
- const ctx = f.getPermissionLogContext(
387
- result,
388
- { path: "/foo.ts" },
389
- pathBearingTools,
390
- );
391
- expect(ctx.toolInputPreview).toContain("/foo.ts");
392
- });
393
-
394
- test("includes origin from check result", () => {
395
- const f = makeFormatter();
396
- const result = makeResult("read", { origin: "project" });
397
- const ctx = f.getPermissionLogContext(result, {}, pathBearingTools);
398
- expect(ctx.origin).toBe("project");
399
- });
400
-
401
- test("toolInputPreview respects toolInputLogPreviewMaxLength", () => {
402
- const f = makeFormatter({ toolInputLogPreviewMaxLength: 15 });
403
- const longPath = `/src/${"a".repeat(50)}.ts`;
404
- const ctx = f.getPermissionLogContext(
405
- makeResult("read"),
406
- { path: longPath },
407
- pathBearingTools,
408
- );
409
- expect(ctx.toolInputPreview).toBeDefined();
410
- expect(ctx.toolInputPreview!.length).toBeLessThanOrEqual(16);
411
- });
412
- });
413
-
414
- // ── resolveToolPreviewLimits ───────────────────────────────────────────────
415
-
416
- describe("resolveToolPreviewLimits", () => {
417
- test("uses configured toolInputPreviewMaxLength when provided", () => {
418
- const opts = resolveToolPreviewLimits({ toolInputPreviewMaxLength: 400 });
419
- expect(opts.toolInputPreviewMaxLength).toBe(400);
420
- });
421
-
422
- test("falls back to TOOL_INPUT_PREVIEW_MAX_LENGTH when toolInputPreviewMaxLength is absent", () => {
423
- const opts = resolveToolPreviewLimits({});
424
- expect(opts.toolInputPreviewMaxLength).toBe(TOOL_INPUT_PREVIEW_MAX_LENGTH);
425
- });
426
-
427
- test("uses configured toolTextSummaryMaxLength when provided", () => {
428
- const opts = resolveToolPreviewLimits({ toolTextSummaryMaxLength: 120 });
429
- expect(opts.toolTextSummaryMaxLength).toBe(120);
430
- });
431
-
432
- test("falls back to TOOL_TEXT_SUMMARY_MAX_LENGTH when toolTextSummaryMaxLength is absent", () => {
433
- const opts = resolveToolPreviewLimits({});
434
- expect(opts.toolTextSummaryMaxLength).toBe(TOOL_TEXT_SUMMARY_MAX_LENGTH);
435
- });
436
-
437
- test("always sets toolInputLogPreviewMaxLength to TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH", () => {
438
- const opts = resolveToolPreviewLimits({
439
- toolInputPreviewMaxLength: 999,
440
- toolTextSummaryMaxLength: 999,
441
- });
442
- expect(opts.toolInputLogPreviewMaxLength).toBe(
443
- TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
444
- );
445
- });
446
-
447
- test("returns all three options when both fields are configured", () => {
448
- const opts = resolveToolPreviewLimits({
449
- toolInputPreviewMaxLength: 400,
450
- toolTextSummaryMaxLength: 120,
451
- });
452
- expect(opts).toEqual({
453
- toolInputPreviewMaxLength: 400,
454
- toolTextSummaryMaxLength: 120,
455
- toolInputLogPreviewMaxLength: TOOL_INPUT_LOG_PREVIEW_MAX_LENGTH,
456
- });
457
- });
458
- });
@@ -1,197 +0,0 @@
1
- import { afterEach, describe, expect, test, vi } from "vitest";
2
-
3
- import {
4
- checkRequestedToolRegistration,
5
- getToolNameFromValue,
6
- } from "#src/tool-registry";
7
-
8
- afterEach(() => {
9
- vi.restoreAllMocks();
10
- });
11
-
12
- describe("getToolNameFromValue", () => {
13
- test("returns string value directly", () => {
14
- expect(getToolNameFromValue("read")).toBe("read");
15
- });
16
-
17
- test("returns null for empty string", () => {
18
- expect(getToolNameFromValue("")).toBeNull();
19
- });
20
-
21
- test("returns null for whitespace-only string", () => {
22
- expect(getToolNameFromValue(" ")).toBeNull();
23
- });
24
-
25
- test("returns null for null", () => {
26
- expect(getToolNameFromValue(null)).toBeNull();
27
- });
28
-
29
- test("returns null for undefined", () => {
30
- expect(getToolNameFromValue(undefined)).toBeNull();
31
- });
32
-
33
- test("extracts toolName from object", () => {
34
- expect(getToolNameFromValue({ toolName: "write" })).toBe("write");
35
- });
36
-
37
- test("extracts name from object", () => {
38
- expect(getToolNameFromValue({ name: "edit" })).toBe("edit");
39
- });
40
-
41
- test("extracts tool from object", () => {
42
- expect(getToolNameFromValue({ tool: "bash" })).toBe("bash");
43
- });
44
-
45
- test("prefers toolName over name over tool", () => {
46
- expect(
47
- getToolNameFromValue({
48
- toolName: "first",
49
- name: "second",
50
- tool: "third",
51
- }),
52
- ).toBe("first");
53
- });
54
-
55
- test("falls back to name when toolName is empty", () => {
56
- expect(getToolNameFromValue({ toolName: "", name: "edit" })).toBe("edit");
57
- });
58
-
59
- test("returns null for object with no recognised keys", () => {
60
- expect(getToolNameFromValue({ unknown: "read" })).toBeNull();
61
- });
62
-
63
- test("returns null for number input", () => {
64
- expect(getToolNameFromValue(42)).toBeNull();
65
- });
66
- });
67
-
68
- describe("checkRequestedToolRegistration", () => {
69
- test("returns missing-tool-name for null requested name", () => {
70
- const result = checkRequestedToolRegistration(null, []);
71
- expect(result.status).toBe("missing-tool-name");
72
- });
73
-
74
- test("returns missing-tool-name for whitespace-only requested name", () => {
75
- const result = checkRequestedToolRegistration(" ", []);
76
- expect(result.status).toBe("missing-tool-name");
77
- });
78
-
79
- test("returns registered when tool name matches a string entry", () => {
80
- const result = checkRequestedToolRegistration("read", ["read", "write"]);
81
- expect(result.status).toBe("registered");
82
- if (result.status === "registered") {
83
- expect(result.requestedToolName).toBe("read");
84
- expect(result.normalizedToolName).toBe("read");
85
- }
86
- });
87
-
88
- test("returns registered when tool name matches an object entry by name", () => {
89
- const result = checkRequestedToolRegistration("edit", [{ name: "edit" }]);
90
- expect(result.status).toBe("registered");
91
- });
92
-
93
- test("returns registered when tool name matches an object entry by toolName", () => {
94
- const result = checkRequestedToolRegistration("bash", [
95
- { toolName: "bash" },
96
- ]);
97
- expect(result.status).toBe("registered");
98
- });
99
-
100
- test("returns unregistered when tool is not in the list", () => {
101
- const result = checkRequestedToolRegistration("ghost", ["read", "write"]);
102
- expect(result.status).toBe("unregistered");
103
- if (result.status === "unregistered") {
104
- expect(result.requestedToolName).toBe("ghost");
105
- expect(result.availableToolNames).toContain("read");
106
- expect(result.availableToolNames).toContain("write");
107
- }
108
- });
109
-
110
- test("available tool names are sorted alphabetically", () => {
111
- const result = checkRequestedToolRegistration("ghost", [
112
- "write",
113
- "read",
114
- "edit",
115
- ]);
116
- if (result.status === "unregistered") {
117
- expect(result.availableToolNames).toEqual(["edit", "read", "write"]);
118
- }
119
- });
120
-
121
- test("resolves alias: requested alias maps to registered canonical name", () => {
122
- const aliases = { Execute: "bash" };
123
- const result = checkRequestedToolRegistration("Execute", ["bash"], aliases);
124
- expect(result.status).toBe("registered");
125
- if (result.status === "registered") {
126
- expect(result.normalizedToolName).toBe("bash");
127
- }
128
- });
129
-
130
- test("resolves alias: registered canonical is found via reverse alias lookup", () => {
131
- // "bash" is registered; alias maps "Execute" → "bash"
132
- // requesting "bash" directly should still resolve via the alias table
133
- const aliases = { Execute: "bash" };
134
- const result = checkRequestedToolRegistration("bash", ["bash"], aliases);
135
- expect(result.status).toBe("registered");
136
- });
137
-
138
- test("returns unregistered with empty availableToolNames for empty tool list", () => {
139
- const result = checkRequestedToolRegistration("read", []);
140
- expect(result.status).toBe("unregistered");
141
- if (result.status === "unregistered") {
142
- expect(result.availableToolNames).toEqual([]);
143
- }
144
- });
145
-
146
- test("skips tool list entries that yield no name", () => {
147
- const result = checkRequestedToolRegistration("read", [
148
- null,
149
- {},
150
- { unrelated: "x" },
151
- "read",
152
- ]);
153
- expect(result.status).toBe("registered");
154
- });
155
- });
156
-
157
- // ---------------------------------------------------------------------------
158
- // Moved from permission-system.test.ts catch-all (#342)
159
- // ---------------------------------------------------------------------------
160
-
161
- test("Tool registry resolves event tool names from string and object payloads", () => {
162
- expect(getToolNameFromValue(" read ")).toBe("read");
163
- expect(getToolNameFromValue({ toolName: "write" })).toBe("write");
164
- expect(getToolNameFromValue({ name: "find" })).toBe("find");
165
- expect(getToolNameFromValue({ tool: "grep" })).toBe("grep");
166
- expect(getToolNameFromValue({})).toBe(null);
167
- });
168
-
169
- test("Tool registry blocks unregistered tools and handles aliases", () => {
170
- const registeredTools = [
171
- { toolName: "mcp" },
172
- { toolName: "read" },
173
- { toolName: "bash" },
174
- ];
175
-
176
- const unknownCheck = checkRequestedToolRegistration(
177
- "third_party_tool",
178
- registeredTools,
179
- );
180
- expect(unknownCheck.status).toBe("unregistered");
181
- if (unknownCheck.status === "unregistered") {
182
- expect(unknownCheck.availableToolNames).toEqual(["bash", "mcp", "read"]);
183
- }
184
-
185
- const aliasCheck = checkRequestedToolRegistration(
186
- "legacy_read",
187
- registeredTools,
188
- { legacy_read: "read" },
189
- );
190
- expect(aliasCheck.status).toBe("registered");
191
-
192
- const missingNameCheck = checkRequestedToolRegistration(
193
- " ",
194
- registeredTools,
195
- );
196
- expect(missingNameCheck.status).toBe("missing-tool-name");
197
- });