rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::{Pacer, Step};
use pretty_assertions::assert_eq;

/// What reaches a watch: a frame the browser drew, acked by its own name,
/// or the client's ack of a frame by its seq.
enum Input {
    Frame(&'static str),
    Ack(u64),
}

use Input::{Ack, Frame};

fn send(frame: &'static str, seq: u64) -> Step<&'static str, &'static str> {
    Step::Send { frame, seq }
}

fn ack_browser(frame: &'static str) -> Step<&'static str, &'static str> {
    Step::AckBrowser(frame)
}

// Covers: a watch sends frames two ahead of its client's acks and lets the
// browser draw more only while the client has room for them; a frame
// drawn past that waits in place of an older waiting one, so the next ack
// brings the newest, and the browser gets every frame's ack back, the
// replaced ones' too; an ack covers the frames before it, and one for a
// frame not yet sent, or older than the last, sends nothing extra. Without
// it a slow link queues stale frames, the browser draws fewer frames for
// the rest of the watch once the client fell behind, or a page stops
// updating while the client waits on a frame it will never get.
// Owner: browser watch pacing
#[test]
fn a_watch_sends_frames_two_ahead_of_acks_and_the_newest_when_behind() {
    let cases = [
        (
            "two frames in flight hold the browser's ack until the client acks",
            vec![
                (Frame("a"), vec![send("a", 1), ack_browser("a")]),
                (Frame("b"), vec![send("b", 2)]),
                (Ack(1), vec![ack_browser("b")]),
                (Frame("c"), vec![send("c", 3)]),
            ],
        ),
        (
            "a frame past the budget waits, the newest replacing the older",
            vec![
                (Frame("a"), vec![send("a", 1), ack_browser("a")]),
                (Frame("b"), vec![send("b", 2)]),
                (Frame("c"), vec![]),
                (Frame("d"), vec![]),
                (
                    Ack(2),
                    vec![
                        send("d", 3),
                        ack_browser("b"),
                        ack_browser("c"),
                        ack_browser("d"),
                    ],
                ),
            ],
        ),
        (
            "an ack covers the frames before it; future and stale acks add nothing",
            vec![
                (Frame("a"), vec![send("a", 1), ack_browser("a")]),
                (Frame("b"), vec![send("b", 2)]),
                (Ack(5), vec![ack_browser("b")]),
                (Ack(1), vec![]),
                (Frame("c"), vec![send("c", 3), ack_browser("c")]),
            ],
        ),
        (
            "a page that stopped changing owes the browser nothing",
            vec![
                (Frame("a"), vec![send("a", 1), ack_browser("a")]),
                (Ack(1), vec![]),
            ],
        ),
    ];
    for (case, inputs) in cases {
        let mut pacer = Pacer::default();
        let (inputs, expected): (Vec<_>, Vec<_>) = inputs.into_iter().unzip();
        let taken: Vec<_> = inputs
            .into_iter()
            .map(|input| match input {
                Frame(frame) => pacer.frame(frame, frame),
                Ack(seq) => pacer.acked(seq),
            })
            .collect();
        assert_eq!(taken, expected, "{case}");
    }
}