Files
codex/codex-rs/tui/src/wrapping_reuse_tests.rs
T
Eric Traut fcaa035438 Preserve logical source text and styles across TUI wrapping (#46708)
## Why

Display wrapping removes whitespace and adds gutters, so rendered rows alone cannot reliably describe the original logical text. Streaming previews also need to detect source changes that leave visible rows unchanged.

## What changed

- Retain shared logical text, styles, exact byte ranges, and display margins alongside wrapped rows; remap hyperlinks using those ranges.
- Preserve source metadata through prompt, Markdown, and prefixed history rendering, including spoken artifact annotations. Clear it when split-flap animation replaces the text.
- Include source metadata when comparing streaming previews and agent or plan tails.
- Keep compound graphemes intact, preserve explicit span styles, and reuse first-fit wrapping results when safe.
- Use the lighter history prompt background for submitted messages.

## Testing

Add regression coverage for source ranges and styles across widths and gutters, distinct repeated lines, source-only streaming updates, compound graphemes, and equivalence between reused and generic wrapping results. Extend Markdown, spoken artifact, and animation tests to check retained source metadata.

GitOrigin-RevId: 9e60746642fc9a8f91c1abfe7c77eefc1fe421ab
2026-09-19 20:22:48 +00:00

115 lines
3.6 KiB
Rust

//! Compare reused first-fit rows with the generic wrapping path, including source provenance.
use super::*;
use pretty_assertions::assert_eq;
use ratatui::style::Stylize;
#[test]
fn custom_word_separator_reprocesses_the_remainder() {
let separator = WordSeparator::Custom(|text| {
let Some(space) = text.find(' ') else {
return Box::new(std::iter::once(Word::from(text)));
};
Box::new([Word::from(&text[..=space]), Word::from(&text[space + 1..])].into_iter())
});
let line = Line::from("one two three");
let rows =
word_wrap_line_with_source(&line, RtOptions::new(/*width*/ 5).word_separator(separator))
.into_iter()
.map(|row| (row.line, row.range))
.collect::<Vec<_>>();
assert_eq!(
rows,
vec![
(Line::from("one"), 0..3),
(Line::from("two"), 4..7),
(Line::from("three"), 8..13),
]
);
}
#[test]
fn first_fit_reuse_preserves_styled_rows_and_source_ranges() {
// A custom first-fit function exercises the unchanged generic path without copying
// its implementation into the test. Compare complete rows, styles and byte ranges.
let generic_first_fit = textwrap::WrapAlgorithm::Custom(|words, widths| {
textwrap::WrapAlgorithm::FirstFit.wrap(words, widths)
});
// Cover distinct split, width, indentation and separator boundaries without a random matrix.
for (text, width, initial, subsequent, break_words, separator) in [
("", 0, "", "", true, WordSeparator::AsciiSpace),
(
"hello world",
0,
"> ",
" ",
true,
WordSeparator::AsciiSpace,
),
(
"cafe\u{301} 中文 👩🏽‍💻 🇧🇷 ガ",
2,
"",
"",
true,
WordSeparator::UnicodeBreakProperties,
),
(
"a-very-long-hyphenated-word",
5,
"界",
"ab",
true,
WordSeparator::AsciiSpace,
),
(
"https://example.com/a/b tail",
17,
"> ",
" ",
false,
WordSeparator::AsciiSpace,
),
(
"alpha beta\u{a0}gamma\u{200b}delta",
5,
"",
" ",
true,
WordSeparator::UnicodeBreakProperties,
),
(
"one\n two\r\nthree\tfour",
5,
"> ",
" ",
true,
WordSeparator::AsciiSpace,
),
(
"\u{1b}[31mred\u{1b}[0m tail",
80,
"",
"",
false,
WordSeparator::AsciiSpace,
),
] {
let line = Line::from(vec![text.bold(), " suffix".cyan()]).italic();
let options = RtOptions::new(width)
.initial_indent(Line::from(initial.red()))
.subsequent_indent(Line::from(subsequent.green()))
.break_words(break_words)
.word_separator(separator);
let actual = word_wrap_line_with_source(&line, options.clone())
.into_iter()
.map(|row| (row.line, row.range, row.prefix_bytes))
.collect::<Vec<_>>();
let expected = word_wrap_line_with_source(&line, options.wrap_algorithm(generic_first_fit))
.into_iter()
.map(|row| (row.line, row.range, row.prefix_bytes))
.collect::<Vec<_>>();
assert_eq!(actual, expected, "text={text:?}, width={width}");
}
}