| 1 | import { beforeEach, describe, expect, it } from "vitest"; |
| 2 | |
| 3 | import { ToolPermissionService } from "./ToolPermissionService.js"; |
| 4 | |
| 5 | describe("ToolPermissionService - Mode Functionality", () => { |
| 6 | let service: ToolPermissionService; |
| 7 | |
| 8 | beforeEach(() => { |
| 9 | service = new ToolPermissionService(); |
| 10 | }); |
| 11 | |
| 12 | describe("mode initialization", () => { |
| 13 | it("should initialize with normal mode by default", () => { |
| 14 | const state = service.initializeSync(); |
| 15 | expect(state.currentMode).toBe("normal"); |
| 16 | }); |
| 17 | |
| 18 | it("should initialize with specified mode", () => { |
| 19 | const state = service.initializeSync({ mode: "plan" }); |
| 20 | expect(state.currentMode).toBe("plan"); |
| 21 | }); |
| 22 | |
| 23 | it("should generate correct policies for plan mode", () => { |
| 24 | const state = service.initializeSync({ mode: "plan" }); |
| 25 | const policies = state.permissions.policies; |
| 26 | |
| 27 | // Should have specific policies for write tools (excluded) |
| 28 | expect( |
| 29 | policies.some((p) => p.tool === "Write" && p.permission === "exclude"), |
| 30 | ).toBe(true); |
| 31 | expect( |
| 32 | policies.some((p) => p.tool === "Edit" && p.permission === "exclude"), |
| 33 | ).toBe(true); |
| 34 | expect( |
| 35 | policies.some((p) => p.tool === "Bash" && p.permission === "allow"), |
| 36 | ).toBe(true); |
| 37 | |
| 38 | // Should have specific policies for read tools (allowed) |
| 39 | expect( |
| 40 | policies.some((p) => p.tool === "Read" && p.permission === "allow"), |
| 41 | ).toBe(true); |
| 42 | expect( |
| 43 | policies.some((p) => p.tool === "Grep" && p.permission === "allow"), |
| 44 | ).toBe(true); |
| 45 | expect( |
| 46 | policies.some((p) => p.tool === "LS" && p.permission === "allow"), |
| 47 | ).toBe(true); |
| 48 | |
| 49 | // Should allow MCP tools |
| 50 | expect( |
| 51 | policies.some((p) => p.tool === "mcp:*" && p.permission === "allow"), |
| 52 | ).toBe(true); |
| 53 | |
| 54 | // Should have an exclusion policy for all other tools as fallback |
| 55 | expect( |
| 56 | policies.some((p) => p.tool === "*" && p.permission === "exclude"), |
| 57 | ).toBe(true); |
| 58 | }); |
| 59 | |
| 60 | it("should generate correct policies for auto mode", () => { |
| 61 | const state = service.initializeSync({ mode: "auto" }); |
| 62 | const policies = state.permissions.policies; |
| 63 | |
| 64 | // First policy should be the mode policy (allow all) |
| 65 | expect(policies[0]).toEqual({ |
| 66 | tool: "*", |
| 67 | permission: "allow", |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | it("should have mode-specific policy positioning", () => { |
| 72 | const normalState = service.initializeSync({ mode: "normal" }); |
| 73 | const planState = service.initializeSync({ mode: "plan" }); |
| 74 | |
| 75 | // Normal mode may have default policies but no mode-specific wildcard at the start |
| 76 | const normalFirstPolicy = normalState.permissions.policies[0]; |
| 77 | expect( |
| 78 | normalFirstPolicy?.tool !== "*" || |
| 79 | normalFirstPolicy?.permission !== "exclude", |
| 80 | ).toBe(true); |
| 81 | |
| 82 | // Plan mode should have write exclusions and read allowances |
| 83 | const planPolicies = planState.permissions.policies; |
| 84 | expect( |
| 85 | planPolicies.some( |
| 86 | (p) => p.tool === "Write" && p.permission === "exclude", |
| 87 | ), |
| 88 | ).toBe(true); |
| 89 | expect( |
| 90 | planPolicies.some((p) => p.tool === "Read" && p.permission === "allow"), |
| 91 | ).toBe(true); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe("mode switching", () => { |
| 96 | beforeEach(() => { |
| 97 | service.initializeSync(); |
| 98 | }); |
| 99 | |
| 100 | it("should switch from normal to plan mode", () => { |
| 101 | expect(service.getCurrentMode()).toBe("normal"); |
| 102 | |
| 103 | const newState = service.switchMode("plan"); |
| 104 | |
| 105 | expect(newState.currentMode).toBe("plan"); |
| 106 | expect(service.getCurrentMode()).toBe("plan"); |
| 107 | }); |
| 108 | |
| 109 | it("should update policies when switching modes", () => { |
| 110 | service.initializeSync({ mode: "normal" }); |
| 111 | const normalPolicies = service.getPermissions().policies; |
| 112 | |
| 113 | service.switchMode("plan"); |
| 114 | const planPolicies = service.getPermissions().policies; |
| 115 | |
| 116 | expect(planPolicies).not.toEqual(normalPolicies); |
| 117 | expect( |
| 118 | planPolicies.some( |
| 119 | (p) => p.tool === "Write" && p.permission === "exclude", |
| 120 | ), |
| 121 | ).toBe(true); |
| 122 | }); |
| 123 | |
| 124 | it("should use absolute override for plan mode (ignore non-mode policies)", () => { |
| 125 | // Initialize with some existing policies |
| 126 | service.initializeSync({ |
| 127 | allow: ["allowedTool"], |
| 128 | exclude: ["excludedTool"], |
| 129 | }); |
| 130 | |
| 131 | const initialPolicies = service.getPermissions().policies; |
| 132 | const nonModePolicyCount = initialPolicies.length; |
| 133 | |
| 134 | service.switchMode("plan"); |
| 135 | const newPolicies = service.getPermissions().policies; |
| 136 | |
| 137 | // Plan mode should use ONLY mode policies (absolute override) |
| 138 | // Should contain Write exclude policy for plan mode |
| 139 | const writePolicy = newPolicies.find((p) => p.tool === "Write"); |
| 140 | expect(writePolicy?.permission).toBe("exclude"); |
| 141 | |
| 142 | // Should not contain any user-defined policies from the initial setup |
| 143 | const userAllowPolicy = newPolicies.find((p) => p.tool === "allowedTool"); |
| 144 | const userExcludePolicy = newPolicies.find( |
| 145 | (p) => p.tool === "excludedTool", |
| 146 | ); |
| 147 | expect(userAllowPolicy).toBeUndefined(); |
| 148 | expect(userExcludePolicy).toBeUndefined(); |
| 149 | }); |
| 150 | |
| 151 | it("should handle switching between different modes", () => { |
| 152 | service.switchMode("plan"); |
| 153 | expect(service.getCurrentMode()).toBe("plan"); |
| 154 | |
| 155 | service.switchMode("auto"); |
| 156 | expect(service.getCurrentMode()).toBe("auto"); |
| 157 | |
| 158 | service.switchMode("normal"); |
| 159 | expect(service.getCurrentMode()).toBe("normal"); |
| 160 | }); |
| 161 | |
| 162 | it("should preserve original user policies when switching modes", () => { |
| 163 | // Initialize with user-defined policies |
| 164 | service.initializeSync({ |
| 165 | allow: ["customAllowedTool"], |
| 166 | exclude: ["customExcludedTool"], |
| 167 | ask: ["customAskTool"], |
| 168 | }); |
| 169 | |
| 170 | const originalPolicies = service.getPermissions().policies; |
| 171 | const userPolicyCount = originalPolicies.filter( |
| 172 | (p) => |
| 173 | p.tool === "customAllowedTool" || |
| 174 | p.tool === "customExcludedTool" || |
| 175 | p.tool === "customAskTool", |
| 176 | ).length; |
| 177 | expect(userPolicyCount).toBe(3); |
| 178 | |
| 179 | // Switch to plan mode (should store original policies) |
| 180 | service.switchMode("plan"); |
| 181 | const planPolicies = service.getPermissions().policies; |
| 182 | |
| 183 | // Plan mode should not have user policies |
| 184 | const planUserPolicyCount = planPolicies.filter( |
| 185 | (p) => |
| 186 | p.tool === "customAllowedTool" || |
| 187 | p.tool === "customExcludedTool" || |
| 188 | p.tool === "customAskTool", |
| 189 | ).length; |
| 190 | expect(planUserPolicyCount).toBe(0); |
| 191 | |
| 192 | // Switch back to normal mode (should restore original policies) |
| 193 | service.switchMode("normal"); |
| 194 | const restoredPolicies = service.getPermissions().policies; |
| 195 | |
| 196 | // Should have user policies restored |
| 197 | const restoredUserPolicyCount = restoredPolicies.filter( |
| 198 | (p) => |
| 199 | p.tool === "customAllowedTool" || |
| 200 | p.tool === "customExcludedTool" || |
| 201 | p.tool === "customAskTool", |
| 202 | ).length; |
| 203 | expect(restoredUserPolicyCount).toBe(3); |
| 204 | |
| 205 | // Verify specific policies are restored correctly |
| 206 | expect( |
| 207 | restoredPolicies.some( |
| 208 | (p) => p.tool === "customAllowedTool" && p.permission === "allow", |
| 209 | ), |
| 210 | ).toBe(true); |
| 211 | expect( |
| 212 | restoredPolicies.some( |
| 213 | (p) => p.tool === "customExcludedTool" && p.permission === "exclude", |
| 214 | ), |
| 215 | ).toBe(true); |
| 216 | expect( |
| 217 | restoredPolicies.some( |
| 218 | (p) => p.tool === "customAskTool" && p.permission === "ask", |
| 219 | ), |
| 220 | ).toBe(true); |
| 221 | }); |
| 222 | |
| 223 | it("should override user permissions in plan mode", () => { |
| 224 | // Initialize with user config that allows write tools |
| 225 | service.initializeSync({ |
| 226 | allow: ["Write", "Edit", "Bash"], |
| 227 | mode: "plan", |
| 228 | }); |
| 229 | |
| 230 | const policies = service.getPermissions().policies; |
| 231 | |
| 232 | // Mode policies should override user config - Write should be excluded despite being in allow list |
| 233 | expect( |
| 234 | policies.some((p) => p.tool === "Write" && p.permission === "exclude"), |
| 235 | ).toBe(true); |
| 236 | expect( |
| 237 | policies.some((p) => p.tool === "Edit" && p.permission === "exclude"), |
| 238 | ).toBe(true); |
| 239 | expect( |
| 240 | policies.some((p) => p.tool === "Bash" && p.permission === "allow"), |
| 241 | ).toBe(true); |
| 242 | |
| 243 | // Read tools should be allowed |
| 244 | expect( |
| 245 | policies.some((p) => p.tool === "Read" && p.permission === "allow"), |
| 246 | ).toBe(true); |
| 247 | }); |
| 248 | |
| 249 | it("should override user permissions in auto mode", () => { |
| 250 | // Initialize with user config that excludes tools |
| 251 | service.initializeSync({ |
| 252 | exclude: ["Write", "Edit", "Bash"], |
| 253 | mode: "auto", |
| 254 | }); |
| 255 | |
| 256 | const policies = service.getPermissions().policies; |
| 257 | |
| 258 | // Mode policies should override user config - everything should be allowed |
| 259 | expect(policies[0]).toEqual({ tool: "*", permission: "allow" }); |
| 260 | }); |
| 261 | }); |
| 262 | |
| 263 | describe("mode policy generation", () => { |
| 264 | it("should generate policies in correct order (mode policies first)", () => { |
| 265 | const state = service.initializeSync({ |
| 266 | mode: "plan", |
| 267 | allow: ["customTool"], |
| 268 | }); |
| 269 | const policies = state.permissions.policies; |
| 270 | |
| 271 | // Mode policies should come first, and there should be policies |
| 272 | expect(policies.length).toBeGreaterThan(0); |
| 273 | |
| 274 | // Should have MCP and wildcard exclusion policies |
| 275 | expect(policies.some((p) => p.tool === "mcp:*")).toBe(true); |
| 276 | expect( |
| 277 | policies.some((p) => p.tool === "*" && p.permission === "exclude"), |
| 278 | ).toBe(true); |
| 279 | }); |
| 280 | }); |
| 281 | |
| 282 | describe("getCurrentMode", () => { |
| 283 | it("should return the current mode", () => { |
| 284 | service.initializeSync({ mode: "plan" }); |
| 285 | expect(service.getCurrentMode()).toBe("plan"); |
| 286 | |
| 287 | service.switchMode("auto"); |
| 288 | expect(service.getCurrentMode()).toBe("auto"); |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | describe("state consistency", () => { |
| 293 | it("should maintain consistent state between getState and getCurrentMode", () => { |
| 294 | service.initializeSync({ mode: "plan" }); |
| 295 | |
| 296 | const state = service.getState(); |
| 297 | const currentMode = service.getCurrentMode(); |
| 298 | |
| 299 | expect(state.currentMode).toBe(currentMode); |
| 300 | expect(currentMode).toBe("plan"); |
| 301 | }); |
| 302 | |
| 303 | it("should update state when switching modes", () => { |
| 304 | service.initializeSync(); |
| 305 | |
| 306 | const initialState = service.getState(); |
| 307 | expect(initialState.currentMode).toBe("normal"); |
| 308 | |
| 309 | service.switchMode("plan"); |
| 310 | |
| 311 | const newState = service.getState(); |
| 312 | expect(newState.currentMode).toBe("plan"); |
| 313 | expect(newState).not.toEqual(initialState); |
| 314 | }); |
| 315 | }); |
| 316 | }); |