use super::*;
use crate::app::ReplApp;
use tokio::sync::mpsc;
struct MockEngine {
picker_name: Option<&'static str>,
}
impl MockEngine {
fn none() -> Self {
Self { picker_name: None }
}
fn with_picker(name: &'static str) -> Self {
Self {
picker_name: Some(name),
}
}
}
#[async_trait::async_trait]
impl TuiEngine for MockEngine {
async fn handle_input(
&self,
line: String,
tx: UnboundedSender<ReplEvent>,
) -> anyhow::Result<bool> {
let _ = tx.send(ReplEvent::StatusMessage(format!("handled: {line}")));
Ok(true)
}
async fn setup(&self, _tx: UnboundedSender<ReplEvent>) -> anyhow::Result<()> {
Ok(())
}
fn picker(&self, name: &str) -> Option<PickerRequest> {
if self.picker_name == Some(name) {
Some(PickerRequest {
title: "Select a model".to_string(),
items: vec![
PickerItem {
id: "opus-4".to_string(),
label: "Claude Opus 4".to_string(),
description: None,
},
PickerItem {
id: "haiku-4-5".to_string(),
label: "Claude Haiku 4.5".to_string(),
description: None,
},
],
dispatch_command: format!("/{name}"),
})
} else {
None
}
}
}
fn engine_command(name: &str, args_hint: Option<&str>) -> CommandDescriptor {
CommandDescriptor {
name: name.to_string(),
summary: format!("do {name}"),
routing: CommandRouting::Engine,
args_hint: args_hint.map(str::to_string),
}
}
#[test]
fn built_in_commands_names_match_route_recognition() {
for cmd in built_in_commands() {
let routed = route(&format!("/{}", cmd.name));
assert!(
matches!(routed, Route::BuiltIn(_)),
"built-in `{}` must route as BuiltIn, got {routed:?}",
cmd.name
);
}
}
#[test]
fn route_recognizes_each_builtin_name() {
assert_eq!(route("/help"), Route::BuiltIn(BuiltIn::Help));
assert_eq!(route("/clear"), Route::BuiltIn(BuiltIn::Clear));
assert_eq!(route("/quit"), Route::BuiltIn(BuiltIn::Quit));
assert_eq!(route("/exit"), Route::BuiltIn(BuiltIn::Quit));
}
#[test]
fn route_forwards_plain_chat() {
assert_eq!(
route("hello there"),
Route::Forward("hello there".to_string())
);
}
#[test]
fn route_forwards_unrecognized_slash_command() {
assert_eq!(
route("/nope --flag"),
Route::Forward("/nope --flag".to_string())
);
}
#[test]
fn route_forwards_engine_domain_command() {
assert_eq!(
route("/workstream activate a1b2c3d4"),
Route::Forward("/workstream activate a1b2c3d4".to_string())
);
}
#[test]
fn route_trims_surrounding_whitespace() {
assert_eq!(route(" /clear "), Route::BuiltIn(BuiltIn::Clear));
assert_eq!(
route(" hello world "),
Route::Forward("hello world".to_string())
);
}
#[test]
fn render_help_lists_builtins_and_engine_commands() {
let engine_commands = vec![engine_command("workstream", None)];
let text = render_help(&engine_commands);
assert!(text.contains("/help"));
assert!(text.contains("/clear"));
assert!(text.contains("/quit"));
assert!(text.contains("/exit"));
assert!(text.contains("/workstream"));
assert!(text.contains("do workstream"));
}
#[test]
fn render_help_includes_args_hint_when_present() {
let engine_commands = vec![engine_command("workstream", Some("activate <id>"))];
let text = render_help(&engine_commands);
assert!(text.contains("/workstream activate <id> — do workstream"));
}
#[test]
fn resolve_forward_opens_picker_for_bare_matching_command() {
let engine = MockEngine::with_picker("model");
let outcome = resolve_forward("/model", &engine);
match outcome {
Forward::OpenPicker(req) => {
assert_eq!(req.title, "Select a model");
assert_eq!(req.items.len(), 2);
}
Forward::ToEngine(_) => panic!("expected OpenPicker, got ToEngine"),
}
}
#[test]
fn resolve_forward_ignores_picker_when_args_present() {
let engine = MockEngine::with_picker("model");
let outcome = resolve_forward("/model opus-4", &engine);
assert_eq!(outcome, Forward::ToEngine("/model opus-4".to_string()));
}
#[test]
fn resolve_forward_falls_back_to_engine_when_no_picker_matches() {
let engine = MockEngine::none();
let outcome = resolve_forward("/model", &engine);
assert_eq!(outcome, Forward::ToEngine("/model".to_string()));
}
#[test]
fn resolve_forward_never_opens_picker_for_plain_chat() {
let engine = MockEngine::with_picker("model");
let outcome = resolve_forward("model", &engine);
assert_eq!(outcome, Forward::ToEngine("model".to_string()));
}
#[test]
fn compose_selection_matches_picker_request_contract() {
let request = PickerRequest {
title: "Select a provider".to_string(),
items: vec![PickerItem {
id: "openrouter".to_string(),
label: "OpenRouter".to_string(),
description: None,
}],
dispatch_command: "/provider".to_string(),
};
let composed = compose_selection(&request, &request.items[0]);
assert_eq!(composed, "/provider openrouter");
}
#[tokio::test]
async fn dispatch_forward_opens_picker_without_calling_engine() {
let mut app = ReplApp::new("demo", "u");
let engine = MockEngine::with_picker("model");
let (tx, mut rx) = mpsc::unbounded_channel();
let ok = dispatch_forward(&mut app, "/model".to_string(), &engine, &tx)
.await
.expect("dispatch_forward must not error");
assert!(ok);
assert!(app.active_picker.is_some(), "picker must be staged");
drop(tx);
assert!(
rx.recv().await.is_none(),
"engine.handle_input must never have been called"
);
}
#[tokio::test]
async fn dispatch_forward_clears_busy_set_by_submit_line_when_opening_picker() {
let mut app = ReplApp::new("demo", "u");
let engine = MockEngine::with_picker("model");
let (tx, _rx) = mpsc::unbounded_channel();
app.submit_line("/model".to_string());
assert!(app.busy, "submit_line must mark a Route::Forward line busy");
let line = app.pending_submit.take().expect("must have staged /model");
dispatch_forward(&mut app, line, &engine, &tx)
.await
.expect("dispatch_forward must not error");
assert!(
!app.busy,
"opening a picker must clear busy — nothing will ever send \
AssistantOutput to clear it while the picker sits open"
);
assert!(app.active_picker.is_some(), "picker must still be staged");
}
#[tokio::test]
async fn dispatch_forward_prevents_spurious_cancel_from_up_arrow_while_picker_open() {
use crate::app::apply;
use crate::event::{KeyCode, KeyInput, KeyModifiers, ReplEvent};
let mut app = ReplApp::new("demo", "u");
let engine = MockEngine::with_picker("model");
let (tx, _rx) = mpsc::unbounded_channel();
app.submit_line("/model".to_string());
let line = app.pending_submit.take().expect("must have staged /model");
dispatch_forward(&mut app, line, &engine, &tx)
.await
.expect("dispatch_forward must not error");
assert!(!app.busy);
apply(
&mut app,
ReplEvent::Key(KeyInput {
code: KeyCode::Up,
modifiers: KeyModifiers::default(),
}),
);
assert!(
!app.pending_cancel,
"Up-arrow while the picker is open (busy already cleared) must \
not signal a spurious cancel"
);
}
#[tokio::test]
async fn dispatch_forward_calls_engine_when_no_picker_matches() {
let mut app = ReplApp::new("demo", "u");
let engine = MockEngine::none();
let (tx, mut rx) = mpsc::unbounded_channel();
let ok = dispatch_forward(&mut app, "/workstream list".to_string(), &engine, &tx)
.await
.expect("dispatch_forward must not error");
assert!(ok);
assert!(app.active_picker.is_none());
let event = rx.recv().await.expect("engine must have sent an event");
assert_eq!(
event,
ReplEvent::StatusMessage("handled: /workstream list".to_string())
);
}
#[test]
fn submit_line_builtin_clear_clears_scrollback() {
let mut app = ReplApp::new("demo", "u");
app.push_assistant("earlier response", false);
assert!(!app.chat.is_empty());
app.submit_line("/clear".to_string());
assert!(app.chat.is_empty(), "scrollback must be cleared");
assert!(app.pending_submit.is_none(), "must never forward /clear");
assert!(!app.busy, "/clear must not mark the app busy");
}
#[test]
fn submit_line_builtin_quit_sets_quit() {
let mut app = ReplApp::new("demo", "u");
app.submit_line("/quit".to_string());
assert!(app.quit);
assert!(app.pending_submit.is_none());
let mut app2 = ReplApp::new("demo", "u");
app2.submit_line("/exit".to_string());
assert!(app2.quit, "/exit must be recognized as the /quit alias");
}
#[test]
fn submit_line_builtin_help_lists_commands() {
let mut app = ReplApp::new("demo", "u");
app.commands = vec![engine_command("workstream", Some("activate <id>"))];
app.submit_line("/help".to_string());
assert!(app.pending_submit.is_none());
assert_eq!(app.chat.len(), 2);
assert!(app.chat[1].text.contains("/workstream activate <id>"));
assert!(app.chat[1].text.contains("/clear"));
}
#[test]
fn submit_line_forwards_non_builtin_and_marks_busy() {
let mut app = ReplApp::new("demo", "u");
app.submit_line("/workstream list".to_string());
assert!(app.busy);
assert_eq!(
app.pending_submit.take(),
Some("/workstream list".to_string())
);
}
#[test]
fn confirm_picker_selection_composes_and_closes() {
let mut app = ReplApp::new("demo", "u");
app.open_picker(PickerRequest {
title: "Select a model".to_string(),
items: vec![PickerItem {
id: "opus-4".to_string(),
label: "Claude Opus 4".to_string(),
description: None,
}],
dispatch_command: "/model".to_string(),
});
let composed = app.confirm_picker_selection(0);
assert_eq!(composed, Some("/model opus-4".to_string()));
assert!(app.active_picker.is_none(), "picker must close on confirm");
}
#[test]
fn confirm_picker_selection_out_of_range_is_noop() {
let mut app = ReplApp::new("demo", "u");
assert_eq!(app.confirm_picker_selection(0), None);
app.open_picker(PickerRequest {
title: "Select a model".to_string(),
items: vec![PickerItem {
id: "opus-4".to_string(),
label: "Claude Opus 4".to_string(),
description: None,
}],
dispatch_command: "/model".to_string(),
});
assert_eq!(app.confirm_picker_selection(5), None);
assert!(
app.active_picker.is_some(),
"an out-of-range confirm must not close the picker"
);
}