mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Keep the composer editable during TUI startup (#38642)
## Why Configuration and app-server initialization can take time before the main TUI is ready, leaving users unable to begin drafting a prompt. ## What changed - Show a provisional composer while startup work runs and carry its text, cursor position, paste state, and attachments into the initialized chat. - Limit the provisional composer to safe editing and cancellation, and quarantine input around session pickers, approvals, and other actionable startup screens. - Preserve configuration validation before terminal checks and recover terminal state cleanly from startup failures and caught panics. ## Testing - Cover startup editing, multiline and large pastes, keymaps, cancellation, session-picker handoff, approval boundaries, and draft restoration. - Verify non-interactive launches report configuration errors before terminal errors. GitOrigin-RevId: fe04a85cfbbcff21ff87fa81fd17474827858575
This commit is contained in:
1 parent
58d2daba45
commit
a0bed4be21
26 files changed
+4348
-1151
No files matched your search
@@ -1,8 +1,33 @@
|
||||
use super::*;
|
||||
use crate::app_event_sender::AppEventSender;
|
||||
use crate::bottom_pane::BottomPane;
|
||||
use crate::bottom_pane::BottomPaneParams;
|
||||
use crate::tui::FrameRequester;
|
||||
use codex_app_server_protocol::ToolRequestUserInputOption;
|
||||
use codex_app_server_protocol::ToolRequestUserInputQuestion;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::sync::mpsc::UnboundedReceiver;
|
||||
use tokio::sync::mpsc::unbounded_channel;
|
||||
|
||||
fn startup_bottom_pane() -> (BottomPane, UnboundedReceiver<AppEvent>) {
|
||||
let (app_event_tx, app_event_rx) = unbounded_channel();
|
||||
(
|
||||
BottomPane::new(BottomPaneParams {
|
||||
app_event_tx: AppEventSender::new(app_event_tx),
|
||||
frame_requester: FrameRequester::test_dummy(),
|
||||
has_input_focus: true,
|
||||
enhanced_keys_supported: false,
|
||||
placeholder_text: "Ask Codex to do anything".to_string(),
|
||||
disable_paste_burst: true,
|
||||
animations_enabled: true,
|
||||
skills: None,
|
||||
}),
|
||||
app_event_rx,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn startup_waiting_gate_is_only_for_fresh_or_exit_session_selection() {
|
||||
@@ -141,6 +166,639 @@ fn startup_waiting_gate_not_applied_for_resume_or_fork_session_selection() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_startup_requests_block_terminal_input_after_draft_handoff() -> Result<()> {
|
||||
for user_input in [false, true] {
|
||||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
app.startup_protected_input_boundary = true;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
while app_event_rx.try_recv().is_ok() {}
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("draft".to_string(), Vec::new(), Vec::new());
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert!(pending_startup_draft.is_none());
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "draft");
|
||||
assert!(!app.has_queued_startup_protected_request());
|
||||
|
||||
let request = if user_input {
|
||||
request_user_input_request(thread_id, "turn-1", "call-1")
|
||||
} else {
|
||||
exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None)
|
||||
};
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
app.enqueue_primary_thread_request(request).await?;
|
||||
|
||||
assert!(
|
||||
app.active_thread_rx
|
||||
.as_ref()
|
||||
.is_some_and(|receiver| !receiver.is_empty())
|
||||
);
|
||||
assert!(
|
||||
app.has_queued_startup_protected_request(),
|
||||
"a queued protected startup request must be handled before terminal input"
|
||||
);
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "draft");
|
||||
while let Ok(op) = op_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(
|
||||
op,
|
||||
Op::UserTurn { .. } | Op::ExecApproval { .. } | Op::UserInputAnswer { .. }
|
||||
),
|
||||
"terminal input must not submit or answer the queued protected request: {op:?}"
|
||||
);
|
||||
}
|
||||
|
||||
let event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("protected request should be queued on the active thread");
|
||||
app.handle_thread_event_now(event);
|
||||
|
||||
assert!(
|
||||
!app.has_queued_startup_protected_request(),
|
||||
"terminal polling must resume after the protected event is drained"
|
||||
);
|
||||
assert!(app.startup_protected_input_boundary);
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "draft");
|
||||
|
||||
app.startup_protected_input_boundary = false;
|
||||
app.enqueue_primary_thread_request(exec_approval_request(
|
||||
thread_id, "turn-2", "call-2", /*approval_id*/ None,
|
||||
))
|
||||
.await?;
|
||||
assert!(
|
||||
!app.has_queued_startup_protected_request(),
|
||||
"ordinary post-startup protected requests must not block terminal input"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_startup_requests_block_draft_restore_until_drained() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.startup_protected_input_boundary = true;
|
||||
let thread_id = ThreadId::new();
|
||||
let request = exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
app.enqueue_primary_thread_request(request).await?;
|
||||
|
||||
assert!(app.has_queued_startup_protected_request());
|
||||
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
while app_event_rx.try_recv().is_ok() {}
|
||||
assert!(app.has_queued_startup_protected_request());
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("draft".to_string(), Vec::new(), Vec::new());
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
if app_event_rx.is_empty() && !app.has_queued_startup_protected_request() {
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
}
|
||||
|
||||
assert!(pending_startup_draft.is_some());
|
||||
assert!(app.chat_widget.composer_is_empty());
|
||||
|
||||
let event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("protected request should be queued on the active thread");
|
||||
app.handle_thread_event_now(event);
|
||||
|
||||
assert!(!app.has_queued_startup_protected_request());
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert!(pending_startup_draft.is_some());
|
||||
assert!(app.chat_widget.composer_is_empty());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_draft_handoff_keeps_approval_shortcuts_in_recently_active_composer() -> Result<()>
|
||||
{
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.handle_key_event(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
|
||||
assert!(pending_startup_draft.is_none());
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "x");
|
||||
|
||||
let approval_request =
|
||||
exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&approval_request);
|
||||
app.enqueue_primary_thread_request(approval_request).await?;
|
||||
let approval_event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("approval should be queued on the active thread");
|
||||
app.handle_thread_event_now(approval_event);
|
||||
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
app.chat_widget
|
||||
.handle_key_event(KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE));
|
||||
app.chat_widget
|
||||
.handle_key_event(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "xy");
|
||||
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(event, AppEvent::SubmitThreadOp { .. }),
|
||||
"startup typeahead should not approve a newly buffered request: {event:?}"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_draft_delayed_approval_becomes_protected_on_redraw() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
app.startup_protected_input_boundary = true;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("draft".to_string(), Vec::new(), Vec::new());
|
||||
let mut draft = startup_pane.composer_draft_snapshot();
|
||||
draft.last_composer_activity_at = Some(Instant::now() - Duration::from_millis(/*millis*/ 950));
|
||||
let mut pending_startup_draft = Some(draft);
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
|
||||
let approval_request =
|
||||
exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&approval_request);
|
||||
app.enqueue_primary_thread_request(approval_request).await?;
|
||||
let approval_event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("approval should be queued on the active thread");
|
||||
app.handle_thread_event_now(approval_event);
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
assert!(app.startup_pending_protected_request);
|
||||
|
||||
app.handle_tui_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE)),
|
||||
)
|
||||
.await?;
|
||||
assert!(app.startup_protected_input_boundary);
|
||||
assert!(app.startup_pending_protected_request);
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(/*millis*/ 75)).await;
|
||||
let redraw_result = app
|
||||
.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Draw)
|
||||
.await;
|
||||
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
assert!(!tui.terminal.viewport_area.is_empty());
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(event, AppEvent::SubmitThreadOp { .. }),
|
||||
"revealing a delayed startup approval must not submit a decision: {event:?}"
|
||||
);
|
||||
}
|
||||
while let Ok(op) = op_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(op, Op::ExecApproval { .. } | Op::UserInputAnswer { .. }),
|
||||
"revealing a delayed startup approval must not answer it: {op:?}"
|
||||
);
|
||||
}
|
||||
if let Err(error) = redraw_result {
|
||||
tracing::debug!(error = %error, "test terminal cannot quarantine interactive input");
|
||||
} else {
|
||||
assert!(!app.startup_pending_protected_request);
|
||||
}
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remotely_resolved_startup_approvals_release_the_draft_after_the_last_request() -> Result<()>
|
||||
{
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.startup_protected_input_boundary = true;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
let app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
app.chat_widget.handle_paste("x".to_string());
|
||||
|
||||
for (request_id, item_id) in [(1, "call-1"), (2, "call-2")] {
|
||||
let mut request =
|
||||
exec_approval_request(thread_id, "turn-1", item_id, /*approval_id*/ None);
|
||||
let ServerRequest::CommandExecutionRequestApproval {
|
||||
request_id: approval_request_id,
|
||||
..
|
||||
} = &mut request
|
||||
else {
|
||||
unreachable!("exec approval helper should return an exec approval");
|
||||
};
|
||||
*approval_request_id = AppServerRequestId::Integer(request_id);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
app.handle_thread_event_now(ThreadBufferedEvent::Request(Box::new(request)));
|
||||
}
|
||||
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
assert!(app.startup_pending_protected_request);
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("startup draft".to_string(), Vec::new(), Vec::new());
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert!(pending_startup_draft.is_some());
|
||||
|
||||
for (request_id, still_pending) in [(1, true), (2, false)] {
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
codex_app_server_client::AppServerEvent::ServerNotification(Box::new(
|
||||
ServerNotification::ServerRequestResolved(
|
||||
codex_app_server_protocol::ServerRequestResolvedNotification {
|
||||
thread_id: thread_id.to_string(),
|
||||
request_id: AppServerRequestId::Integer(request_id),
|
||||
},
|
||||
),
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(app.startup_pending_protected_request, still_pending);
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert_eq!(pending_startup_draft.is_some(), still_pending);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
app.chat_widget.composer_text_with_pending(),
|
||||
"x\nstartup draft"
|
||||
);
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn later_thread_approval_preserves_input_after_startup_boundary_ends() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
|
||||
assert!(!app.startup_protected_input_boundary);
|
||||
app.chat_widget
|
||||
.apply_external_edit("keep this draft".to_string());
|
||||
let approval_request =
|
||||
exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&approval_request);
|
||||
app.enqueue_primary_thread_request(approval_request).await?;
|
||||
let approval_event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("approval should be queued on the active thread");
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
|
||||
app.handle_active_thread_event(&mut tui, &mut app_server, approval_event)
|
||||
.await?;
|
||||
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
assert_eq!(
|
||||
app.chat_widget.composer_text_with_pending(),
|
||||
"keep this draft"
|
||||
);
|
||||
assert!(tui.terminal.viewport_area.is_empty());
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(event, AppEvent::SubmitThreadOp { .. }),
|
||||
"a later protected request must not consume existing input: {event:?}"
|
||||
);
|
||||
}
|
||||
while let Ok(op) = op_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(op, Op::ExecApproval { .. } | Op::UserInputAnswer { .. }),
|
||||
"a later protected request must not submit an answer: {op:?}"
|
||||
);
|
||||
}
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn auto_declined_mcp_elicitations_do_not_leave_startup_quarantine_armed() {
|
||||
for replay in [false, true] {
|
||||
for elicitation in [
|
||||
McpServerElicitationRequest::Url {
|
||||
meta: None,
|
||||
message: "Review the payment details to continue.".to_string(),
|
||||
url: "http://payments.example/checkout/123".to_string(),
|
||||
elicitation_id: "payment-123".to_string(),
|
||||
},
|
||||
McpServerElicitationRequest::OpenAiForm {
|
||||
meta: None,
|
||||
message: "Choose a report.".to_string(),
|
||||
requested_schema: serde_json::json!({}),
|
||||
},
|
||||
] {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.startup_protected_input_boundary = true;
|
||||
let thread_id = ThreadId::new();
|
||||
let request = ServerRequest::McpServerElicitationRequest {
|
||||
request_id: AppServerRequestId::Integer(10),
|
||||
params: McpServerElicitationRequestParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
turn_id: Some("turn-auth".to_string()),
|
||||
server_name: "payments".to_string(),
|
||||
request: elicitation,
|
||||
},
|
||||
};
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
let event = ThreadBufferedEvent::Request(Box::new(request));
|
||||
if replay {
|
||||
app.handle_thread_event_replay(event);
|
||||
} else {
|
||||
app.handle_thread_event_now(event);
|
||||
}
|
||||
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
assert!(!app.startup_pending_protected_request);
|
||||
assert_matches!(
|
||||
app_event_rx.try_recv(),
|
||||
Ok(AppEvent::SubmitThreadOp {
|
||||
thread_id: op_thread_id,
|
||||
op: Op::ResolveElicitation {
|
||||
server_name,
|
||||
request_id: AppServerRequestId::Integer(10),
|
||||
decision: codex_app_server_protocol::McpServerElicitationAction::Decline,
|
||||
content: None,
|
||||
meta: None,
|
||||
},
|
||||
}) if op_thread_id == thread_id && server_name == "payments"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_draft_handoff_recognizes_late_user_input_as_new_protected_view() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_session(
|
||||
test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
Vec::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.handle_key_event(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE));
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
|
||||
let mut request = request_user_input_request(thread_id, "turn-1", "call-1");
|
||||
let ServerRequest::ToolRequestUserInput { params, .. } = &mut request else {
|
||||
panic!("expected a user input request");
|
||||
};
|
||||
params.questions.push(ToolRequestUserInputQuestion {
|
||||
id: "choice".to_string(),
|
||||
header: "Pick one".to_string(),
|
||||
question: "Choose an option.".to_string(),
|
||||
is_other: false,
|
||||
is_secret: false,
|
||||
options: Some(vec![ToolRequestUserInputOption {
|
||||
label: "First".to_string(),
|
||||
description: "First option".to_string(),
|
||||
}]),
|
||||
});
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
app.enqueue_primary_thread_request(request).await?;
|
||||
let event = app
|
||||
.active_thread_rx
|
||||
.as_mut()
|
||||
.expect("primary thread receiver should be active")
|
||||
.try_recv()
|
||||
.expect("user input request should be queued on the active thread");
|
||||
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
app.handle_thread_event_now(event);
|
||||
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
assert_eq!(app.chat_widget.composer_text_with_pending(), "x");
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(event, AppEvent::CodexOp(Op::UserInputAnswer { .. })),
|
||||
"showing the protected user input request must not submit an answer: {event:?}"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fresh_startup_thread_drains_buffered_approval_before_draft_handoff() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
app.pending_startup_thread_start = true;
|
||||
let thread_id = ThreadId::new();
|
||||
let approval_request =
|
||||
exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
.pending_app_server_requests
|
||||
.note_server_request(&approval_request);
|
||||
app.enqueue_primary_thread_request(approval_request).await?;
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("inspect @src".to_string(), Vec::new(), Vec::new());
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
let mut waiting_for_initial_session_configured =
|
||||
App::should_wait_for_initial_session(&SessionSelection::StartFresh);
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
|
||||
let control = Box::pin(app.handle_event(
|
||||
&mut tui,
|
||||
&mut app_server,
|
||||
AppEvent::StartupThreadStarted {
|
||||
result: Ok(AppServerStartedThread {
|
||||
session: test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
turns: Vec::new(),
|
||||
blocks_direct_input: false,
|
||||
}),
|
||||
},
|
||||
))
|
||||
.await?;
|
||||
|
||||
assert!(matches!(control, AppRunControl::Continue));
|
||||
assert!(
|
||||
app.active_thread_rx
|
||||
.as_ref()
|
||||
.is_some_and(|receiver| !receiver.is_empty())
|
||||
);
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
|
||||
if App::should_stop_waiting_for_initial_session(
|
||||
waiting_for_initial_session_configured,
|
||||
app.primary_thread_id,
|
||||
) {
|
||||
waiting_for_initial_session_configured = false;
|
||||
app.drain_active_thread_events(&mut tui).await?;
|
||||
}
|
||||
|
||||
assert!(!waiting_for_initial_session_configured);
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert_eq!(
|
||||
pending_startup_draft
|
||||
.as_ref()
|
||||
.map(|draft| draft.text.as_str()),
|
||||
Some("inspect @src")
|
||||
);
|
||||
assert!(app.chat_widget.composer_is_empty());
|
||||
while let Ok(op) = op_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(op, Op::ExecApproval { .. } | Op::UserInputAnswer { .. }),
|
||||
"showing a protected startup approval must not answer it: {op:?}"
|
||||
);
|
||||
}
|
||||
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(
|
||||
event,
|
||||
AppEvent::StartFileSearch(_) | AppEvent::SubmitThreadOp { .. }
|
||||
),
|
||||
"fresh startup approval must own input before draft side effects: {event:?}"
|
||||
);
|
||||
}
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn queued_startup_app_event_owns_protected_view_before_draft_restore() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
while app_event_rx.try_recv().is_ok() {}
|
||||
|
||||
let (mut startup_pane, _startup_app_event_rx) = startup_bottom_pane();
|
||||
startup_pane.set_composer_text("inspect @src".to_string(), Vec::new(), Vec::new());
|
||||
let mut pending_startup_draft = Some(startup_pane.composer_draft_snapshot());
|
||||
app.app_event_tx.send(AppEvent::OpenApprovalsPopup);
|
||||
|
||||
if app_event_rx.is_empty() {
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
}
|
||||
assert!(pending_startup_draft.is_some());
|
||||
assert!(app.chat_widget.composer_is_empty());
|
||||
|
||||
let event = app_event_rx
|
||||
.try_recv()
|
||||
.expect("protected startup app event should be queued");
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
assert!(!app.chat_widget.has_active_view());
|
||||
let control = Box::pin(app.handle_event(&mut tui, &mut app_server, event)).await?;
|
||||
|
||||
assert!(matches!(control, AppRunControl::Continue));
|
||||
assert!(app.chat_widget.has_active_view());
|
||||
app.chat_widget
|
||||
.restore_startup_draft_when_ready(&mut pending_startup_draft);
|
||||
assert_eq!(
|
||||
pending_startup_draft
|
||||
.as_ref()
|
||||
.map(|draft| draft.text.as_str()),
|
||||
Some("inspect @src")
|
||||
);
|
||||
assert!(app.chat_widget.composer_is_empty());
|
||||
|
||||
while let Ok(event) = app_event_rx.try_recv() {
|
||||
assert!(
|
||||
!matches!(
|
||||
event,
|
||||
AppEvent::StartFileSearch(_)
|
||||
| AppEvent::UpdateWorldWritableWarningAcknowledged(_)
|
||||
| AppEvent::PersistWorldWritableWarningAcknowledged
|
||||
),
|
||||
"protected startup app event must own input before draft side effects: {event:?}"
|
||||
);
|
||||
}
|
||||
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_thread_started_submits_queued_startup_input() {
|
||||
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
|
||||
|
||||
Reference in new issue
Block a user