rho-coding-agent 3.10.0

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

use pretty_assertions::assert_eq;

use super::{prose_mentions, whole_mention, Mention};

const WORKSPACE: &str = "/work/repo";

/// What each written word reads as: its workspace path and the bytes that
/// are the path, or `None` when it is not a workspace file mention.
#[test]
fn whole_mentions_follow_rho_ui_rules() {
    let cases: &[(&str, Option<(&str, usize)>)] = &[
        ("src/main.rs", Some(("src/main.rs", 11))),
        ("src/main.rs:42", Some(("src/main.rs", 11))),
        ("src/main.rs:42:7", Some(("src/main.rs", 11))),
        ("src/main.rs:1-40", Some(("src/main.rs", 11))),
        ("main.rs", Some(("main.rs", 7))),
        (".gitignore", Some((".gitignore", 10))),
        ("src/main.rs.", Some(("src/main.rs.", 12))),
        ("src/main", Some(("src/main", 8))),
        // `..` stays for the check to resolve on disk, past any symlink.
        ("./src/../lib.rs", Some(("src/../lib.rs", 15))),
        ("src/..", None),
        // Only Windows has drives.
        ("C:/work/repo/a.rs", None),
        ("/work/repo/src/lib.rs:3", Some(("src/lib.rs", 21))),
        ("/etc/hosts", None),
        ("/work/repository/a.rs", None),
        ("~/notes.md", None),
        ("../outside.rs", None),
        ("Makefile", None),
        ("v1.2", None),
        ("src/main.rs and", None),
        ("https://example.com/a.rs", None),
    ];
    for &(written, expected) in cases {
        let mention = whole_mention(written, Path::new(WORKSPACE));
        let actual = mention.map(|mention| (mention.path, mention.path_len));
        let expected = expected.map(|(path, len)| (path.to_owned(), len));
        assert_eq!(actual, expected, "{written}");
    }
}

/// On Windows a mention may start with a drive, in either case, and use
/// `\` between names; the drive's colon is not a position.
#[cfg(windows)]
#[test]
fn windows_mentions_take_drives_and_backslashes() {
    let workspace = Path::new(r"C:\Users\me");
    let cases: &[(&str, Option<(&str, usize)>)] = &[
        (
            r"C:\Users\me\.rho\config.toml:3",
            Some((".rho/config.toml", 28)),
        ),
        ("C:/Users/me/src/a.rs", Some(("src/a.rs", 20))),
        ("c:/Users/me/src/a.rs:4", Some(("src/a.rs", 20))),
        (r"src\a.rs:4", Some(("src/a.rs", 8))),
        (r"D:\Users\me\a.rs", None),
        (r"C:\Users\other\a.rs", None),
    ];
    for &(written, expected) in cases {
        let mention = whole_mention(written, workspace);
        let actual = mention.map(|mention| (mention.path, mention.path_len));
        let expected = expected.map(|(path, len)| (path.to_owned(), len));
        assert_eq!(actual, expected, "{written}");
    }
}

/// Prose mentions keep their place in the text and drop a sentence's period
/// and the file picker's `@`.
#[test]
fn prose_mentions_keep_ranges_and_drop_trailing_periods() {
    let text = "Edit src/a.rs:4, then read docs/guide.md. Not v1.2 or words. Open @src/c.rs.";
    let mentions: Vec<Mention> = prose_mentions(text, Path::new(WORKSPACE)).collect();
    assert_eq!(
        mentions,
        vec![
            Mention {
                range: 5..15,
                path_len: 8,
                path: "src/a.rs".into(),
            },
            Mention {
                range: 27..40,
                path_len: 13,
                path: "docs/guide.md".into(),
            },
            Mention {
                range: 67..75,
                path_len: 8,
                path: "src/c.rs".into(),
            },
        ]
    );
}