Files
codex/codex-rs/tui/src/terminal_hyperlinks_tests.rs
T
Benjamin Carlsson 22ebb0fb17 Clamp wrapped source ranges before remapping terminal hyperlinks (#52330)
## Why

Wrapping can retain a cursor sentinel one byte past the end of the input. Using that range to slice source text during hyperlink remapping can panic.

## What changed

Clamp wrapped range boundaries to the input length before calculating hyperlink columns and recording logical source ranges, keeping both aligned with the visible text.

## Testing

Add a regression test that extends a wrapped range past the input boundary and verifies that the line, hyperlinks, and logical source are preserved.

GitOrigin-RevId: c83c817df8d3ea790f7d126768092ae317ff9217
2026-10-09 00:04:15 +00:00

82 lines
2.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn oversized_destinations_remain_plain_text() {
let prefix = "https://example.com/";
let destination = format!(
"{prefix}{}",
"a".repeat(MAX_HYPERLINK_DESTINATION_BYTES - prefix.len())
);
assert_eq!(web_destination(&destination), Some(destination.clone()));
let oversized = format!("{destination}é");
assert_eq!(web_destination(&oversized), None);
assert_eq!(osc8_hyperlink(&oversized, "visible"), "visible");
// Explicit Markdown links can reach the cell marker without bare-URL detection.
let line = HyperlinkLine {
source: None,
line: Line::from("visible"),
hyperlinks: vec![TerminalHyperlink::web(/*columns*/ 0..7, oversized)],
};
let mut buf = Buffer::with_lines(["visible"]);
let expected = buf.clone();
let area = buf.area;
mark_buffer_hyperlinks(&mut buf, area, &[line], /*scroll_rows*/ 0);
assert_eq!(buf, expected);
}
#[test]
fn discovers_many_web_urls_with_incremental_columns() {
let destination = "https://x.io";
let token = format!("👩‍💻 ({destination}).\u{2003}");
let text = token.repeat(/*n*/ 50_000);
let expected = (0..50_000)
.map(|index| {
let start = index * display_width(&token) + 4;
TerminalHyperlink::web(start..start + destination.len(), destination.to_string())
})
.collect::<Vec<_>>();
assert_eq!(web_links_in_text(&text), expected);
}
#[test]
fn trims_long_runs_of_unmatched_closing_delimiters() {
let destination = "https://example.com/a_(b)[c]{d}<e>";
let text = format!("{destination}{}", ")]}>".repeat(/*n*/ 50_000));
assert_eq!(
web_links_in_text(&text),
vec![TerminalHyperlink::web(
/*columns*/ 0..destination.len(),
destination.to_string(),
)]
);
}
#[test]
fn decorated_hyperlinks_preserve_joined_emoji_columns() {
let destination = "https://example.test/👩‍💻/path";
let line = annotate_web_urls_in_line(Line::from(format!("👩‍💻 {destination} tail")));
assert_eq!(
decorate_spans(&line),
vec![Span::from(format!(
"👩‍💻 {} tail",
osc8_hyperlink(destination, destination)
))]
);
}
#[test]
fn source_wrapping_clips_a_cursor_sentinel_at_the_input_boundary() {
let destination = "https://example.test/pull/9410";
let text = format!("› [configuration editor]({destination})");
let source = annotate_web_urls_in_line(Line::from(text.clone()));
let mut wrapped = crate::wrapping::word_wrap_line_with_source(&source.line, text.len());
wrapped.last_mut().expect("wrapped line").range.end += 1;
let actual = remap_source_wrapped_line(&source, wrapped);
let mut expected = source;
expected.source = Some(LogicalLineSource::from_line(&expected.line));
assert!(lines_with_sources_eq(&actual, &[expected]));
}