rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use rho_protocol::{self as wire, EntryId, ErrorCode, HistoryEvent};

use super::fit_message;
use crate::app::{
    daemon_host::sessions::feed::tests::{idle_feed, notice, saved, shown},
    session_events::Boundary,
};

fn id(entry: &str) -> EntryId {
    EntryId::new(entry)
}

/// `event`, stamped with saved entry `after`, if any.
fn stamped(event: wire::SessionEvent, after: Option<&str>) -> HistoryEvent {
    let mut page = HistoryEvent::new(event);
    page.after_entry = after.map(id);
    page
}

/// A page as a client takes it: its events, each without when it was
/// saved, and the newest entry it left out.
fn page(response: wire::SessionHistoryResponse) -> (Vec<HistoryEvent>, Option<EntryId>) {
    let events = response
        .events
        .into_iter()
        .map(|mut event| {
            event.at = None;
            event
        })
        .collect();
    (events, response.earlier)
}

// Covers: a page holds the newest whole saved entries through `through`
// that fit, at least one, stamped as a reset stamps them, and names the
// newest it left out until it reaches the start; one entry the
// conversation no longer holds is `ConversationMoved`. A client paging
// back would otherwise miss or repeat rows, or page forever.
// Owner: daemon session feed history
#[test]
fn history_pages_back_through_the_saved_entries() {
    let feed = idle_feed();
    feed.end_turn(saved(&["one", "two"]));
    feed.end_turn(saved(&["three"]));
    feed.publish([notice("live")]);
    let (two, three) = {
        let state = feed.state();
        (
            state.transcript.bytes(1, 2, /*deferral*/ None),
            state.transcript.bytes(2, 3, /*deferral*/ None),
        )
    };
    for (name, through, max_bytes, expected) in [
        (
            "the newest entry alone",
            "three/0",
            three,
            (
                vec![stamped(shown("three"), Some("two/0"))],
                Some(id("two/0")),
            ),
        ),
        (
            "none fits: still one",
            "three/0",
            1,
            (
                vec![stamped(shown("three"), Some("two/0"))],
                Some(id("two/0")),
            ),
        ),
        (
            "two entries",
            "three/0",
            two + three,
            (
                vec![
                    stamped(shown("two"), Some("one/0")),
                    stamped(shown("three"), Some("two/0")),
                ],
                Some(id("one/0")),
            ),
        ),
        (
            "the start",
            "one/0",
            u64::MAX,
            (vec![stamped(shown("one"), None)], None),
        ),
        (
            "all",
            "three/0",
            u64::MAX,
            (
                vec![
                    stamped(shown("one"), None),
                    stamped(shown("two"), Some("one/0")),
                    stamped(shown("three"), Some("two/0")),
                ],
                None,
            ),
        ),
    ] {
        let response = feed
            .history(&id(through), max_bytes, /*deferral*/ None)
            .unwrap()
            .render();
        assert_eq!(page(response), expected, "{name}");
    }
    let Err(moved) = feed.history(&id("gone/0"), u64::MAX, /*deferral*/ None) else {
        panic!("a page through an entry the conversation does not hold");
    };
    assert_eq!(moved.error_code(), Some(ErrorCode::ConversationMoved));
}

// Covers: a page cuts its oldest entries until the rest fits one message,
// leaving out the newest entry at a cut where several meet, and an entry
// too large for a message alone comes as a notice after the entry before
// it. A page past the limit would close the client's connection.
// Owner: daemon session feed history
#[test]
fn a_page_fits_one_message() {
    let limit = super::PAGE_LIMIT;
    let cut = |index, entry: &str| Boundary {
        index,
        entry: id(entry),
    };
    // Entries "a", "b" and "c" of one event each; "x", which has none,
    // meets "a" where "b" starts.
    let cuts = [cut(1, "a"), cut(1, "x"), cut(2, "b")];
    let page = |sizes: [usize; 3]| {
        ["a", "b", "c"]
            .into_iter()
            .zip(sizes)
            .map(|(text, bytes)| (stamped(notice(text), None), bytes))
            .collect()
    };
    for (name, sizes, expected) in [
        (
            "all fit",
            [1, 1, 1],
            (
                vec![notice("a"), notice("b"), notice("c")],
                Some(id("before")),
            ),
        ),
        (
            "the oldest cut",
            [limit, 1, 1],
            (vec![notice("b"), notice("c")], Some(id("x"))),
        ),
        ("two cut", [1, limit, 1], (vec![notice("c")], Some(id("b")))),
    ] {
        let response = fit_message(page(sizes), &cuts, Some(id("before")));
        let events = response
            .events
            .into_iter()
            .map(|event| event.event)
            .collect();
        assert_eq!((events, response.earlier), expected, "{name}");
    }

    let alone = fit_message(page([1, 1, limit]), &cuts, None);
    assert_eq!(
        (
            alone.events.len(),
            matches!(alone.events[0].event, wire::SessionEvent::Notice { .. }),
            alone.events[0].after_entry.clone(),
            alone.earlier,
        ),
        (1, true, Some(id("b")), Some(id("b"))),
        "an entry too large alone"
    );
}