use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::text::Line;
use ratatui::widgets::{Paragraph, Wrap};
use trusty_code_tui::app::{ChatLine, ChatRole, ReplApp};
use trusty_code_tui::model::{CommandDescriptor, CommandRouting};
use trusty_code_tui::widgets::banner::banner_lines;
use trusty_code_tui::widgets::scrollback::{build_chat_lines, chat_line_count};
const SENTINEL: &str = "@@@";
fn really_rendered_rows(lines: &[Line<'static>], width: u16) -> usize {
let mut all = lines.to_vec();
all.push(Line::from(SENTINEL));
let cells: usize = all.iter().map(|l| l.width()).sum();
let height = u16::try_from(cells / width as usize + all.len() + 2).unwrap_or(u16::MAX);
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).expect("construct terminal");
terminal
.draw(|f| {
let paragraph = Paragraph::new(all.clone()).wrap(Wrap { trim: false });
f.render_widget(paragraph, f.area());
})
.expect("draw");
let buffer = terminal.backend().buffer().clone();
let row = |y: u16| -> String {
(0..width)
.map(|x| buffer[(x, y)].symbol().to_string())
.collect()
};
(0..height)
.position(|y| row(y).contains(SENTINEL))
.expect("the marker must fit in the rendered buffer")
}
fn banner_app() -> ReplApp {
let mut app = ReplApp::new("demo", "bob");
app.commands = vec![
CommandDescriptor {
name: "workstream".to_string(),
summary: "List or activate a workstream".to_string(),
routing: CommandRouting::Engine,
args_hint: None,
},
CommandDescriptor {
name: "q".to_string(),
summary: "Quit".to_string(),
routing: CommandRouting::Engine,
args_hint: None,
},
];
app
}
fn app_with(role: ChatRole, text: &str) -> ReplApp {
let mut app = ReplApp::new("demo", "bob");
app.show_banner = false;
app.chat.push(ChatLine {
role,
text: text.to_string(),
tool: None,
});
app
}
#[test]
fn row_count_agrees_with_a_real_render() {
let long = ["123456789"; 12].join(" ");
let mut cases: Vec<(String, ReplApp, u16)> = vec![
(
"interior whitespace runs".to_string(),
app_with(ChatRole::Status, "ab cd ef gh ij kl"),
12,
),
(
"cjk wide characters".to_string(),
app_with(ChatRole::Status, &"中".repeat(60)),
80,
),
(
"plain long line".to_string(),
app_with(ChatRole::Status, &"x".repeat(200)),
80,
),
(
"the 80-column connect line".to_string(),
app_with(
ChatRole::Status,
"connected to tcode daemon at \
/Users/dev0/Library/Application Support/tcode/tcode.sock — home \
/private/tmp/q8230/repoA, solo agent (no delegation)",
),
80,
),
];
for width in 20u16..=100 {
cases.push((
"assistant continuation indent".to_string(),
app_with(ChatRole::Assistant, &format!("lead\n{long}")),
width,
));
cases.push((
"delegated gutter indent".to_string(),
app_with(ChatRole::Delegated, &long),
width,
));
}
for width in [20u16, 40, 60, 80, 100] {
cases.push(("the banner command column".to_string(), banner_app(), width));
}
let mut wrong: Vec<String> = Vec::new();
for (name, app, width) in cases {
let lines = build_chat_lines(&app, width as usize);
let predicted = chat_line_count(&app, width as usize);
let rendered = really_rendered_rows(&lines, width);
if predicted != rendered {
wrong.push(format!(
"{name} at {width} columns: sized for {predicted} rows, renders {rendered}"
));
}
}
assert!(
wrong.is_empty(),
"the pane is sized by this count, so it must equal the rendered rows: {wrong:#?}"
);
}
#[test]
fn the_banner_renders_whole_below_its_build_width() {
for width in [20u16, 30, 39, 80] {
let app = ReplApp::new("demo", "bob");
assert!(app.show_banner, "this is the banner-only branch of `draw`");
let lines = banner_lines(&app, width as usize);
let needed = really_rendered_rows(&lines, width);
assert!(
needed >= lines.len(),
"a wrapped banner can never need fewer rows than it has lines"
);
let backend = TestBackend::new(width, 80);
let mut terminal = Terminal::new(backend).expect("construct terminal");
terminal
.draw(|f| trusty_code_tui::layout::draw(f, &app))
.expect("draw");
let buffer = terminal.backend().buffer().clone();
let rendered: Vec<String> = (0..80u16)
.map(|y| {
(0..width)
.map(|x| buffer[(x, y)].symbol().to_string())
.collect()
})
.collect();
for rule in ['╭', '╰'] {
assert!(
rendered.iter().any(|r| r.contains(rule)),
"the banner's {rule} rule must render at {width} columns \
(the banner needs {needed} rows): {rendered:#?}"
);
}
}
}