rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::collections::BTreeMap;

use pretty_assertions::assert_eq;
use rho_protocol::{Choice, Question, Selection};

use super::*;
use crate::caller_context::{Access, CallerContext, Scope};

// Covers: an answer may name a choice by its label, in any case, as well as
// by its value, and is sent by value, as the daemon matches choices, so a
// yes/no question's "Yes" is not taken for text of its own where the
// question takes some; text that names no choice, and an answer to a
// question the ask lacks, go as they are for the daemon to refuse or take;
// a decline passes as it is.
// Owner: session-control answer, which maps answers before the daemon.
#[test]
fn an_answer_names_a_choice_by_label_or_value() {
    let question = |id: &str, allow_other| Question {
        id: id.into(),
        prompt: "Deploy?".into(),
        header: None,
        choices: [("yes", "Yes"), ("no", "No")]
            .map(|(value, label)| Choice {
                value: value.into(),
                label: label.into(),
                description: None,
            })
            .to_vec(),
        selection: Selection::One,
        allow_other,
        help: None,
        default: None,
        required: true,
    };
    let asked = QuestionAsk {
        title: "questionnaire".into(),
        reason: None,
        questions: vec![question("closed", false), question("open", true)],
    };
    let answers = |id: &str, value: &str| QuestionAnswer::Answers {
        answers: BTreeMap::from([(id.to_owned(), vec![value.to_owned()])]),
    };
    let rows = [
        (
            "a label",
            answers("closed", "Yes"),
            answers("closed", "yes"),
        ),
        (
            "a label in any case",
            answers("closed", "NO"),
            answers("closed", "no"),
        ),
        ("a value", answers("closed", "no"), answers("closed", "no")),
        (
            "a label where text is taken too",
            answers("open", "Yes"),
            answers("open", "yes"),
        ),
        ("text", answers("open", "Later"), answers("open", "Later")),
        (
            "an unknown question",
            answers("other", "Yes"),
            answers("other", "Yes"),
        ),
        (
            "a decline",
            QuestionAnswer::Decline,
            QuestionAnswer::Decline,
        ),
    ];

    let mapped: Vec<_> = rows
        .iter()
        .map(|(case, answer, _)| (*case, by_choice_values(answer.clone(), &asked)))
        .collect();
    let expected: Vec<_> = rows
        .into_iter()
        .map(|(case, _, expected)| (case, expected))
        .collect();
    assert_eq!(mapped, expected);
}

// Covers: a calling session answers as itself, which the daemon holds to
// the sessions it started; a caller outside any session, a depth-0 one
// included, as a session a person opened is when it calls `rho mcp serve`,
// answers as an outside agent; a caller that declares a nested session's
// depth is refused rather than answering past the starter rule.
// Owner: session-control answer.
#[test]
fn who_answers_follows_the_calling_session() {
    let caller = |session: Option<&str>, depth| {
        let context = CallerContext {
            depth,
            scope: Scope::FULL,
        };
        Caller {
            session: session.map(SessionId::new),
            ..Caller::new(
                super::super::Grant::local(Access::Write),
                &context,
                Default::default(),
            )
        }
    };
    let rows = [
        (
            "a session",
            caller(Some("s"), 1),
            Ok(Answerer::Session(SessionId::new("s"))),
        ),
        ("an outside agent", caller(None, 0), Ok(Answerer::Outside)),
        ("a nested session's agent", caller(None, 1), Err(())),
    ];

    let found: Vec<_> = rows
        .iter()
        .map(|(case, caller, _)| (*case, Answerer::of(caller).map_err(drop)))
        .collect();
    let expected: Vec<_> = rows
        .into_iter()
        .map(|(case, _, expected)| (case, expected))
        .collect();
    assert_eq!(found, expected);
}