@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,155 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
ACTIVE_AGENT_TAG_REGEX,
|
|
4
|
+
type ActiveAgentContext,
|
|
5
|
+
getActiveAgentName,
|
|
6
|
+
getActiveAgentNameFromSystemPrompt,
|
|
7
|
+
normalizeAgentName,
|
|
8
|
+
type SessionEntryView,
|
|
9
|
+
} from "#src/active-agent";
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
vi.restoreAllMocks();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
function makeCtx(entries: SessionEntryView[]): ActiveAgentContext {
|
|
16
|
+
return {
|
|
17
|
+
sessionManager: {
|
|
18
|
+
getEntries: vi.fn(() => entries),
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("ACTIVE_AGENT_TAG_REGEX", () => {
|
|
24
|
+
test("matches double-quoted name attribute", () => {
|
|
25
|
+
const match = '<active_agent name="my-agent">'.match(
|
|
26
|
+
ACTIVE_AGENT_TAG_REGEX,
|
|
27
|
+
);
|
|
28
|
+
expect(match?.[1]).toBe("my-agent");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("matches single-quoted name attribute", () => {
|
|
32
|
+
const match = "<active_agent name='my-agent'>".match(
|
|
33
|
+
ACTIVE_AGENT_TAG_REGEX,
|
|
34
|
+
);
|
|
35
|
+
expect(match?.[1]).toBe("my-agent");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("is case-insensitive", () => {
|
|
39
|
+
const match = '<ACTIVE_AGENT name="bot">'.match(ACTIVE_AGENT_TAG_REGEX);
|
|
40
|
+
expect(match?.[1]).toBe("bot");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("does not match when tag is absent", () => {
|
|
44
|
+
expect("no tag here".match(ACTIVE_AGENT_TAG_REGEX)).toBeNull();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("normalizeAgentName", () => {
|
|
49
|
+
test("returns trimmed string for valid input", () => {
|
|
50
|
+
expect(normalizeAgentName(" my-agent ")).toBe("my-agent");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("returns null for empty string", () => {
|
|
54
|
+
expect(normalizeAgentName("")).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("returns null for whitespace-only string", () => {
|
|
58
|
+
expect(normalizeAgentName(" ")).toBeNull();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("returns null for non-string values", () => {
|
|
62
|
+
expect(normalizeAgentName(null)).toBeNull();
|
|
63
|
+
expect(normalizeAgentName(undefined)).toBeNull();
|
|
64
|
+
expect(normalizeAgentName(42)).toBeNull();
|
|
65
|
+
expect(normalizeAgentName({})).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("getActiveAgentName", () => {
|
|
70
|
+
test("returns null when session has no entries", () => {
|
|
71
|
+
expect(getActiveAgentName(makeCtx([]))).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("returns null when no active_agent custom entry exists", () => {
|
|
75
|
+
const ctx = makeCtx([{ type: "message", data: { name: "agent" } }]);
|
|
76
|
+
expect(getActiveAgentName(ctx)).toBeNull();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("returns agent name from active_agent entry", () => {
|
|
80
|
+
const ctx = makeCtx([
|
|
81
|
+
{ type: "custom", customType: "active_agent", data: { name: "bot" } },
|
|
82
|
+
]);
|
|
83
|
+
expect(getActiveAgentName(ctx)).toBe("bot");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("last-entry-wins: returns name from the last matching entry", () => {
|
|
87
|
+
const ctx = makeCtx([
|
|
88
|
+
{ type: "custom", customType: "active_agent", data: { name: "first" } },
|
|
89
|
+
{ type: "custom", customType: "active_agent", data: { name: "last" } },
|
|
90
|
+
]);
|
|
91
|
+
expect(getActiveAgentName(ctx)).toBe("last");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("entry with name: null resets agent name to null", () => {
|
|
95
|
+
const ctx = makeCtx([
|
|
96
|
+
{ type: "custom", customType: "active_agent", data: { name: "bot" } },
|
|
97
|
+
{ type: "custom", customType: "active_agent", data: { name: null } },
|
|
98
|
+
]);
|
|
99
|
+
expect(getActiveAgentName(ctx)).toBeNull();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("skips entries with whitespace-only name and continues scanning", () => {
|
|
103
|
+
const ctx = makeCtx([
|
|
104
|
+
{ type: "custom", customType: "active_agent", data: { name: "first" } },
|
|
105
|
+
{ type: "custom", customType: "active_agent", data: { name: " " } },
|
|
106
|
+
]);
|
|
107
|
+
// " " normalizes to null — not a sentinel reset, keeps scanning backwards
|
|
108
|
+
expect(getActiveAgentName(ctx)).toBe("first");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("ignores entries with wrong customType", () => {
|
|
112
|
+
const ctx = makeCtx([
|
|
113
|
+
{ type: "custom", customType: "something_else", data: { name: "bot" } },
|
|
114
|
+
]);
|
|
115
|
+
expect(getActiveAgentName(ctx)).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("ignores entries with wrong type", () => {
|
|
119
|
+
const ctx = makeCtx([
|
|
120
|
+
{ type: "tool_call", customType: "active_agent", data: { name: "bot" } },
|
|
121
|
+
]);
|
|
122
|
+
expect(getActiveAgentName(ctx)).toBeNull();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("getActiveAgentNameFromSystemPrompt", () => {
|
|
127
|
+
test("returns null for undefined system prompt", () => {
|
|
128
|
+
expect(getActiveAgentNameFromSystemPrompt(undefined)).toBeNull();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("returns null for empty system prompt", () => {
|
|
132
|
+
expect(getActiveAgentNameFromSystemPrompt("")).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("returns null when tag is absent", () => {
|
|
136
|
+
expect(
|
|
137
|
+
getActiveAgentNameFromSystemPrompt("You are a helpful assistant."),
|
|
138
|
+
).toBeNull();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("extracts agent name from tag in system prompt", () => {
|
|
142
|
+
const prompt = 'You are helpful.\n<active_agent name="my-bot">\nDo work.';
|
|
143
|
+
expect(getActiveAgentNameFromSystemPrompt(prompt)).toBe("my-bot");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("returns null when tag name is empty", () => {
|
|
147
|
+
const prompt = '<active_agent name="">';
|
|
148
|
+
expect(getActiveAgentNameFromSystemPrompt(prompt)).toBeNull();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("trims whitespace from extracted name", () => {
|
|
152
|
+
const prompt = '<active_agent name=" trimmed ">';
|
|
153
|
+
expect(getActiveAgentNameFromSystemPrompt(prompt)).toBe("trimmed");
|
|
154
|
+
});
|
|
155
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { memoizeAsyncWithRetry } from "#src/async-cache";
|
|
4
|
+
|
|
5
|
+
describe("memoizeAsyncWithRetry", () => {
|
|
6
|
+
it("invokes the factory once and shares the resolved value across calls", async () => {
|
|
7
|
+
const factory = vi.fn<() => Promise<number>>().mockResolvedValue(42);
|
|
8
|
+
const memoized = memoizeAsyncWithRetry(factory);
|
|
9
|
+
|
|
10
|
+
const results = await Promise.all([memoized(), memoized(), memoized()]);
|
|
11
|
+
|
|
12
|
+
expect(results).toEqual([42, 42, 42]);
|
|
13
|
+
expect(factory).toHaveBeenCalledTimes(1);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("caches the same promise instance on success", async () => {
|
|
17
|
+
const factory = vi.fn<() => Promise<string>>().mockResolvedValue("parser");
|
|
18
|
+
const memoized = memoizeAsyncWithRetry(factory);
|
|
19
|
+
|
|
20
|
+
await memoized();
|
|
21
|
+
await memoized();
|
|
22
|
+
|
|
23
|
+
expect(factory).toHaveBeenCalledTimes(1);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("surfaces the rejection to the caller each time the factory fails", async () => {
|
|
27
|
+
const error = new Error("init failed");
|
|
28
|
+
const factory = vi.fn<() => Promise<number>>().mockRejectedValue(error);
|
|
29
|
+
const memoized = memoizeAsyncWithRetry(factory);
|
|
30
|
+
|
|
31
|
+
await expect(memoized()).rejects.toThrow("init failed");
|
|
32
|
+
await expect(memoized()).rejects.toThrow("init failed");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("drops a rejected result so the next call re-invokes the factory", async () => {
|
|
36
|
+
const factory = vi
|
|
37
|
+
.fn<() => Promise<number>>()
|
|
38
|
+
.mockRejectedValueOnce(new Error("transient"))
|
|
39
|
+
.mockResolvedValue(7);
|
|
40
|
+
const memoized = memoizeAsyncWithRetry(factory);
|
|
41
|
+
|
|
42
|
+
await expect(memoized()).rejects.toThrow("transient");
|
|
43
|
+
const recovered = await memoized();
|
|
44
|
+
|
|
45
|
+
expect(recovered).toBe(7);
|
|
46
|
+
expect(factory).toHaveBeenCalledTimes(2);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { ARITY, prefix, stripBashCommentLines } from "#src/bash-arity";
|
|
3
|
+
|
|
4
|
+
describe("ARITY dictionary", () => {
|
|
5
|
+
it("is exported as a plain object", () => {
|
|
6
|
+
expect(typeof ARITY).toBe("object");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("maps 'git' to arity 2", () => {
|
|
10
|
+
expect(ARITY.git).toBe(2);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("maps 'npm run' to arity 3", () => {
|
|
14
|
+
expect(ARITY["npm run"]).toBe(3);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("maps 'npm' to arity 2 (fallback when 'npm run' does not match)", () => {
|
|
18
|
+
expect(ARITY.npm).toBe(2);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("maps 'docker compose' to arity 3", () => {
|
|
22
|
+
expect(ARITY["docker compose"]).toBe(3);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("maps 'docker' to arity 2 (fallback)", () => {
|
|
26
|
+
expect(ARITY.docker).toBe(2);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("prefix", () => {
|
|
31
|
+
it("returns empty array for empty input", () => {
|
|
32
|
+
expect(prefix([])).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("returns single-element array for a bare known command", () => {
|
|
36
|
+
// 'git' alone has arity 2 but only 1 token is available — clamp.
|
|
37
|
+
expect(prefix(["git"])).toEqual(["git"]);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("returns arity-2 prefix for git subcommands", () => {
|
|
41
|
+
expect(prefix(["git", "checkout", "main"])).toEqual(["git", "checkout"]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns arity-2 prefix for git status with flags", () => {
|
|
45
|
+
expect(prefix(["git", "status", "--short"])).toEqual(["git", "status"]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns arity-3 prefix for npm run (longest match wins over npm arity-2)", () => {
|
|
49
|
+
expect(prefix(["npm", "run", "dev"])).toEqual(["npm", "run", "dev"]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns arity-2 prefix for npm install (npm fallback, npm run does not match)", () => {
|
|
53
|
+
expect(prefix(["npm", "install", "lodash"])).toEqual(["npm", "install"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("returns arity-3 prefix for docker compose subcommands", () => {
|
|
57
|
+
expect(prefix(["docker", "compose", "up", "--build"])).toEqual([
|
|
58
|
+
"docker",
|
|
59
|
+
"compose",
|
|
60
|
+
"up",
|
|
61
|
+
]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("returns arity-2 prefix for docker pull (docker fallback)", () => {
|
|
65
|
+
expect(prefix(["docker", "pull", "ubuntu"])).toEqual(["docker", "pull"]);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("returns arity-1 prefix for unknown commands", () => {
|
|
69
|
+
expect(prefix(["unknown-tool", "--flag"])).toEqual(["unknown-tool"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns arity-1 prefix for rm (args are targets, not subcommands)", () => {
|
|
73
|
+
expect(prefix(["rm", "-rf", "node_modules"])).toEqual(["rm"]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("returns arity-1 prefix for cat", () => {
|
|
77
|
+
expect(prefix(["cat", "file.txt"])).toEqual(["cat"]);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("is case-insensitive: 'Git' looks up as 'git'", () => {
|
|
81
|
+
// Tokens are preserved as-is; only the lookup key is lowercased.
|
|
82
|
+
expect(prefix(["Git", "checkout", "main"])).toEqual(["Git", "checkout"]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("clamps arity to available token count when command is shorter than arity", () => {
|
|
86
|
+
// npm run has arity 3; only ["npm", "run"] provided → return both.
|
|
87
|
+
expect(prefix(["npm", "run"])).toEqual(["npm", "run"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("returns arity-2 prefix for pnpm run (longest match wins over pnpm)", () => {
|
|
91
|
+
// pnpm run <script> — arity 3 means include the script name.
|
|
92
|
+
expect(prefix(["pnpm", "run", "build"])).toEqual(["pnpm", "run", "build"]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("returns arity-2 prefix for cargo subcommands", () => {
|
|
96
|
+
expect(prefix(["cargo", "build", "--release"])).toEqual(["cargo", "build"]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("returns arity-2 prefix for kubectl subcommands", () => {
|
|
100
|
+
expect(prefix(["kubectl", "get", "pods"])).toEqual(["kubectl", "get"]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("returns arity-1 for bare 'ls' (args are paths)", () => {
|
|
104
|
+
expect(prefix(["ls", "-la", "/tmp"])).toEqual(["ls"]);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("stripBashCommentLines", () => {
|
|
109
|
+
it("removes a single leading comment line", () => {
|
|
110
|
+
expect(
|
|
111
|
+
stripBashCommentLines("# Check debug logs\nfind /home -type f"),
|
|
112
|
+
).toBe("find /home -type f");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("removes multiple leading comment lines", () => {
|
|
116
|
+
expect(
|
|
117
|
+
stripBashCommentLines("# Step 1\n# Step 2\ngit status --short"),
|
|
118
|
+
).toBe("git status --short");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("returns empty string when all lines are comments", () => {
|
|
122
|
+
expect(stripBashCommentLines("# just a comment")).toBe("");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("returns empty string for blank input", () => {
|
|
126
|
+
expect(stripBashCommentLines("")).toBe("");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("returns the command unchanged when no comment lines are present", () => {
|
|
130
|
+
expect(stripBashCommentLines("grep -rn foo src/")).toBe(
|
|
131
|
+
"grep -rn foo src/",
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("trims surrounding whitespace from the result", () => {
|
|
136
|
+
expect(stripBashCommentLines("\n\n ls -la \n")).toBe("ls -la");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("treats indented comment lines as comments", () => {
|
|
140
|
+
expect(stripBashCommentLines(" # indented comment\necho hi")).toBe(
|
|
141
|
+
"echo hi",
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
});
|