aboutsummaryrefslogtreecommitdiffstats
path: root/tests/tools.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tools.rs')
-rw-r--r--tests/tools.rs142
1 files changed, 142 insertions, 0 deletions
diff --git a/tests/tools.rs b/tests/tools.rs
new file mode 100644
index 0000000..0189af8
--- /dev/null
+++ b/tests/tools.rs
@@ -0,0 +1,142 @@
1use std::fs;
2use std::path::PathBuf;
3use std::time::Duration;
4
5use serde_json::json;
6
7use agent686::tools::{self, shell};
8
9fn temp_dir(tag: &str) -> PathBuf {
10 let dir = std::env::temp_dir().join(format!("agent686-test-{tag}-{}", std::process::id()));
11 let _ = fs::remove_dir_all(&dir);
12 fs::create_dir_all(&dir).unwrap();
13 dir
14}
15
16#[test]
17fn edit_refuses_ambiguous_and_missing_text() {
18 let dir = temp_dir("edit-ambiguous");
19 let path = dir.join("a.txt");
20 fs::write(&path, "alpha\nbeta\nalpha\n").unwrap();
21
22 let error = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "gamma" }))
23 .err()
24 .expect("ambiguous edit must fail");
25 assert!(error.contains("appears 2 times"), "{error}");
26 assert!(error.contains("lines 1, 3"), "{error}");
27
28 let error = tools::plan("edit", &json!({ "path": path, "old": "nope", "new": "x" }))
29 .err()
30 .expect("missing text must fail");
31 assert!(error.contains("was not found"), "{error}");
32
33 // The file is untouched by a refused plan.
34 assert_eq!(fs::read_to_string(&path).unwrap(), "alpha\nbeta\nalpha\n");
35}
36
37#[test]
38fn edit_applies_exactly_one_replacement() {
39 let dir = temp_dir("edit-apply");
40 let path = dir.join("main.c");
41 fs::write(&path, "int main(void) {\n return 0;\n}\n").unwrap();
42
43 let change = tools::plan(
44 "edit",
45 &json!({ "path": path, "old": " return 0;", "new": " return 1;" }),
46 )
47 .expect("unique edit should plan");
48
49 let preview = change.preview();
50 assert!(preview.contains("- return 0;"), "{preview}");
51 assert!(preview.contains("+ return 1;"), "{preview}");
52
53 let outcome = change.apply().expect("apply should succeed");
54 assert!(outcome.contains("1 replacement at line 2"), "{outcome}");
55 assert_eq!(
56 fs::read_to_string(&path).unwrap(),
57 "int main(void) {\n return 1;\n}\n"
58 );
59}
60
61#[test]
62fn write_does_not_create_parent_directories() {
63 let dir = temp_dir("write-parent");
64 let path = dir.join("missing").join("file.txt");
65 let change = tools::plan("write", &json!({ "path": path, "content": "hi" })).unwrap();
66 let error = change.apply().err().expect("apply must fail");
67 assert!(error.contains("parent directory does not exist"), "{error}");
68}
69
70#[test]
71fn read_numbers_lines_and_reports_truncation() {
72 let dir = temp_dir("read");
73 let path = dir.join("f.txt");
74 fs::write(&path, "one\ntwo\nthree\nfour\n").unwrap();
75
76 let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap();
77 assert!(text.contains(" 1\tone"), "{text}");
78 assert!(text.contains(" 2\ttwo"), "{text}");
79 assert!(text.contains("2 more line(s); continue with offset=3"), "{text}");
80 assert!(!text.contains("three"), "{text}");
81}
82
83#[test]
84fn grep_finds_lines_and_honours_case() {
85 let dir = temp_dir("grep");
86 let path = dir.join("f.txt");
87 fs::write(&path, "alpha\nbeta\nALPHA\n").unwrap();
88
89 let text = tools::run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap();
90 assert_eq!(text.lines().count(), 1, "{text}");
91
92 let text = tools::run(
93 "grep",
94 &json!({ "pattern": "alpha", "path": dir, "ignore_case": true }),
95 )
96 .unwrap();
97 assert_eq!(text.lines().count(), 2, "{text}");
98}
99
100#[test]
101fn glob_supports_double_star_and_bare_names() {
102 let dir = temp_dir("glob");
103 fs::create_dir_all(dir.join("sub/deep")).unwrap();
104 fs::write(dir.join("a.rs"), "").unwrap();
105 fs::write(dir.join("sub/b.rs"), "").unwrap();
106 fs::write(dir.join("sub/deep/c.rs"), "").unwrap();
107 fs::write(dir.join("sub/note.txt"), "").unwrap();
108
109 let text = tools::run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap();
110 assert_eq!(text.lines().count(), 3, "{text}");
111
112 let text = tools::run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap();
113 assert_eq!(text.lines().count(), 3, "{text}");
114
115 let text = tools::run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap();
116 assert_eq!(text.lines().count(), 1, "{text}");
117 assert!(text.contains("b.rs"), "{text}");
118}
119
120#[test]
121fn bash_reports_exit_code_and_times_out() {
122 let text = tools::run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap();
123 assert!(text.contains("hi"), "{text}");
124 assert!(text.contains("[exit 3]"), "{text}");
125
126 let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150)).unwrap();
127 assert!(text.contains("timed out after"), "{text}");
128}
129
130#[test]
131fn bash_rejects_a_missing_cwd() {
132 let error = tools::run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" }))
133 .err()
134 .expect("missing cwd must fail");
135 assert!(error.contains("cwd is not a directory"), "{error}");
136}
137
138#[test]
139fn unknown_tool_is_an_error_not_a_panic() {
140 let error = tools::run("teleport", &json!({})).err().unwrap();
141 assert!(error.contains("unknown tool"), "{error}");
142}