| 1 | import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js"; |
| 2 | import { checkToolPermission } from "./permissionChecker.js"; |
| 3 | import { resolvePermissionPrecedence } from "./precedenceResolver.js"; |
| 4 | |
| 5 | describe("Headless Permissions Integration", () => { |
| 6 | describe("precedence resolution", () => { |
| 7 | it("should use default policies", () => { |
| 8 | const policies = resolvePermissionPrecedence({ |
| 9 | useDefaults: true, |
| 10 | personalSettings: false, // Disable to avoid file system dependencies |
| 11 | }); |
| 12 | |
| 13 | expect(policies).toEqual(DEFAULT_TOOL_POLICIES); |
| 14 | }); |
| 15 | |
| 16 | it("should allow CLI overrides to supersede defaults", () => { |
| 17 | const policies = resolvePermissionPrecedence({ |
| 18 | commandLineFlags: { |
| 19 | exclude: ["dangerous_tool"], |
| 20 | }, |
| 21 | useDefaults: true, |
| 22 | personalSettings: false, |
| 23 | }); |
| 24 | |
| 25 | // CLI exclusion should come first |
| 26 | expect(policies[0]).toEqual({ |
| 27 | tool: "dangerous_tool", |
| 28 | permission: "exclude", |
| 29 | }); |
| 30 | // Default policies should follow |
| 31 | expect(policies.slice(1)).toEqual(DEFAULT_TOOL_POLICIES); |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | describe("permission checking with default policies", () => { |
| 36 | it("should follow default permission rules", () => { |
| 37 | const permissions = { policies: DEFAULT_TOOL_POLICIES }; |
| 38 | |
| 39 | // Read-only tools should be allowed |
| 40 | expect( |
| 41 | checkToolPermission({ name: "Read", arguments: {} }, permissions) |
| 42 | .permission, |
| 43 | ).toBe("allow"); |
| 44 | expect( |
| 45 | checkToolPermission({ name: "List", arguments: {} }, permissions) |
| 46 | .permission, |
| 47 | ).toBe("allow"); |
| 48 | expect( |
| 49 | checkToolPermission({ name: "Search", arguments: {} }, permissions) |
| 50 | .permission, |
| 51 | ).toBe("allow"); |
| 52 | |
| 53 | // Write operations should require permission |
| 54 | expect( |
| 55 | checkToolPermission({ name: "Write", arguments: {} }, permissions) |
| 56 | .permission, |
| 57 | ).toBe("ask"); |
| 58 | expect( |
| 59 | checkToolPermission({ name: "Bash", arguments: {} }, permissions) |
| 60 | .permission, |
| 61 | ).toBe("ask"); |
| 62 | |
| 63 | // Unknown tools should default to ask |
| 64 | expect( |
| 65 | checkToolPermission( |
| 66 | { name: "unknown_tool", arguments: {} }, |
| 67 | permissions, |
| 68 | ).permission, |
| 69 | ).toBe("ask"); |
| 70 | }); |
| 71 | |
| 72 | it("should respect CLI overrides even in headless mode", () => { |
| 73 | const policies = [ |
| 74 | { tool: "dangerous_tool", permission: "exclude" as const }, |
| 75 | ...DEFAULT_TOOL_POLICIES, |
| 76 | ]; |
| 77 | const permissions = { policies }; |
| 78 | |
| 79 | // Excluded tool should be blocked |
| 80 | const excludedResult = checkToolPermission( |
| 81 | { name: "dangerous_tool", arguments: {} }, |
| 82 | permissions, |
| 83 | ); |
| 84 | expect(excludedResult.permission).toBe("exclude"); |
| 85 | |
| 86 | // Default tools should follow their normal rules |
| 87 | expect( |
| 88 | checkToolPermission({ name: "Read", arguments: {} }, permissions) |
| 89 | .permission, |
| 90 | ).toBe("allow"); |
| 91 | expect( |
| 92 | checkToolPermission({ name: "Write", arguments: {} }, permissions) |
| 93 | .permission, |
| 94 | ).toBe("ask"); |
| 95 | }); |
| 96 | }); |
| 97 | |
| 98 | describe("real-world headless scenarios", () => { |
| 99 | it("should handle typical CLI workflow with permission overrides", () => { |
| 100 | // Simulate CLI with some overrides |
| 101 | const policies = resolvePermissionPrecedence({ |
| 102 | commandLineFlags: { |
| 103 | // Still exclude truly dangerous operations |
| 104 | exclude: ["rm", "sudo"], |
| 105 | // Allow specific tools that would normally require confirmation |
| 106 | allow: ["Write", "Bash"], |
| 107 | }, |
| 108 | useDefaults: true, |
| 109 | personalSettings: false, |
| 110 | }); |
| 111 | |
| 112 | const permissions = { policies }; |
| 113 | |
| 114 | // Excluded tools should be blocked |
| 115 | expect( |
| 116 | checkToolPermission({ name: "rm", arguments: {} }, permissions) |
| 117 | .permission, |
| 118 | ).toBe("exclude"); |
| 119 | expect( |
| 120 | checkToolPermission({ name: "sudo", arguments: {} }, permissions) |
| 121 | .permission, |
| 122 | ).toBe("exclude"); |
| 123 | |
| 124 | // Explicitly allowed tools should be allowed |
| 125 | expect( |
| 126 | checkToolPermission({ name: "Write", arguments: {} }, permissions) |
| 127 | .permission, |
| 128 | ).toBe("allow"); |
| 129 | expect( |
| 130 | checkToolPermission({ name: "Bash", arguments: {} }, permissions) |
| 131 | .permission, |
| 132 | ).toBe("allow"); |
| 133 | |
| 134 | // Read-only tools should still be allowed by default |
| 135 | expect( |
| 136 | checkToolPermission({ name: "Read", arguments: {} }, permissions) |
| 137 | .permission, |
| 138 | ).toBe("allow"); |
| 139 | |
| 140 | // Unknown tools should default to ask (since no wildcard allow override was provided) |
| 141 | expect( |
| 142 | checkToolPermission( |
| 143 | { name: "unknown_tool", arguments: {} }, |
| 144 | permissions, |
| 145 | ).permission, |
| 146 | ).toBe("ask"); |
| 147 | }); |
| 148 | |
| 149 | it("should handle workflow with wildcard allow override", () => { |
| 150 | const policies = resolvePermissionPrecedence({ |
| 151 | commandLineFlags: { |
| 152 | allow: ["*"], // Allow all tools |
| 153 | }, |
| 154 | useDefaults: true, |
| 155 | personalSettings: false, |
| 156 | }); |
| 157 | |
| 158 | const permissions = { policies }; |
| 159 | |
| 160 | // All tools should be allowed due to wildcard override |
| 161 | expect( |
| 162 | checkToolPermission({ name: "Write", arguments: {} }, permissions) |
| 163 | .permission, |
| 164 | ).toBe("allow"); |
| 165 | expect( |
| 166 | checkToolPermission({ name: "Bash", arguments: {} }, permissions) |
| 167 | .permission, |
| 168 | ).toBe("allow"); |
| 169 | expect( |
| 170 | checkToolPermission( |
| 171 | { name: "unknown_tool", arguments: {} }, |
| 172 | permissions, |
| 173 | ).permission, |
| 174 | ).toBe("allow"); |
| 175 | }); |
| 176 | }); |
| 177 | }); |