@gotgenes/pi-permission-system 3.7.0 → 3.9.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.
@@ -1,142 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2
-
3
- import type { PermissionState } from "../src/types";
4
-
5
- // Mock wildcard-matcher before importing the module under test.
6
- vi.mock("../src/wildcard-matcher.js", () => ({
7
- compileWildcardPatterns: vi.fn((patterns: Record<string, PermissionState>) =>
8
- Object.entries(patterns).map(([pattern, state]) => ({
9
- pattern,
10
- state,
11
- regex: new RegExp(`^${pattern.replace(/\*/g, ".*")}$`),
12
- })),
13
- ),
14
- findCompiledWildcardMatch: vi.fn(),
15
- }));
16
-
17
- import { BashFilter } from "../src/bash-filter";
18
- import {
19
- compileWildcardPatterns,
20
- findCompiledWildcardMatch,
21
- } from "../src/wildcard-matcher";
22
-
23
- const mockedCompilePatterns = vi.mocked(compileWildcardPatterns);
24
- const mockedFindMatch = vi.mocked(findCompiledWildcardMatch);
25
-
26
- beforeEach(() => {
27
- mockedCompilePatterns.mockClear();
28
- mockedFindMatch.mockReset();
29
- });
30
-
31
- afterEach(() => {
32
- vi.restoreAllMocks();
33
- });
34
-
35
- describe("BashFilter.check", () => {
36
- test("returns matched state when wildcard-matcher finds a pattern match", () => {
37
- mockedFindMatch.mockReturnValue({
38
- state: "allow",
39
- matchedPattern: "git *",
40
- matchedName: "git status",
41
- });
42
-
43
- const filter = new BashFilter({ "git *": "allow" }, "ask");
44
- const result = filter.check("git status");
45
-
46
- expect(result.state).toBe("allow");
47
- expect(result.matchedPattern).toBe("git *");
48
- expect(result.command).toBe("git status");
49
- expect(mockedFindMatch).toHaveBeenCalledOnce();
50
- });
51
-
52
- test("returns default state when wildcard-matcher returns null", () => {
53
- mockedFindMatch.mockReturnValue(null);
54
-
55
- const filter = new BashFilter({ "git *": "allow" }, "ask");
56
- const result = filter.check("npm install");
57
-
58
- expect(result.state).toBe("ask");
59
- expect(result.matchedPattern).toBeUndefined();
60
- expect(result.command).toBe("npm install");
61
- });
62
-
63
- test("delegates pattern compilation to compileWildcardPatterns", () => {
64
- mockedFindMatch.mockReturnValue(null);
65
-
66
- const permissions: Record<string, PermissionState> = {
67
- "git *": "allow",
68
- "npm *": "deny",
69
- };
70
- new BashFilter(permissions, "ask");
71
-
72
- expect(compileWildcardPatterns).toHaveBeenCalledWith(permissions);
73
- });
74
-
75
- test("default fallback is the configured defaultState", () => {
76
- mockedFindMatch.mockReturnValue(null);
77
-
78
- const denyFilter = new BashFilter({}, "deny");
79
- expect(denyFilter.check("anything").state).toBe("deny");
80
-
81
- const allowFilter = new BashFilter({}, "allow");
82
- expect(allowFilter.check("anything").state).toBe("allow");
83
- });
84
-
85
- test("passes command string to findCompiledWildcardMatch", () => {
86
- mockedFindMatch.mockReturnValue(null);
87
-
88
- const filter = new BashFilter({}, "ask");
89
- filter.check("echo hello");
90
-
91
- expect(mockedFindMatch).toHaveBeenCalledWith(
92
- expect.any(Array),
93
- "echo hello",
94
- );
95
- });
96
-
97
- test("empty command falls through to default state", () => {
98
- mockedFindMatch.mockReturnValue(null);
99
-
100
- const filter = new BashFilter({}, "ask");
101
- const result = filter.check("");
102
-
103
- expect(result.state).toBe("ask");
104
- expect(result.command).toBe("");
105
- });
106
-
107
- test("accepts pre-compiled pattern list instead of permissions object", () => {
108
- mockedFindMatch.mockReturnValue({
109
- state: "deny",
110
- matchedPattern: "rm *",
111
- matchedName: "rm -rf /",
112
- });
113
-
114
- const compiledPatterns = [
115
- {
116
- pattern: "rm *",
117
- state: "deny" as const,
118
- regex: /^rm .*$/,
119
- },
120
- ];
121
- const filter = new BashFilter(compiledPatterns, "ask");
122
- const result = filter.check("rm -rf /");
123
-
124
- // compileWildcardPatterns should NOT be called for a pre-compiled list
125
- expect(compileWildcardPatterns).not.toHaveBeenCalled();
126
- expect(result.state).toBe("deny");
127
- });
128
-
129
- test("last-match-wins: matched pattern state overrides default", () => {
130
- mockedFindMatch.mockReturnValue({
131
- state: "deny",
132
- matchedPattern: "rm *",
133
- matchedName: "rm -rf /",
134
- });
135
-
136
- const filter = new BashFilter({ "rm *": "deny" }, "allow");
137
- const result = filter.check("rm -rf /");
138
-
139
- expect(result.state).toBe("deny");
140
- expect(result.matchedPattern).toBe("rm *");
141
- });
142
- });