use ratatui::Terminal;
use ratatui::backend::TestBackend;
use crate::app::{ReplApp, apply};
use crate::event::ReplEvent;
use crate::widgets::scrollback::{DELEGATED_GUTTER, draw_chat};
const WIDTH: u16 = 60;
const HEIGHT: u16 = 16;
fn rows(app: &ReplApp) -> Vec<String> {
let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).expect("terminal");
terminal
.draw(|f| draw_chat(f, app, f.area()))
.expect("draw chat pane");
let buf = terminal.backend().buffer();
(0..HEIGHT)
.map(|y| {
(0..WIDTH)
.map(|x| buf[(x, y)].symbol())
.collect::<String>()
.trim_end()
.to_string()
})
.collect()
}
fn tool(id: &str, agent_id: &str, name: &str, args: &str, result: Option<&str>) -> ReplEvent {
tool_outcome(id, agent_id, name, args, result, false)
}
fn tool_outcome(
id: &str,
agent_id: &str,
name: &str,
args: &str,
result: Option<&str>,
failed: bool,
) -> ReplEvent {
ReplEvent::ToolInvocation {
id: id.into(),
agent_id: agent_id.into(),
tool_name: name.into(),
args: if result.is_some() {
serde_json::Value::Null
} else {
serde_json::json!(args)
},
result: result.map(str::to_string),
failed,
}
}
fn app() -> ReplApp {
let mut app = ReplApp::new("demo", "u");
app.show_banner = false;
app
}
fn toggle() -> ReplEvent {
ReplEvent::Key(crate::event::KeyInput {
code: crate::event::KeyCode::Char('o'),
modifiers: crate::event::KeyModifiers {
ctrl: true,
alt: false,
shift: false,
},
})
}
#[test]
fn tool_call_and_result_render_as_one_card() {
let mut app = app();
apply(&mut app, tool("c1", "", "git.checkout", "main", None));
let pending = rows(&app);
assert!(
pending.contains(&"⏺ git.checkout(main)".to_string()),
"{pending:#?}"
);
assert!(
pending.contains(&" ⎿ running…".to_string()),
"{pending:#?}"
);
apply(
&mut app,
tool("c1", "", "git.checkout", "", Some("switched to main")),
);
apply(&mut app, toggle()); let done = rows(&app);
let headers = done.iter().filter(|r| r.contains("git.checkout")).count();
assert_eq!(headers, 1, "one card, not two: {done:#?}");
let at = done
.iter()
.position(|r| r == "⏺ git.checkout(main)")
.expect("header keeps the call's args");
assert_eq!(done[at + 1], " ⎿ switched to main", "{done:#?}");
assert!(!done.iter().any(|r| r.contains("running…")), "{done:#?}");
}
#[test]
fn completed_card_collapses_to_a_one_line_summary() {
let mut app = app();
apply(&mut app, tool("r1", "", "read_file", "src/lib.rs", None));
let body = "pub fn add(a: i64, b: i64) -> i64 {\n a + b\n}";
apply(&mut app, tool("r1", "", "read_file", "", Some(body)));
let out = rows(&app);
assert!(
out.contains(&"⏺ read_file(src/lib.rs) · ok · 3 lines".to_string()),
"{out:#?}"
);
assert!(
!out.iter().any(|r| r.contains("pub fn add")),
"the body must be hidden while collapsed: {out:#?}"
);
}
#[test]
fn toggle_round_trips_between_summary_and_full_body() {
let mut app = app();
apply(&mut app, tool("r1", "", "read_file", "src/lib.rs", None));
apply(&mut app, tool("r1", "", "read_file", "", Some("a\nb")));
assert!(rows(&app).iter().any(|r| r.contains("· ok · 2 lines")));
apply(&mut app, toggle());
let open = rows(&app);
assert!(open.contains(&" ⎿ a".to_string()), "{open:#?}");
assert!(open.contains(&" b".to_string()), "{open:#?}");
assert!(
!open.iter().any(|r| r.contains("· ok ·")),
"expanded header drops the summary suffix: {open:#?}"
);
apply(&mut app, toggle());
let shut = rows(&app);
assert!(
shut.iter().any(|r| r.contains("· ok · 2 lines")),
"{shut:#?}"
);
assert!(!shut.contains(&" ⎿ a".to_string()), "{shut:#?}");
}
#[test]
fn errored_card_renders_expanded_by_default() {
let mut app = app();
apply(&mut app, tool("e1", "", "read_file", "missing.rs", None));
apply(
&mut app,
tool_outcome("e1", "", "read_file", "", Some("ERROR: no such file"), true),
);
let out = rows(&app);
assert!(
out.contains(&" ⎿ ERROR: no such file".to_string()),
"the error body must be visible without a keystroke: {out:#?}"
);
assert!(
!out.iter().any(|r| r.contains("· ok")),
"an errored card is not summarized as ok: {out:#?}"
);
}
#[test]
fn failure_flag_alone_drives_the_error_render() {
let mut flagged = app();
let mut noisy = app();
apply(&mut flagged, tool("f1", "", "bash", "cargo test", None));
apply(
&mut flagged,
tool_outcome("f1", "", "bash", "", Some("exit status 1"), true),
);
let out = rows(&flagged);
assert!(
out.contains(&" ⎿ exit status 1".to_string()),
"a flagged failure expands with no marker in the body: {out:#?}"
);
let card = flagged
.chat
.last()
.and_then(|c| c.tool.as_ref())
.expect("a card");
assert!(card.is_error());
assert_eq!(
super::tool_card_lines(card, false)[0].spans[0].style.fg,
Some(ratatui::style::Color::Red),
"a flagged failure paints its glyph red"
);
apply(&mut noisy, tool("s1", "", "grep", "ERROR", None));
apply(
&mut noisy,
tool("s1", "", "grep", "", Some("ERROR: found in log.txt")),
);
let out = rows(&noisy);
assert!(
out.iter().any(|r| r.contains("· ok")),
"an unflagged result is output, however it reads: {out:#?}"
);
}
#[test]
fn multi_line_result_keeps_its_line_breaks() {
let mut app = app();
apply(&mut app, tool("r1", "", "read_file", "src/lib.rs", None));
let body = "pub fn add(a: i64, b: i64) -> i64 {\n a + b\n}";
apply(&mut app, tool("r1", "", "read_file", "", Some(body)));
apply(&mut app, toggle());
let out = rows(&app);
let at = out
.iter()
.position(|r| r == " ⎿ pub fn add(a: i64, b: i64) -> i64 {")
.unwrap_or_else(|| panic!("first result line on its own row: {out:#?}"));
assert_eq!(out[at + 1], " a + b", "{out:#?}");
assert_eq!(out[at + 2], " }", "{out:#?}");
}
#[test]
fn delegated_card_is_guttered_and_top_level_card_is_not() {
let mut app = app();
apply(
&mut app,
ReplEvent::DelegationStarted {
agent_id: "eng-1".into(),
agent: "engineer".into(),
task: "run tests".into(),
},
);
apply(&mut app, tool("d1", "eng-1", "bash", "cargo test", None));
apply(
&mut app,
tool("d1", "eng-1", "bash", "", Some("ok\n2 passed")),
);
apply(&mut app, toggle());
apply(
&mut app,
tool("p1", "pm-1", "delegate_to_agent", "engineer", None),
);
let out = rows(&app);
for expected in ["⏺ bash(cargo test)", " ⎿ ok", " 2 passed"] {
let row = format!("{DELEGATED_GUTTER}{expected}");
assert!(
out.contains(&row),
"missing delegated row {row:?}: {out:#?}"
);
}
assert!(
out.contains(&"⏺ delegate_to_agent(engineer)".to_string()),
"top-level card must be flush left: {out:#?}"
);
}