| 1 | //! Plan hand-off: the answer to the "plan ready" question is applied by the |
| 2 | //! host — permission first, then the mode — and never touches an engine |
| 3 | //! `request_user_input` call. |
| 4 | |
| 5 | use super::*; |
| 6 | use crate::tui::plan_handoff::{self, PlanHandoffChoice}; |
| 7 | use crate::tui::ui::handlers::{ |
| 8 | apply_plan_handoff, apply_plan_handoff_with_checkpoint, enter_work_for_plan, |
| 9 | is_plan_handoff_request, |
| 10 | }; |
| 11 | |
| 12 | /// A session sitting in Plan whose Work permission is Ask. |
| 13 | fn app_in_plan() -> App { |
| 14 | let mut app = create_test_app(); |
| 15 | app.set_agent_approval_posture(ApprovalMode::Suggest); |
| 16 | let _ = app.set_mode(AppMode::Plan); |
| 17 | assert_eq!(app.mode, AppMode::Plan); |
| 18 | app |
| 19 | } |
| 20 | |
| 21 | #[tokio::test] |
| 22 | async fn work_choice_leaves_plan_with_the_chosen_permission() { |
| 23 | let mut app = app_in_plan(); |
| 24 | let mut engine = mock_engine_handle(); |
| 25 | |
| 26 | assert!(enter_work_for_plan(&mut app, &engine.handle, ApprovalMode::Suggest).await); |
| 27 | |
| 28 | assert_eq!(app.mode, AppMode::Agent); |
| 29 | assert_eq!(app.approval_mode, ApprovalMode::Suggest); |
| 30 | match engine.rx_op.recv().await.expect("change mode op") { |
| 31 | codewhale_engine::core::ops::Op::ChangeMode { |
| 32 | mode, |
| 33 | approval_mode, |
| 34 | .. |
| 35 | } => { |
| 36 | assert_eq!(mode, AppMode::Agent); |
| 37 | assert_eq!(approval_mode, ApprovalMode::Suggest); |
| 38 | } |
| 39 | other => panic!("expected ChangeMode, got {other:?}"), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[tokio::test] |
| 44 | async fn a_locked_permission_keeps_the_session_in_plan() { |
| 45 | let mut app = app_in_plan(); |
| 46 | app.mark_approval_policy_locked(); |
| 47 | let mut engine = mock_engine_handle(); |
| 48 | |
| 49 | // Ask is in force and locked; the person asked for Auto-Review. Starting |
| 50 | // the work under a permission they did not pick is the wrong outcome. |
| 51 | assert!(!enter_work_for_plan(&mut app, &engine.handle, ApprovalMode::Auto).await); |
| 52 | |
| 53 | assert_eq!(app.mode, AppMode::Plan); |
| 54 | assert_eq!(app.agent_approval_baseline(), ApprovalMode::Suggest); |
| 55 | assert!( |
| 56 | engine.rx_op.try_recv().is_err(), |
| 57 | "no mode change may reach the engine" |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | #[tokio::test] |
| 62 | async fn keep_planning_changes_nothing_and_sends_nothing() { |
| 63 | let mut app = app_in_plan(); |
| 64 | let request_id = completed_plan(&mut app, "Inspect first, then make the change.", false); |
| 65 | let history_len = app.history.len(); |
| 66 | let mut engine = mock_engine_handle(); |
| 67 | |
| 68 | apply_plan_handoff( |
| 69 | &mut app, |
| 70 | &Config::default(), |
| 71 | &engine.handle, |
| 72 | &request_id, |
| 73 | PlanHandoffChoice::KeepPlanning, |
| 74 | ) |
| 75 | .await |
| 76 | .expect("keep planning"); |
| 77 | |
| 78 | assert_eq!(app.mode, AppMode::Plan); |
| 79 | assert_eq!(app.history.len(), history_len); |
| 80 | assert!(!app.is_loading); |
| 81 | assert!(app.pending_plan_handoff.is_none()); |
| 82 | assert!(app.todos.lock().await.snapshot().is_empty()); |
| 83 | assert!(engine.rx_op.try_recv().is_err()); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn an_engine_question_with_the_handoff_id_still_goes_to_the_engine() { |
| 88 | let mut app = app_in_plan(); |
| 89 | assert!(is_plan_handoff_request(&app, plan_handoff::REQUEST_ID)); |
| 90 | assert!(!is_plan_handoff_request(&app, "call_1")); |
| 91 | |
| 92 | // Tool-call ids come from the provider. One that collides with the |
| 93 | // hand-off id is still the engine's question while it is pending. |
| 94 | app.pending_user_input_prompt = Some(( |
| 95 | plan_handoff::REQUEST_ID.to_string(), |
| 96 | plan_handoff::request(app.ui_locale), |
| 97 | )); |
| 98 | assert!(!is_plan_handoff_request(&app, plan_handoff::REQUEST_ID)); |
| 99 | } |
| 100 | |
| 101 | /// Complete actual typed output inside the current turn; no tool event is needed. |
| 102 | fn completed_plan(app: &mut App, text: &str, checklist: bool) -> String { |
| 103 | app.current_session_id = Some("plan-session".into()); |
| 104 | app.runtime_turn_id = Some("plan-turn".into()); |
| 105 | app.ocean_turn_history_start = app.history.len(); |
| 106 | if checklist { |
| 107 | push_checklist_receipt(app); |
| 108 | } |
| 109 | let index = app.history.len(); |
| 110 | app.push_history_cell(HistoryCell::Assistant { |
| 111 | content: text.into(), |
| 112 | streaming: false, |
| 113 | }); |
| 114 | app.record_completed_assistant_output(index, text); |
| 115 | let has_open_todos = app |
| 116 | .todos |
| 117 | .try_lock() |
| 118 | .expect("test To-dos available") |
| 119 | .snapshot() |
| 120 | .items |
| 121 | .iter() |
| 122 | .any(|item| !item.status.is_settled()); |
| 123 | app.prepare_plan_handoff( |
| 124 | codewhale_engine::core::events::TurnOutcomeStatus::Completed, |
| 125 | Some("plan-turn"), |
| 126 | has_open_todos, |
| 127 | ) |
| 128 | .expect("completed current-turn plan") |
| 129 | } |
| 130 | |
| 131 | fn push_checklist_receipt(app: &mut App) { |
| 132 | app.push_history_cell(HistoryCell::Tool(ToolCell::Generic(GenericToolCell { |
| 133 | name: "todo_write".into(), |
| 134 | status: ToolStatus::Success, |
| 135 | input_summary: None, |
| 136 | output: None, |
| 137 | prompts: None, |
| 138 | spillover_path: None, |
| 139 | output_summary: None, |
| 140 | is_diff: false, |
| 141 | }))); |
| 142 | } |
| 143 | |
| 144 | // Exercise the real graph publication boundary without owning the singleton actor. |
| 145 | fn accept_checkpoint( |
| 146 | app: &mut App, |
| 147 | ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool, String>> + '_>> { |
| 148 | Box::pin(publish_pending_work_projection(app)) |
| 149 | } |
| 150 | |
| 151 | fn reject_checkpoint( |
| 152 | _: &mut App, |
| 153 | ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool, String>> + '_>> { |
| 154 | Box::pin(async { Err("test checkpoint admission refusal".to_string()) }) |
| 155 | } |
| 156 | |
| 157 | fn reject_checkpoint_after_other_work_changes( |
| 158 | app: &mut App, |
| 159 | ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool, String>> + '_>> { |
| 160 | Box::pin(async move { |
| 161 | let work = app.runtime_services.work.as_ref().unwrap(); |
| 162 | let mut todos = work.current_todos().await?; |
| 163 | todos.items[0].content = "Updated unrelated work".into(); |
| 164 | todos.items[0].status = codewhale_engine::tools::todo::TodoStatus::InProgress; |
| 165 | let id = todos.items.iter().map(|item| item.id).max().unwrap() + 1; |
| 166 | todos.items.push(codewhale_engine::tools::todo::TodoItem { |
| 167 | id, |
| 168 | content: "Added while preparing work".into(), |
| 169 | status: codewhale_engine::tools::todo::TodoStatus::Pending, |
| 170 | }); |
| 171 | work.apply_todo_update("plan-session", "todo_write", &todos) |
| 172 | .await?; |
| 173 | Err("test checkpoint admission refusal".to_string()) |
| 174 | }) |
| 175 | } |
| 176 | |
| 177 | fn reject_checkpoint_after_owned_seed_status_changes( |
| 178 | app: &mut App, |
| 179 | ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<bool, String>> + '_>> { |
| 180 | Box::pin(async move { |
| 181 | let work = app.runtime_services.work.as_ref().unwrap(); |
| 182 | let mut todos = work.current_todos().await?; |
| 183 | todos.items.last_mut().unwrap().status = |
| 184 | codewhale_engine::tools::todo::TodoStatus::InProgress; |
| 185 | work.apply_todo_update("plan-session", "todo_write", &todos) |
| 186 | .await?; |
| 187 | Err("test checkpoint admission refusal".to_string()) |
| 188 | }) |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn completed_plan_receipt_excludes_old_empty_failed_and_interrupted_output() { |
| 193 | let mut app = app_in_plan(); |
| 194 | completed_plan(&mut app, "An older plan", false); |
| 195 | app.pending_plan_handoff = None; |
| 196 | app.ocean_turn_history_start = app.history.len(); |
| 197 | assert!( |
| 198 | app.prepare_plan_handoff( |
| 199 | codewhale_engine::core::events::TurnOutcomeStatus::Completed, |
| 200 | Some("plan-turn"), |
| 201 | false, |
| 202 | ) |
| 203 | .is_none() |
| 204 | ); |
| 205 | for status in [ |
| 206 | codewhale_engine::core::events::TurnOutcomeStatus::Interrupted, |
| 207 | codewhale_engine::core::events::TurnOutcomeStatus::Failed, |
| 208 | ] { |
| 209 | app.ocean_turn_history_start = 0; |
| 210 | assert!( |
| 211 | app.prepare_plan_handoff(status, Some("plan-turn"), false) |
| 212 | .is_none() |
| 213 | ); |
| 214 | } |
| 215 | app.ocean_turn_history_start = app.history.len(); |
| 216 | let index = app.history.len(); |
| 217 | app.push_history_cell(HistoryCell::Assistant { |
| 218 | content: " \n".into(), |
| 219 | streaming: false, |
| 220 | }); |
| 221 | app.record_completed_assistant_output(index, " \n"); |
| 222 | assert!( |
| 223 | app.prepare_plan_handoff( |
| 224 | codewhale_engine::core::events::TurnOutcomeStatus::Completed, |
| 225 | Some("plan-turn"), |
| 226 | false, |
| 227 | ) |
| 228 | .is_none() |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | #[tokio::test] |
| 233 | async fn prose_work_seeds_preserves_other_work_and_queues_exact_text_once() { |
| 234 | use codewhale_engine::tools::todo::TodoStatus; |
| 235 | let mut app = app_in_plan(); |
| 236 | app.offline_mode = true; // ordinary composer queue; no provider admission. |
| 237 | app.todos |
| 238 | .lock() |
| 239 | .await |
| 240 | .add("Unrelated work".into(), TodoStatus::Pending); |
| 241 | let text = format!( |
| 242 | "先检查输入。\n\n{}\nThen implement and verify.\n", |
| 243 | "long plan 🐳 ".repeat(150) |
| 244 | ); |
| 245 | let request_id = completed_plan(&mut app, &text, false); |
| 246 | let mut engine = mock_engine_handle(); |
| 247 | apply_plan_handoff_with_checkpoint( |
| 248 | &mut app, |
| 249 | &Config::default(), |
| 250 | &engine.handle, |
| 251 | &request_id, |
| 252 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 253 | accept_checkpoint, |
| 254 | ) |
| 255 | .await |
| 256 | .expect("admitted Work handoff"); |
| 257 | assert_eq!(app.mode, AppMode::Agent); |
| 258 | let todos = app.todos.lock().await.snapshot(); |
| 259 | assert_eq!(todos.items.len(), 2); |
| 260 | assert_eq!(todos.items[0].content, "Unrelated work"); |
| 261 | assert_eq!(todos.items[1].status, TodoStatus::Pending); |
| 262 | assert_eq!(todos.items[1].content, "先检查输入。"); |
| 263 | assert!(app.pending_plan_handoff.is_none()); |
| 264 | assert_eq!(app.queued_message_count(), 1); |
| 265 | assert_eq!( |
| 266 | app.queued_messages.front().unwrap().display, |
| 267 | format!( |
| 268 | "{}\n\n{text}\n\n{}", |
| 269 | app.tr(MessageId::PlanHandoffProceed), |
| 270 | app.tr(MessageId::PlanHandoffTodoNote) |
| 271 | .replace("{title}", &todos.items[1].content) |
| 272 | ) |
| 273 | ); |
| 274 | assert!(matches!( |
| 275 | engine.rx_op.try_recv(), |
| 276 | Ok(codewhale_engine::core::ops::Op::ChangeMode { .. }) |
| 277 | )); |
| 278 | apply_plan_handoff_with_checkpoint( |
| 279 | &mut app, |
| 280 | &Config::default(), |
| 281 | &engine.handle, |
| 282 | &request_id, |
| 283 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 284 | accept_checkpoint, |
| 285 | ) |
| 286 | .await |
| 287 | .unwrap(); |
| 288 | assert_eq!(app.queued_message_count(), 1); |
| 289 | assert_eq!(app.todos.lock().await.snapshot(), todos); |
| 290 | assert!(engine.rx_op.try_recv().is_err()); |
| 291 | } |
| 292 | |
| 293 | #[tokio::test] |
| 294 | async fn existing_current_turn_checklist_is_not_replaced_or_seeded_again() { |
| 295 | use codewhale_engine::tools::todo::TodoStatus; |
| 296 | let mut app = app_in_plan(); |
| 297 | app.offline_mode = true; |
| 298 | app.todos |
| 299 | .lock() |
| 300 | .await |
| 301 | .add("Existing execution step".into(), TodoStatus::Pending); |
| 302 | let before = app.todos.lock().await.snapshot(); |
| 303 | let request_id = completed_plan(&mut app, "Execute the existing checklist.", true); |
| 304 | let engine = mock_engine_handle(); |
| 305 | apply_plan_handoff_with_checkpoint( |
| 306 | &mut app, |
| 307 | &Config::default(), |
| 308 | &engine.handle, |
| 309 | &request_id, |
| 310 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 311 | accept_checkpoint, |
| 312 | ) |
| 313 | .await |
| 314 | .unwrap(); |
| 315 | assert_eq!(app.todos.lock().await.snapshot(), before); |
| 316 | assert_eq!(app.queued_message_count(), 1); |
| 317 | } |
| 318 | |
| 319 | #[tokio::test] |
| 320 | async fn refused_checkpoint_rolls_back_seed_and_retry_queues_once() { |
| 321 | // Cover surrounding/multiline whitespace and a Unicode title. |
| 322 | for text in [ |
| 323 | " \n\tRead the data.\n\nBuild the report.\r\n ".to_string(), |
| 324 | format!("\n{} \nKeep the full message.\t", "鲸".repeat(1023)), |
| 325 | ] { |
| 326 | let mut app = app_in_plan(); |
| 327 | app.offline_mode = true; |
| 328 | let request_id = completed_plan(&mut app, &text, false); |
| 329 | let mut engine = mock_engine_handle(); |
| 330 | apply_plan_handoff_with_checkpoint( |
| 331 | &mut app, |
| 332 | &Config::default(), |
| 333 | &engine.handle, |
| 334 | &request_id, |
| 335 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 336 | reject_checkpoint, |
| 337 | ) |
| 338 | .await |
| 339 | .unwrap(); |
| 340 | assert_eq!(app.mode, AppMode::Plan); |
| 341 | assert_eq!(app.queued_message_count(), 0); |
| 342 | assert!( |
| 343 | app.todos.lock().await.snapshot().is_empty(), |
| 344 | "unpublished seed must stay unpublished" |
| 345 | ); |
| 346 | assert!( |
| 347 | app.pending_plan_handoff |
| 348 | .as_ref() |
| 349 | .unwrap() |
| 350 | .seeded_todo_id |
| 351 | .is_none() |
| 352 | ); |
| 353 | assert!( |
| 354 | app.runtime_services |
| 355 | .work |
| 356 | .as_ref() |
| 357 | .unwrap() |
| 358 | .current_todos() |
| 359 | .await |
| 360 | .unwrap() |
| 361 | .is_empty() |
| 362 | ); |
| 363 | assert_eq!(app.pending_plan_handoff.as_ref().unwrap().text, text); |
| 364 | while app.view_stack.pop().is_some() {} |
| 365 | while let Ok(op) = engine.rx_op.try_recv() { |
| 366 | assert!( |
| 367 | matches!(op, codewhale_engine::core::ops::Op::ChangeMode { .. }), |
| 368 | "no turn dispatch after refusal" |
| 369 | ); |
| 370 | } |
| 371 | apply_plan_handoff_with_checkpoint( |
| 372 | &mut app, |
| 373 | &Config::default(), |
| 374 | &engine.handle, |
| 375 | &request_id, |
| 376 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 377 | accept_checkpoint, |
| 378 | ) |
| 379 | .await |
| 380 | .unwrap(); |
| 381 | let todos = app.todos.lock().await.snapshot(); |
| 382 | assert_eq!(todos.items.len(), 1); |
| 383 | assert_eq!( |
| 384 | todos.items[0].status, |
| 385 | codewhale_engine::tools::todo::TodoStatus::Pending |
| 386 | ); |
| 387 | assert_eq!( |
| 388 | todos.items[0].content, |
| 389 | text.lines() |
| 390 | .map(str::trim) |
| 391 | .find(|line| !line.is_empty()) |
| 392 | .unwrap() |
| 393 | ); |
| 394 | assert_eq!(app.queued_message_count(), 1); |
| 395 | assert_eq!( |
| 396 | app.queued_messages.front().unwrap().display, |
| 397 | format!( |
| 398 | "{}\n\n{text}\n\n{}", |
| 399 | app.tr(MessageId::PlanHandoffProceed), |
| 400 | app.tr(MessageId::PlanHandoffTodoNote) |
| 401 | .replace("{title}", &todos.items[0].content) |
| 402 | ) |
| 403 | ); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | #[tokio::test] |
| 408 | async fn failed_preparation_can_keep_revise_or_escape_without_publishing_its_seed() { |
| 409 | use codewhale_engine::tools::todo::TodoStatus; |
| 410 | for action in ["keep", "revise", "escape"] { |
| 411 | let mut app = app_in_plan(); |
| 412 | app.offline_mode = true; |
| 413 | app.todos |
| 414 | .lock() |
| 415 | .await |
| 416 | .add("Unrelated work".into(), TodoStatus::Pending); |
| 417 | let plan_text = "Build the report after approval."; |
| 418 | let request_id = completed_plan(&mut app, plan_text, false); |
| 419 | let mut engine = mock_engine_handle(); |
| 420 | apply_plan_handoff_with_checkpoint( |
| 421 | &mut app, |
| 422 | &Config::default(), |
| 423 | &engine.handle, |
| 424 | &request_id, |
| 425 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 426 | reject_checkpoint_after_other_work_changes, |
| 427 | ) |
| 428 | .await |
| 429 | .unwrap(); |
| 430 | assert_eq!(app.mode, AppMode::Plan, "{action}"); |
| 431 | assert_eq!(app.queued_message_count(), 0, "{action}"); |
| 432 | assert!( |
| 433 | app.pending_plan_handoff |
| 434 | .as_ref() |
| 435 | .unwrap() |
| 436 | .seeded_todo_id |
| 437 | .is_none() |
| 438 | ); |
| 439 | let work = app.runtime_services.work.as_ref().unwrap().clone(); |
| 440 | let current = work.current_todos().await.unwrap(); |
| 441 | assert_eq!(current.items.len(), 2, "{action}"); |
| 442 | assert_eq!( |
| 443 | current.items[0].content, "Updated unrelated work", |
| 444 | "{action}" |
| 445 | ); |
| 446 | assert_eq!(current.items[0].status, TodoStatus::InProgress, "{action}"); |
| 447 | assert_eq!( |
| 448 | current.items[1].content, "Added while preparing work", |
| 449 | "{action}" |
| 450 | ); |
| 451 | assert_eq!(current.items[1].status, TodoStatus::Pending, "{action}"); |
| 452 | let snapshot = work.capture(Some("plan-session")).unwrap().unwrap(); |
| 453 | let seed = snapshot |
| 454 | .graph |
| 455 | .nodes |
| 456 | .iter() |
| 457 | .find(|node| node.title == plan_text) |
| 458 | .unwrap(); |
| 459 | assert!( |
| 460 | !snapshot.graph.compat.plan_order.contains(&seed.id), |
| 461 | "{action}" |
| 462 | ); |
| 463 | assert!( |
| 464 | !snapshot |
| 465 | .graph |
| 466 | .compat |
| 467 | .todos |
| 468 | .iter() |
| 469 | .any(|binding| binding.node == seed.id), |
| 470 | "{action}" |
| 471 | ); |
| 472 | // A background checkpoint while the retry question is still open |
| 473 | // must publish only unrelated work, never the abandoned seed. |
| 474 | publish_pending_work_projection(&mut app).await.unwrap(); |
| 475 | assert_eq!(app.todos.lock().await.snapshot(), current, "{action}"); |
| 476 | while let Ok(op) = engine.rx_op.try_recv() { |
| 477 | assert!( |
| 478 | matches!(op, codewhale_engine::core::ops::Op::ChangeMode { .. }), |
| 479 | "{action}" |
| 480 | ); |
| 481 | } |
| 482 | let choice = if action == "escape" { |
| 483 | let events = app.view_stack.handle_key(KeyEvent::from(KeyCode::Esc)); |
| 484 | assert!( |
| 485 | matches!(events.as_slice(), [ViewEvent::UserInputCancelled { tool_id }] |
| 486 | if tool_id == &request_id) |
| 487 | ); |
| 488 | // The host cancellation handler uses this same KeepPlanning path. |
| 489 | PlanHandoffChoice::KeepPlanning |
| 490 | } else { |
| 491 | while app.view_stack.pop().is_some() {} |
| 492 | if action == "revise" { |
| 493 | PlanHandoffChoice::Revise("Add a validation step.".into()) |
| 494 | } else { |
| 495 | PlanHandoffChoice::KeepPlanning |
| 496 | } |
| 497 | }; |
| 498 | apply_plan_handoff_with_checkpoint( |
| 499 | &mut app, |
| 500 | &Config::default(), |
| 501 | &engine.handle, |
| 502 | &request_id, |
| 503 | choice, |
| 504 | accept_checkpoint, |
| 505 | ) |
| 506 | .await |
| 507 | .unwrap(); |
| 508 | assert_eq!(app.mode, AppMode::Plan, "{action}"); |
| 509 | assert!(app.pending_plan_handoff.is_none(), "{action}"); |
| 510 | assert_eq!(work.current_todos().await.unwrap(), current, "{action}"); |
| 511 | assert_eq!(app.todos.lock().await.snapshot(), current, "{action}"); |
| 512 | assert!( |
| 513 | engine.rx_op.try_recv().is_err(), |
| 514 | "no execution dispatch for {action}" |
| 515 | ); |
| 516 | if action == "revise" { |
| 517 | assert_eq!(app.queued_message_count(), 1); |
| 518 | assert_eq!( |
| 519 | app.queued_messages.front().unwrap().display, |
| 520 | "Add a validation step." |
| 521 | ); |
| 522 | } else { |
| 523 | assert_eq!(app.queued_message_count(), 0, "{action}"); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | #[tokio::test] |
| 529 | async fn cleanup_refusal_preserves_changed_work_and_owned_pending_without_dispatch() { |
| 530 | use codewhale_engine::tools::todo::TodoStatus; |
| 531 | let mut app = app_in_plan(); |
| 532 | app.offline_mode = true; |
| 533 | app.todos |
| 534 | .lock() |
| 535 | .await |
| 536 | .add("Unrelated work".into(), TodoStatus::Pending); |
| 537 | let request_id = completed_plan(&mut app, "Build this approved report.", false); |
| 538 | let mut engine = mock_engine_handle(); |
| 539 | apply_plan_handoff_with_checkpoint( |
| 540 | &mut app, |
| 541 | &Config::default(), |
| 542 | &engine.handle, |
| 543 | &request_id, |
| 544 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 545 | reject_checkpoint_after_owned_seed_status_changes, |
| 546 | ) |
| 547 | .await |
| 548 | .unwrap(); |
| 549 | let seed = app.pending_plan_handoff.as_ref().unwrap().seeded_todo_id; |
| 550 | assert!(seed.is_some()); |
| 551 | assert_eq!(app.queued_message_count(), 0); |
| 552 | let work = app.runtime_services.work.as_ref().unwrap().clone(); |
| 553 | let changed = work.current_todos().await.unwrap(); |
| 554 | assert_eq!(changed.items[0].content, "Unrelated work"); |
| 555 | assert_eq!(changed.items[1].content, "Build this approved report."); |
| 556 | assert_eq!(changed.items[1].status, TodoStatus::InProgress); |
| 557 | while app.view_stack.pop().is_some() {} |
| 558 | while let Ok(op) = engine.rx_op.try_recv() { |
| 559 | assert!(matches!( |
| 560 | op, |
| 561 | codewhale_engine::core::ops::Op::ChangeMode { .. } |
| 562 | )); |
| 563 | } |
| 564 | // Equal ID/content with a changed status is not unchanged ownership: |
| 565 | // retrying Work must refuse before checkpoint or turn admission. |
| 566 | apply_plan_handoff_with_checkpoint( |
| 567 | &mut app, |
| 568 | &Config::default(), |
| 569 | &engine.handle, |
| 570 | &request_id, |
| 571 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 572 | accept_checkpoint, |
| 573 | ) |
| 574 | .await |
| 575 | .unwrap(); |
| 576 | assert_eq!( |
| 577 | app.pending_plan_handoff.as_ref().unwrap().seeded_todo_id, |
| 578 | seed |
| 579 | ); |
| 580 | assert_eq!(work.current_todos().await.unwrap(), changed); |
| 581 | assert_eq!(app.queued_message_count(), 0); |
| 582 | while app.view_stack.pop().is_some() {} |
| 583 | while let Ok(op) = engine.rx_op.try_recv() { |
| 584 | assert!( |
| 585 | matches!(op, codewhale_engine::core::ops::Op::ChangeMode { .. }), |
| 586 | "no execution after status change" |
| 587 | ); |
| 588 | } |
| 589 | apply_plan_handoff_with_checkpoint( |
| 590 | &mut app, |
| 591 | &Config::default(), |
| 592 | &engine.handle, |
| 593 | &request_id, |
| 594 | PlanHandoffChoice::KeepPlanning, |
| 595 | accept_checkpoint, |
| 596 | ) |
| 597 | .await |
| 598 | .unwrap(); |
| 599 | assert_eq!( |
| 600 | app.pending_plan_handoff.as_ref().unwrap().seeded_todo_id, |
| 601 | seed |
| 602 | ); |
| 603 | assert!( |
| 604 | !app.view_stack.is_empty(), |
| 605 | "failed cleanup stays reviewable" |
| 606 | ); |
| 607 | assert_eq!(work.current_todos().await.unwrap(), changed); |
| 608 | assert_eq!(app.queued_message_count(), 0); |
| 609 | assert!(engine.rx_op.try_recv().is_err()); |
| 610 | while app.view_stack.pop().is_some() {} |
| 611 | app.current_session_id = Some("another-session".into()); |
| 612 | apply_plan_handoff_with_checkpoint( |
| 613 | &mut app, |
| 614 | &Config::default(), |
| 615 | &engine.handle, |
| 616 | &request_id, |
| 617 | PlanHandoffChoice::KeepPlanning, |
| 618 | accept_checkpoint, |
| 619 | ) |
| 620 | .await |
| 621 | .unwrap(); |
| 622 | assert_eq!( |
| 623 | work.current_todos().await.unwrap(), |
| 624 | changed, |
| 625 | "stale cleanup must not touch another session" |
| 626 | ); |
| 627 | assert_eq!( |
| 628 | app.pending_plan_handoff.as_ref().unwrap().seeded_todo_id, |
| 629 | seed |
| 630 | ); |
| 631 | assert_eq!(app.queued_message_count(), 0); |
| 632 | } |
| 633 | |
| 634 | #[tokio::test] |
| 635 | async fn old_checklist_evidence_does_not_suppress_current_prose_seed() { |
| 636 | use codewhale_engine::tools::todo::TodoStatus; |
| 637 | let mut app = app_in_plan(); |
| 638 | app.offline_mode = true; |
| 639 | push_checklist_receipt(&mut app); |
| 640 | app.tool_evidence.push(crate::tui::app::ToolEvidence { |
| 641 | tool_name: "todo_write".into(), |
| 642 | summary: "Earlier turn's checklist".into(), |
| 643 | }); |
| 644 | app.todos |
| 645 | .lock() |
| 646 | .await |
| 647 | .add("Earlier unrelated work".into(), TodoStatus::Pending); |
| 648 | let request_id = completed_plan(&mut app, "Build this newly approved report.", false); |
| 649 | assert!( |
| 650 | !app.pending_plan_handoff |
| 651 | .as_ref() |
| 652 | .unwrap() |
| 653 | .has_current_checklist |
| 654 | ); |
| 655 | let engine = mock_engine_handle(); |
| 656 | apply_plan_handoff_with_checkpoint( |
| 657 | &mut app, |
| 658 | &Config::default(), |
| 659 | &engine.handle, |
| 660 | &request_id, |
| 661 | PlanHandoffChoice::Work(ApprovalMode::Suggest), |
| 662 | accept_checkpoint, |
| 663 | ) |
| 664 | .await |
| 665 | .unwrap(); |
| 666 | let todos = app.todos.lock().await.snapshot(); |
| 667 | assert_eq!(todos.items.len(), 2); |
| 668 | assert_eq!(todos.items[0].content, "Earlier unrelated work"); |
| 669 | assert_eq!(todos.items[1].content, "Build this newly approved report."); |
| 670 | assert_eq!(app.queued_message_count(), 1); |
| 671 | } |
| 672 | |
| 673 | #[tokio::test] |
| 674 | async fn stale_cross_session_busy_and_permission_rejected_answers_seed_nothing() { |
| 675 | for kind in [ |
| 676 | "stale-id", |
| 677 | "session", |
| 678 | "turn", |
| 679 | "epoch", |
| 680 | "busy", |
| 681 | "draft", |
| 682 | "permission", |
| 683 | "engine", |
| 684 | ] { |
| 685 | let mut app = app_in_plan(); |
| 686 | let mut request_id = completed_plan(&mut app, "Keep this exact approved plan.", false); |
| 687 | let mut engine = mock_engine_handle(); |
| 688 | let posture = if kind == "permission" { |
| 689 | ApprovalMode::Auto |
| 690 | } else { |
| 691 | ApprovalMode::Suggest |
| 692 | }; |
| 693 | match kind { |
| 694 | "stale-id" => request_id.push_str("-older"), |
| 695 | "session" => app.current_session_id = Some("another-session".into()), |
| 696 | "turn" => app.runtime_turn_id = Some("another-turn".into()), |
| 697 | "epoch" => app.transcript_identity_epoch += 1, |
| 698 | "busy" => app.is_loading = true, |
| 699 | "draft" => app.input = "unsent draft".into(), |
| 700 | "permission" => app.mark_approval_policy_locked(), |
| 701 | "engine" => engine.rx_op.close(), |
| 702 | _ => unreachable!(), |
| 703 | } |
| 704 | apply_plan_handoff_with_checkpoint( |
| 705 | &mut app, |
| 706 | &Config::default(), |
| 707 | &engine.handle, |
| 708 | &request_id, |
| 709 | PlanHandoffChoice::Work(posture), |
| 710 | accept_checkpoint, |
| 711 | ) |
| 712 | .await |
| 713 | .unwrap(); |
| 714 | assert_eq!(app.mode, AppMode::Plan, "{kind}"); |
| 715 | assert_eq!(app.queued_message_count(), 0, "{kind}"); |
| 716 | assert!(app.todos.lock().await.snapshot().is_empty(), "{kind}"); |
| 717 | assert!( |
| 718 | !app.runtime_services |
| 719 | .work |
| 720 | .as_ref() |
| 721 | .unwrap() |
| 722 | .has_pending_publish(), |
| 723 | "{kind}" |
| 724 | ); |
| 725 | assert!(engine.rx_op.try_recv().is_err(), "{kind}"); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | #[tokio::test] |
| 730 | async fn revise_keeps_plan_without_execution_seed() { |
| 731 | let mut app = app_in_plan(); |
| 732 | app.offline_mode = true; |
| 733 | let request_id = completed_plan(&mut app, "Original plan", false); |
| 734 | let engine = mock_engine_handle(); |
| 735 | apply_plan_handoff_with_checkpoint( |
| 736 | &mut app, |
| 737 | &Config::default(), |
| 738 | &engine.handle, |
| 739 | &request_id, |
| 740 | PlanHandoffChoice::Revise("先补充测试".into()), |
| 741 | accept_checkpoint, |
| 742 | ) |
| 743 | .await |
| 744 | .unwrap(); |
| 745 | assert_eq!(app.mode, AppMode::Plan); |
| 746 | assert!(app.todos.lock().await.snapshot().is_empty()); |
| 747 | assert!(app.pending_plan_handoff.is_none()); |
| 748 | assert_eq!(app.queued_messages.front().unwrap().display, "先补充测试"); |
| 749 | } |
| 750 | |
| 751 | #[tokio::test] |
| 752 | async fn work_auto_records_approved_plan_while_revise_records_person() { |
| 753 | use codewhale_engine::core::ops::{Op, TurnInputAuthorship, TurnSpec}; |
| 754 | |
| 755 | let mut work_app = app_in_plan(); |
| 756 | work_app.offline_mode = true; |
| 757 | let work_req = completed_plan(&mut work_app, "1. Edit src/lib.rs\n2. Run tests", false); |
| 758 | let mut work_engine = mock_engine_handle(); |
| 759 | apply_plan_handoff_with_checkpoint( |
| 760 | &mut work_app, |
| 761 | &Config::default(), |
| 762 | &work_engine.handle, |
| 763 | &work_req, |
| 764 | PlanHandoffChoice::Work(ApprovalMode::Auto), |
| 765 | accept_checkpoint, |
| 766 | ) |
| 767 | .await |
| 768 | .unwrap(); |
| 769 | let queued_work = work_app.queued_messages.pop_front().expect("queued work"); |
| 770 | assert_eq!( |
| 771 | queued_work.approved_plan.as_deref(), |
| 772 | Some("1. Edit src/lib.rs\n2. Run tests") |
| 773 | ); |
| 774 | assert!(!queued_work.owner_input); |
| 775 | let prepared_work = prepare_user_dispatch(&mut work_app, &Config::default(), queued_work) |
| 776 | .expect("prepare work"); |
| 777 | assert!(matches!( |
| 778 | prepared_work.input_authorship, |
| 779 | TurnInputAuthorship::ApprovedPlan(ref p) if p == "1. Edit src/lib.rs\n2. Run tests" |
| 780 | )); |
| 781 | |
| 782 | let mut work_dispatched = app_in_plan(); |
| 783 | work_dispatched.offline_mode = true; |
| 784 | let work_req_2 = completed_plan(&mut work_dispatched, "Plan text only", false); |
| 785 | apply_plan_handoff_with_checkpoint( |
| 786 | &mut work_dispatched, |
| 787 | &Config::default(), |
| 788 | &work_engine.handle, |
| 789 | &work_req_2, |
| 790 | PlanHandoffChoice::Work(ApprovalMode::Auto), |
| 791 | accept_checkpoint, |
| 792 | ) |
| 793 | .await |
| 794 | .unwrap(); |
| 795 | let msg_work = work_dispatched.queued_messages.pop_front().expect("queued"); |
| 796 | dispatch_user_message( |
| 797 | &mut work_dispatched, |
| 798 | &Config::default(), |
| 799 | &work_engine.handle, |
| 800 | msg_work, |
| 801 | ) |
| 802 | .await |
| 803 | .expect("dispatch work"); |
| 804 | let mut saw_plan = false; |
| 805 | for _ in 0..3 { |
| 806 | if let Some(op) = work_engine.rx_op.recv().await |
| 807 | && let Op::SendMessage(TurnSpec { |
| 808 | input_authorship, .. |
| 809 | }) = op |
| 810 | { |
| 811 | assert!(matches!( |
| 812 | input_authorship, |
| 813 | TurnInputAuthorship::ApprovedPlan(ref p) if p == "Plan text only" |
| 814 | )); |
| 815 | saw_plan = true; |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | assert!(saw_plan); |
| 820 | |
| 821 | let mut revise_app = app_in_plan(); |
| 822 | revise_app.offline_mode = true; |
| 823 | let revise_req = completed_plan(&mut revise_app, "Original plan", false); |
| 824 | let revise_engine = mock_engine_handle(); |
| 825 | apply_plan_handoff_with_checkpoint( |
| 826 | &mut revise_app, |
| 827 | &Config::default(), |
| 828 | &revise_engine.handle, |
| 829 | &revise_req, |
| 830 | PlanHandoffChoice::Revise("cover edge cases".into()), |
| 831 | accept_checkpoint, |
| 832 | ) |
| 833 | .await |
| 834 | .unwrap(); |
| 835 | let queued_revise = revise_app |
| 836 | .queued_messages |
| 837 | .pop_front() |
| 838 | .expect("queued revise"); |
| 839 | assert_eq!(queued_revise.approved_plan, None); |
| 840 | assert!(queued_revise.owner_input); |
| 841 | let prepared_revise = prepare_user_dispatch(&mut revise_app, &Config::default(), queued_revise) |
| 842 | .expect("prepare revise"); |
| 843 | assert_eq!( |
| 844 | prepared_revise.input_authorship, |
| 845 | TurnInputAuthorship::Person("cover edge cases".to_string()) |
| 846 | ); |
| 847 | } |
| 848 | |
| 849 | #[test] |
| 850 | fn plan_artifact_persists_atomically_and_rejects_traversal_session_ids() { |
| 851 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 852 | use crate::tui::plan_artifact::{ |
| 853 | PlanNotes, load_plan_markdown, load_plan_notes, parse_plan_steps, plan_markdown_path, |
| 854 | save_plan_markdown, save_plan_notes, validate_session_id, |
| 855 | }; |
| 856 | |
| 857 | let _lock = lock_test_env(); |
| 858 | let home = tempfile::tempdir().unwrap(); |
| 859 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 860 | |
| 861 | for bad in ["", " ", "../escape", "a/b", "a\\b", ".", ".."] { |
| 862 | assert!(validate_session_id(bad).is_err(), "{bad:?}"); |
| 863 | assert!(save_plan_markdown(bad, "# Plan").is_err(), "{bad:?}"); |
| 864 | } |
| 865 | |
| 866 | let md = "# Migration Plan\n\n1. Inspect schema\n2. Apply patch\n- [ ] Run tests\n"; |
| 867 | let saved_path = save_plan_markdown("sess-atomic-1", md).expect("save plan.md"); |
| 868 | assert_eq!( |
| 869 | saved_path, |
| 870 | home.path() |
| 871 | .join("sessions") |
| 872 | .join("sess-atomic-1") |
| 873 | .join("plan.md") |
| 874 | ); |
| 875 | assert_eq!(plan_markdown_path("sess-atomic-1").unwrap(), saved_path); |
| 876 | assert_eq!( |
| 877 | load_plan_markdown("sess-atomic-1").unwrap().as_deref(), |
| 878 | Some(md) |
| 879 | ); |
| 880 | |
| 881 | let steps = parse_plan_steps(md); |
| 882 | assert_eq!(steps.len(), 3); |
| 883 | assert_eq!(steps[0].title, "Inspect schema"); |
| 884 | assert_eq!(steps[1].title, "Apply patch"); |
| 885 | assert_eq!(steps[2].title, "Run tests"); |
| 886 | assert_eq!(steps[2].checked, Some(false)); |
| 887 | |
| 888 | let mut notes = PlanNotes::default(); |
| 889 | notes.add(1, "Keep backward compatibility".to_string()); |
| 890 | let notes_path = save_plan_notes("sess-atomic-1", ¬es).expect("save notes"); |
| 891 | assert!(notes_path.exists()); |
| 892 | assert_eq!(load_plan_notes("sess-atomic-1").unwrap(), notes); |
| 893 | } |
| 894 | |
| 895 | #[tokio::test] |
| 896 | async fn plan_artifact_auto_saves_and_reloads_cleanly_after_resume_or_undo() { |
| 897 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 898 | use crate::tui::plan_artifact::{PlanArtifactView, load_plan_markdown, open_from_app}; |
| 899 | |
| 900 | let _lock = lock_test_env(); |
| 901 | let home = tempfile::tempdir().unwrap(); |
| 902 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 903 | |
| 904 | let mut app = app_in_plan(); |
| 905 | let text = "1. Audit module\n2. Refactor handler\n3. Verify suite"; |
| 906 | let request_id = completed_plan(&mut app, text, false); |
| 907 | let engine = mock_engine_handle(); |
| 908 | |
| 909 | apply_plan_handoff_with_checkpoint( |
| 910 | &mut app, |
| 911 | &Config::default(), |
| 912 | &engine.handle, |
| 913 | &request_id, |
| 914 | PlanHandoffChoice::KeepPlanning, |
| 915 | accept_checkpoint, |
| 916 | ) |
| 917 | .await |
| 918 | .unwrap(); |
| 919 | |
| 920 | assert_eq!( |
| 921 | load_plan_markdown("plan-session").unwrap().as_deref(), |
| 922 | Some(text) |
| 923 | ); |
| 924 | |
| 925 | app.truncate_history_to(0); |
| 926 | app.pending_plan_handoff = None; |
| 927 | app.current_session_id = Some("plan-session".into()); |
| 928 | |
| 929 | open_from_app(&mut app, Some("view")); |
| 930 | assert_eq!(app.view_stack.top_kind(), Some(ModalKind::PlanArtifact)); |
| 931 | let mut boxed = app.view_stack.pop().expect("plan artifact view"); |
| 932 | let view = boxed |
| 933 | .as_any_mut() |
| 934 | .downcast_mut::<PlanArtifactView>() |
| 935 | .expect("downcast"); |
| 936 | assert_eq!(view.markdown, text); |
| 937 | assert_eq!(view.steps.len(), 3); |
| 938 | assert!(view.saved_on_disk); |
| 939 | } |
| 940 | |
| 941 | #[tokio::test] |
| 942 | async fn plan_artifact_view_annotations_paste_editor_and_never_auto_skip_review() { |
| 943 | use crate::test_support::{EnvVarGuard, lock_test_env}; |
| 944 | use crate::tui::plan_artifact::{ |
| 945 | PANEL_COMMAND_PREFIX, PlanArtifactView, PlanViewMode, compose_execution_plan, |
| 946 | load_plan_notes, open_from_app, save_plan_markdown, should_never_auto_skip_plan_review, |
| 947 | }; |
| 948 | use crate::tui::ui::handlers::build_saved_plan_in_work_mode; |
| 949 | use crate::tui::views::{ModalView, ViewAction, ViewEvent}; |
| 950 | |
| 951 | let _lock = lock_test_env(); |
| 952 | let home = tempfile::tempdir().unwrap(); |
| 953 | let _home = EnvVarGuard::set("CODEWHALE_HOME", home.path()); |
| 954 | |
| 955 | let md = "1. Prepare fixture\n2. Run verifier\n"; |
| 956 | save_plan_markdown("sess-review-1", md).unwrap(); |
| 957 | |
| 958 | for posture in [ApprovalMode::Auto, ApprovalMode::Bypass] { |
| 959 | assert!(should_never_auto_skip_plan_review(AppMode::Plan, posture)); |
| 960 | let mut app = create_test_app(); |
| 961 | app.set_agent_approval_posture(posture); |
| 962 | let _ = app.set_mode(AppMode::Plan); |
| 963 | app.current_session_id = Some("sess-review-1".into()); |
| 964 | open_from_app(&mut app, Some("build")); |
| 965 | assert_eq!(app.view_stack.top_kind(), Some(ModalKind::PlanArtifact)); |
| 966 | assert_eq!(app.mode, AppMode::Plan); |
| 967 | assert_eq!(app.queued_message_count(), 0); |
| 968 | let mut boxed = app.view_stack.pop().unwrap(); |
| 969 | let view = boxed |
| 970 | .as_any_mut() |
| 971 | .downcast_mut::<PlanArtifactView>() |
| 972 | .unwrap(); |
| 973 | assert!(view.build_confirm_open); |
| 974 | } |
| 975 | |
| 976 | let mut app = app_in_plan(); |
| 977 | app.offline_mode = true; |
| 978 | app.current_session_id = Some("sess-review-1".into()); |
| 979 | open_from_app(&mut app, Some("view")); |
| 980 | let mut boxed = app.view_stack.pop().unwrap(); |
| 981 | let view = boxed |
| 982 | .as_any_mut() |
| 983 | .downcast_mut::<PlanArtifactView>() |
| 984 | .unwrap(); |
| 985 | |
| 986 | assert_eq!(view.view_mode, PlanViewMode::Steps); |
| 987 | assert!(matches!( |
| 988 | view.handle_key(KeyEvent::from(KeyCode::Tab)), |
| 989 | ViewAction::Redraw |
| 990 | )); |
| 991 | assert_eq!(view.view_mode, PlanViewMode::Raw); |
| 992 | assert!(matches!( |
| 993 | view.handle_key(KeyEvent::from(KeyCode::Char('m'))), |
| 994 | ViewAction::Redraw |
| 995 | )); |
| 996 | assert_eq!(view.view_mode, PlanViewMode::Steps); |
| 997 | |
| 998 | assert!(!view.handle_paste("ignored when not annotating")); |
| 999 | assert!(matches!( |
| 1000 | view.handle_key(KeyEvent::from(KeyCode::Char('a'))), |
| 1001 | ViewAction::Redraw |
| 1002 | )); |
| 1003 | assert!(view.handle_paste("Check edge\ncases")); |
| 1004 | assert!(matches!( |
| 1005 | view.handle_key(KeyEvent::from(KeyCode::Enter)), |
| 1006 | ViewAction::Redraw |
| 1007 | )); |
| 1008 | let persisted_notes = load_plan_notes("sess-review-1").unwrap(); |
| 1009 | assert_eq!( |
| 1010 | persisted_notes.for_step(0), |
| 1011 | &["Check edge cases".to_string()] |
| 1012 | ); |
| 1013 | |
| 1014 | assert!(matches!( |
| 1015 | view.handle_key(KeyEvent::from(KeyCode::Char('s'))), |
| 1016 | ViewAction::Redraw |
| 1017 | )); |
| 1018 | |
| 1019 | let edit_action = view.handle_key(KeyEvent::from(KeyCode::Char('e'))); |
| 1020 | assert!(matches!( |
| 1021 | edit_action, |
| 1022 | ViewAction::Emit(ViewEvent::ExecutePanelCommand { ref command, .. }) |
| 1023 | if command.starts_with(PANEL_COMMAND_PREFIX) && command.ends_with(":edit") |
| 1024 | )); |
| 1025 | |
| 1026 | save_plan_markdown( |
| 1027 | "sess-review-1", |
| 1028 | "1. Prepare fixture\n2. Run verifier\n3. Ship\n", |
| 1029 | ) |
| 1030 | .unwrap(); |
| 1031 | view.reload_from_disk(None); |
| 1032 | assert_eq!(view.steps.len(), 3); |
| 1033 | |
| 1034 | assert!(matches!( |
| 1035 | view.handle_key(KeyEvent::from(KeyCode::Char('b'))), |
| 1036 | ViewAction::Redraw |
| 1037 | )); |
| 1038 | assert!(view.build_confirm_open); |
| 1039 | assert!(matches!( |
| 1040 | view.handle_key(KeyEvent::from(KeyCode::Char('2'))), |
| 1041 | ViewAction::Redraw |
| 1042 | )); |
| 1043 | let build_action = view.handle_key(KeyEvent::from(KeyCode::Enter)); |
| 1044 | assert!(matches!( |
| 1045 | build_action, |
| 1046 | ViewAction::EmitAndClose(ViewEvent::ExecutePanelCommand { ref command, .. }) |
| 1047 | if command.ends_with(":build:accept-edits") |
| 1048 | )); |
| 1049 | |
| 1050 | let combined = compose_execution_plan(&view.markdown, &view.steps, &view.notes); |
| 1051 | assert!(combined.contains("## Review Notes")); |
| 1052 | assert!(combined.contains("Check edge cases")); |
| 1053 | |
| 1054 | let mut engine = mock_engine_handle(); |
| 1055 | build_saved_plan_in_work_mode( |
| 1056 | &mut app, |
| 1057 | &Config::default(), |
| 1058 | &engine.handle, |
| 1059 | ApprovalMode::AcceptEdits, |
| 1060 | combined.clone(), |
| 1061 | ) |
| 1062 | .await |
| 1063 | .unwrap(); |
| 1064 | assert_eq!(app.mode, AppMode::Agent); |
| 1065 | assert_eq!(app.approval_mode, ApprovalMode::AcceptEdits); |
| 1066 | assert_eq!(app.queued_message_count(), 1); |
| 1067 | let queued = app.queued_messages.pop_front().unwrap(); |
| 1068 | assert_eq!(queued.approved_plan.as_deref(), Some(combined.as_str())); |
| 1069 | assert!(matches!( |
| 1070 | engine.rx_op.try_recv(), |
| 1071 | Ok(codewhale_engine::core::ops::Op::ChangeMode { |
| 1072 | mode: AppMode::Agent, |
| 1073 | approval_mode: ApprovalMode::AcceptEdits, |
| 1074 | .. |
| 1075 | }) |
| 1076 | )); |
| 1077 | } |
| 1078 |