rpi-cli 0.36.1

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
Documentation
//! OSC 52 clipboard fallback: sequence encoding, size limit, and the
//! remote/headless session conditions that decide when it applies.
//!
//! Mirrors upstream's `clipboard.ts` contract: remote sessions (SSH markers or
//! `PI_CLIPBOARD_OSC52=1`) and display-less Linux fall back to OSC 52, with a
//! 100_000-character base64 payload cap.

use super::*;

#[test]
fn osc52_sequence_encodes_text() {
    let sequence = osc52_sequence("hi", DEFAULT_OSC52_MAX_ENCODED_LENGTH).unwrap();
    assert_eq!(sequence, "\x1b]52;c;aGk=\x07");
}

#[test]
fn osc52_sequence_encodes_multibyte_text() {
    let sequence = osc52_sequence("你好", DEFAULT_OSC52_MAX_ENCODED_LENGTH).unwrap();
    assert_eq!(sequence, "\x1b]52;c;5L2g5aW9\x07");
}

#[test]
fn osc52_sequence_rejects_oversized_payload() {
    let limit = DEFAULT_OSC52_MAX_ENCODED_LENGTH;
    assert!(osc52_sequence(&"a".repeat(75_000), limit).is_some());
    assert!(osc52_sequence(&"a".repeat(75_001), limit).is_none());
}

#[test]
fn osc52_limit_is_configurable_via_env() {
    let _guard = crate::config::test_support::env_lock().lock().unwrap();
    let prev = std::env::var_os("RPI_OSC52_MAX_ENCODED_LENGTH");
    let restore = || match &prev {
        Some(value) => std::env::set_var("RPI_OSC52_MAX_ENCODED_LENGTH", value),
        None => std::env::remove_var("RPI_OSC52_MAX_ENCODED_LENGTH"),
    };

    // A smaller cap rejects payloads the default accepts.
    std::env::set_var("RPI_OSC52_MAX_ENCODED_LENGTH", "4");
    let limit = osc52_max_encoded_length();
    assert_eq!(limit, 4);
    assert!(osc52_sequence("hello", limit).is_none());
    assert!(osc52_sequence("ab", limit).is_some());

    // `0` disables the cap entirely.
    std::env::set_var("RPI_OSC52_MAX_ENCODED_LENGTH", "0");
    assert_eq!(osc52_max_encoded_length(), usize::MAX);
    assert!(osc52_sequence(&"a".repeat(200_000), osc52_max_encoded_length()).is_some());

    // Invalid values fall back to the default instead of erroring.
    std::env::set_var("RPI_OSC52_MAX_ENCODED_LENGTH", "not-a-number");
    assert_eq!(osc52_max_encoded_length(), DEFAULT_OSC52_MAX_ENCODED_LENGTH);
    std::env::set_var("RPI_OSC52_MAX_ENCODED_LENGTH", "-1");
    assert_eq!(osc52_max_encoded_length(), DEFAULT_OSC52_MAX_ENCODED_LENGTH);

    restore();
}

#[test]
fn remote_session_detected_from_ssh_markers() {
    let _guard = crate::config::test_support::env_lock().lock().unwrap();
    let prev = [
        ("SSH_CONNECTION", std::env::var_os("SSH_CONNECTION")),
        ("SSH_CLIENT", std::env::var_os("SSH_CLIENT")),
        ("MOSH_CONNECTION", std::env::var_os("MOSH_CONNECTION")),
        ("PI_CLIPBOARD_OSC52", std::env::var_os("PI_CLIPBOARD_OSC52")),
    ];
    for (key, _) in prev.iter() {
        std::env::remove_var(key);
    }
    assert!(
        !is_remote_session(),
        "no SSH markers and no force flag means local session"
    );

    std::env::set_var("SSH_CONNECTION", "10.0.0.1 22 10.0.0.2 5000");
    assert!(is_remote_session(), "SSH_CONNECTION marks a remote session");
    std::env::remove_var("SSH_CONNECTION");

    std::env::set_var("PI_CLIPBOARD_OSC52", "1");
    assert!(
        is_remote_session(),
        "PI_CLIPBOARD_OSC52=1 forces the remote path"
    );
    std::env::set_var("PI_CLIPBOARD_OSC52", "0");
    assert!(
        !is_remote_session(),
        "only PI_CLIPBOARD_OSC52=1 forces the remote path"
    );

    for (key, value) in prev.iter() {
        match value {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }
}

#[cfg(target_os = "linux")]
#[test]
fn headless_linux_detected_without_display() {
    let _guard = crate::config::test_support::env_lock().lock().unwrap();
    let prev = [
        ("DISPLAY", std::env::var_os("DISPLAY")),
        ("WAYLAND_DISPLAY", std::env::var_os("WAYLAND_DISPLAY")),
        ("TERMUX_VERSION", std::env::var_os("TERMUX_VERSION")),
    ];
    for (key, _) in prev.iter() {
        std::env::remove_var(key);
    }
    assert!(
        is_headless_linux(),
        "no display variables on Linux means headless"
    );

    std::env::set_var("WAYLAND_DISPLAY", "wayland-0");
    assert!(!is_headless_linux(), "a Wayland display rules out headless");

    std::env::remove_var("WAYLAND_DISPLAY");
    std::env::set_var("DISPLAY", ":0");
    assert!(!is_headless_linux(), "an X11 display rules out headless");

    for (key, value) in prev.iter() {
        match value {
            Some(value) => std::env::set_var(key, value),
            None => std::env::remove_var(key),
        }
    }
}

#[test]
fn emit_osc52_degrades_without_a_tty() {
    // Tests run with piped/captured stdout; the TTY guard must refuse to
    // write escape sequences there rather than corrupting the stream.
    let terminal = ProcessTerminal::new();
    assert!(!emit_osc52(
        &terminal,
        "tty-guard probe",
        DEFAULT_OSC52_MAX_ENCODED_LENGTH
    ));
}