rho-coding-agent 3.12.0

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

const ROOT: &str = "root";

/// An option list holding the mode `mode`.
fn options(mode: &str) -> Vec<v2::SessionConfigOption> {
    let values = ["default", "plan", "bypassPermissions"]
        .map(|value| v2::SessionConfigSelectOption::new(value, value));
    vec![v2::SessionConfigOption::select(
        "mode",
        "Mode",
        mode.to_owned(),
        values.to_vec(),
    )]
}

fn mode_update(session: &str, mode: &str) -> Inbound {
    Inbound::Update(Box::new(v2::UpdateSessionNotification::new(
        session.to_owned(),
        v2::SessionUpdate::ConfigOptionUpdate(v2::ConfigOptionUpdate::new(options(mode))),
    )))
}

fn running() -> Inbound {
    Inbound::Update(Box::new(v2::UpdateSessionNotification::new(
        ROOT.to_owned(),
        v2::SessionUpdate::StateUpdate(v2::StateUpdate::Running(v2::RunningStateUpdate::new())),
    )))
}

/// The mode an option list holds.
fn mode_of(options: &[v2::SessionConfigOption]) -> String {
    match &options[0].kind {
        v2::SessionConfigKind::Select(select) => select.current_value.to_string(),
        _ => unreachable!("the test sets selects only"),
    }
}

/// What an item of the stream is, for comparison.
fn label(inbound: &Inbound) -> String {
    match inbound {
        Inbound::Update(notification) => match &notification.update {
            v2::SessionUpdate::ConfigOptionUpdate(update) => {
                format!(
                    "{} mode {}",
                    notification.session_id,
                    mode_of(&update.config_options)
                )
            }
            _ => format!("{} update", notification.session_id),
        },
        Inbound::Took(_) => "took".to_owned(),
        Inbound::Answered => "answered".to_owned(),
    }
}

// Covers: setup applies its answers and config updates in the order the
// agent sent them: an update before an answer is replaced by it, one after
// it replaces the answer's options, another session's is no option of the
// root's, and the rest waits for the turn in order.
// Owner: ACP v2 inbound stream (setup reads).
#[tokio::test]
async fn setup_reads_answers_and_config_updates_in_order() {
    // (case, stream before the answer `default`, stream after it,
    // expected: answered mode, latest options' mode, what waits for the turn)
    let cases = [
        (
            "a stale update before the answer loses to it",
            vec![mode_update(ROOT, "bypassPermissions")],
            vec![],
            ("default", None, vec![]),
        ),
        (
            "an update after the answer moves the options on",
            vec![],
            vec![mode_update(ROOT, "bypassPermissions")],
            ("default", Some("bypassPermissions"), vec![]),
        ),
        (
            "another session's update is no option of the root's",
            vec![],
            vec![mode_update("child", "bypassPermissions")],
            ("default", None, vec!["child mode bypassPermissions"]),
        ),
        (
            "other updates wait for the turn in order",
            vec![running()],
            vec![running(), mode_update(ROOT, "plan")],
            ("default", Some("plan"), vec!["root update", "root update"]),
        ),
    ];
    for (case, before, after, expected) in cases {
        let (mut updates, tx) = Updates::new();
        for inbound in before {
            tx.send(inbound).unwrap();
        }
        tx.send(Inbound::Answered).unwrap();
        for inbound in after {
            tx.send(inbound).unwrap();
        }
        let (answer_tx, answer_rx) = oneshot::channel();
        answer_tx.send(Ok(options("default"))).unwrap();
        let answered = mode_of(&updates.answer(answer_rx).await.unwrap());
        let latest = updates
            .latest_options(ROOT)
            .map(|options| mode_of(&options));
        let waiting: Vec<String> = updates.queued.iter().map(label).collect();
        assert_eq!(
            (answered.as_str(), latest.as_deref(), waiting),
            (
                expected.0,
                expected.1,
                expected.2.into_iter().map(str::to_owned).collect()
            ),
            "{case}"
        );
    }
}