use super::super::{tests::test_app, CommandChoice, CommandChoiceKind, InputSubmissionMode};
use crate::tui::composer_buffer::ComposerEditKey;
use pretty_assertions::assert_eq;
#[test]
fn argument_choice_completion_replaces_placeholder() {
let mut app = test_app();
app.input_ui.set_text("/codemode ".to_string());
app.input_ui.set_cursor(app.input_ui.text().chars().count());
app.clamp_command_selection();
app.input_ui.move_command_selection(1);
let on = app.selected_command().unwrap();
app.complete_command_choice(&on);
assert_eq!(app.input_ui.text(), "/codemode on");
assert_eq!(app.input_ui.cursor(), "/codemode on".chars().count());
assert_eq!(
app.input_ui.submission_mode(),
InputSubmissionMode::ParseCommands
);
}
#[test]
fn prompt_template_resolves_edits_without_restart() {
let project = tempfile::tempdir().unwrap();
let prompts = project.path().join(".rho/prompts");
std::fs::create_dir_all(project.path().join(".git")).unwrap();
std::fs::create_dir_all(&prompts).unwrap();
let mut app = test_app();
app.info.runtime.cwd = project.path().to_path_buf();
std::fs::write(prompts.join("zz-reload.md"), "first draft").unwrap();
assert_eq!(
app.resolve_prompt_template("prompt:zz-reload", ""),
Some("first draft".to_string())
);
std::fs::write(prompts.join("zz-reload.md"), "second draft").unwrap();
assert_eq!(
app.resolve_prompt_template("PrOmPt:ZZ-RELOAD", "details"),
Some("second draft details".to_string())
);
assert_eq!(app.resolve_prompt_template("prompt:missing", ""), None);
}
#[test]
fn recalling_a_command_keeps_the_palette_closed_until_edit() {
let mut app = test_app();
app.push_input_history("/info");
app.push_input_history("/model");
app.input_ui.set_text("/c".to_string());
app.input_ui.set_cursor(2);
app.input_changed();
assert!(app.command_palette_visible());
app.apply_input_edit_key(ComposerEditKey::Up);
assert_eq!(app.input_ui.text(), "/model");
assert!(!app.command_palette_visible());
app.apply_input_edit_key(ComposerEditKey::Up);
assert_eq!(app.input_ui.text(), "/info");
assert!(!app.command_palette_visible());
app.apply_input_edit_key(ComposerEditKey::Down);
app.apply_input_edit_key(ComposerEditKey::Down);
assert_eq!(app.input_ui.text(), "/c");
assert!(app.command_palette_visible());
app.apply_input_edit_key(ComposerEditKey::Up);
assert!(!app.command_palette_visible());
app.apply_input_edit_key(ComposerEditKey::Backspace);
assert_eq!(app.input_ui.text(), "/mode");
assert!(app.command_palette_visible());
}
#[test]
fn dismiss_on_esc_clears_only_a_bare_slash() {
for (typed, expected) in [("/", ""), ("/mo", "/mo")] {
let mut app = test_app();
app.input_ui.set_text(typed.to_string());
app.input_ui.set_cursor(typed.chars().count());
assert!(app.command_palette_visible(), "{typed}");
app.dismiss_command_palette_on_esc();
assert_eq!(app.input_ui.text(), expected, "{typed}");
assert_eq!(app.input_ui.cursor(), expected.chars().count(), "{typed}");
assert!(app.input_ui.command_palette_dismissed(), "{typed}");
}
}
#[test]
fn enter_keeps_bare_command_until_an_argument_row_is_picked() {
let mut app = test_app();
app.input_ui.set_text("/codemode ".to_string());
app.input_ui.set_cursor(app.input_ui.text().chars().count());
app.clamp_command_selection();
let matches = app.command_matches();
assert!(!app.enter_completes_choice(&matches[0]));
app.input_ui.move_command_selection(1);
assert!(app.enter_completes_choice(&matches[1]));
app.input_ui.set_text("/codemode".to_string());
app.input_ui.set_cursor(app.input_ui.text().chars().count());
app.input_changed();
assert!(!app.input_ui.command_selection_explicit());
}
#[test]
fn clamping_below_a_pick_drops_the_explicit_flag() {
let mut app = test_app();
app.input_ui.set_text("/codemode ".to_string());
app.input_ui.set_cursor(app.input_ui.text().chars().count());
app.clamp_command_selection();
app.input_ui.move_command_selection(1);
assert!(app.input_ui.command_selection_explicit());
app.input_ui.set_text("/agents ".to_string());
app.input_ui.set_cursor(app.input_ui.text().chars().count());
app.input_changed();
assert_eq!(app.input_ui.command_selection(), 0);
assert!(!app.input_ui.command_selection_explicit());
}
#[test]
fn enter_on_mcp_argument_rows_follows_the_pick_rule() {
let choice = CommandChoice {
name: "alice".into(),
usage: "alice".into(),
description: String::new(),
kind: CommandChoiceKind::McpPromptArgument { value: 0..0 },
};
let mut app = test_app();
assert!(!app.enter_completes_choice(&choice));
app.input_ui.move_command_selection(1);
assert!(app.enter_completes_choice(&choice));
}