rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use super::{command, Command, HeldButton};
use pretty_assertions::assert_eq;
use rho_browser::cdp::input::MouseButton;
use rho_browser::cdp::Command as _;
use rho_protocol::{
    BrowserInput, BrowserKeyAction, BrowserModifiers, BrowserMouseAction, BrowserMouseButton,
};
use serde_json::{json, Value};

fn mouse(
    action: BrowserMouseAction,
    button: Option<BrowserMouseButton>,
    clicks: u32,
) -> BrowserInput {
    BrowserInput::Mouse {
        action,
        x: 10.0,
        y: 20.0,
        button,
        clicks,
        modifiers: BrowserModifiers::NONE,
    }
}

fn key(
    action: BrowserKeyAction,
    key: &str,
    text: Option<&str>,
    modifiers: BrowserModifiers,
) -> BrowserInput {
    BrowserInput::Key {
        action,
        key: key.into(),
        code: Some(format!("Key{}", key.to_uppercase())),
        text: text.map(str::to_owned),
        modifiers,
    }
}

/// The CDP method and params `command` sends.
fn sent(command: Command) -> (&'static str, Value) {
    match command {
        Command::Mouse(event) => (
            rho_browser::cdp::input::DispatchMouseEvent::METHOD,
            serde_json::to_value(event).unwrap(),
        ),
        Command::Key(event) => (
            rho_browser::cdp::input::DispatchKeyEvent::METHOD,
            serde_json::to_value(event).unwrap(),
        ),
        Command::Text(event) => (
            rho_browser::cdp::input::InsertText::METHOD,
            serde_json::to_value(event).unwrap(),
        ),
    }
}

// Covers: a person's input reaches the tab as the browser's input events
// want it: a press holds its button so the moves after it drag until the
// release, a press counts at least one click, the wheel scrolls by its
// deltas, a key types its text only with no modifier but shift held and
// carries its legacy key code so Enter and Backspace act, and composed
// text is inserted. Otherwise drags select nothing, shortcuts type
// letters, or editing keys do nothing.
// Owner: person driving input mapping
#[test]
fn a_persons_input_becomes_the_browsers_input_events() {
    let mouse_event = "Input.dispatchMouseEvent";
    let key_event = "Input.dispatchKeyEvent";
    let cases = [
        (
            "a press holds its button",
            mouse(
                BrowserMouseAction::Pressed,
                Some(BrowserMouseButton::Left),
                0,
            ),
            None,
            (
                mouse_event,
                json!({"type": "mousePressed", "x": 10.0, "y": 20.0, "button": "left", "clickCount": 1}),
            ),
            Some(MouseButton::Left),
        ),
        (
            "a move while held drags",
            mouse(BrowserMouseAction::Moved, None, 0),
            Some(MouseButton::Left),
            (
                mouse_event,
                json!({"type": "mouseMoved", "x": 10.0, "y": 20.0, "button": "left"}),
            ),
            Some(MouseButton::Left),
        ),
        (
            "a release lets go",
            mouse(
                BrowserMouseAction::Released,
                Some(BrowserMouseButton::Left),
                2,
            ),
            Some(MouseButton::Left),
            (
                mouse_event,
                json!({"type": "mouseReleased", "x": 10.0, "y": 20.0, "button": "left", "clickCount": 2}),
            ),
            None,
        ),
        (
            "the wheel",
            BrowserInput::Wheel {
                x: 1.0,
                y: 2.0,
                dx: 0.0,
                dy: 120.0,
                modifiers: BrowserModifiers::NONE,
            },
            None,
            (
                mouse_event,
                json!({"type": "mouseWheel", "x": 1.0, "y": 2.0, "deltaX": 0.0, "deltaY": 120.0}),
            ),
            None,
        ),
        (
            "a key that types",
            key(
                BrowserKeyAction::Down,
                "a",
                Some("a"),
                BrowserModifiers::NONE.with_shift(),
            ),
            None,
            (
                key_event,
                json!({"type": "keyDown", "key": "a", "code": "KeyA", "text": "a", "windowsVirtualKeyCode": 65, "modifiers": 8}),
            ),
            None,
        ),
        (
            "a shortcut types nothing",
            key(
                BrowserKeyAction::Down,
                "a",
                Some("a"),
                BrowserModifiers::NONE.with_ctrl(),
            ),
            None,
            (
                key_event,
                json!({"type": "rawKeyDown", "key": "a", "code": "KeyA", "windowsVirtualKeyCode": 65, "modifiers": 2}),
            ),
            None,
        ),
        (
            "an editing key carries its key code",
            key(BrowserKeyAction::Up, "Enter", None, BrowserModifiers::NONE),
            None,
            (
                key_event,
                json!({"type": "keyUp", "key": "Enter", "code": "KeyENTER", "windowsVirtualKeyCode": 13}),
            ),
            None,
        ),
        (
            "composed text",
            BrowserInput::Text {
                text: "日本".into(),
            },
            None,
            ("Input.insertText", json!({"text": "日本"})),
            None,
        ),
    ];
    for (case, input, held_before, expected, held_after) in cases {
        let mut held = held_before;
        let command = command(input, &mut held);
        assert_eq!((sent(command), held), (expected, held_after), "{case}");
    }
}

// Covers: a mouse button a person pressed is let go when the browser is
// taken over again, as when their client left between the press and its
// release, so the next person's plain moves do not drag.
// Owner: person driving input mapping
#[test]
fn a_held_button_ends_with_the_takeover_it_was_pressed_under() {
    let mut held = HeldButton::default();
    *held.under(1) = Some(MouseButton::Left);
    let same_takeover = *held.under(1);
    let next_takeover = *held.under(2);

    assert_eq!(
        (same_takeover, next_takeover),
        (Some(MouseButton::Left), None)
    );
}