diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 15:25:33 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 15:25:33 +0100 |
| commit | d64e8a21565c333a2a35ca06984ad460dd2381d5 (patch) | |
| tree | 4105cf82660f5852fe052a6c4e3a5472f3661d74 /tests/tools.rs | |
| parent | Input editor: history, bracketed paste, in-line editing (diff) | |
| download | agent686-d64e8a21565c333a2a35ca06984ad460dd2381d5.tar.gz agent686-d64e8a21565c333a2a35ca06984ad460dd2381d5.zip | |
0.2.0: streaming line-range reads
read seeks to offset with a buffered reader and consumes over-long lines
without keeping them: 16 MB RSS on a 200 MB file, 0.8s to line 999000.
Adds CHANGELOG and the versioning policy.
Diffstat (limited to '')
| -rw-r--r-- | tests/tools.rs | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/tests/tools.rs b/tests/tools.rs index 0189af8..3df353f 100644 --- a/tests/tools.rs +++ b/tests/tools.rs | |||
| @@ -76,7 +76,7 @@ fn read_numbers_lines_and_reports_truncation() { | |||
| 76 | let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); | 76 | let text = tools::run("read", &json!({ "path": path, "limit": 2 })).unwrap(); |
| 77 | assert!(text.contains(" 1\tone"), "{text}"); | 77 | assert!(text.contains(" 1\tone"), "{text}"); |
| 78 | assert!(text.contains(" 2\ttwo"), "{text}"); | 78 | assert!(text.contains(" 2\ttwo"), "{text}"); |
| 79 | assert!(text.contains("2 more line(s); continue with offset=3"), "{text}"); | 79 | assert!(text.contains("continue with offset=3"), "{text}"); |
| 80 | assert!(!text.contains("three"), "{text}"); | 80 | assert!(!text.contains("three"), "{text}"); |
| 81 | } | 81 | } |
| 82 | 82 | ||
| @@ -140,3 +140,64 @@ fn unknown_tool_is_an_error_not_a_panic() { | |||
| 140 | let error = tools::run("teleport", &json!({})).err().unwrap(); | 140 | let error = tools::run("teleport", &json!({})).err().unwrap(); |
| 141 | assert!(error.contains("unknown tool"), "{error}"); | 141 | assert!(error.contains("unknown tool"), "{error}"); |
| 142 | } | 142 | } |
| 143 | |||
| 144 | #[test] | ||
| 145 | fn read_streams_large_files_and_reports_the_next_offset() { | ||
| 146 | let dir = temp_dir("read-large"); | ||
| 147 | let path = dir.join("big.txt"); | ||
| 148 | let mut body = String::new(); | ||
| 149 | for n in 1..=1000 { | ||
| 150 | body.push_str(&format!("line {n}\n")); | ||
| 151 | } | ||
| 152 | fs::write(&path, body).unwrap(); | ||
| 153 | |||
| 154 | let text = tools::run("read", &json!({ "path": path, "offset": 990, "limit": 5 })).unwrap(); | ||
| 155 | assert!(text.contains(" 990\tline 990"), "{text}"); | ||
| 156 | assert!(text.contains(" 994\tline 994"), "{text}"); | ||
| 157 | assert!(!text.contains("line 995"), "{text}"); | ||
| 158 | assert!(text.contains("continue with offset=995"), "{text}"); | ||
| 159 | } | ||
| 160 | |||
| 161 | #[test] | ||
| 162 | fn read_clips_a_single_enormous_line() { | ||
| 163 | let dir = temp_dir("read-huge-line"); | ||
| 164 | let path = dir.join("one-line.txt"); | ||
| 165 | fs::write(&path, "x".repeat(5 * 1024 * 1024)).unwrap(); | ||
| 166 | |||
| 167 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | ||
| 168 | assert!(text.starts_with(" 1\tx"), "{text}"); | ||
| 169 | assert!(text.contains("line truncated"), "{text}"); | ||
| 170 | // Bounded by the per-line cap, not by the file size. | ||
| 171 | assert!(text.len() < 1000, "read produced {} bytes", text.len()); | ||
| 172 | } | ||
| 173 | |||
| 174 | #[test] | ||
| 175 | fn read_reports_an_offset_past_the_end() { | ||
| 176 | let dir = temp_dir("read-eof"); | ||
| 177 | let path = dir.join("short.txt"); | ||
| 178 | fs::write(&path, "only\n").unwrap(); | ||
| 179 | let text = tools::run("read", &json!({ "path": path, "offset": 9 })).unwrap(); | ||
| 180 | assert!(text.contains("fewer than 9 lines"), "{text}"); | ||
| 181 | } | ||
| 182 | |||
| 183 | #[test] | ||
| 184 | fn read_refuses_binaries_and_directories() { | ||
| 185 | let dir = temp_dir("read-binary"); | ||
| 186 | let binary = dir.join("blob.bin"); | ||
| 187 | fs::write(&binary, [0u8, 1, 2, 3, 0, 9]).unwrap(); | ||
| 188 | let error = tools::run("read", &json!({ "path": binary })).err().unwrap(); | ||
| 189 | assert!(error.contains("binary"), "{error}"); | ||
| 190 | |||
| 191 | let error = tools::run("read", &json!({ "path": dir })).err().unwrap(); | ||
| 192 | assert!(error.contains("is a directory"), "{error}"); | ||
| 193 | } | ||
| 194 | |||
| 195 | #[test] | ||
| 196 | fn read_handles_a_missing_trailing_newline() { | ||
| 197 | let dir = temp_dir("read-no-newline"); | ||
| 198 | let path = dir.join("f.txt"); | ||
| 199 | fs::write(&path, "one\ntwo").unwrap(); | ||
| 200 | let text = tools::run("read", &json!({ "path": path })).unwrap(); | ||
| 201 | assert!(text.contains(" 2\ttwo"), "{text}"); | ||
| 202 | assert!(!text.contains("continue with offset"), "{text}"); | ||
| 203 | } | ||
