rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
use std::fs;

use pretty_assertions::assert_eq;
use ratatui::text::Line;

use super::{render_markdown_linked, CodeFenceState};
use crate::tui::{
    path_links::{PathIndex, PathLinker},
    text_links::RowLink,
};

/// The text each link covers on its row.
fn linked_texts(lines: &[Line<'_>], links: &[RowLink]) -> Vec<(usize, String)> {
    links
        .iter()
        .map(|link| {
            let row: String = lines[link.row]
                .spans
                .iter()
                .map(|span| span.content.as_ref())
                .collect();
            let text = row
                .chars()
                .skip(link.columns.start)
                .take(link.columns.len())
                .collect();
            (link.row, text)
        })
        .collect()
}

/// Links land on exactly their path's text in prose, a wrapped list item,
/// and a table cell; a fenced code block links nothing.
#[test]
fn links_cover_their_text_in_prose_lists_and_tables() {
    let workspace = tempfile::tempdir().expect("tempdir");
    fs::create_dir_all(workspace.path().join("src")).expect("mkdir");
    fs::write(workspace.path().join("src/a.rs"), "").expect("write");
    let mut index = PathIndex::new(workspace.path().to_path_buf());
    index.want(["src/a.rs"]);
    let check = index.take_check().expect("queued");
    index.apply(check.run());

    let text = "\
See `src/a.rs:4` now.
- a list item long enough to wrap before src/a.rs
| file | note |
| --- | --- |
| src/a.rs | x |
```
src/a.rs
```";
    let mut linker = PathLinker::new(&index);
    let rendered = render_markdown_linked(text, 30, &mut CodeFenceState::default(), &mut linker);
    let linked = linked_texts(&rendered.lines, &rendered.links);
    let texts: Vec<&str> = linked.iter().map(|(_, text)| text.as_str()).collect();
    assert_eq!(texts, ["src/a.rs:4", "src/a.rs", "src/a.rs"]);
    // The list item's mention sits on its wrapped continuation row.
    let rows: Vec<usize> = linked.iter().map(|(row, _)| *row).collect();
    assert!(rows[1] > rows[0] + 1, "{rows:?}");
    assert!(linker.into_unlinked().is_empty());
}

/// With icons on, a table sizes its columns to the cells as painted: every
/// row keeps the border width and the linked cell stays on one row.
#[test]
fn icons_widen_table_columns_instead_of_wrapping_cells() {
    let workspace = tempfile::tempdir().expect("tempdir");
    fs::create_dir_all(workspace.path().join("src")).expect("mkdir");
    fs::write(workspace.path().join("src/a.rs"), "").expect("write");
    let mut index = PathIndex::new(workspace.path().to_path_buf());
    index.want(["src/a.rs"]);
    let check = index.take_check().expect("queued");
    index.apply(check.run());

    let text = "| file | note |\n| --- | --- |\n| src/a.rs | x |";
    let mut linker = PathLinker::new(&index).with_icons();
    let rendered = render_markdown_linked(text, 40, &mut CodeFenceState::default(), &mut linker);
    let rows: Vec<String> = rendered
        .lines
        .iter()
        .map(|line| {
            line.spans
                .iter()
                .map(|span| span.content.as_ref())
                .collect()
        })
        .collect();
    let widths: Vec<usize> = rows.iter().map(|row| row.chars().count()).collect();
    assert!(widths.iter().all(|width| *width == widths[0]), "{rows:#?}");
    // Borders, header, separator, one body row, border.
    assert_eq!(rows.len(), 5, "{rows:#?}");
    assert!(rows[3].contains("\u{e7a8}\u{a0}src/a.rs"), "{rows:#?}");
}