rho-coding-agent 3.10.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use pretty_assertions::assert_eq;
use ratatui::{backend::CrosstermBackend, buffer::Cell};
use regex::Regex;

use super::{draw_linked, ScreenLink};

/// The text `cells` write with `links`, with cursor moves and colors
/// stripped, so only the printed text and OSC 8 sequences remain.
fn written(cells: &[(u16, u16, &'static str)], links: &[ScreenLink]) -> String {
    let cells: Vec<(u16, u16, Cell)> = cells
        .iter()
        .map(|&(x, y, symbol)| (x, y, Cell::new(symbol)))
        .collect();
    let mut bytes = Vec::new();
    let refs = cells.iter().map(|(x, y, cell)| (*x, *y, cell)).collect();
    draw_linked(&mut CrosstermBackend::new(&mut bytes), refs, links).expect("draw");
    let bytes = String::from_utf8(bytes).expect("utf8");
    let csi = Regex::new(r"\x1b\[[0-9;?]*[A-Za-z]").expect("pattern");
    csi.replace_all(&bytes, "").into_owned()
}

/// Linked runs, and only they, are wrapped in OSC 8, and a URL holding a
/// control character, which could end the sequence early, links nothing.
#[test]
fn linked_runs_are_wrapped_in_osc8() {
    let cells = [
        (0, 0, "x"),
        (1, 0, "a"),
        (2, 0, "b"),
        (3, 0, "y"),
        (1, 1, "c"),
    ];
    let link = |y, x, url: &str| ScreenLink {
        y,
        x,
        url: url.into(),
    };

    let links = [
        link(0, 1..3, "file://h/w/a.rs"),
        link(1, 1..2, "file://h/w/c.rs"),
    ];
    assert_eq!(
        written(&cells, &links),
        "x\x1b]8;;file://h/w/a.rs\x1b\\ab\x1b]8;;\x1b\\y\
         \x1b]8;;file://h/w/c.rs\x1b\\c\x1b]8;;\x1b\\"
    );

    let hostile = [link(0, 1..3, "file://h/\x1b]8;;evil")];
    assert_eq!(written(&cells, &hostile), "xabyc");
}

/// Takes every write, except one holding `poison`, which fails.
struct FailsOn {
    poison: &'static [u8],
    bytes: Vec<u8>,
}

impl std::io::Write for FailsOn {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        if buf
            .windows(self.poison.len())
            .any(|window| window == self.poison)
        {
            return Err(std::io::Error::other("write failed"));
        }
        self.bytes.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// A linked run whose draw fails still closes its link, so the open link
/// cannot swallow later output, and the draw's error is what returns.
#[test]
fn a_failed_draw_still_closes_its_link() {
    let cell = Cell::new("Z");
    let link = ScreenLink {
        y: 0,
        x: 0..1,
        url: "file://h/w/a.rs".into(),
    };
    let mut writer = FailsOn {
        poison: b"Z",
        bytes: Vec::new(),
    };
    let error = draw_linked(
        &mut CrosstermBackend::new(&mut writer),
        vec![(0, 0, &cell)],
        &[link],
    )
    .expect_err("the draw fails");
    assert_eq!(error.to_string(), "write failed");
    let bytes = String::from_utf8(writer.bytes).expect("utf8");
    assert!(bytes.ends_with("\x1b]8;;\x1b\\"), "{bytes:?}");
}