ralph-coder 0.2.1

An agentic code generation CLI powered by multiple LLM backends
Documentation
/// File tool tests — all operations in a TempDir sandbox.
use ralph::tools::file_tools;
use tempfile::TempDir;

#[test]
fn write_and_read_file() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("hello.txt");

    file_tools::write_file(&path, "hello world\n").unwrap();
    let content = file_tools::read_file(&path).unwrap();
    assert_eq!(content, "hello world\n");
}

#[test]
fn read_missing_file_errors() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("nonexistent.txt");
    assert!(file_tools::read_file(&path).is_err());
}

#[test]
fn write_creates_parent_dirs() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("a").join("b").join("c").join("file.txt");
    file_tools::write_file(&path, "deep").unwrap();
    assert!(path.exists());
}

#[test]
fn edit_file_replaces_first_occurrence() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("code.rs");
    file_tools::write_file(&path, "fn foo() {}\nfn foo_bar() {}").unwrap();
    file_tools::edit_file(&path, "fn foo()", "fn baz()").unwrap();
    let content = file_tools::read_file(&path).unwrap();
    assert!(content.contains("fn baz()"));
    assert!(content.contains("fn foo_bar()"));
    assert!(!content.contains("fn foo()"));
}

#[test]
fn edit_file_missing_old_string_errors() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("code.rs");
    file_tools::write_file(&path, "fn main() {}").unwrap();
    let result = file_tools::edit_file(&path, "fn missing()", "fn replacement()");
    assert!(result.is_err());
}

#[test]
fn delete_file_works() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("delete_me.txt");
    file_tools::write_file(&path, "bye").unwrap();
    assert!(path.exists());
    file_tools::delete_file(&path).unwrap();
    assert!(!path.exists());
}

#[test]
fn delete_missing_file_errors() {
    let dir = TempDir::new().unwrap();
    let path = dir.path().join("ghost.txt");
    assert!(file_tools::delete_file(&path).is_err());
}

#[test]
fn list_dir_returns_files() {
    let dir = TempDir::new().unwrap();
    file_tools::write_file(&dir.path().join("a.txt"), "a").unwrap();
    file_tools::write_file(&dir.path().join("b.txt"), "b").unwrap();
    let listing = file_tools::list_dir(dir.path()).unwrap();
    assert!(listing.contains("a.txt"));
    assert!(listing.contains("b.txt"));
}

#[test]
fn list_dir_missing_errors() {
    let dir = TempDir::new().unwrap();
    let missing = dir.path().join("nonexistent");
    assert!(file_tools::list_dir(&missing).is_err());
}