summaryrefslogtreecommitdiffstats
path: root/tests/tools.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tools.rs')
-rw-r--r--tests/tools.rs68
1 files changed, 42 insertions, 26 deletions
diff --git a/tests/tools.rs b/tests/tools.rs
index 0321bf8..a5099e8 100644
--- a/tests/tools.rs
+++ b/tests/tools.rs
@@ -1,3 +1,10 @@
1/*
2 * This file is part of agent686
3 * Copyright (C) 2026 Salmonik <salmon@2137697.xyz>
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 * https://git.2137697.xyz/agent686
6 */
7
1use std::fs; 8use std::fs;
2use std::path::PathBuf; 9use std::path::PathBuf;
3use std::time::Duration; 10use std::time::Duration;
@@ -24,9 +31,12 @@ fn edit_refuses_ambiguous_and_missing_text() {
24 let path = dir.join("a.txt"); 31 let path = dir.join("a.txt");
25 fs::write(&path, "alpha\nbeta\nalpha\n").unwrap(); 32 fs::write(&path, "alpha\nbeta\nalpha\n").unwrap();
26 33
27 let error = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "gamma" })) 34 let error = tools::plan(
28 .err() 35 "edit",
29 .expect("ambiguous edit must fail"); 36 &json!({ "path": path, "old": "alpha", "new": "gamma" }),
37 )
38 .err()
39 .expect("ambiguous edit must fail");
30 assert!(error.contains("appears 2 times"), "{error}"); 40 assert!(error.contains("appears 2 times"), "{error}");
31 assert!(error.contains("lines 1, 3"), "{error}"); 41 assert!(error.contains("lines 1, 3"), "{error}");
32 42
@@ -128,15 +138,23 @@ fn bash_reports_exit_code_and_times_out() {
128 assert!(text.contains("hi"), "{text}"); 138 assert!(text.contains("hi"), "{text}");
129 assert!(text.contains("[exit 3]"), "{text}"); 139 assert!(text.contains("[exit 3]"), "{text}");
130 140
131 let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150), &AtomicBool::new(false)).unwrap(); 141 let text = shell::bash(
142 &json!({ "cmd": "sleep 5" }),
143 Duration::from_millis(150),
144 &AtomicBool::new(false),
145 )
146 .unwrap();
132 assert!(text.contains("timed out after"), "{text}"); 147 assert!(text.contains("timed out after"), "{text}");
133} 148}
134 149
135#[test] 150#[test]
136fn bash_rejects_a_missing_cwd() { 151fn bash_rejects_a_missing_cwd() {
137 let error = run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) 152 let error = run(
138 .err() 153 "bash",
139 .expect("missing cwd must fail"); 154 &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" }),
155 )
156 .err()
157 .expect("missing cwd must fail");
140 assert!(error.contains("cwd is not a directory"), "{error}"); 158 assert!(error.contains("cwd is not a directory"), "{error}");
141} 159}
142 160
@@ -210,11 +228,7 @@ fn read_handles_a_missing_trailing_newline() {
210#[test] 228#[test]
211fn grep_uses_regular_expressions_by_default() { 229fn grep_uses_regular_expressions_by_default() {
212 let dir = temp_dir("grep-regex"); 230 let dir = temp_dir("grep-regex");
213 fs::write( 231 fs::write(dir.join("f.rs"), "fn main() {\n fn other() {\n}\n").unwrap();
214 dir.join("f.rs"),
215 "fn main() {\n fn other() {\n}\n",
216 )
217 .unwrap();
218 232
219 let text = run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); 233 let text = run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap();
220 assert_eq!(text.lines().count(), 1, "{text}"); 234 assert_eq!(text.lines().count(), 1, "{text}");
@@ -283,7 +297,11 @@ fn bash_timeout_kills_the_whole_process_group() {
283 assert!(text.contains("timed out"), "{text}"); 297 assert!(text.contains("timed out"), "{text}");
284 298
285 std::thread::sleep(Duration::from_millis(300)); 299 std::thread::sleep(Duration::from_millis(300));
286 let pid: i32 = fs::read_to_string(&pidfile).unwrap().trim().parse().unwrap(); 300 let pid: i32 = fs::read_to_string(&pidfile)
301 .unwrap()
302 .trim()
303 .parse()
304 .unwrap();
287 assert!(!running(pid), "grandchild {pid} survived the timeout"); 305 assert!(!running(pid), "grandchild {pid} survived the timeout");
288} 306}
289 307
@@ -309,7 +327,10 @@ fn bash_does_not_inherit_the_api_key() {
309 ) 327 )
310 .unwrap(); 328 .unwrap();
311 std::env::remove_var("OPENCODE_API_KEY"); 329 std::env::remove_var("OPENCODE_API_KEY");
312 assert!(text.contains('0'), "the key was visible to the child: {text}"); 330 assert!(
331 text.contains('0'),
332 "the key was visible to the child: {text}"
333 );
313} 334}
314 335
315#[test] 336#[test]
@@ -342,8 +363,11 @@ fn edit_refuses_when_the_file_changed_while_waiting_for_approval() {
342 let path = dir.join("f.txt"); 363 let path = dir.join("f.txt");
343 fs::write(&path, "alpha\n").unwrap(); 364 fs::write(&path, "alpha\n").unwrap();
344 365
345 let change = tools::plan("edit", &json!({ "path": path, "old": "alpha", "new": "beta" })) 366 let change = tools::plan(
346 .unwrap(); 367 "edit",
368 &json!({ "path": path, "old": "alpha", "new": "beta" }),
369 )
370 .unwrap();
347 // Something else (cargo fmt, the user's editor) ran before approval. 371 // Something else (cargo fmt, the user's editor) ran before approval.
348 fs::write(&path, "alpha\nalpha\n").unwrap(); 372 fs::write(&path, "alpha\nalpha\n").unwrap();
349 373
@@ -470,11 +494,7 @@ fn bash_stderr_alone_is_labelled() {
470#[test] 494#[test]
471fn bash_honours_a_per_call_timeout() { 495fn bash_honours_a_per_call_timeout() {
472 let started = std::time::Instant::now(); 496 let started = std::time::Instant::now();
473 let text = run( 497 let text = run("bash", &json!({ "cmd": "sleep 5", "timeout_seconds": 1 })).unwrap();
474 "bash",
475 &json!({ "cmd": "sleep 5", "timeout_seconds": 1 }),
476 )
477 .unwrap();
478 assert!(text.contains("timed out after 1.0s"), "{text}"); 498 assert!(text.contains("timed out after 1.0s"), "{text}");
479 assert!(started.elapsed() < std::time::Duration::from_secs(4)); 499 assert!(started.elapsed() < std::time::Duration::from_secs(4));
480} 500}
@@ -507,11 +527,7 @@ fn validate_requires_content_and_new_but_allows_empty() {
507fn a_pipeline_that_exceeds_the_file_limit_is_reported() { 527fn a_pipeline_that_exceeds_the_file_limit_is_reported() {
508 // 70 MiB through a pipeline: the grandchild (head) is the writer, so only the 528 // 70 MiB through a pipeline: the grandchild (head) is the writer, so only the
509 // file-length check can detect the limit. 529 // file-length check can detect the limit.
510 let text = run( 530 let text = run("bash", &json!({ "cmd": "yes | head -c 70000000" })).unwrap();
511 "bash",
512 &json!({ "cmd": "yes | head -c 70000000" }),
513 )
514 .unwrap();
515 assert!( 531 assert!(
516 text.contains("output exceeded 64 MiB"), 532 text.contains("output exceeded 64 MiB"),
517 "should report the file limit: {text}" 533 "should report the file limit: {text}"