| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use crate::commands::fleet_host::{fleet_catalog_rejection, fleet_provider_rejection}; |
| 4 | use crate::tui::app::{App, AppAction, TuiOptions}; |
| 5 | use codewhale_lane::control::operations_for_domain; |
| 6 | use codewhale_lane::{ControlDomain, ControlSurface}; |
| 7 | use codewhale_localization::Locale; |
| 8 | use codewhale_runtime::config::Config; |
| 9 | use std::path::PathBuf; |
| 10 | |
| 11 | fn test_app() -> App { |
| 12 | let options = TuiOptions { |
| 13 | ..crate::tui::test_support::test_tui_options(PathBuf::from(".")) |
| 14 | }; |
| 15 | App::new(options, &Config::default()) |
| 16 | } |
| 17 | |
| 18 | fn app_in(workspace: PathBuf) -> App { |
| 19 | let options = TuiOptions { |
| 20 | ..crate::tui::test_support::test_tui_options(workspace.clone()) |
| 21 | }; |
| 22 | let mut app = App::new(options, &Config::default()); |
| 23 | app.workspace = workspace; |
| 24 | app |
| 25 | } |
| 26 | |
| 27 | fn isolated_workspace() -> (tempfile::TempDir, crate::test_support::EnvVarGuard, PathBuf) { |
| 28 | let temp = tempfile::tempdir().expect("tempdir"); |
| 29 | let home = temp.path().join("home"); |
| 30 | let guard = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", home.as_os_str()); |
| 31 | let workspace = temp.path().join("repo"); |
| 32 | std::fs::create_dir_all(&workspace).expect("workspace"); |
| 33 | (temp, guard, workspace) |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn fleet_add_and_remove_parse_into_actions_the_ui_applies_with_the_live_config() { |
| 38 | let mut app = test_app(); |
| 39 | let added = |
| 40 | crate::commands::execute("/fleet add openrouter z-ai/glm-5.3-flash explore", &mut app); |
| 41 | assert_eq!( |
| 42 | added.action, |
| 43 | Some(AppAction::FleetAddModel { |
| 44 | provider: "openrouter".to_string(), |
| 45 | model: "z-ai/glm-5.3-flash".to_string(), |
| 46 | roles: vec!["explore".to_string()], |
| 47 | }) |
| 48 | ); |
| 49 | assert!(!added.is_error && added.message.is_none(), "{added:?}"); |
| 50 | |
| 51 | let removed = |
| 52 | crate::commands::execute("/fleet remove openrouter z-ai/glm-5.3-flash", &mut app); |
| 53 | assert_eq!( |
| 54 | removed.action, |
| 55 | Some(AppAction::FleetRemoveModel { |
| 56 | provider: "openrouter".to_string(), |
| 57 | model: "z-ai/glm-5.3-flash".to_string(), |
| 58 | }) |
| 59 | ); |
| 60 | |
| 61 | for arg in ["add openrouter", "add", "remove openrouter", "remove"] { |
| 62 | let usage = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 63 | assert!(usage.is_error && usage.action.is_none(), "{arg}: {usage:?}"); |
| 64 | assert!( |
| 65 | usage |
| 66 | .message |
| 67 | .as_deref() |
| 68 | .unwrap_or_default() |
| 69 | .contains("Usage: /fleet"), |
| 70 | "{arg}: {usage:?}" |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn fleet_models_lists_the_selected_fleet_and_names_a_broken_selection() { |
| 77 | let _lock = crate::test_support::lock_test_env(); |
| 78 | let (_temp, _home, workspace) = isolated_workspace(); |
| 79 | let mut app = app_in(workspace.clone()); |
| 80 | |
| 81 | let empty = crate::commands::execute("/fleet models", &mut app); |
| 82 | assert!( |
| 83 | empty |
| 84 | .message |
| 85 | .as_deref() |
| 86 | .unwrap_or_default() |
| 87 | .contains("session model only"), |
| 88 | "got: {empty:?}" |
| 89 | ); |
| 90 | |
| 91 | codewhale_engine::fleet::members::add_fleet_model( |
| 92 | &workspace, |
| 93 | "openrouter", |
| 94 | "z-ai/glm-5.3-flash", |
| 95 | &["explore".to_string()], |
| 96 | ) |
| 97 | .expect("add"); |
| 98 | let listed = crate::commands::execute("/fleet models", &mut app) |
| 99 | .message |
| 100 | .unwrap_or_default(); |
| 101 | assert!( |
| 102 | listed.contains("openrouter/z-ai/glm-5.3-flash · explore"), |
| 103 | "got: {listed}" |
| 104 | ); |
| 105 | assert!( |
| 106 | listed.starts_with("Your team `My fleet` (1 models)"), |
| 107 | "got: {listed}" |
| 108 | ); |
| 109 | |
| 110 | let selected = codewhale_engine::fleet::store::resolve_selected_fleet(&workspace) |
| 111 | .expect("ok") |
| 112 | .expect("selected"); |
| 113 | std::fs::remove_file(&selected.path).expect("remove"); |
| 114 | let broken = crate::commands::execute("/fleet models", &mut app) |
| 115 | .message |
| 116 | .unwrap_or_default(); |
| 117 | assert!(broken.contains("could not be loaded"), "got: {broken}"); |
| 118 | assert!(!broken.contains("session model only"), "got: {broken}"); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn fleet_add_rejections_come_from_the_live_config_and_the_catalog() { |
| 123 | let app = test_app(); |
| 124 | let mut live = Config::default(); |
| 125 | assert!( |
| 126 | fleet_provider_rejection(&app, &live, "unknown-provider") |
| 127 | .is_some_and(|reason| reason.contains("not a configured provider")) |
| 128 | ); |
| 129 | assert!(fleet_provider_rejection(&app, &live, "openrouter").is_some()); |
| 130 | live.provider_config_for_mut( |
| 131 | &live.test_identity_for_kind(codewhale_config::ProviderKind::Openrouter), |
| 132 | ) |
| 133 | .unwrap() |
| 134 | .api_key = Some("test-key".to_string()); |
| 135 | assert_eq!(fleet_provider_rejection(&app, &live, "openrouter"), None); |
| 136 | |
| 137 | assert!( |
| 138 | fleet_catalog_rejection(Locale::En, "anthropic", "not-a-real-model") |
| 139 | .is_some_and(|reason| reason.contains("does not serve")) |
| 140 | ); |
| 141 | assert_eq!( |
| 142 | fleet_catalog_rejection(Locale::En, "some-custom-provider", "anything"), |
| 143 | None |
| 144 | ); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn fleet_command_opens_mission_view() { |
| 149 | let mut app = test_app(); |
| 150 | |
| 151 | let result = crate::commands::execute("/fleet", &mut app); |
| 152 | |
| 153 | assert_eq!(result.action, Some(AppAction::OpenFleetMissions)); |
| 154 | assert!(result.message.is_none()); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn fleet_saved_fleet_verbs_open_the_named_fleet_list() { |
| 159 | for arg in ["teams", "team", "fleets", "saved", "manage"] { |
| 160 | let mut app = test_app(); |
| 161 | |
| 162 | let result = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 163 | |
| 164 | assert_eq!(result.action, Some(AppAction::OpenFleetList), "{arg}"); |
| 165 | assert!(result.message.is_none(), "{arg}"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn retired_pod_invocations_are_rejected() { |
| 171 | let mut app = test_app(); |
| 172 | let rejected = crate::commands::execute("/pod", &mut app); |
| 173 | assert!( |
| 174 | rejected.is_error, |
| 175 | "/pod must not dispatch, got: {rejected:?}" |
| 176 | ); |
| 177 | assert!( |
| 178 | rejected |
| 179 | .message |
| 180 | .as_deref() |
| 181 | .unwrap_or_default() |
| 182 | .contains("Unknown command: /pod"), |
| 183 | "got: {rejected:?}" |
| 184 | ); |
| 185 | |
| 186 | let mut app = test_app(); |
| 187 | let retired_verb = crate::commands::execute("/fleet pods", &mut app); |
| 188 | assert!(retired_verb.is_error); |
| 189 | assert!( |
| 190 | retired_verb |
| 191 | .message |
| 192 | .as_deref() |
| 193 | .is_some_and(|message| message.contains("Unknown /fleet target 'pods'")), |
| 194 | "got: {retired_verb:?}" |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn fleet_members_and_roster_aliases_open_roster_view() { |
| 200 | for arg in [ |
| 201 | "members", "member", "roster", "roles", "role", "profiles", "profile", |
| 202 | ] { |
| 203 | let mut app = test_app(); |
| 204 | |
| 205 | let result = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 206 | |
| 207 | assert_eq!(result.action, Some(AppAction::OpenFleetRoster), "{arg}"); |
| 208 | assert!(result.message.is_none(), "{arg}"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn fleet_setup_args_open_setup_wizard() { |
| 214 | for arg in ["setup", "edit", "new"] { |
| 215 | let mut app = test_app(); |
| 216 | |
| 217 | let result = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 218 | |
| 219 | assert_eq!(result.action, Some(AppAction::OpenFleetSetup), "{arg}"); |
| 220 | assert!(result.message.is_none(), "{arg}"); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[test] |
| 225 | fn fleet_workers_arg_opens_the_session_subagent_view() { |
| 226 | for arg in ["workers", "worker", "agents", "subagents"] { |
| 227 | let mut app = test_app(); |
| 228 | |
| 229 | let result = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 230 | |
| 231 | assert_eq!(result.action, Some(AppAction::ListSubAgents), "{arg}"); |
| 232 | assert!(result.message.is_none(), "{arg}"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn fleet_status_reads_the_durable_ledger_not_session_subagents() { |
| 238 | let workspace = tempfile::tempdir().unwrap(); |
| 239 | let mut app = app_in(workspace.path().to_path_buf()); |
| 240 | |
| 241 | let result = crate::commands::execute("/fleet status", &mut app); |
| 242 | |
| 243 | assert_eq!( |
| 244 | result.action, None, |
| 245 | "/fleet status must not open the session sub-agent view" |
| 246 | ); |
| 247 | let message = result.message.as_deref().unwrap_or_default(); |
| 248 | assert!(message.contains("fleet.status"), "got: {message}"); |
| 249 | assert!(message.contains("no_fleet_ledger"), "got: {message}"); |
| 250 | assert!( |
| 251 | !workspace |
| 252 | .path() |
| 253 | .join(".codewhale") |
| 254 | .join("fleet.jsonl") |
| 255 | .exists(), |
| 256 | "a read verb must not create the durable ledger" |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn fleet_control_verbs_route_through_the_shared_contract() { |
| 262 | let workspace = tempfile::tempdir().unwrap(); |
| 263 | for (arg, expected_id) in [ |
| 264 | ("list", "fleet.list"), |
| 265 | ("status", "fleet.status"), |
| 266 | ("interrupt worker-1", "fleet.interrupt"), |
| 267 | ("resume run-1", "fleet.resume"), |
| 268 | ("restart worker-1", "fleet.restart"), |
| 269 | ] { |
| 270 | let mut app = app_in(workspace.path().to_path_buf()); |
| 271 | let result = crate::commands::execute(&format!("/fleet {arg}"), &mut app); |
| 272 | let message = result.message.as_deref().unwrap_or_default(); |
| 273 | assert!( |
| 274 | message.contains(expected_id), |
| 275 | "/fleet {arg} must report {expected_id}, got: {message}" |
| 276 | ); |
| 277 | assert_eq!(result.action, None, "/fleet {arg}"); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | #[test] |
| 282 | fn fleet_help_arg_distinguishes_durable_from_session_state() { |
| 283 | let mut app = test_app(); |
| 284 | |
| 285 | let result = crate::commands::execute("/fleet help", &mut app); |
| 286 | |
| 287 | assert!(!result.is_error); |
| 288 | assert!(result.action.is_none()); |
| 289 | let message = result.message.as_deref().unwrap_or_default(); |
| 290 | for surface in [ |
| 291 | "/fleet members", |
| 292 | "/fleet setup", |
| 293 | "/fleet teams", |
| 294 | "/fleet status", |
| 295 | ] { |
| 296 | assert!(message.contains(surface), "help must describe {surface}"); |
| 297 | } |
| 298 | assert!( |
| 299 | !message.contains("compatibility alias"), |
| 300 | "no retired alias may be documented: {message}" |
| 301 | ); |
| 302 | assert!( |
| 303 | !message.contains("codewhale pod"), |
| 304 | "no retired CLI spelling may be documented: {message}" |
| 305 | ); |
| 306 | for truth in [ |
| 307 | "current TUI session", |
| 308 | "codewhale fleet status", |
| 309 | ".codewhale/fleet.jsonl", |
| 310 | ] { |
| 311 | assert!(message.contains(truth), "help must distinguish {truth}"); |
| 312 | } |
| 313 | for descriptor in operations_for_domain(ControlDomain::Fleet) { |
| 314 | assert!( |
| 315 | message.contains(descriptor.cli_invocation), |
| 316 | "help must name the CLI twin of {}", |
| 317 | descriptor.id |
| 318 | ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn fleet_unknown_arg_reports_error() { |
| 324 | let mut app = test_app(); |
| 325 | |
| 326 | let result = crate::commands::execute("/fleet bogus", &mut app); |
| 327 | |
| 328 | assert!(result.is_error); |
| 329 | assert!(result.action.is_none()); |
| 330 | assert!( |
| 331 | result |
| 332 | .message |
| 333 | .as_deref() |
| 334 | .is_some_and(|message| message.contains("Unknown /fleet target 'bogus'")) |
| 335 | ); |
| 336 | assert!( |
| 337 | result |
| 338 | .message |
| 339 | .as_deref() |
| 340 | .is_some_and(|message| message.contains("Use members, setup, teams")) |
| 341 | ); |
| 342 | } |
| 343 | |
| 344 | #[test] |
| 345 | fn fleet_aliases_are_registered_on_command_info() { |
| 346 | let info = crate::commands::get_command_info("fleet").expect("fleet info"); |
| 347 | assert_eq!(info.name, "fleet"); |
| 348 | assert!(info.aliases.is_empty()); |
| 349 | assert!(info.usage.contains("teams")); |
| 350 | assert!(info.usage.contains("workers")); |
| 351 | for advanced in ["save-as", "interrupt", "resume", "models", "status"] { |
| 352 | assert!( |
| 353 | !info.usage.contains(advanced), |
| 354 | "usage must not advertise {advanced}" |
| 355 | ); |
| 356 | } |
| 357 | assert!(!info.usage.contains("pods")); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn fleet_dispatches_and_retired_pod_does_not() { |
| 362 | let mut app = test_app(); |
| 363 | let result = crate::commands::execute("/fleet", &mut app); |
| 364 | assert_eq!(result.action, Some(AppAction::OpenFleetMissions)); |
| 365 | assert!(!result.is_error); |
| 366 | |
| 367 | assert!(crate::commands::get_command_info("pod").is_none()); |
| 368 | assert!(crate::commands::get_command_info("team").is_none()); |
| 369 | |
| 370 | let workspace = tempfile::tempdir().expect("workspace"); |
| 371 | let mut fleet_app = app_in(workspace.path().to_path_buf()); |
| 372 | let mut retired_app = app_in(workspace.path().to_path_buf()); |
| 373 | let fleet_status = crate::commands::execute("/fleet status", &mut fleet_app); |
| 374 | let retired_status = crate::commands::execute("/pod status", &mut retired_app); |
| 375 | assert!(retired_status.is_error); |
| 376 | assert_ne!(fleet_status.message, retired_status.message); |
| 377 | } |
| 378 | |
| 379 | #[test] |
| 380 | fn slash_command_and_cli_agree_on_fleet_verb_ids() { |
| 381 | let info = crate::commands::get_command_info("fleet").expect("fleet info"); |
| 382 | let mut app = test_app(); |
| 383 | let help = crate::commands::execute("/fleet help", &mut app) |
| 384 | .message |
| 385 | .unwrap_or_default(); |
| 386 | for descriptor in operations_for_domain(ControlDomain::Fleet) { |
| 387 | assert_eq!(descriptor.slash_command, info.name); |
| 388 | assert_eq!(descriptor.hotbar_action_id(), "slash.fleet"); |
| 389 | assert!( |
| 390 | help.contains(&format!("/fleet {}", descriptor.verb)), |
| 391 | "/fleet help must document {} now that usage does not", |
| 392 | descriptor.verb |
| 393 | ); |
| 394 | assert!(descriptor.offers(ControlSurface::Cli)); |
| 395 | } |
| 396 | assert!( |
| 397 | !info.requires_required_argument(), |
| 398 | "/fleet must stay directly runnable from the palette and hotbar" |
| 399 | ); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn fleet_usage_presents_a_prioritized_core_and_help_keeps_the_rest_reachable() { |
| 404 | let info = crate::commands::get_command_info("fleet").expect("fleet info"); |
| 405 | let verbs: Vec<&str> = info |
| 406 | .usage |
| 407 | .trim_start_matches("/fleet [") |
| 408 | .trim_end_matches(']') |
| 409 | .split('|') |
| 410 | .collect(); |
| 411 | assert_eq!( |
| 412 | verbs, |
| 413 | [ |
| 414 | "members", "setup", "teams", "workers", "context", "board", "help" |
| 415 | ], |
| 416 | "the usage line is exactly the primary set" |
| 417 | ); |
| 418 | |
| 419 | let mut app = test_app(); |
| 420 | let help = crate::commands::execute("/fleet help", &mut app) |
| 421 | .message |
| 422 | .unwrap_or_default(); |
| 423 | for advanced in [ |
| 424 | "/fleet models", |
| 425 | "/fleet add", |
| 426 | "/fleet remove", |
| 427 | "/fleet save", |
| 428 | "/fleet save-as", |
| 429 | ] { |
| 430 | assert!(help.contains(advanced), "help must name {advanced}"); |
| 431 | } |
| 432 | for descriptor in operations_for_domain(ControlDomain::Fleet) { |
| 433 | assert!( |
| 434 | help.contains(&format!("/fleet {}", descriptor.verb)), |
| 435 | "help must keep {} reachable", |
| 436 | descriptor.verb |
| 437 | ); |
| 438 | } |
| 439 | assert!(help.contains("/fleet context")); |
| 440 | assert!(help.contains("/fleet board")); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn fleet_context_and_board_commands_operate_locally_without_creating_on_read() { |
| 445 | let workspace = tempfile::tempdir().expect("workspace"); |
| 446 | let mut app = app_in(workspace.path().to_path_buf()); |
| 447 | |
| 448 | let empty_ctx = crate::commands::execute("/fleet context", &mut app); |
| 449 | assert!(!empty_ctx.is_error); |
| 450 | assert!( |
| 451 | empty_ctx |
| 452 | .message |
| 453 | .as_deref() |
| 454 | .unwrap_or_default() |
| 455 | .contains("No shared project context notes") |
| 456 | ); |
| 457 | assert!(!workspace.path().join(".codewhale").join("context").exists()); |
| 458 | |
| 459 | let store = |
| 460 | codewhale_engine::fleet::project_context::ProjectContextStore::new(workspace.path()); |
| 461 | store |
| 462 | .write_note("decisions", "# Decisions\nKeep state local.") |
| 463 | .expect("write note"); |
| 464 | |
| 465 | let listed = crate::commands::execute("/fleet context list", &mut app); |
| 466 | assert!(!listed.is_error); |
| 467 | assert!( |
| 468 | listed |
| 469 | .message |
| 470 | .as_deref() |
| 471 | .unwrap_or_default() |
| 472 | .contains("decisions.md") |
| 473 | ); |
| 474 | |
| 475 | let read = crate::commands::execute("/fleet context read decisions", &mut app); |
| 476 | assert!(!read.is_error); |
| 477 | assert!( |
| 478 | read.message |
| 479 | .as_deref() |
| 480 | .unwrap_or_default() |
| 481 | .contains("Keep state local.") |
| 482 | ); |
| 483 | |
| 484 | let traversal = crate::commands::execute("/fleet context read ../secret", &mut app); |
| 485 | assert!(traversal.is_error); |
| 486 | |
| 487 | let added = crate::commands::execute("/fleet board add Verify local resume", &mut app); |
| 488 | assert!(!added.is_error); |
| 489 | assert!( |
| 490 | added |
| 491 | .message |
| 492 | .as_deref() |
| 493 | .unwrap_or_default() |
| 494 | .contains("Added task-1") |
| 495 | ); |
| 496 | |
| 497 | let done = crate::commands::execute("/fleet board done task-1", &mut app); |
| 498 | assert!(!done.is_error); |
| 499 | assert!( |
| 500 | done.message |
| 501 | .as_deref() |
| 502 | .unwrap_or_default() |
| 503 | .contains("Completed task-1") |
| 504 | ); |
| 505 | |
| 506 | let board = crate::commands::execute("/fleet board list", &mut app); |
| 507 | assert!(!board.is_error); |
| 508 | assert!( |
| 509 | board |
| 510 | .message |
| 511 | .as_deref() |
| 512 | .unwrap_or_default() |
| 513 | .contains("[done] task-1 Verify local resume") |
| 514 | ); |
| 515 | } |
| 516 | } |
| 517 |