rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use pretty_assertions::assert_eq;
use rho_protocol::{SecretAsk, SecretDestination, SecretValue};
use tokio::sync::oneshot;

use super::{SecretAskComposer, SecretKey, SecretReply};

fn composer() -> SecretAskComposer {
    let (reply, _) = oneshot::channel();
    let ask = SecretAsk {
        server: "probe".into(),
        name: "TOKEN".into(),
        reason: None,
        destination: SecretDestination::Http {
            url: "https://mcp.example".into(),
        },
    };
    SecretAskComposer::new(ask, reply, /*restore_side*/ false)
}

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

/// One case: a prompt `prepare` set up, `pasted` and `typed` into, then
/// `last` pressed.
struct Case {
    case: &'static str,
    prepare: fn(&mut SecretAskComposer),
    pasted: &'static str,
    typed: &'static str,
    last: KeyEvent,
    /// The outcome of `last`, and whether the prompt then says the field
    /// needs a value.
    expected: (SecretKey, bool),
}

impl Case {
    fn run(&self) -> (SecretKey, bool) {
        let mut composer = composer();
        (self.prepare)(&mut composer);
        composer.insert_text(self.pasted);
        for ch in self.typed.chars() {
            assert_eq!(composer.apply_key(key(KeyCode::Char(ch))), SecretKey::Kept);
        }
        let outcome = composer.apply_key(self.last);
        (outcome, composer.blank_refused)
    }
}

fn asking(_: &mut SecretAskComposer) {}

fn refused_blank(composer: &mut SecretAskComposer) {
    assert_eq!(composer.apply_key(key(KeyCode::Enter)), SecretKey::Kept);
}

// Covers: Enter sends only a non-blank secret, trimmed as a pasted key
// needs; a blank Enter sends nothing and says so until the person types;
// Esc and Ctrl+C decline whatever was typed; and once the request closed
// elsewhere, only Enter or Esc do anything: they dismiss the prompt.
// Owner: secret ask prompt
#[test]
fn the_prompt_sends_only_what_the_person_meant() {
    let save = |value: &str| SecretKey::Reply(SecretReply::Save(SecretValue::new(value)));
    let decline = || SecretKey::Reply(SecretReply::Decline);
    let enter = key(KeyCode::Enter);
    let ctrl_c = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL);
    let case = |case, prepare, pasted, typed, last, expected| Case {
        case,
        prepare,
        pasted,
        typed,
        last,
        expected,
    };
    let cases = [
        case("blank", asking, "", "", enter, (SecretKey::Kept, true)),
        case("spaces", asking, "", "   ", enter, (SecretKey::Kept, true)),
        case(
            "trimmed",
            asking,
            " tok",
            "-pty ",
            enter,
            (save("tok-pty"), false),
        ),
        case(
            "refused, typed",
            refused_blank,
            "",
            "t",
            key(KeyCode::Left),
            (SecretKey::Kept, false),
        ),
        case(
            "refused, moved",
            refused_blank,
            "",
            "",
            key(KeyCode::Left),
            (SecretKey::Kept, true),
        ),
        case(
            "esc",
            asking,
            "",
            "tok",
            key(KeyCode::Esc),
            (decline(), false),
        ),
        case("ctrl+c", asking, "", "tok", ctrl_c, (decline(), false)),
        case(
            "closed, enter",
            SecretAskComposer::close,
            "tok",
            "tok",
            enter,
            (SecretKey::Dismiss, false),
        ),
        case(
            "closed, ctrl+c",
            SecretAskComposer::close,
            "",
            "tok",
            ctrl_c,
            (SecretKey::Kept, false),
        ),
    ];
    for case in cases {
        assert_eq!(case.run(), case.expected, "{}", case.case);
    }
}

// Covers: a prompt whose request closed while the person typed keeps none
// of it, and takes none of what they paste after.
// Owner: secret ask prompt
#[test]
fn a_closed_prompt_keeps_nothing_typed() {
    let mut composer = composer();
    composer.insert_text("tok");
    composer.close();
    composer.insert_text("-rest");
    assert_eq!(composer.editor.value, "");
}