mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
## What changed Render web URLs in warning details as terminal hyperlinks, preserving the full destination when a URL wraps across lines or the viewer scrolls. ## Testing Add a regression test for hyperlink destinations after wrapping and scrolling, plus a snapshot of a warning containing a wrapped URL. GitOrigin-RevId: 6500e6129a95b2dea8e8f53296ca056e63196a57
220 lines
7.6 KiB
Rust
220 lines
7.6 KiB
Rust
//! Independent warning navigation, full-text copying, and bounded scrolling.
|
|
|
|
use super::*;
|
|
use crate::history_cell::WarningId;
|
|
use crate::render::renderable::Renderable;
|
|
use crossterm::event::KeyCode;
|
|
use crossterm::event::KeyModifiers;
|
|
use pretty_assertions::assert_eq;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::layout::Rect;
|
|
use tokio::sync::mpsc::unbounded_channel;
|
|
|
|
fn entries() -> Vec<WarningEntry> {
|
|
vec![
|
|
WarningEntry {
|
|
id: WarningId::McpServer("example".into()),
|
|
source: "MCP · example".into(),
|
|
details: "MCP example could not connect\nSign in again using codex mcp login example"
|
|
.into(),
|
|
},
|
|
WarningEntry {
|
|
id: WarningId::Message("config".into()),
|
|
source: "Startup".into(),
|
|
details: "Unknown setting `old_option`\nRemove it from config.toml".into(),
|
|
},
|
|
]
|
|
}
|
|
|
|
fn key(view: &mut WarningsView, code: KeyCode) -> bool {
|
|
view.handle_key(KeyEvent::new(code, KeyModifiers::NONE))
|
|
}
|
|
|
|
fn draw(view: &WarningsView, width: u16, height: u16) -> String {
|
|
let area = Rect::new(/*x*/ 0, /*y*/ 0, width, height);
|
|
let mut buffer = Buffer::empty(area);
|
|
view.render(area, &mut buffer);
|
|
buffer
|
|
.content
|
|
.chunks(usize::from(width))
|
|
.map(|row| {
|
|
row.iter()
|
|
.map(|cell| crate::terminal_hyperlinks::strip_osc8(cell.symbol()))
|
|
.collect::<String>()
|
|
.trim_end()
|
|
.to_string()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
#[test]
|
|
fn warning_pages_copy_only_the_current_diagnostic() {
|
|
let (tx, mut rx) = unbounded_channel();
|
|
let mut view = WarningsView::new(
|
|
entries(),
|
|
Arc::default(),
|
|
RuntimeKeymap::from_config(&toml::from_str("[list]\nmove_right = 'x'").unwrap()).unwrap(),
|
|
AppEventSender::new(tx),
|
|
);
|
|
let (code, modifiers) = RuntimeKeymap::defaults().app.copy[0].parts();
|
|
for (navigation, index) in [(KeyCode::Char('x'), 1), (KeyCode::Left, 0)] {
|
|
key(&mut view, navigation);
|
|
let screen = draw(&view, /*width*/ 80, /*height*/ 12);
|
|
assert!(screen.contains(&format!("{} of 2", index + 1)));
|
|
assert!(screen.contains(entries()[index].details.lines().next().unwrap()));
|
|
view.handle_key(KeyEvent::new(code, modifiers));
|
|
assert!(
|
|
matches!(rx.try_recv(), Ok(AppEvent::CopyWarning(text)) if text == entries()[index].details)
|
|
);
|
|
}
|
|
assert!(key(&mut view, KeyCode::Esc));
|
|
}
|
|
|
|
#[test]
|
|
fn warning_pages_wrap_and_scroll_long_diagnostics() {
|
|
let (tx, _) = unbounded_channel();
|
|
let mut data = entries();
|
|
data[0].details = format!(
|
|
"Long diagnostic 日本語\n{}\nEND OF DIAGNOSTIC",
|
|
"Details retained in full, including remediation instructions.\n".repeat(/*n*/ 20)
|
|
);
|
|
let mut view = WarningsView::new(
|
|
data,
|
|
Arc::default(),
|
|
RuntimeKeymap::defaults(),
|
|
AppEventSender::new(tx),
|
|
);
|
|
insta::assert_snapshot!("warnings_narrow", draw(&view, /*width*/ 40, /*height*/ 9));
|
|
key(&mut view, KeyCode::PageDown);
|
|
assert!(!draw(&view, /*width*/ 40, /*height*/ 9).contains("Long diagnostic"));
|
|
key(&mut view, KeyCode::End);
|
|
assert!(draw(&view, /*width*/ 40, /*height*/ 9).contains("END OF DIAGNOSTIC"));
|
|
key(&mut view, KeyCode::Right);
|
|
assert!(draw(&view, /*width*/ 40, /*height*/ 9).contains("Unknown setting"));
|
|
key(&mut view, KeyCode::Left);
|
|
assert!(draw(&view, /*width*/ 40, /*height*/ 9).contains("Long diagnostic"));
|
|
}
|
|
|
|
#[test]
|
|
fn warnings_dismiss_only_drawn_pages_after_navigation_skips_a_frame() {
|
|
let (tx, mut rx) = unbounded_channel();
|
|
let mut data = entries();
|
|
data.push(WarningEntry {
|
|
id: WarningId::Message("later".into()),
|
|
source: "Startup".into(),
|
|
details: "Not yet viewed".into(),
|
|
});
|
|
let mut view = WarningsView::new(
|
|
data,
|
|
Arc::default(),
|
|
RuntimeKeymap::defaults(),
|
|
AppEventSender::new(tx),
|
|
);
|
|
assert!(draw(&view, /*width*/ 80, /*height*/ 12).contains("1 of 3"));
|
|
assert!(!key(&mut view, KeyCode::Right));
|
|
assert!(!key(&mut view, KeyCode::Right));
|
|
assert!(draw(&view, /*width*/ 80, /*height*/ 12).contains("3 of 3"));
|
|
key(&mut view, KeyCode::Left);
|
|
assert!(key(&mut view, KeyCode::F(2)));
|
|
view.close();
|
|
let Ok(AppEvent::UpdateWarnings { dismissed, .. }) = rx.try_recv() else {
|
|
panic!("expected warning decisions");
|
|
};
|
|
assert_eq!(
|
|
dismissed,
|
|
vec![
|
|
entries()[0].clone(),
|
|
WarningEntry {
|
|
id: WarningId::Message("later".into()),
|
|
source: "Startup".into(),
|
|
details: "Not yet viewed".into(),
|
|
},
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn warnings_keep_current_and_next_without_dismissing_unvisited_pages() {
|
|
let (tx, mut rx) = unbounded_channel();
|
|
let mut data = entries();
|
|
data.push(WarningEntry {
|
|
id: WarningId::Message("later".into()),
|
|
source: "Startup".into(),
|
|
details: "Not yet viewed".into(),
|
|
});
|
|
let expected = vec![data[1].clone()];
|
|
// Keep owns k in this viewer even when a list action is remapped to it.
|
|
let keymap = RuntimeKeymap::from_config(
|
|
&toml::from_str("[list]\nmove_up = 'up'\ncancel = 'k'").unwrap(),
|
|
)
|
|
.unwrap();
|
|
let mut view = WarningsView::new(data, Arc::default(), keymap, AppEventSender::new(tx));
|
|
assert!(!key(&mut view, KeyCode::Char('k')));
|
|
let screen = draw(&view, /*width*/ 80, /*height*/ 12);
|
|
assert!(screen.contains("2 of 3"));
|
|
assert!(screen.contains("⌃c dismiss & close"));
|
|
let repeat =
|
|
KeyEvent::new_with_kind(KeyCode::Char('k'), KeyModifiers::NONE, KeyEventKind::Repeat);
|
|
assert!(!view.handle_key(repeat));
|
|
assert!(draw(&view, /*width*/ 80, /*height*/ 12).contains("2 of 3"));
|
|
key(&mut view, KeyCode::Left);
|
|
assert!(key(&mut view, KeyCode::F(2)));
|
|
view.close();
|
|
let Ok(AppEvent::UpdateWarnings {
|
|
dismissed, kept, ..
|
|
}) = rx.try_recv()
|
|
else {
|
|
panic!("expected warning decisions");
|
|
};
|
|
assert_eq!((dismissed, kept), (expected, vec![entries()[0].clone()]));
|
|
}
|
|
|
|
#[test]
|
|
fn warning_links_preserve_destinations_when_wrapped_and_scrolled() {
|
|
let url = "https://github.com/openai/codex/pull/12345?diff=split";
|
|
let (tx, _) = unbounded_channel();
|
|
let mut data = entries();
|
|
data[0].details = format!("Review {url} before continuing.");
|
|
let mut view = WarningsView::new(
|
|
data,
|
|
Arc::default(),
|
|
RuntimeKeymap::defaults(),
|
|
AppEventSender::new(tx),
|
|
);
|
|
let area = Rect::new(0, 0, 32, 9);
|
|
let mut buffer = Buffer::empty(area);
|
|
view.render(area, &mut buffer);
|
|
let linked = buffer
|
|
.content
|
|
.iter()
|
|
.filter(|cell| cell.symbol().contains("\x1b]8;;"))
|
|
.map(|cell| {
|
|
assert!(cell.symbol().starts_with(&format!("\x1b]8;;{url}\x07")));
|
|
crate::terminal_hyperlinks::strip_osc8(cell.symbol())
|
|
})
|
|
.collect::<String>();
|
|
assert_eq!(linked, url);
|
|
insta::assert_snapshot!(
|
|
"warnings_wrapped_url",
|
|
draw(&view, /*width*/ 32, /*height*/ 9)
|
|
);
|
|
key(&mut view, KeyCode::Down);
|
|
let area = Rect::new(0, 0, 32, 5);
|
|
let mut buffer = Buffer::empty(area);
|
|
view.render(area, &mut buffer);
|
|
key(&mut view, KeyCode::Down);
|
|
view.render(area, &mut buffer);
|
|
let linked = buffer
|
|
.content
|
|
.iter()
|
|
.filter(|cell| cell.symbol().contains("\x1b]8;;"))
|
|
.collect::<Vec<_>>();
|
|
assert!(!linked.is_empty());
|
|
assert!(
|
|
linked
|
|
.iter()
|
|
.all(|cell| cell.symbol().starts_with(&format!("\x1b]8;;{url}\x07")))
|
|
);
|
|
}
|