mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Refactor shared TUI input routing (#42199)
## What changed - Centralize app-level keymap actions in a shared dispatcher, including raw-output and external-editor chords. - Move read-only transcript input handling into its own module while preserving prompt backtracking and older-history loading. - Add regression coverage for transcript draft isolation, backtrack selection, global chords, and cached history when a thread is unavailable. GitOrigin-RevId: a1465942d1ea6c57c2f54e5e313025aceb9f8510
This commit is contained in:
1 parent
798833fe97
commit
0b509e930e
8 files changed
+327
-145
No files matched your search
@@ -115,6 +115,30 @@ async fn completed_global_chord_reuses_the_existing_action_handler() -> Result<(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn completed_global_chords_toggle_output_and_request_external_editor() -> Result<()> {
|
||||
let (mut app, mut tui, mut app_server) = chord_app().await?;
|
||||
let config = toml::from_str(
|
||||
"[global]\ntoggle_raw_output = [\"ctrl-x r\"]\nopen_external_editor = [\"ctrl-x e\"]",
|
||||
)?;
|
||||
app.keymap = RuntimeKeymap::from_config(&config).expect("valid global chords");
|
||||
app.chat_widget.apply_keymap_update(config, &app.keymap);
|
||||
|
||||
for key in [ctrl('x'), KeyCode::Char('r').into()] {
|
||||
press(&mut app, &mut tui, &mut app_server, key).await?;
|
||||
}
|
||||
assert!(app.chat_widget.raw_output_mode());
|
||||
|
||||
for key in [ctrl('x'), KeyCode::Char('e').into()] {
|
||||
press(&mut app, &mut tui, &mut app_server, key).await?;
|
||||
}
|
||||
assert_eq!(
|
||||
app.chat_widget.external_editor_state(),
|
||||
super::ExternalEditorState::Requested
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn wrong_second_stroke_passes_through_but_escape_is_consumed() -> Result<()> {
|
||||
let (mut app, mut tui, mut app_server) = chord_app().await?;
|
||||
|
||||
@@ -390,7 +390,19 @@ async fn reconnect_daemon_command_center_after_socket_replacement_without_a_conv
|
||||
let history = drain_history(&mut app, &mut tui, &mut session, &mut events).await?;
|
||||
assert!(history.contains("Cached previous conversation"));
|
||||
|
||||
let content = &history[history.find("Cached previous conversation").unwrap()..];
|
||||
app.handle_tui_event(
|
||||
&mut tui,
|
||||
&mut session,
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Char('l'), KeyModifiers::CONTROL)),
|
||||
)
|
||||
.await?;
|
||||
let preserved_history =
|
||||
drain_history(&mut app, &mut tui, &mut session, &mut events).await?;
|
||||
assert_eq!(preserved_history, history);
|
||||
|
||||
let content = &preserved_history[preserved_history
|
||||
.find("Cached previous conversation")
|
||||
.unwrap()..];
|
||||
assert_snapshot!(
|
||||
"reconnected_unavailable_conversation",
|
||||
format!(
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
---
|
||||
source: tui/src/app/tests/transcript_composer.rs
|
||||
expression: text
|
||||
---
|
||||
/ T R A N S C R I P T / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
|
||||
|
||||
› first
|
||||
|
||||
|
||||
|
||||
› second
|
||||
|
||||
───────────────────────────────────────────────────────────────────────────────────────────── 100% ─
|
||||
↑/↓ to scroll pgup/pgdn to page home/end to jump
|
||||
q to quit esc to edit prev
|
||||
@@ -0,0 +1,109 @@
|
||||
//! Regression coverage for transcript viewer input and prompt selection.
|
||||
//!
|
||||
//! The default-off feature must leave the existing viewer and its draft intact.
|
||||
|
||||
use super::*;
|
||||
use crossterm::event::KeyCode;
|
||||
use crossterm::event::KeyEvent;
|
||||
use crossterm::event::KeyModifiers;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
async fn press_key(
|
||||
app: &mut App,
|
||||
tui: &mut crate::tui::Tui,
|
||||
app_server: &mut AppServerSession,
|
||||
code: KeyCode,
|
||||
) -> Result<()> {
|
||||
app.handle_tui_event(
|
||||
tui,
|
||||
app_server,
|
||||
TuiEvent::Key(KeyEvent::new(code, KeyModifiers::NONE)),
|
||||
)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn transcript_flag_off_preserves_viewer_and_backtracking() -> Result<()> {
|
||||
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let keymap_config = toml::from_str("[composer]\nsubmit = [\"ctrl-x enter\"]")?;
|
||||
app.keymap =
|
||||
crate::keymap::RuntimeKeymap::from_config(&keymap_config).expect("valid composer chord");
|
||||
app.chat_widget
|
||||
.apply_keymap_update(keymap_config, &app.keymap);
|
||||
let mut app_server = start_config_write_test_app_server(&app).await?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let session = test_thread_session(ThreadId::new(), app.config.cwd.to_path_buf());
|
||||
app.chat_widget.handle_thread_session(session);
|
||||
app.transcript_cells = ["first", "second"]
|
||||
.map(|message| {
|
||||
Arc::new(UserHistoryCell {
|
||||
message: message.into(),
|
||||
text_elements: Vec::new(),
|
||||
local_image_paths: Vec::new(),
|
||||
remote_image_urls: Vec::new(),
|
||||
}) as Arc<dyn HistoryCell>
|
||||
})
|
||||
.to_vec();
|
||||
app.chat_widget
|
||||
.apply_external_edit("preserved draft".into());
|
||||
app.open_transcript_overlay(&mut tui);
|
||||
for event in [
|
||||
TuiEvent::Paste("not composer input".into()),
|
||||
TuiEvent::Key(KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE)),
|
||||
] {
|
||||
app.handle_tui_event(&mut tui, &mut app_server, event)
|
||||
.await?;
|
||||
}
|
||||
assert_eq!(
|
||||
app.chat_widget.composer_text_with_pending(),
|
||||
"preserved draft"
|
||||
);
|
||||
let chord_prefix = KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL);
|
||||
app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Key(chord_prefix))
|
||||
.await?;
|
||||
assert!(!app.key_chord_matcher.is_pending());
|
||||
assert!(!app.backtrack.overlay_preview_active);
|
||||
let area = Rect::new(
|
||||
/*x*/ 0, /*y*/ 0, /*width*/ 100, /*height*/ 12,
|
||||
);
|
||||
let mut buffer = ratatui::buffer::Buffer::empty(area);
|
||||
let Some(Overlay::Transcript(overlay)) = &mut app.overlay else {
|
||||
panic!("viewer closed")
|
||||
};
|
||||
overlay.render(area, &mut buffer);
|
||||
let text = buffer
|
||||
.content()
|
||||
.chunks(usize::from(area.width))
|
||||
.map(|row| {
|
||||
row.iter()
|
||||
.map(ratatui::buffer::Cell::symbol)
|
||||
.collect::<String>()
|
||||
.trim_end()
|
||||
.to_string()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
insta::assert_snapshot!("transcript_flag_off_viewer", text);
|
||||
for (key, selected) in [
|
||||
(KeyCode::Esc, 1),
|
||||
(KeyCode::Esc, 0),
|
||||
(KeyCode::Right, 1),
|
||||
(KeyCode::Right, 1),
|
||||
] {
|
||||
press_key(&mut app, &mut tui, &mut app_server, key).await?;
|
||||
assert_eq!(app.backtrack.nth_user_message, selected);
|
||||
}
|
||||
press_key(&mut app, &mut tui, &mut app_server, KeyCode::Enter).await?;
|
||||
assert!(app.overlay.is_none());
|
||||
assert!(
|
||||
std::iter::from_fn(|| app_event_rx.try_recv().ok()).any(|event| matches!(
|
||||
event,
|
||||
AppEvent::ForkSessionForPromptEdit {
|
||||
nth_user_message: 1,
|
||||
..
|
||||
}
|
||||
))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user