| 1 | //! Public-host observations captured before FEAT-027 production migration. |
| 2 | //! Fixtures stay outside the portable command closure. |
| 3 | use crate::commands::{CommandResult, execute}; |
| 4 | use crate::tui::app::{App, AppAction, TuiOptions}; |
| 5 | use codewhale_config::ProviderKind; |
| 6 | use codewhale_localization::Locale; |
| 7 | use codewhale_runtime::config::Config; |
| 8 | use serde_json::{Value, json}; |
| 9 | use std::fs; |
| 10 | use tempfile::TempDir; |
| 11 | |
| 12 | fn app(temp: &TempDir) -> App { |
| 13 | let options = TuiOptions { |
| 14 | skills_dir: temp.path().join("skills"), |
| 15 | memory_path: temp.path().join("memory.md"), |
| 16 | mcp_config_path: temp.path().join("mcp.json"), |
| 17 | ..crate::tui::test_support::test_tui_options(temp.path()) |
| 18 | }; |
| 19 | let mut app = App::new(options, &Config::default()); |
| 20 | app.config_path = Some(temp.path().join("config.toml")); |
| 21 | app.ui_locale = Locale::En; |
| 22 | app.api_provider = ProviderKind::Deepseek; |
| 23 | app.sandbox_backend = None; |
| 24 | app |
| 25 | } |
| 26 | |
| 27 | fn normalize(message: &str, temp: &TempDir) -> String { |
| 28 | let mut text = message.to_string(); |
| 29 | // The permission path is canonicalised by the config layer |
| 30 | // (`normalize_config_file_path`): `/private/var/...` on macOS and |
| 31 | // `\\?\C:\...` with the long name on Windows. Replace the canonical form |
| 32 | // first — substituting the raw workspace prefix inside it would leave a |
| 33 | // stray `/private` behind — then the raw form used by the fixture. |
| 34 | let canonical = temp |
| 35 | .path() |
| 36 | .canonicalize() |
| 37 | .unwrap_or_else(|_| temp.path().to_path_buf()); |
| 38 | for workspace in [canonical.as_path(), temp.path()] { |
| 39 | text = text.replace( |
| 40 | &codewhale_config::quote_os_path(&workspace.join("permissions.toml")), |
| 41 | "\"<WORKSPACE>/permissions.toml\"", |
| 42 | ); |
| 43 | text = text.replace( |
| 44 | &codewhale_util::utils::display_path(workspace), |
| 45 | "<WORKSPACE>", |
| 46 | ); |
| 47 | if let Some(workspace) = workspace.to_str() { |
| 48 | text = text.replace(workspace, "<WORKSPACE>"); |
| 49 | } |
| 50 | } |
| 51 | if let Some(audit) = codewhale_runtime::audit::audit_log_path() { |
| 52 | text = text.replace( |
| 53 | &codewhale_config::quote_os_path(&audit), |
| 54 | "\"<TEST_AUDIT_LOG>\"", |
| 55 | ); |
| 56 | } |
| 57 | if let Some(home) = codewhale_paths::user_home() { |
| 58 | text = text.replace(home.to_str().unwrap(), "<TEST_HOME>"); |
| 59 | } |
| 60 | // Catalog freshness depends on the bundled snapshot's age against the |
| 61 | // wall clock, so it cannot be part of a stable baseline. |
| 62 | while let Some(start) = text.find(" · Catalog: ") { |
| 63 | let Some(end) = text[start..].find(" · Cloud facts:") else { |
| 64 | break; |
| 65 | }; |
| 66 | text.replace_range(start..start + end, ""); |
| 67 | } |
| 68 | text |
| 69 | } |
| 70 | |
| 71 | fn observed(result: &CommandResult, temp: &TempDir) -> Value { |
| 72 | json!({"message": result.message.as_deref().map(|m| normalize(m, temp)), |
| 73 | "error": result.is_error, "action": format!("{:?}", result.action)}) |
| 74 | } |
| 75 | |
| 76 | fn golden(name: &str, actual: Value) { |
| 77 | let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 78 | .join("src/commands/contract/fixtures/config_policy") |
| 79 | .join(format!("{name}.json")); |
| 80 | let expected: Value = serde_json::from_str(&fs::read_to_string(path).unwrap()).unwrap(); |
| 81 | assert_eq!(actual, expected, "public command baseline {name}"); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn permissions_public_routes_errors_and_transaction_match_baseline() { |
| 86 | let _env = crate::test_support::lock_test_env(); |
| 87 | let temp = TempDir::new().unwrap(); |
| 88 | let mut app = app(&temp); |
| 89 | let path = temp.path().join("permissions.toml"); |
| 90 | let mut results = Vec::new(); |
| 91 | results.push(observed(&execute("/permissions", &mut app), &temp)); |
| 92 | fs::write(&path, "").unwrap(); |
| 93 | results.push(observed(&execute("/permissions status", &mut app), &temp)); |
| 94 | let original = "# preserve this header\n[[rules]]\ntool = \"exec_shell\"\ncommand = \"cargo test\"\ncommand_exact = true\naction = \"allow\"\n\n[[rules]]\ntool = \"edit_file\"\npath = \"src/lib.rs\"\naction = \"deny\"\n"; |
| 95 | fs::write(&path, original).unwrap(); |
| 96 | let listing = execute("/permissions list", &mut app); |
| 97 | results.push(observed(&listing, &temp)); |
| 98 | for name in ["/permission-rules", "/permission_rules"] { |
| 99 | assert_eq!( |
| 100 | observed(&execute(name, &mut app), &temp), |
| 101 | observed(&listing, &temp) |
| 102 | ); |
| 103 | } |
| 104 | for token in [ |
| 105 | "ask-rules", |
| 106 | "ask_rules", |
| 107 | "askrules", |
| 108 | "rules", |
| 109 | "permission-rules", |
| 110 | "permission_rules", |
| 111 | "permissions", |
| 112 | ] { |
| 113 | assert_eq!( |
| 114 | observed(&execute(&format!("/config {token} list"), &mut app), &temp), |
| 115 | observed(&listing, &temp) |
| 116 | ); |
| 117 | } |
| 118 | for args in [ |
| 119 | "unknown", |
| 120 | "remove", |
| 121 | "remove bad", |
| 122 | "remove 0", |
| 123 | "remove 99", |
| 124 | "remove 1 extra", |
| 125 | "remove 1 --bad token", |
| 126 | ] { |
| 127 | let result = execute(&format!("/permissions {args}"), &mut app); |
| 128 | assert!(result.is_error); |
| 129 | assert!(result.action.is_none()); |
| 130 | assert_eq!(fs::read_to_string(&path).unwrap(), original); |
| 131 | results.push(observed(&result, &temp)); |
| 132 | } |
| 133 | let preview = execute("/permissions remove 1", &mut app); |
| 134 | assert!(!preview.is_error); |
| 135 | assert!(preview.action.is_none()); |
| 136 | assert_eq!(fs::read_to_string(&path).unwrap(), original); |
| 137 | let command = preview |
| 138 | .message |
| 139 | .as_deref() |
| 140 | .unwrap() |
| 141 | .split('`') |
| 142 | .find(|s| s.starts_with("/permissions remove 1 --confirm ")) |
| 143 | .unwrap() |
| 144 | .to_string(); |
| 145 | let token = command.split_whitespace().last().unwrap(); |
| 146 | let mut preview_observed = observed(&preview, &temp); |
| 147 | preview_observed["message"] = json!( |
| 148 | normalize(preview.message.as_ref().unwrap(), &temp).replace(token, "<CONFIRM_TOKEN>") |
| 149 | ); |
| 150 | results.push(preview_observed); |
| 151 | let changed = format!("{original}\n# changed since preview\n"); |
| 152 | fs::write(&path, &changed).unwrap(); |
| 153 | let stale = execute(&command, &mut app); |
| 154 | assert!(stale.is_error); |
| 155 | assert!(stale.action.is_none()); |
| 156 | assert_eq!(fs::read_to_string(&path).unwrap(), changed); |
| 157 | results.push(observed(&stale, &temp)); |
| 158 | let preview = execute("/permissions remove 1", &mut app); |
| 159 | let command = preview |
| 160 | .message |
| 161 | .as_deref() |
| 162 | .unwrap() |
| 163 | .split('`') |
| 164 | .find(|s| s.starts_with("/permissions remove 1 --confirm ")) |
| 165 | .unwrap() |
| 166 | .to_string(); |
| 167 | let removed = execute(&command, &mut app); |
| 168 | assert!(!removed.is_error); |
| 169 | assert_eq!(removed.action, Some(AppAction::PermissionRulesChanged)); |
| 170 | let persisted = fs::read_to_string(&path).unwrap(); |
| 171 | assert!(persisted.contains("# preserve this header")); |
| 172 | let parsed: codewhale_config::PermissionsToml = toml::from_str(&persisted).unwrap(); |
| 173 | assert_eq!(parsed.rules.len(), 1); |
| 174 | assert_eq!(parsed.rules[0].tool, "edit_file"); |
| 175 | results.push(observed(&removed, &temp)); |
| 176 | golden("permissions", json!(results)); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn status_public_output_and_read_only_state_match_baseline() { |
| 181 | let _env = crate::test_support::lock_test_env(); |
| 182 | let temp = TempDir::new().unwrap(); |
| 183 | fs::write(temp.path().join("AGENTS.md"), "fixture instructions").unwrap(); |
| 184 | let mut app = app(&temp); |
| 185 | app.current_session_id = Some("baseline-{model}-session".into()); |
| 186 | app.model = "baseline-{provider}-model".into(); |
| 187 | app.turn_counter = 4; |
| 188 | app.session.total_tokens = 1234; |
| 189 | app.session.last_prompt_tokens = Some(100); |
| 190 | app.session.last_completion_tokens = Some(25); |
| 191 | let before = ( |
| 192 | app.model.clone(), |
| 193 | app.current_session_id.clone(), |
| 194 | app.turn_counter, |
| 195 | app.session.total_tokens, |
| 196 | ); |
| 197 | let first = execute("/status", &mut app); |
| 198 | assert!(!first.is_error); |
| 199 | assert!(first.action.is_none()); |
| 200 | let second = execute("/status ignored-arguments", &mut app); |
| 201 | assert_eq!(observed(&first, &temp), observed(&second, &temp)); |
| 202 | assert_eq!( |
| 203 | before, |
| 204 | ( |
| 205 | app.model.clone(), |
| 206 | app.current_session_id.clone(), |
| 207 | app.turn_counter, |
| 208 | app.session.total_tokens |
| 209 | ) |
| 210 | ); |
| 211 | assert!(!temp.path().join("config.toml").exists()); |
| 212 | golden("status", observed(&first, &temp)); |
| 213 | } |
| 214 |