| 1 | import { |
| 2 | yamlConfigToPolicies, |
| 3 | parseToolPattern, |
| 4 | } from "./permissionsYamlLoader.js"; |
| 5 | |
| 6 | describe("permissionsYamlLoader", () => { |
| 7 | describe("yamlConfigToPolicies", () => { |
| 8 | it("should convert YAML config to policies with proper order", () => { |
| 9 | const config = { |
| 10 | allow: ["Read", "List"], |
| 11 | ask: ["Write"], |
| 12 | exclude: ["Bash"], |
| 13 | }; |
| 14 | |
| 15 | const policies = yamlConfigToPolicies(config); |
| 16 | |
| 17 | expect(policies).toEqual([ |
| 18 | { tool: "Bash", permission: "exclude" }, |
| 19 | { tool: "Write", permission: "ask" }, |
| 20 | { tool: "Read", permission: "allow" }, |
| 21 | { tool: "List", permission: "allow" }, |
| 22 | ]); |
| 23 | }); |
| 24 | |
| 25 | it("should use exact tool names without normalization", () => { |
| 26 | const config = { |
| 27 | allow: ["read", "READ", "Read"], |
| 28 | }; |
| 29 | |
| 30 | const policies = yamlConfigToPolicies(config); |
| 31 | |
| 32 | expect(policies).toEqual([ |
| 33 | { tool: "read", permission: "allow" }, |
| 34 | { tool: "READ", permission: "allow" }, |
| 35 | { tool: "Read", permission: "allow" }, |
| 36 | ]); |
| 37 | }); |
| 38 | |
| 39 | it("should handle empty config", () => { |
| 40 | const config = {}; |
| 41 | |
| 42 | const policies = yamlConfigToPolicies(config); |
| 43 | |
| 44 | expect(policies).toEqual([]); |
| 45 | }); |
| 46 | |
| 47 | it("should handle wildcard patterns", () => { |
| 48 | const config = { |
| 49 | allow: ["mcp__*"], |
| 50 | exclude: ["*"], |
| 51 | }; |
| 52 | |
| 53 | const policies = yamlConfigToPolicies(config); |
| 54 | |
| 55 | expect(policies).toEqual([ |
| 56 | { tool: "*", permission: "exclude" }, |
| 57 | { tool: "mcp__*", permission: "allow" }, |
| 58 | ]); |
| 59 | }); |
| 60 | |
| 61 | it("should handle missing permission types", () => { |
| 62 | const config = { |
| 63 | allow: ["Read"], |
| 64 | // ask and exclude are undefined |
| 65 | }; |
| 66 | |
| 67 | const policies = yamlConfigToPolicies(config); |
| 68 | |
| 69 | expect(policies).toEqual([{ tool: "Read", permission: "allow" }]); |
| 70 | }); |
| 71 | |
| 72 | it("should handle all permission types", () => { |
| 73 | const config = { |
| 74 | allow: ["Read", "List"], |
| 75 | ask: ["Write", "Terminal"], |
| 76 | exclude: ["Fetch", "Exit"], |
| 77 | }; |
| 78 | |
| 79 | const policies = yamlConfigToPolicies(config); |
| 80 | |
| 81 | expect(policies).toEqual([ |
| 82 | { tool: "Fetch", permission: "exclude" }, |
| 83 | { tool: "Exit", permission: "exclude" }, |
| 84 | { tool: "Write", permission: "ask" }, |
| 85 | { tool: "Terminal", permission: "ask" }, |
| 86 | { tool: "Read", permission: "allow" }, |
| 87 | { tool: "List", permission: "allow" }, |
| 88 | ]); |
| 89 | }); |
| 90 | }); |
| 91 | |
| 92 | describe("parseToolPattern", () => { |
| 93 | it("should parse tool name without arguments", () => { |
| 94 | const policy = parseToolPattern("Write", "allow"); |
| 95 | |
| 96 | expect(policy).toEqual({ |
| 97 | tool: "Write", |
| 98 | permission: "allow", |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | it("should parse tool name with file path pattern", () => { |
| 103 | const policy = parseToolPattern("Write(**/*.ts)", "exclude"); |
| 104 | |
| 105 | expect(policy).toEqual({ |
| 106 | tool: "Write", |
| 107 | permission: "exclude", |
| 108 | argumentMatches: { |
| 109 | file_path: "**/*.ts", |
| 110 | }, |
| 111 | }); |
| 112 | }); |
| 113 | |
| 114 | it("should parse bash command with argument", () => { |
| 115 | const policy = parseToolPattern("Bash(npm install)", "ask"); |
| 116 | |
| 117 | expect(policy).toEqual({ |
| 118 | tool: "Bash", |
| 119 | permission: "ask", |
| 120 | argumentMatches: { |
| 121 | command: "npm install", |
| 122 | }, |
| 123 | }); |
| 124 | }); |
| 125 | |
| 126 | it("should handle various tool types with arguments", () => { |
| 127 | const testCases = [ |
| 128 | { |
| 129 | pattern: "Read(src/**/*.js)", |
| 130 | expected: { |
| 131 | tool: "Read", |
| 132 | permission: "allow", |
| 133 | argumentMatches: { file_path: "src/**/*.js" }, |
| 134 | }, |
| 135 | }, |
| 136 | { |
| 137 | pattern: "List(/home/user)", |
| 138 | expected: { |
| 139 | tool: "List", |
| 140 | permission: "allow", |
| 141 | argumentMatches: { path: "/home/user" }, |
| 142 | }, |
| 143 | }, |
| 144 | { |
| 145 | pattern: "Search(TODO)", |
| 146 | expected: { |
| 147 | tool: "Search", |
| 148 | permission: "allow", |
| 149 | argumentMatches: { query: "TODO" }, |
| 150 | }, |
| 151 | }, |
| 152 | { |
| 153 | pattern: "Fetch(https://api.example.com)", |
| 154 | expected: { |
| 155 | tool: "Fetch", |
| 156 | permission: "allow", |
| 157 | argumentMatches: { url: "https://api.example.com" }, |
| 158 | }, |
| 159 | }, |
| 160 | ]; |
| 161 | |
| 162 | testCases.forEach(({ pattern, expected }) => { |
| 163 | const policy = parseToolPattern(pattern, "allow"); |
| 164 | expect(policy).toEqual(expected); |
| 165 | }); |
| 166 | }); |
| 167 | |
| 168 | it("should handle empty arguments", () => { |
| 169 | const policy = parseToolPattern("Write()", "allow"); |
| 170 | |
| 171 | expect(policy).toEqual({ |
| 172 | tool: "Write", |
| 173 | permission: "allow", |
| 174 | }); |
| 175 | }); |
| 176 | |
| 177 | it("should handle whitespace in arguments", () => { |
| 178 | const policy = parseToolPattern("Bash( npm run test )", "ask"); |
| 179 | |
| 180 | expect(policy).toEqual({ |
| 181 | tool: "Bash", |
| 182 | permission: "ask", |
| 183 | argumentMatches: { |
| 184 | command: "npm run test", |
| 185 | }, |
| 186 | }); |
| 187 | }); |
| 188 | |
| 189 | it("should handle unknown tools with default argument key", () => { |
| 190 | const policy = parseToolPattern("CustomTool(some-pattern)", "allow"); |
| 191 | |
| 192 | expect(policy).toEqual({ |
| 193 | tool: "CustomTool", |
| 194 | permission: "allow", |
| 195 | argumentMatches: { |
| 196 | pattern: "some-pattern", |
| 197 | }, |
| 198 | }); |
| 199 | }); |
| 200 | |
| 201 | it("should throw error for invalid pattern", () => { |
| 202 | expect(() => parseToolPattern("Write(unclosed", "allow")).toThrow( |
| 203 | "Invalid tool pattern: Write(unclosed", |
| 204 | ); |
| 205 | }); |
| 206 | |
| 207 | it("should use exact tool names in patterns", () => { |
| 208 | const policy = parseToolPattern("write(**/*.ts)", "allow"); |
| 209 | |
| 210 | expect(policy).toEqual({ |
| 211 | tool: "write", |
| 212 | permission: "allow", |
| 213 | argumentMatches: { |
| 214 | pattern: "**/*.ts", |
| 215 | }, |
| 216 | }); |
| 217 | }); |
| 218 | }); |
| 219 | }); |