| 1 | //! Existing permission host regressions, moved out of the portable closure. |
| 2 | use crate::commands::config_policy_host::permissions as permissions_command; |
| 3 | use crate::commands::contract::config_policy::rule_applies_in_workspace; |
| 4 | use crate::tui::app::{App, AppAction}; |
| 5 | use codewhale_config::ToolAskRule; |
| 6 | use codewhale_localization::{MessageId, tr}; |
| 7 | |
| 8 | mod tests { |
| 9 | use std::fs; |
| 10 | |
| 11 | use crate::tui::app::TuiOptions; |
| 12 | use codewhale_localization::Locale; |
| 13 | |
| 14 | use super::*; |
| 15 | |
| 16 | fn test_app(config_path: std::path::PathBuf, workspace: std::path::PathBuf) -> App { |
| 17 | let config = codewhale_runtime::config::Config::default(); |
| 18 | let mut app = App::new( |
| 19 | TuiOptions { |
| 20 | workspace, |
| 21 | ..crate::tui::test_support::test_tui_options(std::path::PathBuf::from(".")) |
| 22 | }, |
| 23 | &config, |
| 24 | ); |
| 25 | app.config_path = Some(config_path); |
| 26 | app.ui_locale = Locale::En; |
| 27 | app |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn list_shows_source_scope_matcher_and_workspace_applicability() { |
| 32 | let dir = tempfile::tempdir().expect("tempdir"); |
| 33 | let other = tempfile::tempdir().expect("other tempdir"); |
| 34 | let config_path = dir.path().join("config.toml"); |
| 35 | let permissions_path = dir.path().join("permissions.toml"); |
| 36 | fs::write( |
| 37 | &permissions_path, |
| 38 | format!( |
| 39 | r#" |
| 40 | [[rules]] |
| 41 | tool = "exec_shell" |
| 42 | command = "cargo test" |
| 43 | command_exact = true |
| 44 | workspace = {workspace:?} |
| 45 | action = "allow" |
| 46 | |
| 47 | [[rules]] |
| 48 | tool = "edit_file" |
| 49 | path = "src/lib.rs" |
| 50 | workspace = {other:?} |
| 51 | "#, |
| 52 | workspace = dir.path().to_string_lossy(), |
| 53 | other = other.path().to_string_lossy(), |
| 54 | ), |
| 55 | ) |
| 56 | .expect("write permissions"); |
| 57 | let displayed_permissions_path = |
| 58 | codewhale_config::resolve_permissions_path(Some(config_path.clone())) |
| 59 | .expect("resolve permissions path"); |
| 60 | let mut app = test_app(config_path, dir.path().to_path_buf()); |
| 61 | |
| 62 | let result = permissions_command(&mut app, Some("list")); |
| 63 | let message = result.message.expect("list message"); |
| 64 | |
| 65 | assert!(!result.is_error); |
| 66 | assert!(message.contains(&codewhale_config::quote_os_path( |
| 67 | &displayed_permissions_path |
| 68 | ))); |
| 69 | assert!(message.contains("#1 | allow | exec_shell")); |
| 70 | assert!(message.contains("exact command `cargo test`")); |
| 71 | assert!(message.contains("active in this workspace")); |
| 72 | assert!(message.contains("#2 | ask | edit_file")); |
| 73 | assert!(message.contains("exact normalized path `src/lib.rs`")); |
| 74 | assert!(message.contains("not active in this workspace")); |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn list_preserves_missing_empty_and_malformed_diagnostics() { |
| 79 | let dir = tempfile::tempdir().expect("tempdir"); |
| 80 | let config_path = dir.path().join("config.toml"); |
| 81 | let permissions_path = dir.path().join("permissions.toml"); |
| 82 | let displayed_permissions_path = |
| 83 | codewhale_config::resolve_permissions_path(Some(config_path.clone())) |
| 84 | .expect("resolve permissions path"); |
| 85 | let mut app = test_app(config_path, dir.path().to_path_buf()); |
| 86 | |
| 87 | let missing = permissions_command(&mut app, None); |
| 88 | let missing_message = missing.message.expect("missing message"); |
| 89 | assert!(!missing.is_error); |
| 90 | assert!(missing_message.contains("File status: missing")); |
| 91 | assert!(missing_message.contains("Rule count: 0")); |
| 92 | |
| 93 | fs::write(&permissions_path, "").expect("write empty permissions"); |
| 94 | let empty = permissions_command(&mut app, Some("status")); |
| 95 | let empty_message = empty.message.expect("empty message"); |
| 96 | assert!(!empty.is_error); |
| 97 | assert!(empty_message.contains("File status: empty")); |
| 98 | |
| 99 | fs::write( |
| 100 | &permissions_path, |
| 101 | "[[rules]]\ntool = \"do-not-echo-this\"\ncommand = ", |
| 102 | ) |
| 103 | .expect("write malformed permissions"); |
| 104 | let malformed = permissions_command(&mut app, Some("list")); |
| 105 | let malformed_message = malformed.message.expect("malformed message"); |
| 106 | assert!(malformed.is_error); |
| 107 | assert!(malformed_message.contains("Could not read or change permission rules")); |
| 108 | assert!(malformed_message.contains(&codewhale_config::quote_os_path( |
| 109 | &displayed_permissions_path |
| 110 | ))); |
| 111 | assert!(malformed_message.contains("file contents were omitted")); |
| 112 | assert!(!malformed_message.contains("do-not-echo-this")); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn remove_requires_preview_token_then_emits_live_reload_action() { |
| 117 | let dir = tempfile::tempdir().expect("tempdir"); |
| 118 | let config_path = dir.path().join("config.toml"); |
| 119 | let permissions_path = dir.path().join("permissions.toml"); |
| 120 | let original = "[[rules]]\ntool = \"exec_shell\"\ncommand = \"cargo test\"\n"; |
| 121 | fs::write(&permissions_path, original).expect("write permissions"); |
| 122 | let mut app = test_app(config_path, dir.path().to_path_buf()); |
| 123 | |
| 124 | let preview = permissions_command(&mut app, Some("remove 1")); |
| 125 | let preview_message = preview.message.expect("preview message"); |
| 126 | assert!(!preview.is_error); |
| 127 | assert_eq!( |
| 128 | fs::read_to_string(&permissions_path).expect("read previewed permissions"), |
| 129 | original |
| 130 | ); |
| 131 | let confirm_command = preview_message |
| 132 | .split('`') |
| 133 | .find(|part| part.starts_with("/permissions remove 1 --confirm ")) |
| 134 | .expect("confirmation command"); |
| 135 | let confirm_arg = confirm_command |
| 136 | .strip_prefix("/permissions ") |
| 137 | .expect("command prefix"); |
| 138 | |
| 139 | let confirmed = permissions_command(&mut app, Some(confirm_arg)); |
| 140 | |
| 141 | assert!(!confirmed.is_error); |
| 142 | assert_eq!(confirmed.action, Some(AppAction::PermissionRulesChanged)); |
| 143 | let persisted = fs::read_to_string(&permissions_path).expect("read edited permissions"); |
| 144 | let parsed: codewhale_config::PermissionsToml = |
| 145 | toml::from_str(&persisted).expect("parse edited permissions"); |
| 146 | assert!(parsed.rules.is_empty()); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn legacy_config_ask_rules_entry_uses_the_permissions_editor_list() { |
| 151 | let dir = tempfile::tempdir().expect("tempdir"); |
| 152 | let config_path = dir.path().join("config.toml"); |
| 153 | fs::write( |
| 154 | dir.path().join("permissions.toml"), |
| 155 | "[[rules]]\ntool = \"exec_shell\"\ncommand = \"cargo test\"\n", |
| 156 | ) |
| 157 | .expect("write permissions"); |
| 158 | let mut app = test_app(config_path, dir.path().to_path_buf()); |
| 159 | |
| 160 | let result = { |
| 161 | let contract = crate::commands::groups::config::config::config( |
| 162 | app.command_contexts_with_config(None) |
| 163 | .contexts(crate::commands::groups::config::config::CONFIG_CAPS), |
| 164 | Some("ask-rules list"), |
| 165 | ); |
| 166 | crate::commands::CommandResult { |
| 167 | message: contract.message, |
| 168 | action: contract |
| 169 | .action |
| 170 | .map(crate::commands::config_host::config_action_to_app_action), |
| 171 | is_error: contract.is_error, |
| 172 | } |
| 173 | }; |
| 174 | let message = result.message.expect("compatibility list message"); |
| 175 | |
| 176 | assert!(!result.is_error); |
| 177 | assert!(message.contains("Permission rules")); |
| 178 | assert!(message.contains("#1 | ask | exec_shell")); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn permissions_command_is_registered_with_compatibility_aliases() { |
| 183 | let info = crate::commands::get_command_info("permissions").expect("permissions command"); |
| 184 | |
| 185 | assert_eq!(info.name, "permissions"); |
| 186 | assert!(info.aliases.contains(&"permission-rules")); |
| 187 | assert!(info.usage.contains("remove <rule-number>")); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn invalid_workspace_scopes_never_appear_active() { |
| 192 | let mut rule = ToolAskRule::exec_shell("cargo test"); |
| 193 | rule.workspace = Some("../not-an-absolute-scope".to_string()); |
| 194 | |
| 195 | assert!(!rule_applies_in_workspace( |
| 196 | &rule, |
| 197 | std::path::Path::new("also-relative") |
| 198 | )); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn permission_messages_keep_placeholder_parity_across_complete_locales() { |
| 203 | let ids = [ |
| 204 | MessageId::PermissionsListHeader, |
| 205 | MessageId::PermissionsRuleEntry, |
| 206 | MessageId::PermissionsMatchExactCommand, |
| 207 | MessageId::PermissionsMatchCommandPrefix, |
| 208 | MessageId::PermissionsMatchExactPath, |
| 209 | MessageId::PermissionsScopeRepo, |
| 210 | MessageId::PermissionsRemovePreview, |
| 211 | MessageId::PermissionsRemoved, |
| 212 | MessageId::PermissionsRuleNotFound, |
| 213 | MessageId::PermissionsOperationFailed, |
| 214 | ]; |
| 215 | for id in ids { |
| 216 | let english = placeholders(&tr(Locale::En, id)); |
| 217 | for locale in Locale::shipped_complete() { |
| 218 | assert_eq!( |
| 219 | placeholders(&tr(*locale, id)), |
| 220 | english, |
| 221 | "{} {id:?} placeholder drift", |
| 222 | locale.tag() |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | fn placeholders(message: &str) -> std::collections::BTreeSet<String> { |
| 229 | message |
| 230 | .split('{') |
| 231 | .skip(1) |
| 232 | .filter_map(|suffix| suffix.split_once('}').map(|(name, _)| name.to_string())) |
| 233 | .collect() |
| 234 | } |
| 235 | } |
| 236 |