summaryrefslogtreecommitdiffstats
path: root/tests/tools.rs
blob: 0189af84d1bed9884bc1960d6c52fcee25fef5a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fs;
use std::path::PathBuf;
use std::time::Duration;

use serde_json::json;

use agent686::tools::{self, shell};

fn temp_dir(tag: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!("agent686-test-{tag}-{}", std::process::id()));
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).unwrap();
    dir
}

#[test]
fn edit_refuses_ambiguous_and_missing_text() {
    let dir = temp_dir("edit-ambiguous");
    let path = dir.join("a.txt");
    fs::write(&path, "alpha\nbeta\nalpha\n").unwrap();

    let error = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "gamma" }))
        .err()
        .expect("ambiguous edit must fail");
    assert!(error.contains("appears 2 times"), "{error}");
    assert!(error.contains("lines 1, 3"), "{error}");

    let error = tools::plan("edit", &json!({ "path": path, "old": "nope", "new": "x" }))
        .err()
        .expect("missing text must fail");
    assert!(error.contains("was not found"), "{error}");

    // The file is untouched by a refused plan.
    assert_eq!(fs::read_to_string(&path).unwrap(), "alpha\nbeta\nalpha\n");
}

#[test]
fn edit_applies_exactly_one_replacement() {
    let dir = temp_dir("edit-apply");
    let path = dir.join("main.c");
    fs::write(&path, "int main(void) {\n    return 0;\n}\n").unwrap();

    let change = tools::plan(
        "edit",
        &json!({ "path": path, "old": "    return 0;", "new": "    return 1;" }),
    )
    .expect("unique edit should plan");

    let preview = change.preview();
    assert!(preview.contains("-     return 0;"), "{preview}");
    assert!(preview.contains("+     return 1;"), "{preview}");

    let outcome = change.apply().expect("apply should succeed");
    assert!(outcome.contains("1 replacement at line 2"), "{outcome}");
    assert_eq!(
        fs::read_to_string(&path).unwrap(),
        "int main(void) {\n    return 1;\n}\n"
    );
}

#[test]
fn write_does_not_create_parent_directories() {
    let dir = temp_dir("write-parent");
    let path = dir.join("missing").join("file.txt");
    let change = tools::plan("write", &json!({ "path": path, "content": "hi" })).unwrap();
    let error = change.apply().err().expect("apply must fail");
    assert!(error.contains("parent directory does not exist"), "{error}");
}

#[test]
fn read_numbers_lines_and_reports_truncation() {
    let dir = temp_dir("read");
    let path = dir.join("f.txt");
    fs::write(&path, "one\ntwo\nthree\nfour\n").unwrap();

    let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap();
    assert!(text.contains("     1\tone"), "{text}");
    assert!(text.contains("     2\ttwo"), "{text}");
    assert!(text.contains("2 more line(s); continue with offset=3"), "{text}");
    assert!(!text.contains("three"), "{text}");
}

#[test]
fn grep_finds_lines_and_honours_case() {
    let dir = temp_dir("grep");
    let path = dir.join("f.txt");
    fs::write(&path, "alpha\nbeta\nALPHA\n").unwrap();

    let text = tools::run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap();
    assert_eq!(text.lines().count(), 1, "{text}");

    let text = tools::run(
        "grep",
        &json!({ "pattern": "alpha", "path": dir, "ignore_case": true }),
    )
    .unwrap();
    assert_eq!(text.lines().count(), 2, "{text}");
}

#[test]
fn glob_supports_double_star_and_bare_names() {
    let dir = temp_dir("glob");
    fs::create_dir_all(dir.join("sub/deep")).unwrap();
    fs::write(dir.join("a.rs"), "").unwrap();
    fs::write(dir.join("sub/b.rs"), "").unwrap();
    fs::write(dir.join("sub/deep/c.rs"), "").unwrap();
    fs::write(dir.join("sub/note.txt"), "").unwrap();

    let text = tools::run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap();
    assert_eq!(text.lines().count(), 3, "{text}");

    let text = tools::run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap();
    assert_eq!(text.lines().count(), 3, "{text}");

    let text = tools::run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap();
    assert_eq!(text.lines().count(), 1, "{text}");
    assert!(text.contains("b.rs"), "{text}");
}

#[test]
fn bash_reports_exit_code_and_times_out() {
    let text = tools::run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap();
    assert!(text.contains("hi"), "{text}");
    assert!(text.contains("[exit 3]"), "{text}");

    let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150)).unwrap();
    assert!(text.contains("timed out after"), "{text}");
}

#[test]
fn bash_rejects_a_missing_cwd() {
    let error = tools::run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" }))
        .err()
        .expect("missing cwd must fail");
    assert!(error.contains("cwd is not a directory"), "{error}");
}

#[test]
fn unknown_tool_is_an_error_not_a_panic() {
    let error = tools::run("teleport", &json!({})).err().unwrap();
    assert!(error.contains("unknown tool"), "{error}");
}