rho-coding-agent 2.15.0

A fast Rust agent harness with a small footprint and opinionated defaults
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use pretty_assertions::assert_eq;

use super::{InlineChoice, InlineChoiceKeyOutcome, InlineChoiceOption};

fn key(code: KeyCode) -> KeyEvent {
    KeyEvent::new(code, KeyModifiers::NONE)
}

#[test]
fn navigation_skips_unavailable_options() {
    let mut choice = InlineChoice::new(
        "choose",
        "details",
        vec![
            InlineChoiceOption::unavailable("first", '1', "first", "unavailable"),
            InlineChoiceOption::available("second", '2', "second", "available"),
            InlineChoiceOption::available("third", '3', "third", "available"),
        ],
    )
    .unwrap();

    assert_eq!(choice.selected_value(), "second");
    assert_eq!(
        choice.handle_key(key(KeyCode::Down)),
        InlineChoiceKeyOutcome::Handled
    );
    assert_eq!(choice.selected_value(), "third");
    assert_eq!(
        choice.handle_key(key(KeyCode::Up)),
        InlineChoiceKeyOutcome::Handled
    );
    assert_eq!(choice.selected_value(), "second");
}

#[test]
fn shortcut_selects_and_submits_option() {
    for (case, modifiers) in [
        ("plain", KeyModifiers::NONE),
        ("shift", KeyModifiers::SHIFT),
    ] {
        let mut choice = InlineChoice::new(
            "choose",
            "details",
            vec![
                InlineChoiceOption::available("compact", '1', "compact", "first"),
                InlineChoiceOption::available("direct", '2', "direct", "second"),
            ],
        )
        .unwrap();

        assert_eq!(
            choice.handle_key(KeyEvent::new(KeyCode::Char('2'), modifiers)),
            InlineChoiceKeyOutcome::Selected("direct".into()),
            "{case}"
        );
        assert_eq!(choice.selected_value(), "direct", "{case}");
        assert_eq!(
            choice.handle_key(key(KeyCode::Esc)),
            InlineChoiceKeyOutcome::Cancelled,
            "{case}"
        );
    }
}