| 1 | #[test] |
| 2 | fn exec_only_installs_an_explicit_headless_turn_cap() { |
| 3 | let cli = parse_cli(&["codewhale", "exec", "--auto", "benchmark this"]); |
| 4 | let Some(Commands::Exec(args)) = cli.command else { |
| 5 | panic!("expected exec command"); |
| 6 | }; |
| 7 | |
| 8 | assert_eq!(args.max_turns, None); |
| 9 | let defaulted = exec_max_steps(args.max_turns); |
| 10 | assert_eq!( |
| 11 | defaulted, |
| 12 | codewhale_runtime::turn_budget::DEFAULT_MAX_MODEL_STEPS |
| 13 | ); |
| 14 | assert_eq!( |
| 15 | codewhale_engine::core::turn::TurnContext::new(defaulted).step_limit(), |
| 16 | None |
| 17 | ); |
| 18 | assert_eq!(exec_max_steps(Some(7)), 7); |
| 19 | let mut capped = codewhale_engine::core::turn::TurnContext::new(exec_max_steps(Some(7))); |
| 20 | for _ in 0..7 { |
| 21 | capped.next_step(); |
| 22 | } |
| 23 | assert!(capped.at_max_steps()); |
| 24 | assert_eq!(capped.stop_diagnostics.effective_max_steps, Some(7)); |
| 25 | assert_eq!( |
| 26 | exec_max_steps(Some(u32::MAX)), |
| 27 | codewhale_runtime::turn_budget::DEFAULT_MAX_MODEL_STEPS, |
| 28 | "the compatibility sentinel remains unbounded" |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | fn exec_accepts_continue_for_latest_workspace_session() { |
| 34 | let cli = parse_cli(&["codewhale", "exec", "--continue", "follow up"]); |
| 35 | let Some(Commands::Exec(args)) = cli.command else { |
| 36 | panic!("expected exec command"); |
| 37 | }; |
| 38 | |
| 39 | assert!(args.continue_session); |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn sessions_footer_points_to_resume_subcommand() { |
| 44 | let cli = parse_cli(&["codewhale", "resume", "abc123"]); |
| 45 | let Some(Commands::Resume { session_id, last }) = cli.command else { |
| 46 | panic!("expected resume command"); |
| 47 | }; |
| 48 | |
| 49 | assert_eq!(session_id.as_deref(), Some("abc123")); |
| 50 | assert!(!last); |
| 51 | assert_eq!(sessions_resume_command(), "codewhale resume"); |
| 52 | assert!(!sessions_resume_command().contains("--resume")); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn plugin_registry_initialization_precedes_dotenv_for_all_launch_paths() { |
| 57 | use std::cell::Cell; |
| 58 | |
| 59 | #[derive(Clone, Copy)] |
| 60 | enum Expected { |
| 61 | Plain, |
| 62 | Resume, |
| 63 | Fork, |
| 64 | Exec, |
| 65 | Serve, |
| 66 | } |
| 67 | |
| 68 | let cases: &[(&[&str], Expected)] = &[ |
| 69 | (&["codewhale"], Expected::Plain), |
| 70 | (&["codewhale", "resume", "--last"], Expected::Resume), |
| 71 | (&["codewhale", "fork", "--last"], Expected::Fork), |
| 72 | (&["codewhale", "exec", "probe"], Expected::Exec), |
| 73 | (&["codewhale", "serve", "--mcp"], Expected::Serve), |
| 74 | ]; |
| 75 | |
| 76 | for (args, expected) in cases { |
| 77 | let phase = Cell::new(0); |
| 78 | let (_cli, command) = prepare_cli_startup( |
| 79 | parse_cli(args), |
| 80 | || { |
| 81 | assert_eq!(phase.get(), 0, "plugin init order for {args:?}"); |
| 82 | phase.set(1); |
| 83 | }, |
| 84 | || { |
| 85 | assert_eq!(phase.get(), 1, "dotenv load order for {args:?}"); |
| 86 | phase.set(2); |
| 87 | }, |
| 88 | ); |
| 89 | |
| 90 | assert_eq!(phase.get(), 2, "startup phases for {args:?}"); |
| 91 | let correct_variant = matches!( |
| 92 | (expected, command.as_ref()), |
| 93 | (Expected::Plain, None) |
| 94 | | (Expected::Resume, Some(Commands::Resume { .. })) |
| 95 | | (Expected::Fork, Some(Commands::Fork { .. })) |
| 96 | | (Expected::Exec, Some(Commands::Exec(_))) |
| 97 | | (Expected::Serve, Some(Commands::Serve(_))) |
| 98 | ); |
| 99 | assert!(correct_variant, "unexpected command for {args:?}"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn workspace_dotenv_is_found_from_the_launch_workspace_not_the_process_directory() { |
| 105 | let workspace = tempfile::tempdir().expect("workspace tempdir"); |
| 106 | let nested = workspace.path().join("crates/app"); |
| 107 | std::fs::create_dir_all(&nested).expect("mkdir nested"); |
| 108 | std::fs::create_dir_all(workspace.path().join(".git")).expect("mkdir .git"); |
| 109 | std::fs::write(workspace.path().join(".env"), "OPENAI_API_KEY=workspace\n") |
| 110 | .expect("write .env"); |
| 111 | |
| 112 | // `--workspace <dir>` from a process directory in another tree. |
| 113 | assert_eq!( |
| 114 | find_workspace_dotenv(workspace.path()).expect("search"), |
| 115 | Some(workspace.path().join(".env")) |
| 116 | ); |
| 117 | // Nested launch directory walks up to the repository root. |
| 118 | assert_eq!( |
| 119 | find_workspace_dotenv(&nested).expect("search"), |
| 120 | Some(workspace.path().join(".env")) |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn workspace_dotenv_loads_only_provider_credentials_and_preserves_shell_values() { |
| 126 | let _lock = crate::test_support::lock_test_env(); |
| 127 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 128 | let _nvidia = crate::test_support::EnvVarGuard::set("NVIDIA_API_KEY", "shell-key"); |
| 129 | let _home = crate::test_support::EnvVarGuard::remove("CODEWHALE_HOME"); |
| 130 | let _config = crate::test_support::EnvVarGuard::remove("CODEWHALE_CONFIG_PATH"); |
| 131 | let _shell = crate::test_support::EnvVarGuard::remove("DEEPSEEK_ALLOW_SHELL"); |
| 132 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 133 | let dotenv = tmp.path().join(".env"); |
| 134 | std::fs::write( |
| 135 | &dotenv, |
| 136 | "DEEPSEEK_API_KEY=workspace-key\n\ |
| 137 | NVIDIA_API_KEY=repo-must-not-override-shell\n\ |
| 138 | CODEWHALE_HOME=./attacker-home\n\ |
| 139 | CODEWHALE_CONFIG_PATH=./attacker.toml\n\ |
| 140 | DEEPSEEK_ALLOW_SHELL=true\n", |
| 141 | ) |
| 142 | .expect("write dotenv"); |
| 143 | |
| 144 | let report = load_workspace_dotenv_credentials_from_path(&dotenv).expect("safe load"); |
| 145 | |
| 146 | assert_eq!( |
| 147 | std::env::var("DEEPSEEK_API_KEY").as_deref(), |
| 148 | Ok("workspace-key") |
| 149 | ); |
| 150 | assert_eq!(std::env::var("NVIDIA_API_KEY").as_deref(), Ok("shell-key")); |
| 151 | assert!(std::env::var_os("CODEWHALE_HOME").is_none()); |
| 152 | assert!(std::env::var_os("CODEWHALE_CONFIG_PATH").is_none()); |
| 153 | assert!(std::env::var_os("DEEPSEEK_ALLOW_SHELL").is_none()); |
| 154 | assert_eq!( |
| 155 | report.loaded, |
| 156 | BTreeSet::from(["DEEPSEEK_API_KEY".to_string()]) |
| 157 | ); |
| 158 | assert_eq!( |
| 159 | report.ignored, |
| 160 | BTreeSet::from([ |
| 161 | "CODEWHALE_CONFIG_PATH".to_string(), |
| 162 | "CODEWHALE_HOME".to_string(), |
| 163 | "DEEPSEEK_ALLOW_SHELL".to_string(), |
| 164 | ]) |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn workspace_dotenv_rejects_ambient_variable_substitution() { |
| 170 | let _lock = crate::test_support::lock_test_env(); |
| 171 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 172 | let _ambient = crate::test_support::EnvVarGuard::set( |
| 173 | "CODEWHALE_JS_SECRET_LEAK_TEST", |
| 174 | "ambient-secret-must-not-expand", |
| 175 | ); |
| 176 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 177 | let dotenv = tmp.path().join(".env"); |
| 178 | std::fs::write( |
| 179 | &dotenv, |
| 180 | "DEEPSEEK_API_KEY=${CODEWHALE_JS_SECRET_LEAK_TEST}\n", |
| 181 | ) |
| 182 | .expect("write dotenv"); |
| 183 | |
| 184 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 185 | .expect_err("expansion must fail closed") |
| 186 | .to_string(); |
| 187 | |
| 188 | assert!(error.contains("variable expansion")); |
| 189 | assert!(!error.contains("ambient-secret-must-not-expand")); |
| 190 | assert!(std::env::var_os("DEEPSEEK_API_KEY").is_none()); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn workspace_dotenv_rejects_multiline_ambient_variable_substitution() { |
| 195 | let _lock = crate::test_support::lock_test_env(); |
| 196 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 197 | let _ambient = crate::test_support::EnvVarGuard::set( |
| 198 | "CODEWHALE_JS_SECRET_LEAK_TEST", |
| 199 | "ambient-secret-must-not-expand", |
| 200 | ); |
| 201 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 202 | let dotenv = tmp.path().join(".env"); |
| 203 | std::fs::write( |
| 204 | &dotenv, |
| 205 | "DEEPSEEK_API_KEY=\"prefix\n$CODEWHALE_JS_SECRET_LEAK_TEST=bar\nsuffix\"\n", |
| 206 | ) |
| 207 | .expect("write dotenv"); |
| 208 | |
| 209 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 210 | .expect_err("multiline expansion must fail closed") |
| 211 | .to_string(); |
| 212 | |
| 213 | assert!(error.contains("variable expansion")); |
| 214 | assert!(!error.contains("ambient-secret-must-not-expand")); |
| 215 | assert!(std::env::var_os("DEEPSEEK_API_KEY").is_none()); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn workspace_dotenv_comment_quote_cannot_hide_later_expansion() { |
| 220 | let _lock = crate::test_support::lock_test_env(); |
| 221 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 222 | let _ambient = crate::test_support::EnvVarGuard::set( |
| 223 | "CODEWHALE_JS_SECRET_LEAK_TEST", |
| 224 | "ambient-secret-must-not-expand", |
| 225 | ); |
| 226 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 227 | let dotenv = tmp.path().join(".env"); |
| 228 | std::fs::write( |
| 229 | &dotenv, |
| 230 | "# unmatched quote in ignored comment: '\n\ |
| 231 | DEEPSEEK_API_KEY=$CODEWHALE_JS_SECRET_LEAK_TEST\n", |
| 232 | ) |
| 233 | .expect("write dotenv"); |
| 234 | |
| 235 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 236 | .expect_err("comment quote must not hide expansion") |
| 237 | .to_string(); |
| 238 | |
| 239 | assert!(error.contains("variable expansion")); |
| 240 | assert!(!error.contains("ambient-secret-must-not-expand")); |
| 241 | assert!(std::env::var_os("DEEPSEEK_API_KEY").is_none()); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn workspace_dotenv_allows_single_quoted_literal_dollar() { |
| 246 | let _lock = crate::test_support::lock_test_env(); |
| 247 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 248 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 249 | let dotenv = tmp.path().join(".env"); |
| 250 | std::fs::write(&dotenv, "DEEPSEEK_API_KEY='$literal-value'\n").expect("write dotenv"); |
| 251 | |
| 252 | load_workspace_dotenv_credentials_from_path(&dotenv).expect("literal dollar load"); |
| 253 | |
| 254 | assert_eq!( |
| 255 | std::env::var("DEEPSEEK_API_KEY").as_deref(), |
| 256 | Ok("$literal-value") |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn workspace_dotenv_parse_failure_applies_no_earlier_credentials() { |
| 262 | let _lock = crate::test_support::lock_test_env(); |
| 263 | let _deepseek = crate::test_support::EnvVarGuard::remove("DEEPSEEK_API_KEY"); |
| 264 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 265 | let dotenv = tmp.path().join(".env"); |
| 266 | std::fs::write( |
| 267 | &dotenv, |
| 268 | "DEEPSEEK_API_KEY=must-not-survive\nBROKEN=\"unterminated\n", |
| 269 | ) |
| 270 | .expect("write dotenv"); |
| 271 | |
| 272 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 273 | .expect_err("parse failure must be transactional") |
| 274 | .to_string(); |
| 275 | |
| 276 | assert!(error.contains("could not be parsed safely"), "{error}"); |
| 277 | assert!(!error.contains("must-not-survive")); |
| 278 | assert!(std::env::var_os("DEEPSEEK_API_KEY").is_none()); |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn workspace_dotenv_credential_allowlist_excludes_control_plane_names() { |
| 283 | for provider in codewhale_config::provider::providers_sorted_for_display() { |
| 284 | for key in provider.env_vars() { |
| 285 | assert!( |
| 286 | is_workspace_dotenv_credential_key(key), |
| 287 | "provider credential {key} must remain supported" |
| 288 | ); |
| 289 | } |
| 290 | } |
| 291 | for key in [ |
| 292 | "CODEWHALE_HOME", |
| 293 | "CODEWHALE_CONFIG_PATH", |
| 294 | "DEEPSEEK_CONFIG_PATH", |
| 295 | "DEEPSEEK_PROFILE", |
| 296 | "DEEPSEEK_MANAGED_CONFIG_PATH", |
| 297 | "DEEPSEEK_REQUIREMENTS_PATH", |
| 298 | "DEEPSEEK_PROVIDER", |
| 299 | "DEEPSEEK_BASE_URL", |
| 300 | "DEEPSEEK_MODEL", |
| 301 | "DEEPSEEK_APPROVAL_POLICY", |
| 302 | "DEEPSEEK_SANDBOX_MODE", |
| 303 | "DEEPSEEK_ALLOW_SHELL", |
| 304 | "DEEPSEEK_YOLO", |
| 305 | "DEEPSEEK_MCP_CONFIG", |
| 306 | "CODEWHALE_RUNTIME_TOKEN", |
| 307 | "PATH", |
| 308 | "NODE_OPTIONS", |
| 309 | "PYTHONPATH", |
| 310 | "LD_PRELOAD", |
| 311 | "DYLD_INSERT_LIBRARIES", |
| 312 | ] { |
| 313 | assert!( |
| 314 | !is_workspace_dotenv_credential_key(key), |
| 315 | "control-plane variable {key} must not load from a workspace" |
| 316 | ); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | #[cfg(unix)] |
| 321 | #[test] |
| 322 | fn workspace_dotenv_does_not_follow_symbolic_links() { |
| 323 | use std::os::unix::fs::symlink; |
| 324 | |
| 325 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 326 | let external = tmp.path().join("external-credentials"); |
| 327 | let dotenv = tmp.path().join(".env"); |
| 328 | std::fs::write(&external, "DEEPSEEK_API_KEY=external-secret\n") |
| 329 | .expect("write external fixture"); |
| 330 | symlink(&external, &dotenv).expect("create dotenv symlink"); |
| 331 | |
| 332 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 333 | .expect_err("symlink must fail closed") |
| 334 | .to_string(); |
| 335 | |
| 336 | assert!(error.contains("securely open"), "{error}"); |
| 337 | assert!(!error.contains("external-secret")); |
| 338 | } |
| 339 | |
| 340 | #[cfg(unix)] |
| 341 | #[test] |
| 342 | fn workspace_dotenv_rejects_hard_links_to_external_files() { |
| 343 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 344 | let external = tmp.path().join("external-credentials"); |
| 345 | let dotenv = tmp.path().join(".env"); |
| 346 | std::fs::write(&external, "DEEPSEEK_API_KEY=external-secret\n") |
| 347 | .expect("write external fixture"); |
| 348 | std::fs::hard_link(&external, &dotenv).expect("create dotenv hard link"); |
| 349 | |
| 350 | let error = load_workspace_dotenv_credentials_from_path(&dotenv) |
| 351 | .expect_err("hard link must fail closed") |
| 352 | .to_string(); |
| 353 | |
| 354 | assert!(error.contains("multiple filesystem links"), "{error}"); |
| 355 | assert!(!error.contains("external-secret")); |
| 356 | } |
| 357 | |
| 358 | #[cfg(unix)] |
| 359 | #[test] |
| 360 | fn workspace_dotenv_rejects_fifo_without_blocking_startup() { |
| 361 | use std::ffi::CString; |
| 362 | use std::os::unix::ffi::OsStrExt; |
| 363 | use std::sync::mpsc; |
| 364 | use std::time::Duration; |
| 365 | |
| 366 | let tmp = tempfile::TempDir::new().expect("temp workspace"); |
| 367 | let dotenv = tmp.path().join(".env"); |
| 368 | let c_path = CString::new(dotenv.as_os_str().as_bytes()).expect("fifo path"); |
| 369 | // SAFETY: `c_path` is a live, NUL-terminated path and the requested |
| 370 | // mode grants access only to the current user. |
| 371 | let result = unsafe { libc::mkfifo(c_path.as_ptr(), libc::S_IRUSR | libc::S_IWUSR) }; |
| 372 | assert_eq!(result, 0, "mkfifo failed: {}", io::Error::last_os_error()); |
| 373 | |
| 374 | let (tx, rx) = mpsc::channel(); |
| 375 | let worker_path = dotenv.clone(); |
| 376 | let worker = std::thread::spawn(move || { |
| 377 | let result = load_workspace_dotenv_credentials_from_path(&worker_path) |
| 378 | .map(|_| "unexpected success".to_string()) |
| 379 | .unwrap_or_else(|error| error.to_string()); |
| 380 | tx.send(result).expect("send loader result"); |
| 381 | }); |
| 382 | |
| 383 | let error = match rx.recv_timeout(Duration::from_secs(1)) { |
| 384 | Ok(error) => error, |
| 385 | Err(timeout) => { |
| 386 | // Release a regressed blocking reader so the test can fail |
| 387 | // promptly instead of leaving a stuck process behind. |
| 388 | let _writer = std::fs::OpenOptions::new() |
| 389 | .write(true) |
| 390 | .open(&dotenv) |
| 391 | .expect("open fifo writer to release blocked reader"); |
| 392 | let _ = rx.recv_timeout(Duration::from_secs(1)); |
| 393 | worker.join().expect("join released loader"); |
| 394 | panic!("workspace .env FIFO blocked startup: {timeout}"); |
| 395 | } |
| 396 | }; |
| 397 | worker.join().expect("join loader"); |
| 398 | |
| 399 | assert!(error.contains("not a regular file"), "{error}"); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn exec_json_conflicts_with_stream_json_output() { |
| 404 | let err = Cli::try_parse_from([ |
| 405 | "codewhale", |
| 406 | "exec", |
| 407 | "--json", |
| 408 | "--output-format", |
| 409 | "stream-json", |
| 410 | "hello", |
| 411 | ]) |
| 412 | .expect_err("json summary and stream-json must not mix"); |
| 413 | |
| 414 | assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict); |
| 415 | } |
| 416 | |
| 417 | #[test] |
| 418 | fn exec_stream_final_answer_excerpt_is_bounded_and_redacted() { |
| 419 | assert_eq!( |
| 420 | exec_stream_final_answer_excerpt(" short reply \n"), |
| 421 | "short reply" |
| 422 | ); |
| 423 | let long = |
| 424 | "x".repeat(codewhale_engine::fleet::executor::EXEC_STREAM_FINAL_ANSWER_EXCERPT_CHARS + 5); |
| 425 | let excerpt = exec_stream_final_answer_excerpt(&long); |
| 426 | assert_eq!( |
| 427 | excerpt.chars().count(), |
| 428 | codewhale_engine::fleet::executor::EXEC_STREAM_FINAL_ANSWER_EXCERPT_CHARS + 3 |
| 429 | ); |
| 430 | assert!(excerpt.ends_with("...")); |
| 431 | let leaked = exec_stream_final_answer_excerpt("token: sk-ant-must-not-leak-1234567890"); |
| 432 | assert!(!leaked.contains("sk-ant-must-not-leak"), "{leaked}"); |
| 433 | } |
| 434 | |
| 435 | #[test] |
| 436 | fn review_receipt_check_public_json_omits_private_details() { |
| 437 | let validation = codewhale_engine::tools::review::ReviewReceiptValidation { |
| 438 | passed: false, |
| 439 | reason: "secret reason with /tmp/private/receipt.json".to_string(), |
| 440 | diff_fingerprint: "sha256:current".to_string(), |
| 441 | receipt_fingerprint: Some("sha256:current".to_string()), |
| 442 | receipt_path: Some(PathBuf::from("/tmp/private/receipt.json")), |
| 443 | unresolved_risk: Some(codewhale_engine::tools::review::ReviewReceiptRisk { |
| 444 | unresolved: true, |
| 445 | level: "error".to_string(), |
| 446 | summary: "secret summary".to_string(), |
| 447 | }), |
| 448 | }; |
| 449 | |
| 450 | let public = review_receipt_validation_public_json(&validation); |
| 451 | let encoded = serde_json::to_string(&public).expect("public json"); |
| 452 | |
| 453 | assert_eq!(public["passed"], false); |
| 454 | assert_eq!(public["status"], "unresolved_risk"); |
| 455 | assert_eq!(public["risk_level"], "error"); |
| 456 | assert!(!encoded.contains("secret")); |
| 457 | assert!(!encoded.contains("/tmp/private")); |
| 458 | } |
| 459 | |
| 460 | #[test] |
| 461 | fn removed_no_alt_screen_flag_is_rejected() { |
| 462 | // Negative test: the retired compatibility flag must not be silently |
| 463 | // accepted and must not reach the alternate-screen decision at all. |
| 464 | let error = Cli::try_parse_from(["codewhale", "--no-alt-screen"]) |
| 465 | .expect_err("--no-alt-screen must no longer parse"); |
| 466 | assert_eq!( |
| 467 | error.kind(), |
| 468 | clap::error::ErrorKind::UnknownArgument, |
| 469 | "retired flag should fail as an unknown argument, not be absorbed" |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | #[test] |
| 474 | #[cfg(not(windows))] |
| 475 | fn mouse_capture_defaults_on_when_alternate_screen_is_active() { |
| 476 | let cli = parse_cli(&["codewhale"]); |
| 477 | let config = Config::default(); |
| 478 | |
| 479 | assert!(should_use_mouse_capture_with( |
| 480 | &cli, &config, true, None, None, None |
| 481 | )); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | #[cfg(windows)] |
| 486 | fn mouse_capture_defaults_off_on_legacy_windows_console() { |
| 487 | // Legacy conhost (no `WT_SESSION` and no `ConEmuPID`) keeps the |
| 488 | // v0.8.x default-off behavior: mouse-mode reporting on legacy console |
| 489 | // can leak SGR escapes into the composer. |
| 490 | let cli = parse_cli(&["codewhale"]); |
| 491 | let config = Config::default(); |
| 492 | |
| 493 | assert!(!should_use_mouse_capture_with( |
| 494 | &cli, &config, true, None, None, None |
| 495 | )); |
| 496 | } |
| 497 | |
| 498 | // #1169: Windows Terminal sets `WT_SESSION` and handles mouse-mode |
| 499 | // reporting cleanly, so default-on there gives users in-app text |
| 500 | // selection (and the side-effect of clamping selection to the |
| 501 | // transcript region instead of the terminal painting across the |
| 502 | // sidebar via native selection). |
| 503 | #[test] |
| 504 | #[cfg(windows)] |
| 505 | fn mouse_capture_defaults_on_in_windows_terminal() { |
| 506 | let cli = parse_cli(&["codewhale"]); |
| 507 | let config = Config::default(); |
| 508 | |
| 509 | assert!(should_use_mouse_capture_with( |
| 510 | &cli, |
| 511 | &config, |
| 512 | true, |
| 513 | None, |
| 514 | Some("{a3a3b3a8-aa00-0000-0000-000000000000}"), |
| 515 | None, |
| 516 | )); |
| 517 | } |
| 518 | |
| 519 | // ConEmu/Cmder sets `ConEmuPID` and handles VT mouse-mode reporting |
| 520 | // cleanly; default mouse capture on there so users get in-app scrolling. |
| 521 | #[test] |
| 522 | #[cfg(windows)] |
| 523 | fn mouse_capture_defaults_on_in_conemu() { |
| 524 | let cli = parse_cli(&["codewhale"]); |
| 525 | let config = Config::default(); |
| 526 | |
| 527 | assert!(should_use_mouse_capture_with( |
| 528 | &cli, |
| 529 | &config, |
| 530 | true, |
| 531 | None, |
| 532 | None, |
| 533 | Some("12345"), |
| 534 | )); |
| 535 | } |
| 536 | |
| 537 | #[test] |
| 538 | fn no_mouse_capture_flag_disables_mouse_capture() { |
| 539 | let cli = parse_cli(&["codewhale", "--no-mouse-capture"]); |
| 540 | let config = Config::default(); |
| 541 | |
| 542 | assert!(!should_use_mouse_capture_with( |
| 543 | &cli, &config, true, None, None, None |
| 544 | )); |
| 545 | } |
| 546 | |
| 547 | #[test] |
| 548 | fn config_can_disable_default_mouse_capture() { |
| 549 | let cli = parse_cli(&["codewhale"]); |
| 550 | let config = { |
| 551 | let __cw_config_init_1_0 = Some(codewhale_runtime::config::TuiConfig { |
| 552 | mouse_capture: Some(false), |
| 553 | selection_copy_markdown: None, |
| 554 | stream_chunk_timeout_secs: None, |
| 555 | max_model_steps: None, |
| 556 | turn_wall_clock_secs: None, |
| 557 | stream_max_content_mb: None, |
| 558 | stream_max_duration_secs: None, |
| 559 | stream_max_resumes: None, |
| 560 | stream_max_transparent_retries: None, |
| 561 | stream_max_errors: None, |
| 562 | stream_open_timeout_secs: None, |
| 563 | connect_timeout_secs: None, |
| 564 | force_http1: None, |
| 565 | status_items: None, |
| 566 | posture_bar: None, |
| 567 | metrics_line: None, |
| 568 | osc8_links: None, |
| 569 | composer_arrows_scroll: None, |
| 570 | notification_condition: None, |
| 571 | }); |
| 572 | let mut __cw_config_init_1_value: Config = Config::default(); |
| 573 | __cw_config_init_1_value.tui = __cw_config_init_1_0; |
| 574 | __cw_config_init_1_value |
| 575 | }; |
| 576 | |
| 577 | assert!(!should_use_mouse_capture_with( |
| 578 | &cli, &config, true, None, None, None |
| 579 | )); |
| 580 | } |
| 581 | |
| 582 | #[test] |
| 583 | fn mouse_capture_flag_enables_mouse_capture() { |
| 584 | let cli = parse_cli(&["codewhale", "--mouse-capture"]); |
| 585 | let config = Config::default(); |
| 586 | |
| 587 | assert!(should_use_mouse_capture_with( |
| 588 | &cli, &config, true, None, None, None |
| 589 | )); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn config_can_enable_mouse_capture() { |
| 594 | let cli = parse_cli(&["codewhale"]); |
| 595 | let config = { |
| 596 | let __cw_config_init_2_0 = Some(codewhale_runtime::config::TuiConfig { |
| 597 | mouse_capture: Some(true), |
| 598 | selection_copy_markdown: None, |
| 599 | stream_chunk_timeout_secs: None, |
| 600 | max_model_steps: None, |
| 601 | turn_wall_clock_secs: None, |
| 602 | stream_max_content_mb: None, |
| 603 | stream_max_duration_secs: None, |
| 604 | stream_max_resumes: None, |
| 605 | stream_max_transparent_retries: None, |
| 606 | stream_max_errors: None, |
| 607 | stream_open_timeout_secs: None, |
| 608 | connect_timeout_secs: None, |
| 609 | force_http1: None, |
| 610 | status_items: None, |
| 611 | posture_bar: None, |
| 612 | metrics_line: None, |
| 613 | osc8_links: None, |
| 614 | composer_arrows_scroll: None, |
| 615 | notification_condition: None, |
| 616 | }); |
| 617 | let mut __cw_config_init_2_value: Config = Config::default(); |
| 618 | __cw_config_init_2_value.tui = __cw_config_init_2_0; |
| 619 | __cw_config_init_2_value |
| 620 | }; |
| 621 | |
| 622 | assert!(should_use_mouse_capture_with( |
| 623 | &cli, &config, true, None, None, None |
| 624 | )); |
| 625 | } |
| 626 | |
| 627 | #[test] |
| 628 | fn mouse_capture_is_off_without_alternate_screen() { |
| 629 | let cli = parse_cli(&["codewhale", "--mouse-capture"]); |
| 630 | let config = Config::default(); |
| 631 | |
| 632 | assert!(!should_use_mouse_capture_with( |
| 633 | &cli, &config, false, None, None, None |
| 634 | )); |
| 635 | } |
| 636 | |
| 637 | // Issue #878 / #898: JetBrains JediTerm advertises mouse support but |
| 638 | // forwards SGR mouse-event escapes as raw input characters, producing |
| 639 | // the "input box auto-fills with garbled characters when I move the |
| 640 | // mouse" failure mode in PyCharm/IDEA terminals. Default the capture |
| 641 | // off when we see TERMINAL_EMULATOR=JetBrains-JediTerm; explicit |
| 642 | // config / --mouse-capture still wins. |
| 643 | |
| 644 | #[test] |
| 645 | fn mouse_capture_defaults_off_in_jetbrains_jediterm() { |
| 646 | let cli = parse_cli(&["codewhale"]); |
| 647 | let config = Config::default(); |
| 648 | |
| 649 | assert!(!should_use_mouse_capture_with( |
| 650 | &cli, |
| 651 | &config, |
| 652 | true, |
| 653 | Some("JetBrains-JediTerm"), |
| 654 | None, |
| 655 | None, |
| 656 | )); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn jetbrains_default_off_is_case_insensitive() { |
| 661 | let cli = parse_cli(&["codewhale"]); |
| 662 | let config = Config::default(); |
| 663 | |
| 664 | // JetBrains has occasionally varied the casing across releases; |
| 665 | // a case-insensitive match keeps the protection in place. |
| 666 | assert!(!should_use_mouse_capture_with( |
| 667 | &cli, |
| 668 | &config, |
| 669 | true, |
| 670 | Some("jetbrains-jediterm"), |
| 671 | None, |
| 672 | None, |
| 673 | )); |
| 674 | } |
| 675 | |
| 676 | #[test] |
| 677 | fn mouse_capture_flag_overrides_jetbrains_default() { |
| 678 | let cli = parse_cli(&["codewhale", "--mouse-capture"]); |
| 679 | let config = Config::default(); |
| 680 | |
| 681 | assert!(should_use_mouse_capture_with( |
| 682 | &cli, |
| 683 | &config, |
| 684 | true, |
| 685 | Some("JetBrains-JediTerm"), |
| 686 | None, |
| 687 | None, |
| 688 | )); |
| 689 | } |
| 690 | |
| 691 | #[test] |
| 692 | fn config_mouse_capture_true_overrides_jetbrains_default() { |
| 693 | let cli = parse_cli(&["codewhale"]); |
| 694 | let config = { |
| 695 | let __cw_config_init_3_0 = Some(codewhale_runtime::config::TuiConfig { |
| 696 | mouse_capture: Some(true), |
| 697 | selection_copy_markdown: None, |
| 698 | stream_chunk_timeout_secs: None, |
| 699 | max_model_steps: None, |
| 700 | turn_wall_clock_secs: None, |
| 701 | stream_max_content_mb: None, |
| 702 | stream_max_duration_secs: None, |
| 703 | stream_max_resumes: None, |
| 704 | stream_max_transparent_retries: None, |
| 705 | stream_max_errors: None, |
| 706 | stream_open_timeout_secs: None, |
| 707 | connect_timeout_secs: None, |
| 708 | force_http1: None, |
| 709 | status_items: None, |
| 710 | posture_bar: None, |
| 711 | metrics_line: None, |
| 712 | osc8_links: None, |
| 713 | composer_arrows_scroll: None, |
| 714 | notification_condition: None, |
| 715 | }); |
| 716 | let mut __cw_config_init_3_value: Config = Config::default(); |
| 717 | __cw_config_init_3_value.tui = __cw_config_init_3_0; |
| 718 | __cw_config_init_3_value |
| 719 | }; |
| 720 | |
| 721 | assert!(should_use_mouse_capture_with( |
| 722 | &cli, |
| 723 | &config, |
| 724 | true, |
| 725 | Some("JetBrains-JediTerm"), |
| 726 | None, |
| 727 | None, |
| 728 | )); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn cli_defaults_doctor_offline_and_keeps_json_incompatible_with_live_flags() { |
| 733 | let cli = |
| 734 | crate::Cli::try_parse_from(["codewhale-tui", "doctor"]).expect("parse default doctor"); |
| 735 | let Some(crate::Commands::Doctor(args)) = cli.command else { |
| 736 | panic!("expected doctor command"); |
| 737 | }; |
| 738 | assert!(!args.check_updates); |
| 739 | assert!(!args.probe_api); |
| 740 | assert!(!args.probe_local); |
| 741 | assert!(!args.probe_mcp); |
| 742 | assert!(!args.probe_search); |
| 743 | |
| 744 | let cli = crate::Cli::try_parse_from(["codewhale-tui", "doctor", "--probe-search"]) |
| 745 | .expect("parse search probe"); |
| 746 | let Some(crate::Commands::Doctor(args)) = cli.command else { |
| 747 | panic!("expected doctor command"); |
| 748 | }; |
| 749 | assert!(args.probe_search); |
| 750 | assert!(!args.probe_api); |
| 751 | assert!(!args.probe_local); |
| 752 | assert!(!args.probe_mcp); |
| 753 | |
| 754 | for output_flag in ["--json", "--context-json"] { |
| 755 | for live_flag in [ |
| 756 | "--check-updates", |
| 757 | "--probe-api", |
| 758 | "--probe-local", |
| 759 | "--probe-mcp", |
| 760 | "--probe-search", |
| 761 | ] { |
| 762 | assert!( |
| 763 | crate::Cli::try_parse_from(["codewhale-tui", "doctor", output_flag, live_flag,]) |
| 764 | .is_err(), |
| 765 | "{output_flag} unexpectedly accepted live flag {live_flag}" |
| 766 | ); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | #[test] |
| 772 | fn default_max_turns_does_not_add_a_hidden_worker_cap() { |
| 773 | use codewhale_config::FleetExecConfig; |
| 774 | use codewhale_engine::fleet::executor::build_worker_exec_command_with_launch_spec; |
| 775 | use codewhale_engine::fleet::worker_runtime::fleet_task_to_worker_spec_with_profiles; |
| 776 | use codewhale_protocol::fleet::{FleetHostSpec, FleetTaskSpec, FleetWorkerSpec}; |
| 777 | |
| 778 | let exec = FleetExecConfig::default(); |
| 779 | let workspace = tempfile::TempDir::new().unwrap(); |
| 780 | let worker = FleetWorkerSpec { |
| 781 | environment: None, |
| 782 | id: "worker-1".to_string(), |
| 783 | name: "Worker 1".to_string(), |
| 784 | host: FleetHostSpec::Local, |
| 785 | trust_level: None, |
| 786 | labels: std::collections::BTreeMap::new(), |
| 787 | capabilities: Vec::new(), |
| 788 | max_concurrent_tasks: Some(1), |
| 789 | }; |
| 790 | for requested_steps in [None, Some(0), Some(13)] { |
| 791 | let mut task = serde_json::json!({ |
| 792 | "id": "t1", |
| 793 | "name": "Smoke", |
| 794 | "objective": "prove it runs", |
| 795 | "instructions": "x", |
| 796 | "worker": { "role": "reviewer", "tool_profile": "read-only" } |
| 797 | }); |
| 798 | if let Some(max_steps) = requested_steps { |
| 799 | task["budget"] = serde_json::json!({ "max_steps": max_steps }); |
| 800 | } |
| 801 | let task: FleetTaskSpec = serde_json::from_value(task).unwrap(); |
| 802 | let spec = fleet_task_to_worker_spec_with_profiles( |
| 803 | "worker-1", |
| 804 | "run-1", |
| 805 | &task, |
| 806 | &worker, |
| 807 | "auto", |
| 808 | workspace.path(), |
| 809 | workspace.path(), |
| 810 | &[], |
| 811 | None, |
| 812 | ) |
| 813 | .unwrap(); |
| 814 | let cmd = |
| 815 | build_worker_exec_command_with_launch_spec("codewhale", &task, &spec, &exec, None, &[]) |
| 816 | .expect("production worker command"); |
| 817 | let cli = Cli::try_parse_from( |
| 818 | std::iter::once("codewhale").chain(cmd.args.iter().map(String::as_str)), |
| 819 | ) |
| 820 | .expect("production worker args must parse"); |
| 821 | let Some(Commands::Exec(args)) = cli.command else { |
| 822 | panic!("expected worker exec command"); |
| 823 | }; |
| 824 | let expected = requested_steps.filter(|steps| *steps > 0); |
| 825 | assert_eq!(args.max_turns, expected); |
| 826 | assert_eq!(args.max_tool_calls, None); |
| 827 | // Follow omission beyond argv through the real CLI resolver. This |
| 828 | // previously installed 200 despite correct unbounded launch args. |
| 829 | let turn = codewhale_engine::core::turn::TurnContext::new(exec_max_steps(args.max_turns)); |
| 830 | assert_eq!(turn.step_limit(), expected); |
| 831 | assert_eq!(turn.stop_diagnostics.effective_max_steps, expected); |
| 832 | } |
| 833 | } |
| 834 |