rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::*;
use pretty_assertions::assert_eq;

fn key(key: &str, code: &'static str, key_code: u32, text: Option<&str>) -> Key {
    Key {
        key: key.to_owned(),
        code: Some(code),
        key_code: Some(key_code),
        text: text.map(str::to_owned),
    }
}

/// A character with no key of its own here.
fn uncoded(c: &str) -> Key {
    Key {
        key: c.to_owned(),
        code: None,
        key_code: None,
        text: Some(c.to_owned()),
    }
}

fn chord(modifiers: Modifiers, key: Key) -> Chord {
    Chord { modifiers, key }
}

// Covers: the keys and chords the model writes become the key events a US
// keyboard sends: names in either case and their X11 spellings, single
// characters, function keys, a modifier alone, shift's capital, the `+`
// key, and space-separated sequences.
// Owner: browser key names
#[test]
fn names_and_chords_become_key_events() {
    let none = Modifiers::NONE;
    let enter = key("Enter", "Enter", 13, Some("\r"));
    let backspace = key("Backspace", "Backspace", 8, None);
    let cases = [
        ("Enter", vec![chord(none, enter.clone())]),
        ("return", vec![chord(none, enter)]),
        (
            "BackSpace Backspace",
            vec![chord(none, backspace.clone()), chord(none, backspace)],
        ),
        (
            "Page_Down",
            vec![chord(none, key("PageDown", "PageDown", 34, None))],
        ),
        ("F5", vec![chord(none, key("F5", "F5", 116, None))]),
        (
            "ctrl+a",
            vec![chord(Modifiers::CTRL, key("a", "KeyA", 65, Some("a")))],
        ),
        (
            "ctrl+shift+t",
            vec![chord(
                Modifiers::CTRL.with(Modifiers::SHIFT),
                key("T", "KeyT", 84, Some("T")),
            )],
        ),
        (
            "cmd+Left",
            vec![chord(
                Modifiers::META,
                key("ArrowLeft", "ArrowLeft", 37, None),
            )],
        ),
        (
            "shift",
            vec![chord(none, key("Shift", "ShiftLeft", 16, None))],
        ),
        ("space", vec![chord(none, key(" ", "Space", 32, Some(" ")))]),
        ("7", vec![chord(none, key("7", "Digit7", 55, Some("7")))]),
        ("ctrl++", vec![chord(Modifiers::CTRL, uncoded("+"))]),
        ("+", vec![chord(none, uncoded("+"))]),
    ];
    for (text, expected) in cases {
        assert_eq!(sequence(text), Ok(expected), "{text}");
    }
}

// Covers: text that names no key, an unknown name, or an unknown modifier
// is refused before anything is pressed, for `key` and for a click's
// modifiers.
// Owner: browser key names
#[test]
fn unknown_keys_and_modifiers_are_refused() {
    for text in ["", "   ", "Enterr", "hyper+a", "ctrl+", "F13", "F0"] {
        assert!(sequence(text).is_err(), "{text:?}");
    }
    let parsed: Vec<_> = ["shift", "Ctrl+Shift", "cmd", "ctrl+a", "", "shift+"]
        .into_iter()
        .map(|text| modifiers(text).ok())
        .collect();
    assert_eq!(
        parsed,
        [
            Some(Modifiers::SHIFT),
            Some(Modifiers::CTRL.with(Modifiers::SHIFT)),
            Some(Modifiers::META),
            None,
            None,
            None,
        ]
    );
}

// Covers: typing presses a key for each character a US keyboard types,
// Enter for a line break whatever its form, Tab for a tab, and inserts the
// rest as input-method text, a run at a time.
// Owner: browser typing
#[test]
fn typing_presses_keys_and_inserts_the_rest() {
    let a = || Typed::Key(key("a", "KeyA", 65, Some("a")));
    let enter = || Typed::Key(key("Enter", "Enter", 13, Some("\r")));
    let cases = [
        ("a", vec![a()]),
        (
            "a\nb",
            vec![a(), enter(), Typed::Key(key("b", "KeyB", 66, Some("b")))],
        ),
        ("\r\n\r", vec![enter(), enter()]),
        ("\t", vec![Typed::Key(key("Tab", "Tab", 9, None))]),
        ("é👋a", vec![Typed::Text("é👋".to_owned()), a()]),
        ("!", vec![Typed::Key(uncoded("!"))]),
    ];
    for (text, expected) in cases {
        assert_eq!(typing(text), expected, "{text:?}");
    }
}