use tokio::sync::mpsc::unbounded_channel;
use trusty_code_tui::ReplEvent;
use super::forward_session_event;
use crate::events::{Event, SessionEventEnvelope};
fn envelope(event: Event) -> SessionEventEnvelope {
SessionEventEnvelope::new("s-1".to_string(), 1, chrono::Utc::now(), event)
}
fn forward_tool(event: Event) -> (Option<String>, bool) {
let (tx, mut rx) = unbounded_channel();
forward_session_event(envelope(event), &tx);
match rx.try_recv().expect("a ToolInvocation event") {
ReplEvent::ToolInvocation { result, failed, .. } => (result, failed),
other => panic!("unexpected {other:?}"),
}
}
fn tool_finished(success: bool) -> Event {
Event::ToolFinished {
session_id: "s-1".into(),
agent: "pm".into(),
agent_id: String::new(),
tool: "bash".into(),
call_id: "call-1".into(),
success,
result_preview: "1 test failed".into(),
}
}
#[test]
fn tool_finished_unsuccessful_sets_the_failed_flag() {
let (result, failed) = forward_tool(tool_finished(false));
assert!(failed, "an unsuccessful call must say so in data");
assert_eq!(
result.as_deref(),
Some("FAILED: 1 test failed"),
"the prefix stays, as display text"
);
}
#[test]
fn tool_error_sets_the_failed_flag() {
let (result, failed) = forward_tool(Event::ToolError {
session_id: "s-1".into(),
agent: "pm".into(),
agent_id: String::new(),
tool: "read_file".into(),
call_id: "call-2".into(),
error: "no such file".into(),
});
assert!(failed);
assert_eq!(result.as_deref(), Some("ERROR: no such file"));
}
#[test]
fn successful_and_in_flight_calls_are_not_marked_failed() {
let (result, failed) = forward_tool(tool_finished(true));
assert!(!failed);
assert_eq!(result.as_deref(), Some("1 test failed"), "no prefix added");
let (result, failed) = forward_tool(Event::ToolStarted {
session_id: "s-1".into(),
agent: "pm".into(),
agent_id: String::new(),
tool: "bash".into(),
call_id: "call-3".into(),
args_preview: "cargo test".into(),
});
assert!(!failed, "a call that has not finished has not failed");
assert!(result.is_none());
}
#[test]
fn a_run_that_fails_early_renders_its_reason_and_an_error_line() {
let (tx, mut rx) = unbounded_channel();
let reason = "PM config error: failed to load agent config for 'pm'";
let sequence = [
Event::Log {
session_id: "s-1".into(),
level: "error".into(),
message: reason.into(),
},
Event::SessionDone {
session_id: "s-1".into(),
status: "failed".into(),
},
];
let terminal: Vec<bool> = sequence
.into_iter()
.map(|event| forward_session_event(envelope(event), &tx))
.collect();
assert_eq!(terminal, [false, true], "only SessionDone ends the turn");
let mut rendered = Vec::new();
while let Ok(event) = rx.try_recv() {
rendered.push(event);
}
assert_eq!(
rendered.first(),
Some(&ReplEvent::StatusMessage(format!("error: {reason}"))),
"the failure reason must reach the TUI: {rendered:?}"
);
match rendered.last() {
Some(ReplEvent::AssistantOutput {
chunk,
done: true,
is_error: true,
}) => assert!(!chunk.is_empty(), "a failed run must not end in silence"),
other => panic!("expected a visible terminal error, got {other:?}"),
}
}
#[test]
fn a_non_error_log_is_not_rendered() {
let (tx, mut rx) = unbounded_channel();
let terminal = forward_session_event(
envelope(Event::Log {
session_id: "s-1".into(),
level: "info".into(),
message: "indexing skipped".into(),
}),
&tx,
);
assert!(!terminal);
assert!(rx.try_recv().is_err(), "an info log must not render");
}