diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/sessions.rs | 6 | ||||
| -rw-r--r-- | tests/tools.rs | 106 |
2 files changed, 90 insertions, 22 deletions
diff --git a/tests/sessions.rs b/tests/sessions.rs index 8d17fc5..b445242 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs | |||
| @@ -35,6 +35,12 @@ fn sessions_create_list_and_replay() { | |||
| 35 | ) | 35 | ) |
| 36 | .unwrap(); | 36 | .unwrap(); |
| 37 | 37 | ||
| 38 | { | ||
| 39 | use std::os::unix::fs::PermissionsExt; | ||
| 40 | let mode = fs::metadata(alpha.path()).unwrap().permissions().mode() & 0o777; | ||
| 41 | assert_eq!(mode, 0o600, "sessions must not be group/world readable"); | ||
| 42 | } | ||
| 43 | |||
| 38 | assert!(session::exists("alpha")); | 44 | assert!(session::exists("alpha")); |
| 39 | assert!(!session::exists("nope")); | 45 | assert!(!session::exists("nope")); |
| 40 | 46 | ||
diff --git a/tests/tools.rs b/tests/tools.rs index 8463d8d..c30128e 100644 --- a/tests/tools.rs +++ b/tests/tools.rs | |||
| @@ -5,6 +5,11 @@ use std::time::Duration; | |||
| 5 | use serde_json::json; | 5 | use serde_json::json; |
| 6 | 6 | ||
| 7 | use agent686::tools::{self, shell}; | 7 | use agent686::tools::{self, shell}; |
| 8 | use std::sync::atomic::AtomicBool; | ||
| 9 | |||
| 10 | fn run(name: &str, args: &serde_json::Value) -> Result<String, String> { | ||
| 11 | tools::run(name, args, &AtomicBool::new(false)) | ||
| 12 | } | ||
| 8 | 13 | ||
| 9 | fn temp_dir(tag: &str) -> PathBuf { | 14 | fn temp_dir(tag: &str) -> PathBuf { |
| 10 | let dir = std::env::temp_dir().join(format!("agent686-test-{tag}-{}", std::process::id())); | 15 | let dir = std::env::temp_dir().join(format!("agent686-test-{tag}-{}", std::process::id())); |
| @@ -73,7 +78,7 @@ fn read_numbers_lines_and_reports_truncation() { | |||
| 73 | let path = dir.join("f.txt"); | 78 | let path = dir.join("f.txt"); |
| 74 | fs::write(&path, "one\ntwo\nthree\nfour\n").unwrap(); | 79 | fs::write(&path, "one\ntwo\nthree\nfour\n").unwrap(); |
| 75 | 80 | ||
| 76 | let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); | 81 | let text = run("read", &json!({ "path": path, "limit": 2 })).unwrap(); |
| 77 | assert!(text.contains(" 1\tone"), "{text}"); | 82 | assert!(text.contains(" 1\tone"), "{text}"); |
| 78 | assert!(text.contains(" 2\ttwo"), "{text}"); | 83 | assert!(text.contains(" 2\ttwo"), "{text}"); |
| 79 | assert!(text.contains("continue with offset=3"), "{text}"); | 84 | assert!(text.contains("continue with offset=3"), "{text}"); |
| @@ -86,10 +91,10 @@ fn grep_finds_lines_and_honours_case() { | |||
| 86 | let path = dir.join("f.txt"); | 91 | let path = dir.join("f.txt"); |
| 87 | fs::write(&path, "alpha\nbeta\nALPHA\n").unwrap(); | 92 | fs::write(&path, "alpha\nbeta\nALPHA\n").unwrap(); |
| 88 | 93 | ||
| 89 | let text = tools::run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap(); | 94 | let text = run("grep", &json!({ "pattern": "alpha", "path": dir })).unwrap(); |
| 90 | assert_eq!(text.lines().count(), 1, "{text}"); | 95 | assert_eq!(text.lines().count(), 1, "{text}"); |
| 91 | 96 | ||
| 92 | let text = tools::run( | 97 | let text = run( |
| 93 | "grep", | 98 | "grep", |
| 94 | &json!({ "pattern": "alpha", "path": dir, "ignore_case": true }), | 99 | &json!({ "pattern": "alpha", "path": dir, "ignore_case": true }), |
| 95 | ) | 100 | ) |
| @@ -106,30 +111,30 @@ fn glob_supports_double_star_and_bare_names() { | |||
| 106 | fs::write(dir.join("sub/deep/c.rs"), "").unwrap(); | 111 | fs::write(dir.join("sub/deep/c.rs"), "").unwrap(); |
| 107 | fs::write(dir.join("sub/note.txt"), "").unwrap(); | 112 | fs::write(dir.join("sub/note.txt"), "").unwrap(); |
| 108 | 113 | ||
| 109 | let text = tools::run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap(); | 114 | let text = run("glob", &json!({ "pattern": "**/*.rs", "path": dir })).unwrap(); |
| 110 | assert_eq!(text.lines().count(), 3, "{text}"); | 115 | assert_eq!(text.lines().count(), 3, "{text}"); |
| 111 | 116 | ||
| 112 | let text = tools::run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); | 117 | let text = run("glob", &json!({ "pattern": "*.rs", "path": dir })).unwrap(); |
| 113 | assert_eq!(text.lines().count(), 3, "{text}"); | 118 | assert_eq!(text.lines().count(), 3, "{text}"); |
| 114 | 119 | ||
| 115 | let text = tools::run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap(); | 120 | let text = run("glob", &json!({ "pattern": "sub/*.rs", "path": dir })).unwrap(); |
| 116 | assert_eq!(text.lines().count(), 1, "{text}"); | 121 | assert_eq!(text.lines().count(), 1, "{text}"); |
| 117 | assert!(text.contains("b.rs"), "{text}"); | 122 | assert!(text.contains("b.rs"), "{text}"); |
| 118 | } | 123 | } |
| 119 | 124 | ||
| 120 | #[test] | 125 | #[test] |
| 121 | fn bash_reports_exit_code_and_times_out() { | 126 | fn bash_reports_exit_code_and_times_out() { |
| 122 | let text = tools::run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap(); | 127 | let text = run("bash", &json!({ "cmd": "echo hi; exit 3" })).unwrap(); |
| 123 | assert!(text.contains("hi"), "{text}"); | 128 | assert!(text.contains("hi"), "{text}"); |
| 124 | assert!(text.contains("[exit 3]"), "{text}"); | 129 | assert!(text.contains("[exit 3]"), "{text}"); |
| 125 | 130 | ||
| 126 | let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150)).unwrap(); | 131 | let text = shell::bash(&json!({ "cmd": "sleep 5" }), Duration::from_millis(150), &AtomicBool::new(false)).unwrap(); |
| 127 | assert!(text.contains("timed out after"), "{text}"); | 132 | assert!(text.contains("timed out after"), "{text}"); |
| 128 | } | 133 | } |
| 129 | 134 | ||
| 130 | #[test] | 135 | #[test] |
| 131 | fn bash_rejects_a_missing_cwd() { | 136 | fn bash_rejects_a_missing_cwd() { |
| 132 | let error = tools::run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) | 137 | let error = run("bash", &json!({ "cmd": "true", "cwd": "/nonexistent-dir-xyz" })) |
| 133 | .err() | 138 | .err() |
| 134 | .expect("missing cwd must fail"); | 139 | .expect("missing cwd must fail"); |
| 135 | assert!(error.contains("cwd is not a directory"), "{error}"); | 140 | assert!(error.contains("cwd is not a directory"), "{error}"); |
| @@ -137,7 +142,7 @@ fn bash_rejects_a_missing_cwd() { | |||
| 137 | 142 | ||
| 138 | #[test] | 143 | #[test] |
| 139 | fn unknown_tool_is_an_error_not_a_panic() { | 144 | fn unknown_tool_is_an_error_not_a_panic() { |
| 140 | let error = tools::run("teleport", &json!({})).err().unwrap(); | 145 | let error = run("teleport", &json!({})).err().unwrap(); |
| 141 | assert!(error.contains("unknown tool"), "{error}"); | 146 | assert!(error.contains("unknown tool"), "{error}"); |
| 142 | } | 147 | } |
| 143 | 148 | ||
| @@ -151,7 +156,7 @@ fn read_streams_large_files_and_reports_the_next_offset() { | |||
| 151 | } | 156 | } |
| 152 | fs::write(&path, body).unwrap(); | 157 | fs::write(&path, body).unwrap(); |
| 153 | 158 | ||
| 154 | let text = tools::run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); | 159 | let text = run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); |
| 155 | assert!(text.contains(" 990\tline 990"), "{text}"); | 160 | assert!(text.contains(" 990\tline 990"), "{text}"); |
| 156 | assert!(text.contains(" 994\tline 994"), "{text}"); | 161 | assert!(text.contains(" 994\tline 994"), "{text}"); |
| 157 | assert!(!text.contains("line 995"), "{text}"); | 162 | assert!(!text.contains("line 995"), "{text}"); |
| @@ -164,7 +169,7 @@ fn read_clips_a_single_enormous_line() { | |||
| 164 | let path = dir.join("one-line.txt"); | 169 | let path = dir.join("one-line.txt"); |
| 165 | fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); | 170 | fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); |
| 166 | 171 | ||
| 167 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | 172 | let text = run("read", &json!({ "path": path })).unwrap(); |
| 168 | assert!(text.starts_with(" 1\tx"), "{text}"); | 173 | assert!(text.starts_with(" 1\tx"), "{text}"); |
| 169 | assert!(text.contains("line truncated"), "{text}"); | 174 | assert!(text.contains("line truncated"), "{text}"); |
| 170 | // Bounded by the per-line cap, not by the file size. | 175 | // Bounded by the per-line cap, not by the file size. |
| @@ -176,7 +181,7 @@ fn read_reports_an_offset_past_the_end() { | |||
| 176 | let dir = temp_dir("read-eof"); | 181 | let dir = temp_dir("read-eof"); |
| 177 | let path = dir.join("short.txt"); | 182 | let path = dir.join("short.txt"); |
| 178 | fs::write(&path, "only\n").unwrap(); | 183 | fs::write(&path, "only\n").unwrap(); |
| 179 | let text = tools::run("read", &json!({ "path": path, "offset": 9 })).unwrap(); | 184 | let text = run("read", &json!({ "path": path, "offset": 9 })).unwrap(); |
| 180 | assert!(text.contains("fewer than 9 lines"), "{text}"); | 185 | assert!(text.contains("fewer than 9 lines"), "{text}"); |
| 181 | } | 186 | } |
| 182 | 187 | ||
| @@ -185,10 +190,10 @@ fn read_refuses_binaries_and_directories() { | |||
| 185 | let dir = temp_dir("read-binary"); | 190 | let dir = temp_dir("read-binary"); |
| 186 | let binary = dir.join("blob.bin"); | 191 | let binary = dir.join("blob.bin"); |
| 187 | fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); | 192 | fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); |
| 188 | let error = tools::run("read", &json!({ "path": binary })).err().unwrap(); | 193 | let error = run("read", &json!({ "path": binary })).err().unwrap(); |
| 189 | assert!(error.contains("binary"), "{error}"); | 194 | assert!(error.contains("binary"), "{error}"); |
| 190 | 195 | ||
| 191 | let error = tools::run("read", &json!({ "path": dir })).err().unwrap(); | 196 | let error = run("read", &json!({ "path": dir })).err().unwrap(); |
| 192 | assert!(error.contains("is a directory"), "{error}"); | 197 | assert!(error.contains("is a directory"), "{error}"); |
| 193 | } | 198 | } |
| 194 | 199 | ||
| @@ -197,7 +202,7 @@ fn read_handles_a_missing_trailing_newline() { | |||
| 197 | let dir = temp_dir("read-no-newline"); | 202 | let dir = temp_dir("read-no-newline"); |
| 198 | let path = dir.join("f.txt"); | 203 | let path = dir.join("f.txt"); |
| 199 | fs::write(&path, "one\ntwo").unwrap(); | 204 | fs::write(&path, "one\ntwo").unwrap(); |
| 200 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | 205 | let text = run("read", &json!({ "path": path })).unwrap(); |
| 201 | assert!(text.contains(" 2\ttwo"), "{text}"); | 206 | assert!(text.contains(" 2\ttwo"), "{text}"); |
| 202 | assert!(!text.contains("continue with offset"), "{text}"); | 207 | assert!(!text.contains("continue with offset"), "{text}"); |
| 203 | } | 208 | } |
| @@ -211,13 +216,13 @@ fn grep_uses_regular_expressions_by_default() { | |||
| 211 | ) | 216 | ) |
| 212 | .unwrap(); | 217 | .unwrap(); |
| 213 | 218 | ||
| 214 | let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); | 219 | let text = run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); |
| 215 | assert_eq!(text.lines().count(), 1, "{text}"); | 220 | assert_eq!(text.lines().count(), 1, "{text}"); |
| 216 | 221 | ||
| 217 | let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); | 222 | let text = run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); |
| 218 | assert_eq!(text.lines().count(), 2, "{text}"); | 223 | assert_eq!(text.lines().count(), 2, "{text}"); |
| 219 | 224 | ||
| 220 | let text = tools::run( | 225 | let text = run( |
| 221 | "grep", | 226 | "grep", |
| 222 | &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), | 227 | &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), |
| 223 | ) | 228 | ) |
| @@ -231,11 +236,11 @@ fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { | |||
| 231 | fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); | 236 | fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); |
| 232 | 237 | ||
| 233 | // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". | 238 | // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". |
| 234 | let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); | 239 | let text = run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); |
| 235 | assert_eq!(text.lines().count(), 1, "{text}"); | 240 | assert_eq!(text.lines().count(), 1, "{text}"); |
| 236 | assert!(text.contains("and ab"), "{text}"); | 241 | assert!(text.contains("and ab"), "{text}"); |
| 237 | 242 | ||
| 238 | let text = tools::run( | 243 | let text = run( |
| 239 | "grep", | 244 | "grep", |
| 240 | &json!({ "pattern": "a+b", "path": dir, "fixed": true }), | 245 | &json!({ "pattern": "a+b", "path": dir, "fixed": true }), |
| 241 | ) | 246 | ) |
| @@ -243,9 +248,66 @@ fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { | |||
| 243 | assert_eq!(text.lines().count(), 1, "{text}"); | 248 | assert_eq!(text.lines().count(), 1, "{text}"); |
| 244 | assert!(text.contains("cost is a+b"), "{text}"); | 249 | assert!(text.contains("cost is a+b"), "{text}"); |
| 245 | 250 | ||
| 246 | let error = tools::run("grep", &json!({ "pattern": "(", "path": dir })) | 251 | let error = run("grep", &json!({ "pattern": "(", "path": dir })) |
| 247 | .err() | 252 | .err() |
| 248 | .expect("invalid regex must fail"); | 253 | .expect("invalid regex must fail"); |
| 249 | assert!(error.contains("invalid regex"), "{error}"); | 254 | assert!(error.contains("invalid regex"), "{error}"); |
| 250 | assert!(error.contains("fixed"), "{error}"); | 255 | assert!(error.contains("fixed"), "{error}"); |
| 251 | } | 256 | } |
| 257 | |||
| 258 | fn running(pid: i32) -> bool { | ||
| 259 | // A zombie has already exited; anything but Z means still alive. | ||
| 260 | match fs::read_to_string(format!("/proc/{pid}/stat")) { | ||
| 261 | Ok(stat) => stat | ||
| 262 | .rsplit(')') | ||
| 263 | .next() | ||
| 264 | .and_then(|rest| rest.split_whitespace().next()) | ||
| 265 | .map(|state| state != "Z") | ||
| 266 | .unwrap_or(false), | ||
| 267 | Err(_) => false, | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | #[test] | ||
| 272 | fn bash_timeout_kills_the_whole_process_group() { | ||
| 273 | let dir = temp_dir("bash-group"); | ||
| 274 | let pidfile = dir.join("pid"); | ||
| 275 | let cmd = format!("sleep 30 & echo $! > {}; wait", pidfile.display()); | ||
| 276 | |||
| 277 | let text = shell::bash( | ||
| 278 | &json!({ "cmd": cmd }), | ||
| 279 | Duration::from_millis(300), | ||
| 280 | &AtomicBool::new(false), | ||
| 281 | ) | ||
| 282 | .unwrap(); | ||
| 283 | assert!(text.contains("timed out"), "{text}"); | ||
| 284 | |||
| 285 | std::thread::sleep(Duration::from_millis(300)); | ||
| 286 | let pid: i32 = fs::read_to_string(&pidfile).unwrap().trim().parse().unwrap(); | ||
| 287 | assert!(!running(pid), "grandchild {pid} survived the timeout"); | ||
| 288 | } | ||
| 289 | |||
| 290 | #[test] | ||
| 291 | fn bash_stops_promptly_when_the_turn_is_cancelled() { | ||
| 292 | let started = std::time::Instant::now(); | ||
| 293 | let text = shell::bash( | ||
| 294 | &json!({ "cmd": "sleep 30" }), | ||
| 295 | Duration::from_secs(20), | ||
| 296 | &AtomicBool::new(true), | ||
| 297 | ) | ||
| 298 | .unwrap(); | ||
| 299 | assert!(text.contains("aborted by the user"), "{text}"); | ||
| 300 | assert!(started.elapsed() < Duration::from_secs(2)); | ||
| 301 | } | ||
| 302 | |||
| 303 | #[test] | ||
| 304 | fn bash_does_not_inherit_the_api_key() { | ||
| 305 | std::env::set_var("OPENCODE_API_KEY", "do-not-leak-me"); | ||
| 306 | let text = run( | ||
| 307 | "bash", | ||
| 308 | &json!({ "cmd": "env | grep -c '^OPENCODE_API_KEY=' || true" }), | ||
| 309 | ) | ||
| 310 | .unwrap(); | ||
| 311 | std::env::remove_var("OPENCODE_API_KEY"); | ||
| 312 | assert!(text.contains('0'), "the key was visible to the child: {text}"); | ||
| 313 | } | ||
